From a2ffb08bd490d03964edb723b0a381179d8390f2 Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Thu, 27 May 2021 21:48:57 +0200
Subject: [PATCH] Refactoring: vaccination

---
 .../blueprints/data_ecdc/ecdc_model.py        | 23 ++++++++----
 .../blueprints/data_owid/owid_model.py        |  2 +-
 .../blueprints/data_rki/rki_model.py          | 35 +++++++++++++------
 .../data_vaccination/vaccination_model.py     |  7 ++++
 .../blueprints/data_who/who_model.py          |  7 ++++
 5 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
index d7e4e5fd..5665bece 100644
--- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
+++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model.py
@@ -108,13 +108,13 @@ class EcdcCountry(BlueprintLocation):
     @classmethod
     def find_by_location_group(cls, location_group: EcdcContinent):
         return db.session.query(cls)\
-            .filter(cls.location_group == location_group)\
+            .filter(cls.location_group_id == location_group.id)\
             .all()
 
     @classmethod
     def get_by_location_group(cls, location_group: EcdcContinent, page: int):
         return db.session.query(cls)\
-            .filter(cls.location_group == location_group)\
+            .filter(cls.location_group_id == location_group.id)\
             .paginate(page, per_page=ITEMS_PER_PAGE)
 
     @classmethod
@@ -150,17 +150,20 @@ 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) \
+        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())
 
     @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 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())
 
     @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 db.session.query(cls)\
+            .filter(cls.date_reported_id == date_reported.id) \
             .order_by(cls.deaths.desc())
 
     @classmethod
@@ -172,8 +175,7 @@ class EcdcData(BlueprintFactTable):
     @classmethod
     def __query_by_location(cls, location: EcdcCountry):
         return db.session.query(cls)\
-            .filter(cls.location_id == location.id) \
-            .order_by(cls.date_reported.desc())
+            .filter(cls.location_id == location.id)
 
     @classmethod
     def __query_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry):
@@ -232,3 +234,10 @@ class EcdcData(BlueprintFactTable):
     def get_by_date_reported_and_location(cls, date_reported: EcdcDateReported, location: EcdcCountry, page: int):
         return cls.__query_by_date_reported_and_location(date_reported, location).one()
 
+    @classmethod
+    def delete_data_for_one_day(cls, date_reported: EcdcDateReported):
+        for data in cls.find_by_date_reported(date_reported):
+            db.session.delete(data)
+        db.session.delete(date_reported)
+        db.session.commit()
+
diff --git a/src/flask_covid19/blueprints/data_owid/owid_model.py b/src/flask_covid19/blueprints/data_owid/owid_model.py
index 36449d2d..44448137 100644
--- a/src/flask_covid19/blueprints/data_owid/owid_model.py
+++ b/src/flask_covid19/blueprints/data_owid/owid_model.py
@@ -265,7 +265,7 @@ class OwidData(BlueprintFactTable):
 
     @classmethod
     def delete_data_for_one_day(cls, date_reported: OwidDateReported):
-        for data in cls.get_data_for_one_day(date_reported):
+        for data in cls.find_by_date_reported(date_reported):
             db.session.delete(data)
         db.session.delete(date_reported)
         db.session.commit()
diff --git a/src/flask_covid19/blueprints/data_rki/rki_model.py b/src/flask_covid19/blueprints/data_rki/rki_model.py
index 51b89124..fa1b819e 100644
--- a/src/flask_covid19/blueprints/data_rki/rki_model.py
+++ b/src/flask_covid19/blueprints/data_rki/rki_model.py
@@ -246,7 +246,7 @@ class RkiData(BlueprintFactTable):
         lazy='joined',
         cascade='save-update',
         order_by='desc(RkiAltersgruppe.altersgruppe)')
-    neuer_fall = db.Column(db.String(255), nullable=False)
+    # neuer_fall = db.Column(db.String(255), nullable=False)
     geschlecht = db.Column(db.String(255), nullable=False)
     anzahl_fall = db.Column(db.String(255), nullable=False)
     anzahl_todesfall = db.Column(db.String(255), nullable=False)
@@ -272,12 +272,6 @@ class RkiData(BlueprintFactTable):
     ist_erkrankungsbeginn = db.Column(db.String(255), nullable=False)
     altersgruppe2 = db.Column(db.String(255), nullable=False)
 
-    @classmethod
-    def find_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis):
-        return db.session.query(cls).filter(
-            and_((cls.date_reported == date_reported), (cls.location == location))
-        ).one_or_none()
-
     @classmethod
     def delete_all(cls):
         db.session.query(cls).delete()
@@ -286,14 +280,33 @@ class RkiData(BlueprintFactTable):
 
     @classmethod
     def get_by_location(cls, location: RkiLandkreis, page: int):
-        return db.session.query(cls).filter(cls.location == location).paginate(page, per_page=ITEMS_PER_PAGE)
+        return db.session.query(cls)\
+            .filter(cls.location_id == location.id)\
+            .order_by(cls.date_reported)\
+            .paginate(page, per_page=ITEMS_PER_PAGE)
 
     @classmethod
     def find_by_location(cls, location: RkiLandkreis):
-        return db.session.query(cls).filter(cls.location == location).all()
+        return db.session.query(cls)\
+            .filter(cls.location_id == location.id)\
+            .order_by(cls.date_reported)\
+            .all()
 
     @classmethod
-    def get_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis):
+    def find_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis):
         return db.session.query(cls)\
-            .filter(and_((cls.date_reported == date_reported), (cls.location == location)))\
+            .filter(and_((cls.date_reported_id == date_reported.id), (cls.location_id == location.id)))\
             .all()
+
+    @classmethod
+    def get_by_date_reported_and_location(cls, date_reported: RkiMeldedatum, location: RkiLandkreis, page: int):
+        return db.session.query(cls)\
+            .filter(and_((cls.date_reported_id == date_reported.id), (cls.location_id == location.id)))\
+            .paginate(page, per_page=ITEMS_PER_PAGE)
+
+    @classmethod
+    def delete_data_for_one_day(cls, date_reported: RkiMeldedatum):
+        for data in cls.find_by_date_reported(date_reported):
+            db.session.delete(data)
+        db.session.delete(date_reported)
+        db.session.commit()
diff --git a/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py b/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py
index 2e7229d5..eb6ede6f 100644
--- a/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py
+++ b/src/flask_covid19/blueprints/data_vaccination/vaccination_model.py
@@ -77,3 +77,10 @@ class VaccinationData(BlueprintFactTableTimeSeries):
         return db.session.query(cls) \
             .filter(cls.date_reported_id == date_reported.id) \
             .one_or_none()
+
+    @classmethod
+    def delete_data_for_one_day(cls, date_reported: VaccinationDateReported):
+        for data in cls.find_by_date_reported(date_reported):
+            db.session.delete(data)
+        db.session.delete(date_reported)
+        db.session.commit()
diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py
index c5378542..704904ca 100644
--- a/src/flask_covid19/blueprints/data_who/who_model.py
+++ b/src/flask_covid19/blueprints/data_who/who_model.py
@@ -308,3 +308,10 @@ class WhoData(BlueprintFactTable):
     @classmethod
     def find_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported):
         return cls.__query_by_location_order_by_deaths_cumulative(location).all()
+
+    @classmethod
+    def delete_data_for_one_day(cls, date_reported: WhoDateReported):
+        for data in cls.find_by_date_reported(date_reported):
+            db.session.delete(data)
+        db.session.delete(date_reported)
+        db.session.commit()
-- 
GitLab