From 0612f543f46df462a4352d080a3f3a72a3e9860d Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Tue, 25 May 2021 17:23:22 +0200
Subject: [PATCH] Refactoring: vaccination

---
 .../blueprints/app_all/all_model.py           | 65 ++++++++++++++
 .../blueprints/app_all/all_model_import.py    | 86 -------------------
 .../blueprints/data_divi/divi_model_import.py | 37 ++------
 .../blueprints/data_ecdc/ecdc_model_import.py | 29 ++-----
 .../blueprints/data_owid/owid_model_import.py |  5 +-
 .../blueprints/data_rki/rki_model_import.py   |  2 +-
 .../vaccination_model_import.py               | 25 ++----
 .../blueprints/data_who/who_model.py          |  4 +-
 .../blueprints/data_who/who_model_import.py   |  3 +-
 9 files changed, 98 insertions(+), 158 deletions(-)

diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py
index 0ff9734c..12e608be 100644
--- a/src/flask_covid19/blueprints/app_all/all_model.py
+++ b/src/flask_covid19/blueprints/app_all/all_model.py
@@ -392,3 +392,68 @@ class BlueprintFactTable(BlueprintFactTableTimeSeries):
     @classmethod
     def get_by_date_reported_and_location(cls, date_reported, location, page: int):
         pass
+
+
+class AllImport(BlueprintEntity):
+    __tablename__ = 'all_import'
+    __mapper_args__ = {'concrete': True}
+
+    def __str__(self):
+        return " [ " + self.datum.isoformat() + " ] " \
+               + "updated: " + str(self.processed_update) \
+               + " | full_updated: " + str(self.processed_full_update)
+
+    id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
+    #
+    date_reported_import_str = db.Column(db.String(255), nullable=False)
+    datum = db.Column(db.Date, nullable=False)
+
+    @classmethod
+    def find_by_datum(cls, datum: date):
+        return db.session.query(cls) \
+            .filter(cls.datum == datum) \
+            .all()
+
+    @classmethod
+    def get_datum_list(cls):
+        return db.session.query(cls.datum) \
+            .order_by(cls.datum.desc()) \
+            .distinct() \
+            .all()
+
+
+class AllFlat(AllImport):
+    __tablename__ = 'all_import_flat'
+    __mapper_args__ = {'concrete': True}
+
+    def __str__(self):
+        return " [ " + self.datum.isoformat() + " ] " \
+               + self.location_group + " : " \
+               + self.location_code + " | " \
+               + self.location\
+               + " | updated: " + str(self.processed_update) \
+               + " | full_updated: " + str(self.processed_full_update)
+
+    id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
+    #
+    date_reported_import_str = db.Column(db.String(255), nullable=False)
+    datum = db.Column(db.Date, nullable=False)
+    location = db.Column(db.String(255), nullable=False)
+    location_group = db.Column(db.String(255), nullable=False)
+    location_code = db.Column(db.String(255), nullable=False)
+    #
+    year = db.Column(db.Integer, nullable=False)
+    year_month = db.Column(db.String(255), nullable=False)
+    year_week = db.Column(db.String(255), nullable=False)
+    year_day_of_year = db.Column(db.String(255), nullable=False)
+    #
+    month = db.Column(db.Integer, nullable=False)
+    day_of_month = db.Column(db.Integer, nullable=False)
+    day_of_week = db.Column(db.Integer, nullable=False)
+    week_of_year = db.Column(db.Integer, nullable=False)
+    day_of_year = db.Column(db.Integer, nullable=False)
+    #
diff --git a/src/flask_covid19/blueprints/app_all/all_model_import.py b/src/flask_covid19/blueprints/app_all/all_model_import.py
index 5c60c27a..02dade66 100644
--- a/src/flask_covid19/blueprints/app_all/all_model_import.py
+++ b/src/flask_covid19/blueprints/app_all/all_model_import.py
@@ -1,89 +1,3 @@
 from database import db, ITEMS_PER_PAGE
 
 
