From fd7544c0ca7ae5d84d52d082451fbc10f9067fb0 Mon Sep 17 00:00:00 2001 From: thomaswoehlke <thomas.woehlke@gmail.com> Date: Fri, 21 May 2021 22:39:58 +0200 Subject: [PATCH] Refactoring: rename covid19 to flask_covid19 --- data | 2 +- .../blueprints/app_all/all_model.py | 38 ++++++++++----- .../blueprints/data_divi/divi_model.py | 14 ++++-- .../data_divi/divi_service_update.py | 2 +- .../blueprints/data_ecdc/ecdc_model.py | 46 ++++++++++--------- .../blueprints/data_owid/owid_model.py | 43 ++--------------- .../blueprints/data_rki_cases/rki_model.py | 28 ++--------- .../rki_vaccination_model.py | 30 +++--------- .../blueprints/data_who/who_model.py | 22 +-------- 9 files changed, 81 insertions(+), 144 deletions(-) diff --git a/data b/data index 33b38616..88a085e1 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 33b3861681e64d3a4dc5ee56260755e01a3ce4c2 +Subproject commit 88a085e1b71885e0c05bd13775d1a184b1836ab3 diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py index 96833d97..efc680bb 100644 --- a/src/flask_covid19/blueprints/app_all/all_model.py +++ b/src/flask_covid19/blueprints/app_all/all_model.py @@ -1,6 +1,7 @@ from datetime import date, datetime from database import db, ITEMS_PER_PAGE from sqlalchemy.orm import subqueryload +from sqlalchemy import not_ class BlueprintDateReported(db.Model): @@ -113,11 +114,11 @@ class BlueprintDateReported(db.Model): date_reported_import_str = my_date_rep my_datum = date.fromisoformat(my_date_rep) (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) + my_year_week = str(my_iso_year) if week_number < 10: - my_year_week += "-0" + my_year_week += '-0' else: - my_year_week += "-" + my_year_week += '-' my_year_week += str(week_number) day_of_year = cls.my_day_of_year(my_datum) year_day_of_year = cls.my_year_day_of_year(my_datum) @@ -191,11 +192,11 @@ class BlueprintDateReported(db.Model): date_reported_import_str = my_date_rep my_datum = date.fromisoformat(my_date_rep) (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) + my_year_week = str(my_iso_year) if week_number < 10: - my_year_week += "-0" + my_year_week += '-0' else: - my_year_week += "-" + my_year_week += '-' my_year_week += str(week_number) day_of_year = cls.my_day_of_year(my_datum) year_day_of_year = cls.my_year_day_of_year(my_datum) @@ -218,11 +219,11 @@ class BlueprintDateReported(db.Model): date_reported_import_str = my_date_rep my_datum = date.fromisoformat(my_date_rep) (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) + my_year_week = str(my_iso_year) if week_number < 10: - my_year_week += "-0" + my_year_week += '-0' else: - my_year_week += "-" + my_year_week += '-' my_year_week += str(week_number) day_of_year = cls.my_day_of_year(my_datum) year_day_of_year = cls.my_year_day_of_year(my_datum) @@ -247,9 +248,9 @@ class BlueprintDateReported(db.Model): @classmethod def my_year_day_of_year(cls, my_datum, day_of_year): if day_of_year < 10: - return my_datum.year + "-0" + str(day_of_year) + return my_datum.year + '-0' + str(day_of_year) else: - return my_datum.year + "-" + str(day_of_year) + return my_datum.year + '-' + str(day_of_year) @classmethod def remove_all(cls): @@ -322,6 +323,21 @@ class BlueprintDateReported(db.Model): .order_by(cls.date_reported_import_str.desc())\ .first() + @classmethod + def set_all_to_processed(cls): + for o in cls.get_unprocessed(): + o.set_processed() + db.session.commit() + return None + + def set_processed(self): + self.processed = True + return self + + @classmethod + def get_unprocessed(cls): + return db.session.query(cls).filter(not_(cls.processed)).all() + class BlueprintLocationGroup(db.Model): __tablename__ = 'blueprint_location_group' diff --git a/src/flask_covid19/blueprints/data_divi/divi_model.py b/src/flask_covid19/blueprints/data_divi/divi_model.py index 9efd1da3..fcb89600 100644 --- a/src/flask_covid19/blueprints/data_divi/divi_model.py +++ b/src/flask_covid19/blueprints/data_divi/divi_model.py @@ -6,14 +6,14 @@ from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, Bl class DiviDateReported(BlueprintDateReported): - __tablename__ = 'divi_datereported' + __tablename__ = 'divi_date_reported' __mapper_args__ = {'concrete': True} __table_args__ = ( db.UniqueConstraint( 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_divi_datereported" + name="uix_divi_date_reported" ), ) @@ -32,6 +32,10 @@ class DiviDateReported(BlueprintDateReported): @classmethod def create_new_object_factory(cls, my_date_rep): + return super().create_new_object_factory_for_divi(my_date_rep) + + @classmethod + def create_new_object_factory_alt(cls, my_date_rep): my_datum = date.fromisoformat(my_date_rep) (my_iso_year, week_number, weekday) = my_datum.isocalendar() my_year_week = "" + str(my_iso_year) @@ -177,7 +181,7 @@ class DiviData(db.Model): deaths_new = db.Column(db.Integer, nullable=False) deaths_cumulative = db.Column(db.Integer, nullable=False) date_reported_id = db.Column(db.Integer, - db.ForeignKey('divi_datereported.id'), nullable=False) + db.ForeignKey('divi_date_reported.id'), nullable=False) date_reported = db.relationship( 'DiviDateReported', lazy='joined', @@ -354,8 +358,8 @@ class DiviData(db.Model): @classmethod def get_datum_of_all_data(cls): datum_of_all_divi_data = [] - for data in db.session.query(cls).options(subqueryload("date_reported").load_only("date_reported_import_str")): - datum = data.date_reported.date_reported_import_str + for data in db.session.query(cls).options(subqueryload("date_reported").load_only("datum")): + datum = data.date_reported.datum.isoformat() if not datum in datum_of_all_divi_data: datum_of_all_divi_data.append(datum) datum_of_all_divi_data.sort() diff --git a/src/flask_covid19/blueprints/data_divi/divi_service_update.py b/src/flask_covid19/blueprints/data_divi/divi_service_update.py index 7e60c8ce..a703398d 100644 --- a/src/flask_covid19/blueprints/data_divi/divi_service_update.py +++ b/src/flask_covid19/blueprints/data_divi/divi_service_update.py @@ -96,7 +96,7 @@ class DiviServiceUpdateFull(DiviServiceUpdateBase): def get_new_dates_as_array_from_divi_import(self): new_dates_reported_from_import = [] - list_datum_of_all_divi_data = DiviData.get_datum_of_all_divi_data() + list_datum_of_all_divi_data = DiviData.get_datum_of_all_data() for item in DiviImport.get_datum_of_all_divi_import(): if not item in list_datum_of_all_divi_data: new_dates_reported_from_import.append(item) diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py index f692c03b..a00e7c06 100644 --- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py +++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py @@ -4,14 +4,14 @@ from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, Bl class EcdcDateReported(BlueprintDateReported): - __tablename__ = 'ecdc_datereported' + __tablename__ = 'ecdc_date_reported' __mapper_args__ = {'concrete': True} __table_args__ = ( db.UniqueConstraint( 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_ecdc_datereported" + name="uix_ecdc_date_reported" ), ) @@ -28,6 +28,28 @@ class EcdcDateReported(BlueprintDateReported): date_reported_import_str = db.Column(db.String(255), nullable=False, unique=True) processed = db.Column(db.Boolean, nullable=False) + @classmethod + def create_new_object_factory(cls, my_date_rep): + return super().create_new_object_factory_for_ecdc(my_date_rep) + + @classmethod + def create_new_object_factory_alt(cls, my_date_rep: str): + (my_year, my_month, my_day) = cls.get_datum_parts(my_date_rep) + date_reported_import_str = super().get_datum_as_str(my_year, my_month, my_day) + my_datum = super().get_datum(my_year, my_month, my_day) + (my_iso_year, week_number, weekday) = my_datum.isocalendar() + my_year_week = super().my_year_week(my_iso_year, week_number) + return EcdcDateReported( + date_reported_import_str=date_reported_import_str, + datum=my_datum, + year=my_datum.year, + month=my_datum.month, + day_of_month=my_datum.day, + day_of_week=weekday, + week_of_year=week_number, + year_week=my_year_week + ) + def get_name_for_datum(self): return self.date_reported_import_str @@ -63,24 +85,6 @@ class EcdcDateReported(BlueprintDateReported): datum_parts = (my_year, my_month, my_day) return datum_parts - @classmethod - def create_new_object_factory(cls, my_date_rep: str): - (my_year, my_month, my_day) = cls.get_datum_parts(my_date_rep) - date_reported_import_str = super().get_datum_as_str(my_year, my_month, my_day) - my_datum = super().get_datum(my_year, my_month, my_day) - (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = super().my_year_week(my_iso_year, week_number) - return EcdcDateReported( - date_reported_import_str=date_reported_import_str, - datum=my_datum, - year=my_datum.year, - month=my_datum.month, - day_of_month=my_datum.day, - day_of_week=weekday, - week_of_year=week_number, - year_week=my_year_week - ) - class EcdcContinent(BlueprintLocationGroup): __tablename__ = 'ecdc_country_continent' @@ -210,7 +214,7 @@ class EcdcData(db.Model): order_by='asc(EcdcCountry.countries_and_territories)' ) - ecdc_datereported_id = db.Column(db.Integer, db.ForeignKey('ecdc_datereported.id'), nullable=False) + ecdc_datereported_id = db.Column(db.Integer, db.ForeignKey('ecdc_date_reported.id'), nullable=False) ecdc_datereported = db.relationship( 'EcdcDateReported', lazy='joined', diff --git a/src/flask_covid19/blueprints/data_owid/owid_model.py b/src/flask_covid19/blueprints/data_owid/owid_model.py index 9e49378d..cdeb854d 100644 --- a/src/flask_covid19/blueprints/data_owid/owid_model.py +++ b/src/flask_covid19/blueprints/data_owid/owid_model.py @@ -7,14 +7,14 @@ from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, Bl class OwidDateReported(BlueprintDateReported): - __tablename__ = 'owid_datereported' + __tablename__ = 'owid_date_reported' __mapper_args__ = {'concrete': True} __table_args__ = ( db.UniqueConstraint( 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_owid_datereported" + name="uix_owid_date_reported" ), ) @@ -59,41 +59,8 @@ class OwidDateReported(BlueprintDateReported): return result @classmethod - def set_all_to_processed(cls): - for o in cls.get_unprocessed(): - o.set_processed() - db.session.commit() - return None - - def set_processed(self): - self.processed = True - return self - - @classmethod - def get_unprocessed(cls): - return db.session.query(cls).filter(cls.processed == False).all() - - @classmethod - def create_new_object_factory(cls, my_date_rep: str): - my_datum = date.fromisoformat(my_date_rep) - (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) - if week_number < 10: - my_year_week += '-0' - else: - my_year_week += '-' - my_year_week += str(week_number) - return OwidDateReported( - date_reported_import_str=my_date_rep, - datum=my_datum, - year=my_datum.year, - month=my_datum.month, - day_of_month=my_datum.day, - day_of_week=weekday, - week_of_year=week_number, - year_week=my_year_week, - processed=False - ) + def create_new_object_factory(cls, my_date_rep): + return super().create_new_object_factory_for_owid(my_date_rep) class OwidContinent(BlueprintLocationGroup): @@ -250,7 +217,7 @@ class OwidData(db.Model): id = db.Column(db.Integer, primary_key=True) date_reported_id = db.Column(db.Integer, - db.ForeignKey('owid_datereported.id'), nullable=False) + db.ForeignKey('owid_date_reported.id'), nullable=False) date_reported = db.relationship( 'OwidDateReported', lazy='joined', diff --git a/src/flask_covid19/blueprints/data_rki_cases/rki_model.py b/src/flask_covid19/blueprints/data_rki_cases/rki_model.py index 58b1179a..7685495d 100644 --- a/src/flask_covid19/blueprints/data_rki_cases/rki_model.py +++ b/src/flask_covid19/blueprints/data_rki_cases/rki_model.py @@ -1,19 +1,18 @@ from sqlalchemy import and_ -from datetime import date from database import db, ITEMS_PER_PAGE from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup class RkiMeldedatum(BlueprintDateReported): - __tablename__ = 'rki_cases_datereported' + __tablename__ = 'rki_cases_date_reported' __mapper_args__ = {'concrete': True} __table_args__ = ( db.UniqueConstraint( 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_rki_cases_datereported" + name="uix_rki_cases_date_reported" ), ) @@ -32,24 +31,7 @@ class RkiMeldedatum(BlueprintDateReported): @classmethod def create_new_object_factory(cls, my_date_rep): - my_datum = date.fromisoformat(my_date_rep) - (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) - if week_number < 10: - my_year_week += "-0" - else: - my_year_week += "-" - my_year_week += str(week_number) - return RkiMeldedatum( - date_reported_import_str=my_date_rep, - datum=my_datum, - year=my_datum.year, - month=my_datum.month, - day_of_month=my_datum.day, - day_of_week=weekday, - week_of_year=week_number, - year_week=my_year_week - ) + return super().create_new_object_factory_for_rki(my_date_rep) class RkiBundesland(BlueprintLocationGroup): @@ -196,12 +178,12 @@ class RkiData(db.Model): ist_erkrankungsbeginn = db.Column(db.String(255), nullable=False) altersgruppe2 = db.Column(db.String(255), nullable=False) meldedatum_id = db.Column(db.Integer, - db.ForeignKey('rki_cases_datereported.id'), nullable=False) + db.ForeignKey('rki_cases_date_reported.id'), nullable=False) meldedatum = db.relationship( 'RkiMeldedatum', lazy='joined', cascade='save-update', - order_by='desc(RkiMeldedatum.date_reported_import_str)') + order_by='desc(RkiMeldedatum.datum)') landkreis_id = db.Column(db.Integer, db.ForeignKey('rki_cases_landkreis.id'), nullable=False) landkreis = db.relationship( diff --git a/src/flask_covid19/blueprints/data_rki_vaccination/rki_vaccination_model.py b/src/flask_covid19/blueprints/data_rki_vaccination/rki_vaccination_model.py index c31553be..027d2d72 100644 --- a/src/flask_covid19/blueprints/data_rki_vaccination/rki_vaccination_model.py +++ b/src/flask_covid19/blueprints/data_rki_vaccination/rki_vaccination_model.py @@ -1,11 +1,10 @@ -from datetime import date from database import db, ITEMS_PER_PAGE from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported class RkiVaccinationDateReported(BlueprintDateReported): - __tablename__ = 'rki_vaccination_datereported' + __tablename__ = 'rki_vaccination_date_reported' __mapper_args__ = { 'concrete': True } @@ -14,7 +13,7 @@ class RkiVaccinationDateReported(BlueprintDateReported): 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_rki_vaccination_datereported" + name="uix_rki_vaccination_date_reported" ), ) @@ -32,37 +31,20 @@ class RkiVaccinationDateReported(BlueprintDateReported): processed = db.Column(db.Boolean, nullable=False) @classmethod - def create_new_object_factory(cls, my_date_rep: str): - my_datum = date.fromisoformat(my_date_rep) - (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) - if week_number < 10: - my_year_week += "-0" - else: - my_year_week += "-" - my_year_week += str(week_number) - return RkiVaccinationDateReported( - date_reported_import_str=my_date_rep, - datum=my_datum, - year=my_datum.year, - month=my_datum.month, - day_of_month=my_datum.day, - day_of_week=weekday, - week_of_year=week_number, - year_week=my_year_week - ) + def create_new_object_factory(cls, my_date_rep): + return super().create_new_object_factory_rki_vaccination(my_date_rep) class RkiVaccinationData(db.Model): __tablename__ = 'rki_vaccination' id = db.Column(db.Integer, primary_key=True) - date_reported_id = db.Column(db.Integer, db.ForeignKey('rki_vaccination_datereported.id'), nullable=False) + date_reported_id = db.Column(db.Integer, db.ForeignKey('rki_vaccination_date_reported.id'), nullable=False) date_reported = db.relationship( 'RkiVaccinationDateReported', lazy='joined', cascade='save-update', - order_by='desc(RkiVaccinationDateReported.date_reported_import_str)') + order_by='desc(RkiVaccinationDateReported.datum)') dosen_kumulativ = db.Column(db.Integer, nullable=False) dosen_differenz_zum_vortag = db.Column(db.Integer, nullable=False) dosen_biontech_kumulativ = db.Column(db.Integer, nullable=False) diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py index 7111dd8e..6fc322a8 100644 --- a/src/flask_covid19/blueprints/data_who/who_model.py +++ b/src/flask_covid19/blueprints/data_who/who_model.py @@ -1,6 +1,5 @@ from sqlalchemy import and_ -from datetime import date -from sqlalchemy.orm import joinedload, subqueryload +from sqlalchemy.orm import joinedload from database import db, ITEMS_PER_PAGE from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintFactTable from flask_covid19.blueprints.app_all.all_model import BlueprintLocationGroup, BlueprintLocation @@ -33,24 +32,7 @@ class WhoDateReported(BlueprintDateReported): @classmethod def create_new_object_factory(cls, my_date_rep): - my_datum = date.fromisoformat(my_date_rep) - (my_iso_year, week_number, weekday) = my_datum.isocalendar() - my_year_week = "" + str(my_iso_year) - if week_number < 10: - my_year_week += "-0" - else: - my_year_week += "-" - my_year_week += str(week_number) - return WhoDateReported( - date_reported_import_str=my_date_rep, - datum=my_datum, - year=my_datum.year, - month=my_datum.month, - day_of_month=my_datum.day, - day_of_week=weekday, - week_of_year=week_number, - year_week=my_year_week - ) + return super().create_new_object_factory_for_who(my_date_rep) class WhoCountryRegion(BlueprintLocationGroup): -- GitLab