From cbb6b0dc7233a6e3a133eb2a3a49d60b0a8bc243 Mon Sep 17 00:00:00 2001 From: thomaswoehlke <thomas.woehlke@gmail.com> Date: Thu, 27 May 2021 22:18:21 +0200 Subject: [PATCH] Refactoring: vaccination --- .../blueprints/data_ecdc/ecdc_model.py | 33 ++++--- .../blueprints/data_owid/owid_model.py | 91 +++++++++++-------- .../blueprints/data_rki/rki_model.py | 29 ++++-- .../blueprints/data_who/who_model.py | 2 +- 4 files changed, 92 insertions(+), 63 deletions(-) diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py index 5665bece..696e401d 100644 --- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py +++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py @@ -1,4 +1,5 @@ from sqlalchemy import and_ +from sqlalchemy.orm import joinedload, subqueryload from database import db, ITEMS_PER_PAGE from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup from flask_covid19.blueprints.app_all.all_model import BlueprintLocation, BlueprintFactTable @@ -150,33 +151,37 @@ class EcdcData(BlueprintFactTable): @classmethod def __query_by_date_reported(cls, date_reported: EcdcDateReported): - return db.session.query(cls)\ - .filter(cls.date_reported_id == date_reported.id) \ - .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc()) + return db.session.query(cls).filter( + cls.date_reported_id == date_reported.id + ).populate_existing().options( + joinedload(cls.location).joinedload(EcdcDateReported.location_group), + joinedload(cls.date_reported) + ) + + @classmethod + def __query_by_location(cls, location: EcdcCountry): + return db.session.query(cls).filter( + cls.location_id == location.id + ).populate_existing().options( + joinedload(cls.location).joinedload(EcdcCountry.location_group), + joinedload(cls.date_reported) + ) @classmethod def __query_by_date_reported_order_by_notification_rate(cls, date_reported: EcdcDateReported): - return db.session.query(cls)\ - .filter(cls.date_reported_id == date_reported.id) \ + return cls.__query_by_date_reported(date_reported) \ .order_by(cls.cumulative_number_for_14_days_of_covid19_cases_per_100000.desc()) @classmethod def __query_by_date_reported_order_by_deaths(cls, date_reported: EcdcDateReported): - return db.session.query(cls)\ - .filter(cls.date_reported_id == date_reported.id) \ + return cls.__query_by_date_reported(date_reported) \ .order_by(cls.deaths.desc()) @classmethod def __query_by_date_reported_order_by_cases(cls, date_reported: EcdcDateReported): - return db.session.query(cls)\ - .filter(cls.date_reported_id == date_reported.id) \ + return cls.__query_by_date_reported(date_reported) \ .order_by(cls.cases.desc()) - @classmethod - def __query_by_location(cls, location: EcdcCountry): - return db.session.query(cls)\ - .filter(cls.location_id == location.id) - @classmethod def __query_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry): return db.session.query(cls) \ diff --git a/src/flask_covid19/blueprints/data_owid/owid_model.py b/src/flask_covid19/blueprints/data_owid/owid_model.py index 44448137..f20afa2e 100644 --- a/src/flask_covid19/blueprints/data_owid/owid_model.py +++ b/src/flask_covid19/blueprints/data_owid/owid_model.py @@ -185,44 +185,44 @@ class OwidData(BlueprintFactTable): stringency_index = db.Column(db.String(255), nullable=False) @classmethod - def delete_by_location(cls, location: OwidCountry): - db.session.query(cls).filter( + def __query_by_location(cls, location: OwidCountry): + return db.session.query(cls).filter( cls.location_id == location.id ).populate_existing().options( - joinedload(cls.country), - joinedload(cls.date_reported), - ).delete() - db.session.commit() - return None + joinedload(cls.location).joinedload(OwidCountry.location_group), + joinedload(cls.date_reported) + ) @classmethod - def find_by_location(cls, location: OwidCountry): + def __query_by_date_reported(cls, date_reported: OwidDateReported): return db.session.query(cls).filter( - cls.location_id == location.id + cls.date_reported_id == date_reported.id ).populate_existing().options( - joinedload(cls.country), - joinedload(cls.date_reported), - ).all() + joinedload(cls.location).joinedload(OwidCountry.location_group), + joinedload(cls.date_reported) + ) + + @classmethod + def find_by_location(cls, location: OwidCountry): + return cls.__query_by_location(location)\ + .order_by(cls.date_reported.datum.desc())\ + .all() @classmethod def get_by_location(cls, location: OwidCountry, page: int): - return db.session.query(cls).filter( - cls.location_id == location.id - ).populate_existing().options( - joinedload(cls.location), - joinedload(cls.date_reported), - ).order_by( - cls.date_reported.desc() - ).paginate(page, per_page=ITEMS_PER_PAGE) + return cls.__query_by_location(location) \ + .order_by(cls.date_reported.datum.desc())\ + .paginate(page, per_page=ITEMS_PER_PAGE) + + @classmethod + def delete_by_location(cls, location: OwidCountry): + cls.__query_by_location(location).delete() + db.session.commit() + return None @classmethod def find_by_date_reported(cls, date_reported: OwidDateReported): - return db.session.query(cls).filter( - cls.date_reported_id == date_reported.id - ).populate_existing().options( - joinedload(cls.date_reported), - joinedload(cls.country), - ).order_by( + return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths_per_million.desc(), cls.new_cases_per_million.desc(), cls.new_deaths.desc(), @@ -231,12 +231,7 @@ class OwidData(BlueprintFactTable): @classmethod def get_by_date_reported(cls, date_reported: OwidDateReported, page: int): - return db.session.query(cls).filter( - cls.date_reported_id == date_reported.id - ).populate_existing().options( - joinedload(cls.date_reported), - joinedload(cls.country), - ).order_by( + return cls.__query_by_date_reported(date_reported).order_by( cls.new_deaths_per_million.desc(), cls.new_cases_per_million.desc(), cls.new_deaths.desc(), @@ -245,23 +240,39 @@ class OwidData(BlueprintFactTable): @classmethod def find_by_date_reported_order_by_deaths_new(cls, date_reported: OwidDateReported, page: int): - # TODO - pass + return cls.__query_by_date_reported(date_reported).order_by( + cls.new_deaths.desc(), + cls.new_deaths_per_million.desc(), + cls.new_cases.desc(), + cls.new_cases_per_million.desc(), + ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def find_by_date_reported_order_by_deaths_cumulative(cls, date_reported: OwidDateReported, page: int): - # TODO - pass + return cls.__query_by_date_reported(date_reported).order_by( + cls.new_deaths_per_million.desc(), + cls.new_deaths.desc(), + cls.new_cases_per_million.desc(), + cls.new_cases.desc(), + ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def find_by_date_reported_order_by_cases_new(cls, date_reported: OwidDateReported, page: int): - # TODO - pass + return cls.__query_by_date_reported(date_reported).order_by( + cls.new_cases.desc(), + cls.new_cases_per_million.desc(), + cls.new_deaths.desc(), + cls.new_deaths_per_million.desc(), + ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def find_by_date_reported_order_by_cases_cumulative(cls, date_reported: OwidDateReported, page: int): - # TODO - pass + return cls.__query_by_date_reported(date_reported).order_by( + cls.new_cases_per_million.desc(), + cls.new_cases.desc(), + cls.new_deaths_per_million.desc(), + cls.new_deaths.desc(), + ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def delete_data_for_one_day(cls, date_reported: OwidDateReported): diff --git a/src/flask_covid19/blueprints/data_rki/rki_model.py b/src/flask_covid19/blueprints/data_rki/rki_model.py index fa1b819e..925d1a4f 100644 --- a/src/flask_covid19/blueprints/data_rki/rki_model.py +++ b/src/flask_covid19/blueprints/data_rki/rki_model.py @@ -1,4 +1,5 @@ from sqlalchemy import and_ +from sqlalchemy.orm import joinedload from database import db, ITEMS_PER_PAGE from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintLocationGroup @@ -278,19 +279,31 @@ class RkiData(BlueprintFactTable): db.session.commit() return None + @classmethod + def __query_by_location(cls, location: RkiLandkreis): + return db.session.query(cls).filter( + cls.location_id == location.id + ).populate_existing().options( + joinedload(cls.location).joinedload(RkiLandkreis.location_group), + joinedload(cls.date_reported) + ) + + @classmethod + def __query_by_date_reported(cls, date_reported: RkiMeldedatum): + return db.session.query(cls).filter( + cls.date_reported_id == date_reported.id + ).populate_existing().options( + joinedload(cls.location).joinedload(RkiMeldedatum.location_group), + joinedload(cls.date_reported) + ) + @classmethod def get_by_location(cls, location: RkiLandkreis, page: int): - return db.session.query(cls)\ - .filter(cls.location_id == location.id)\ - .order_by(cls.date_reported)\ - .paginate(page, per_page=ITEMS_PER_PAGE) + return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def find_by_location(cls, location: RkiLandkreis): - return db.session.query(cls)\ - .filter(cls.location_id == location.id)\ - .order_by(cls.date_reported)\ - .all() + return cls.__query_by_location(location).all() @classmethod def find_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis): diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py index 704904ca..039cf5f6 100644 --- a/src/flask_covid19/blueprints/data_who/who_model.py +++ b/src/flask_covid19/blueprints/data_who/who_model.py @@ -210,7 +210,7 @@ class WhoData(BlueprintFactTable): return cls.__query_by_date_reported_and_location(date_reported, location).one_or_none() @classmethod - def get_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): + def get_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry, page: int): return cls.__query_by_date_reported_and_location(date_reported, location).one() @classmethod -- GitLab