From 6d17f240b2750f519736c348c0b38c070794cab7 Mon Sep 17 00:00:00 2001 From: thomaswoehlke <thomas.woehlke@gmail.com> Date: Tue, 25 May 2021 15:08:25 +0200 Subject: [PATCH] Refactoring: vaccination --- .../blueprints/app_all/all_model.py | 4 + .../blueprints/data_divi/divi_model.py | 169 +++++------------- .../blueprints/data_ecdc/ecdc_model.py | 79 ++------ 3 files changed, 62 insertions(+), 190 deletions(-) diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py index c62a7fb2..cae81bf9 100644 --- a/src/flask_covid19/blueprints/app_all/all_model.py +++ b/src/flask_covid19/blueprints/app_all/all_model.py @@ -218,6 +218,10 @@ class BlueprintLocation(db.Model): __table_args__ = ( db.UniqueConstraint('location', name='uix_blueprint_location'), ) + + def __str__(self): + return self.location_group.__str__() + " : " + self.location_code + " | " + self.location + id = db.Column(db.Integer, primary_key=True) location_code = db.Column(db.String(255), unique=True, nullable=True) location = db.Column(db.String(255), nullable=False, unique=True) diff --git a/src/flask_covid19/blueprints/data_divi/divi_model.py b/src/flask_covid19/blueprints/data_divi/divi_model.py index 687a5ab7..b3b9d215 100644 --- a/src/flask_covid19/blueprints/data_divi/divi_model.py +++ b/src/flask_covid19/blueprints/data_divi/divi_model.py @@ -54,9 +54,6 @@ class DiviCountry(BlueprintLocation): db.UniqueConstraint('location_code', 'location', name="uix_divi_country"), ) - def __str__(self): - return self.location_group.__str__() + " : " + self.location_code + " | " + self.location - id = db.Column(db.Integer, primary_key=True) location_code = db.Column(db.String(255), unique=True, nullable=True) location = db.Column(db.String(255), nullable=False, unique=True) @@ -68,92 +65,33 @@ class DiviCountry(BlueprintLocation): order_by='DiviRegion.location_group') processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) - #country_code = db.Column(db.String(255), unique=True, nullable=False) - #country = db.Column(db.String(255), unique=True, nullable=False) - #region_id = db.Column(db.Integer, db.ForeignKey('divi_country_region.id'), nullable=False) - #region = db.relationship( - # 'DiviRegion', - # lazy='joined', - # cascade='save-update', - # order_by='DiviRegion.region') - - @classmethod - def remove_all(cls): - for one in cls.get_all(): - db.session.delete(one) - db.session.commit() - return None - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls)\ - .order_by(cls.country)\ - .paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all(cls): - return db.session.query(cls)\ - .order_by(cls.country)\ - .all() - - @classmethod - def get_all_as_dict(cls): - countries = {} - for my_country in cls.get_all(): - countries[my_country.country_code] = my_country - return countries - - @classmethod - def get_by_id(cls, other_id): - return db.session.query(cls)\ - .filter(cls.id == other_id)\ - .one() - - @classmethod - def get_germany(cls): - return db.session.query(cls)\ - .filter(cls.country_code == 'DE')\ - .one_or_none() @classmethod - def find_by_country_code_and_country_and_divi_region_id(cls, i_country_code, i_country, my_region): + def get_by(cls, location_code: str, location: str, location_group: DiviRegion): return db.session.query(cls).filter( and_( - cls.country_code == i_country_code, - cls.country == i_country, - cls.region_id == my_region.id + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id ) ).one_or_none() @classmethod - def find_by_country_code(cls, i_country_code): - return db.session.query(cls).filter( - cls.country_code == i_country_code - ).one_or_none() - - @classmethod - def find_by_country(cls, i_country): + def find_by(cls, location_code: str, location: str, location_group: DiviRegion): return db.session.query(cls).filter( - cls.country == i_country + and_( + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id + ) ).one_or_none() @classmethod - def get_by_country_code(cls, i_country_code): - return db.session.query(cls).filter( - cls.country_code == i_country_code - ).one() - - @classmethod - def get_by_country(cls, i_country): - return db.session.query(cls).filter( - cls.country == i_country - ).one() - - @classmethod - def get_divi_countries_for_region(cls, region, page): - return db.session.query(cls).filter( - cls.region == region - ).order_by(cls.country).paginate(page, per_page=ITEMS_PER_PAGE) + def find_by_location_group(cls, location_group, page): + return db.session.query(cls)\ + .filter(cls.location_group == location_group)\ + .order_by(cls.location) \ + .paginate(page, per_page=ITEMS_PER_PAGE) class DiviData(BlueprintFactTable): @@ -182,44 +120,25 @@ class DiviData(BlueprintFactTable): deaths_cumulative = db.Column(db.Integer, nullable=False) @classmethod - def remove_all(cls): - for one in cls.get_all(): - db.session.delete(one) - db.session.commit() - return None - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls).paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all(cls): - return db.session.query(cls).all() - - @classmethod - def get_by_id(cls, other_id): - return db.session.query(cls).filter(cls.id == other_id).one() - - @classmethod - def find_one_or_none_by_date_and_country(cls, my_date_reported, my_country): + def find_by_date_reported_and_location(cls, date_reported: DiviDateReported, location: DiviCountry): return db.session.query(cls).filter( and_( - cls.date_reported_id == my_date_reported.id, - cls.country_id == my_country.id + cls.date_reported_id == date_reported.id, + cls.location_id == location.id ) ).one_or_none() @classmethod - def get_data_for_country(cls, divi_country, page): + def find_by_location(cls, location: DiviCountry, page): return db.session.query(cls).filter( - cls.country_id == divi_country.id + cls.location_id == location.id ).populate_existing().options( joinedload(cls.country).joinedload(DiviCountry.region), joinedload(cls.date_reported) ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_day(cls, date_reported, page): + def find_by_date_reported_paged(cls, date_reported: DiviDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( @@ -233,7 +152,7 @@ class DiviData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_one_day(cls, date_reported): + def find_by_date_reported(cls, date_reported: DiviDateReported): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( @@ -247,14 +166,14 @@ class DiviData(BlueprintFactTable): ).all() @classmethod - def delete_data_for_one_day(cls, date_reported): - for one_divi_date in cls.get_data_for_one_day(date_reported): + def delete_data_for_one_day(cls, date_reported: DiviDateReported): + for one_divi_date in cls.find_by_date_reported(date_reported): db.session.delete(one_divi_date) db.session.commit() return None @classmethod - def get_data_for_day_order_by_cases_new(cls, date_reported, page): + def find_by_date_reported_order_by_cases_new(cls, date_reported: DiviDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( @@ -265,7 +184,7 @@ class DiviData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_day_order_by_cases_cumulative(cls, date_reported, page): + def find_by_date_reported_order_by_cases_cumulative(cls, date_reported: DiviDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( @@ -276,7 +195,7 @@ class DiviData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_day_order_by_deaths_new(cls, date_reported, page): + def find_by_date_reported_order_by_deaths_new(cls, date_reported: DiviDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( @@ -287,62 +206,62 @@ class DiviData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_day_order_by_deaths_cumulative(cls, date_reported, page): + def find_by_location_by_deaths_cumulative(cls, date_reported: DiviDateReported, page): return db.session.query(cls).filter( cls.date_reported_id == date_reported.id ).populate_existing().options( - joinedload(cls.country).joinedload(DiviCountry.region), + joinedload(cls.location).joinedload(DiviCountry.location_group), joinedload(cls.date_reported) ).order_by( cls.deaths_cumulative.desc() ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_country_order_by_cases_new(cls, divi_country, page): + def find_by_location_order_by_cases_new(cls, location: DiviCountry, page): return db.session.query(cls).filter( - cls.country_id == divi_country.id + cls.location_id == location.id ).populate_existing().options( - joinedload(cls.country).joinedload(DiviCountry.region), + joinedload(cls.location).joinedload(DiviCountry.location_group), joinedload(cls.date_reported) ).order_by( cls.cases_new.desc() ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_country_order_by_cases_cumulative(cls, divi_country, page): + def find_by_location_order_by_cases_cumulative(cls, location: DiviCountry, page): return db.session.query(cls).filter( - cls.country_id == divi_country.id + cls.location_id == location.id ).populate_existing().options( - joinedload(cls.country).joinedload(DiviCountry.region), + joinedload(cls.location).joinedload(DiviCountry.location_group), joinedload(cls.date_reported) ).order_by( cls.cases_cumulative.desc() ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_country_order_by_deaths_new(cls, divi_country, page): + def find_by_location_order_by_deaths_new(cls, location: DiviCountry, page): return db.session.query(cls).filter( - cls.country_id == divi_country.id + cls.location_id == location.id ).populate_existing().options( - joinedload(cls.country).joinedload(DiviCountry.region), + joinedload(cls.location).joinedload(DiviCountry.location_group), joinedload(cls.date_reported) ).order_by( cls.deaths_new.desc() ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_data_for_country_order_by_deaths_cumulative(cls, divi_country, page): + def find_by_date_reported_order_by_deaths_cumulative(cls, location: DiviCountry, page): return db.session.query(cls).filter( - cls.country_id == divi_country.id + cls.location_id == location.id ).populate_existing().options( - joinedload(cls.country).joinedload(DiviCountry.region), + joinedload(cls.location).joinedload(DiviCountry.location_group), joinedload(cls.date_reported) ).order_by( cls.deaths_cumulative.desc() ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def get_datum_of_all_data(cls): + def get_date_reported(cls): datum_of_all_divi_data = [] for data in db.session.query(cls).options(subqueryload("date_reported").load_only("datum")): datum = data.date_reported.datum.isoformat() @@ -352,5 +271,5 @@ class DiviData(BlueprintFactTable): return datum_of_all_divi_data @classmethod - def get_joungest_datum(cls): - return cls.get_datum_of_all_divi_data().pop() + def get_joungest_date_reported(cls): + return cls.get_date_reported().pop() diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py index 01c4240a..37843414 100644 --- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py +++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py @@ -83,34 +83,6 @@ class EcdcCountry(BlueprintLocation): pop_data_2019 = db.Column(db.String(255), nullable=False) geo_id = db.Column(db.String(255), nullable=False) - @classmethod - def remove_all(cls): - num_rows_deleted = 0 - try: - num_rows_deleted = db.session.query(cls).delete() - db.session.commit() - except Exception: - db.session.rollback() - return num_rows_deleted - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls)\ - .order_by(cls.countries_and_territories.asc())\ - .paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all(cls): - return db.session.query(cls)\ - .order_by(cls.countries_and_territories.asc())\ - .all() - - @classmethod - def get_by_id(cls, other_id: int): - return db.session.query(cls)\ - .filter(cls.id == other_id)\ - .one() - @classmethod def find_by_id(cls, other_id: int): return db.session.query(cls)\ @@ -118,7 +90,7 @@ class EcdcCountry(BlueprintLocation): .one_or_none() @classmethod - def get_by(cls, location: str, geo_id: str, location_code: str): + def get_by(cls, location: str = '', geo_id: str = '', location_code: str = ''): return db.session.query(cls).filter(and_( (cls.location == location), (cls.geo_id == geo_id), @@ -134,25 +106,11 @@ class EcdcCountry(BlueprintLocation): )).one_or_none() @classmethod - def get_by(cls, location: str = '', geo_id: str = '', location_code: str = ''): - return db.session.query(cls).filter(and_( - (cls.location == location), - (cls.geo_id == geo_id), - (cls.location_code == location_code) - )).one() - - @classmethod - def find_by_continent(cls, continent: EcdcContinent, page: int): + def find_by_location_group(cls, location_group: EcdcContinent, page: int): return db.session.query(cls)\ - .filter(cls.location_group_id == continent.id)\ + .filter(cls.location_group_id == location_group.id)\ .paginate(page, per_page=ITEMS_PER_PAGE) - @classmethod - def get_germany(cls): - return db.session.query(cls) \ - .filter(cls.location_code == 'DEU') \ - .one() - @classmethod def find_germany(cls): return db.session.query(cls) \ @@ -185,45 +143,36 @@ class EcdcData(BlueprintFactTable): cumulative_number_for_14_days_of_covid19_cases_per_100000 = db.Column(db.Float, nullable=False) @classmethod - def remove_all(cls): - num_rows_deleted = 0 - try: - num_rows_deleted = db.session.query(cls).delete() - db.session.commit() - except Exception: - db.session.rollback() - return num_rows_deleted - - @classmethod - def find_by_date_reported(cls, ecdc_datereported, page: int): + def find_by_date_reported(cls, date_reported: EcdcDateReported, page: int): return db.session.query(cls).filter( - cls.ecdc_datereported_id == ecdc_datereported.id)\ + cls.date_reported_id == date_reported.id)\ .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc())\ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_date_reported_notification_rate(cls, ecdc_datereported, page: int): + def find_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported, page: int): return db.session.query(cls).filter( - cls.ecdc_datereported_id == ecdc_datereported.id) \ + cls.date_reported_id == date_reported.id) \ .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc()) \ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_date_reported_deaths_weekly(cls, ecdc_datereported, page: int): + def find_by_date_reported_order_by_deaths_weekly(cls, date_reported: EcdcDateReported, page: int): return db.session.query(cls).filter( - cls.ecdc_datereported_id == ecdc_datereported.id) \ + cls.date_reported_id == date_reported.id) \ .order_by(cls.deaths.desc()) \ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_date_reported_cases_weekly(cls, ecdc_datereported, page: int): + def find_by_date_reported_order_by_cases_weekly(cls, date_reported: EcdcDateReported, page: int): return db.session.query(cls).filter( - cls.ecdc_datereported_id == ecdc_datereported.id) \ + cls.date_reported_id == date_reported.id) \ .order_by(cls.cases.desc()) \ .paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_country(cls, ecdc_country, page: int): + def find_by_location(cls, location: EcdcCountry, page: int): return db.session.query(cls).filter( - cls.ecdc_country_id == ecdc_country.id) \ + cls.location_id == location.id) \ + .order_by(cls.date_reported.desc()) \ .paginate(page, per_page=ITEMS_PER_PAGE) -- GitLab