diff --git a/src/covid19/blueprints/application/application_model.py b/src/covid19/blueprints/application/application_model.py index afcddef265f749d8d3a67a342689c378c8e0640a..0f85612e123a4e8aff8ec3f1608b1a08f1774a63 100644 --- a/src/covid19/blueprints/application/application_model.py +++ b/src/covid19/blueprints/application/application_model.py @@ -43,18 +43,48 @@ class ApplicationDateReported(db.Model): return {1: "Montag", 2: "Dienstag", 3: "Mittwoch", 4: "Donnerstag", 5: "Freitag", 6: "Samstag", 7: "Sonntag"} - @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() + def __get_datum_parts(cls, my_date_rep: str): + my_date_parts = my_date_rep.split("-") + my_year = int(my_date_parts[0]) + my_month = int(my_date_parts[1]) + my_day = int(my_date_parts[2]) + datum_parts = (my_year, my_month, my_day) + return datum_parts + + def __get_datum(cls, my_year: int, my_month: int, my_day: int): + my_datum = date(my_year, my_month, my_day) + return my_datum + + def __get_datum_as_str(cls, my_year: int, my_month: int, my_day: int): + my_datum_tp_be_stored = str(my_year) + my_datum_tp_be_stored += "-" + if my_month < 10: + my_datum_tp_be_stored += "0" + my_datum_tp_be_stored += str(my_month) + my_datum_tp_be_stored += "-" + if my_day < 10: + my_datum_tp_be_stored += "0" + my_datum_tp_be_stored += str(my_day) + return my_datum_tp_be_stored + + def __my_year_week(cls, my_iso_year: int, week_number: int): 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 my_year_week + + @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 = cls.__get_datum_as_str(my_year, my_month, my_day) + my_datum = cls.__get_datum(my_year, my_month, my_day) + (my_iso_year, week_number, weekday) = my_datum.isocalendar() + my_year_week = cls.__my_year_week(my_iso_year, week_number) return ApplicationDateReported( - date_reported=my_date_rep, + date_reported=date_reported, datum=my_datum, year=my_datum.year, month=my_datum.month, diff --git a/src/covid19/blueprints/ecdc/ecdc_model.py b/src/covid19/blueprints/ecdc/ecdc_model.py index a07e10ac51c7c80f533f4f50243bf067b498cef3..5cdcb9b869ef306d8338384adb1c35ceda4ee543 100644 --- a/src/covid19/blueprints/ecdc/ecdc_model.py +++ b/src/covid19/blueprints/ecdc/ecdc_model.py @@ -23,22 +23,23 @@ class EcdcDateReported(ApplicationDateReported): day_of_week = db.Column(db.Integer, nullable=False) week_of_year = db.Column(db.Integer, nullable=False) - @classmethod - def create_new_object_factory(cls, my_date_rep): + def __get_datum_parts(cls, my_date_rep: str): my_date_parts = my_date_rep.split("/") my_year = int(my_date_parts[2]) my_month = int(my_date_parts[1]) my_day = int(my_date_parts[0]) - my_datum = date(my_year, my_month, my_day) + 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 = cls.__get_datum_as_str(my_year, my_month, my_day) + my_datum = cls.__get_datum(my_year, my_month, my_day) (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) + my_year_week = cls.__my_year_week(my_iso_year, week_number) return EcdcDateReported( - date_reported=my_date_rep, + date_reported=date_reported, datum=my_datum, year=my_datum.year, month=my_datum.month, diff --git a/src/covid19/blueprints/ecdc/ecdc_model_import.py b/src/covid19/blueprints/ecdc/ecdc_model_import.py index 55109684586222db72bedf3b408d3af6b927edbb..7344a66aeaf50d4964eee4093024d1d18a94d2ea 100644 --- a/src/covid19/blueprints/ecdc/ecdc_model_import.py +++ b/src/covid19/blueprints/ecdc/ecdc_model_import.py @@ -47,9 +47,9 @@ class EcdcImport(db.Model): # sql = "select distinct date_rep, year_week from edcd_import order by year_week desc" #return db.session.execute(sql).fetchall() return db.session.query(cls.date_rep) \ - .group_by(cls.date_rep) \ + .group_by(cls.date_rep).distinct() \ .order_by(cls.date_rep.desc())\ - .distinct().all() + .all() @classmethod def get_continent(cls): @@ -57,52 +57,55 @@ class EcdcImport(db.Model): # sql = "select distinct continent_exp from edcd_import order by continent_exp asc" #return db.session.execute(sql).fetchall() return db.session.query(cls.continent_exp) \ - .group_by(cls.continent_exp) \ + .group_by(cls.continent_exp)\ + .distinct() \ .order_by(cls.continent_exp.asc()) \ - .distinct().all() + .all() @classmethod - def get_countries_of_continent(cls, my_continent: str): + def get_countries_of_continent(cls, my_continent): my_continent_exp = my_continent.region my_params = {} my_params['my_continent_param'] = my_continent_exp #TODO: #107 SQLalchemy instead of SQL in: EcdcImport.get_countries_of_continent #TODO: #108 BUG: change to ORM ClassHierarchy in: EcdcImport.get_countries_of_continent - #return db.session.query( - # cls.countries_and_territories, - # cls.geo_id, - # cls.country_territory_code, - # cls.pop_data_2019, - # cls.continent_exp - #).group_by( - # cls.countries_and_sterritories, - # cls.geo_id, - # cls.country_territory_code, - # cls.pop_data_2019, - # cls.continent_exp - #).order_by(cls.countries_and_territories.asc()).filter( - # cls.continent_exp == my_continent - #).distinct().all() - sql = """ - select distinct - countries_and_territories, - geo_id, - pop_data_2019, - continent_exp - from - ecdc_import - group by - countries_and_territories, - geo_id, - country_territory_code, - pop_data_2019, - continent_exp - having - continent_exp = :my_continent_param - order by - countries_and_territories - """ - return db.session.execute(sql, my_params).fetchall() + return db.session.query( + cls.countries_and_territories, + cls.pop_data_2019, + cls.geo_id, + cls.country_territory_code, + cls.continent_exp, + ).distinct().group_by( + cls.countries_and_territories, + cls.pop_data_2019, + cls.geo_id, + cls.country_territory_code, + cls.continent_exp + ).order_by(cls.countries_and_territories.asc()).filter( + cls.continent_exp == my_continent + ).all() + #sql = """ + #select distinct + # countries_and_territories, + # geo_id, + # pop_data_2019, + # continent_exp, + # country_territory_code + #from + # ecdc_import + #group by + # countries_and_territories, + # geo_id, + # country_territory_code, + # pop_data_2019, + # continent_exp, + # country_territory_code + #having + # continent_exp = :my_continent_param + #order by + # countries_and_territories + #""" + #return db.session.execute(sql, my_params).fetchall() @classmethod def find_by_date_reported(cls, europe_date_reported): diff --git a/src/covid19/blueprints/ecdc/ecdc_service_update.py b/src/covid19/blueprints/ecdc/ecdc_service_update.py index 6a6a604f56669e7c2cc69ef152637b5cbd1a1b74..e3d067ed2b044c31253b4ec1c8001e4ef1a577c8 100644 --- a/src/covid19/blueprints/ecdc/ecdc_service_update.py +++ b/src/covid19/blueprints/ecdc/ecdc_service_update.py @@ -60,9 +60,9 @@ class EcdcServiceUpdate: for c in result_countries_of_continent: o = EcdcCountry( countries_and_territories=c['countries_and_territories'], + pop_data_2019=c['pop_data_2019'], geo_id=c['geo_id'], country_territory_code=c['country_territory_code'], - pop_data_2019=c['pop_data_2019'], continent=my_continent) app.logger.info("| " + str(o) + " |") db.session.add(o)