-class AllImport(db.Model):
-    __tablename__ = 'all_import'
-
-    id = db.Column(db.Integer, primary_key=True)
-    date_reported_import_str = db.Column(db.String(255), nullable=False)
-    datum = db.Column(db.Date, nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
-
-    def __str__(self):
-        return self.datum.isoformat() + " " + self.country_code + " " + self.country + " " + str(self.row_imported)
-
-    @classmethod
-    def remove_all(cls):
-        for one in cls.get_all():
-            db.session.delete(one)
-        db.session.commit()
-        return None
-
-    @classmethod
-    def get_by_id(cls, other_id):
-        return db.session.query(cls)\
-            .filter(cls.id == other_id)\
-            .one()
-
-    @classmethod
-    def get_for_one_datum(cls, d_datum):
-        return db.session.query(cls) \
-            .filter(cls.datum == d_datum) \
-            .order_by(cls.country.asc()) \
-            .all()
-
-    @classmethod
-    def get_all_datum(cls):
-        return db.session.query(cls.datum) \
-            .order_by(cls.datum.desc()) \
-            .distinct().all()
-
-
-class AllFlat(db.Model):
-    __tablename__ = 'all_import_flat'
-
-    id = db.Column(db.Integer, primary_key=True)
-    #
-    date_reported_import_str = db.Column(db.String(255), nullable=False)
-    datum = db.Column(db.Date, nullable=False)
-    #
-    year = db.Column(db.Integer, nullable=False)
-    year_month = db.Column(db.String(255), nullable=False)
-    year_week = db.Column(db.String(255), nullable=False)
-    year_day_of_year = db.Column(db.String(255), nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
-    #
-    month = db.Column(db.Integer, nullable=False)
-    day_of_month = db.Column(db.Integer, nullable=False)
-    day_of_week = db.Column(db.Integer, nullable=False)
-    week_of_year = db.Column(db.Integer, nullable=False)
-    day_of_year = db.Column(db.Integer, nullable=False)
-    location = db.Column(db.String(255), nullable=False)
-    location_group = db.Column(db.String(255), nullable=False)
-    location_code = db.Column(db.String(255), nullable=False)
-    #
-
-    def __str__(self):
-        return self.datum.isoformat() + " " + self.location_code + " " + self.location + " " + str(self.location_group)
-
-    @classmethod
-    def remove_all(cls):
-        for one in cls.get_all():
-            db.session.delete(one)
-        db.session.commit()
-        return None
-
-    @classmethod
-    def get_all(cls):
-        return db.session.query(cls).order_by(
-            cls.datum.desc()
-        ).all()
-
-    @classmethod
-    def get_all_as_page(cls, page):
-        return db.session.query(cls).order_by(
-            cls.datum.desc(),
-            cls.country.asc()
-        ).paginate(page, per_page=ITEMS_PER_PAGE)
diff --git a/src/flask_covid19/blueprints/data_divi/divi_model_import.py b/src/flask_covid19/blueprints/data_divi/divi_model_import.py
index 4b525785..0619d641 100644
--- a/src/flask_covid19/blueprints/data_divi/divi_model_import.py
+++ b/src/flask_covid19/blueprints/data_divi/divi_model_import.py
@@ -1,11 +1,19 @@
 from sqlalchemy.orm import Bundle
 from database import db, ITEMS_PER_PAGE
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
 
 
-class DiviImport(db.Model):
+class DiviImport(AllImport):
     __tablename__ = 'divi_import'
+    __mapper_args__ = {'concrete': True}
 
     id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
+    #
+    date_reported_import_str = db.Column(db.String(255), nullable=False)
+    datum = db.Column(db.Date, nullable=False)
+    #
     date_reported = db.Column(db.String(255), nullable=False)
     country_code = db.Column(db.String(255), nullable=False)
     country = db.Column(db.String(255), nullable=False)
@@ -15,33 +23,6 @@ class DiviImport(db.Model):
     new_deaths = db.Column(db.String(255), nullable=False)
     cumulative_deaths = db.Column(db.String(255), 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).order_by(
-            cls.date_reported.desc(),
-            cls.country.asc()
-        ).paginate(page, per_page=ITEMS_PER_PAGE)
-
-    @classmethod
-    def get_all(cls):
-        return db.session.query(cls).order_by(
-            cls.date_reported.desc(),
-            cls.country.asc()
-        ).all()
-
-    @classmethod
-    def get_by_id(cls, other_id):
-        return db.session.query(cls)\
-            .filter(cls.id == other_id)\
-            .one()
-
     @classmethod
     def get_regions(cls):
         return db.session.query(cls.divi_region)\
diff --git a/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py b/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py
index 0b19a840..5a49015b 100644
--- a/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py
+++ b/src/flask_covid19/blueprints/data_ecdc/ecdc_model_import.py
@@ -1,5 +1,5 @@
 from database import db, ITEMS_PER_PAGE
-from flask_covid19.blueprints.app_all.all_model_import import AllImport, AllFlat
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
 
 
 class EcdcImport(AllImport):
@@ -16,13 +16,16 @@ class EcdcImport(AllImport):
     day = db.Column(db.String(255), nullable=False)
     month = db.Column(db.String(255), nullable=False)
     year = db.Column(db.String(255), nullable=False)
+    #
     cases = db.Column(db.String(255), nullable=False)
     deaths = db.Column(db.String(255), nullable=False)
     pop_data_2019 = db.Column(db.String(255), nullable=False)
+    #
     countries_and_territories = db.Column(db.String(255), nullable=False)
     geo_id = db.Column(db.String(255), nullable=False)
     country_territory_code = db.Column(db.String(255), nullable=False)
     continent_exp = db.Column(db.String(255), nullable=False)
+    #
     cumulative_number_for_14_days_of_covid19_cases_per_100000 = db.Column(db.String(255), nullable=False)
 
     @classmethod
@@ -132,25 +135,25 @@ class EcdcFlat(AllFlat):
     __mapper_args__ = {'concrete': True}
 
     id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
     #
     date_reported_import_str = db.Column(db.String(255), nullable=False)
     datum = db.Column(db.Date, nullable=False)
+    location = db.Column(db.String(255), nullable=False)
+    location_group = db.Column(db.String(255), nullable=False)
+    location_code = db.Column(db.String(255), nullable=False)
     #
     year = db.Column(db.Integer, nullable=False)
     year_month = db.Column(db.String(255), nullable=False)
     year_week = db.Column(db.String(255), nullable=False)
     year_day_of_year = db.Column(db.String(255), nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
     #
     month = db.Column(db.Integer, nullable=False)
     day_of_month = db.Column(db.Integer, nullable=False)
     day_of_week = db.Column(db.Integer, nullable=False)
     week_of_year = db.Column(db.Integer, nullable=False)
     day_of_year = db.Column(db.Integer, nullable=False)
-    location = db.Column(db.String(255), nullable=False)
-    location_group = db.Column(db.String(255), nullable=False)
-    location_code = db.Column(db.String(255), nullable=False)
     #
     cases = db.Column(db.String(255), nullable=False)
     deaths = db.Column(db.String(255), nullable=False)
@@ -158,17 +161,3 @@ class EcdcFlat(AllFlat):
     pop_data_2019 = db.Column(db.String(255), nullable=False)
     cumulative_number_for_14_days_of_covid19_cases_per_100000 = db.Column(db.String(255), 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(cls):
-        return db.session.query(cls).order_by(
-            cls.datum.desc()
-        ).all()
-
-
diff --git a/src/flask_covid19/blueprints/data_owid/owid_model_import.py b/src/flask_covid19/blueprints/data_owid/owid_model_import.py
index b5006f0f..0a35abf6 100644
--- a/src/flask_covid19/blueprints/data_owid/owid_model_import.py
+++ b/src/flask_covid19/blueprints/data_owid/owid_model_import.py
@@ -1,7 +1,7 @@
 from sqlalchemy.orm import Bundle
 from database import db, ITEMS_PER_PAGE
 from sqlalchemy import and_
-from flask_covid19.blueprints.app_all.all_model_import import AllImport, AllFlat
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
 
 
 class OwidImport(AllImport):
@@ -76,9 +76,6 @@ class OwidImport(AllImport):
     hospital_beds_per_thousand = db.Column(db.String(255), nullable=False)
     life_expectancy = db.Column(db.String(255), nullable=False)
     human_development_index = db.Column(db.String(255), nullable=False)
-    datum = db.Column(db.Date, nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
 
     @classmethod
     def remove_all(cls):
diff --git a/src/flask_covid19/blueprints/data_rki/rki_model_import.py b/src/flask_covid19/blueprints/data_rki/rki_model_import.py
index 754d20f4..a8d2bb46 100644
--- a/src/flask_covid19/blueprints/data_rki/rki_model_import.py
+++ b/src/flask_covid19/blueprints/data_rki/rki_model_import.py
@@ -1,7 +1,7 @@
 from datetime import date
 from sqlalchemy.orm import Bundle
 from database import db, ITEMS_PER_PAGE
-from flask_covid19.blueprints.app_all.all_model_import import AllImport, AllFlat
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
 
 
 class RkiImport(AllImport):
diff --git a/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py b/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py
index 330051b6..0551eaac 100644
--- a/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py
+++ b/src/flask_covid19/blueprints/data_vaccination/vaccination_model_import.py
@@ -1,10 +1,14 @@
 from database import db, ITEMS_PER_PAGE
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
 
 
-class VaccinationImport(db.Model):
+class VaccinationImport(AllImport):
     __tablename__ = 'vaccination_import'
+    __mapper_args__ = {'concrete': True}
 
     id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
     datum = db.Column(db.Date, nullable=False)
     date_reported_import_str = db.Column(db.String(255), nullable=False)
     dosen_kumulativ = db.Column(db.Integer, nullable=False)
@@ -27,8 +31,6 @@ class VaccinationImport(db.Model):
     indikation_beruf_voll = db.Column(db.Integer, nullable=False)
     indikation_medizinisch_voll = db.Column(db.Integer, nullable=False)
     indikation_pflegeheim_voll = db.Column(db.Integer, nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
 
     @classmethod
     def remove_all(cls):
@@ -124,10 +126,13 @@ class VaccinationImport(db.Model):
         return new_dates
 
 
-class VaccinationFlat(db.Model):
+class VaccinationFlat(AllImport):
     __tablename__ = 'vaccination_import_flat'
+    __mapper_args__ = {'concrete': True}
 
     id = db.Column(db.Integer, primary_key=True)
+    processed_update = db.Column(db.Boolean, nullable=False)
+    processed_full_update = db.Column(db.Boolean, nullable=False)
     #
     date_reported_import_str = db.Column(db.String(255), nullable=False)
     datum = db.Column(db.Date, nullable=False)
@@ -136,8 +141,6 @@ class VaccinationFlat(db.Model):
     year_month = db.Column(db.String(255), nullable=False)
     year_week = db.Column(db.String(255), nullable=False)
     year_day_of_year = db.Column(db.String(255), nullable=False)
-    processed_update = db.Column(db.Boolean, nullable=False)
-    processed_full_update = db.Column(db.Boolean, nullable=False)
     #
     month = db.Column(db.Integer, nullable=False)
     day_of_month = db.Column(db.Integer, nullable=False)
@@ -168,13 +171,3 @@ class VaccinationFlat(db.Model):
     indikation_beruf_voll = db.Column(db.Integer, nullable=False)
     indikation_medizinisch_voll = db.Column(db.Integer, nullable=False)
     indikation_pflegeheim_voll = db.Column(db.Integer, 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
diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py
index ee3884f1..97875f1b 100644
--- a/src/flask_covid19/blueprints/data_who/who_model.py
+++ b/src/flask_covid19/blueprints/data_who/who_model.py
@@ -1,8 +1,8 @@
 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, BlueprintFactTable
-from flask_covid19.blueprints.app_all.all_model import BlueprintLocationGroup, BlueprintLocation
+from flask_covid19.blueprints.app_all.all_model import BlueprintDateReported, BlueprintFactTable, AllImport
+from flask_covid19.blueprints.app_all.all_model import BlueprintLocationGroup, BlueprintLocation, AllFlat
 
 
 class WhoDateReported(BlueprintDateReported):
diff --git a/src/flask_covid19/blueprints/data_who/who_model_import.py b/src/flask_covid19/blueprints/data_who/who_model_import.py
index a2e6fe37..30c3045b 100644
--- a/src/flask_covid19/blueprints/data_who/who_model_import.py
+++ b/src/flask_covid19/blueprints/data_who/who_model_import.py
@@ -1,6 +1,7 @@
 from sqlalchemy.orm import Bundle
 from database import db, ITEMS_PER_PAGE
-from flask_covid19.blueprints.app_all.all_model_import import AllImport, AllFlat
+from flask_covid19.blueprints.app_all.all_model import AllImport, AllFlat
+
 
 class WhoImport(AllImport):
     __tablename__ = 'who_import'
-- 
GitLab