From 5083549a3c06c6fba49acf345586be2015add157 Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Sun, 14 Feb 2021 20:53:10 +0100
Subject: [PATCH] * Fixed #123 split RkiBundeslaenderService into two Services,
 one for bundeslaender and one for landkreise * Fixed #128 add fields from csv
 to RkiLandkreiseImport * Fixed #139 refactor RkiBundeslaenderServiceDownload
 to new method scheme introduced 07.02.2021 * Fixed #140 move WhoImport to RKI
 in: rk_service_import.py * Fixed #125 implement RkiLandkreise * Fixed #126
 implement RkiBundeslaenderImport

---
 .../application/application_model.py          |  2 +-
 src/covid19/blueprints/ecdc/ecdc_model.py     |  4 +--
 .../blueprints/ecdc/ecdc_model_import.py      |  3 +-
 .../blueprints/ecdc/ecdc_service_update.py    | 32 +++++++++----------
 4 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/src/covid19/blueprints/application/application_model.py b/src/covid19/blueprints/application/application_model.py
index 811912e4..2bc6fdf5 100644
--- a/src/covid19/blueprints/application/application_model.py
+++ b/src/covid19/blueprints/application/application_model.py
@@ -162,7 +162,7 @@ class ApplicationDateReported(db.Model):
 class ApplicationRegion(db.Model):
     __tablename__ = 'application_region'
     __table_args__ = (
-        db.UniqueConstraint('region', name='uix_application_region_reported'),
+        db.UniqueConstraint('region', name='uix_application_region'),
     )
     id = db.Column(db.Integer, primary_key=True)
     region = db.Column(db.String(255), nullable=False, unique=True)
diff --git a/src/covid19/blueprints/ecdc/ecdc_model.py b/src/covid19/blueprints/ecdc/ecdc_model.py
index bbed5139..eb3866a3 100644
--- a/src/covid19/blueprints/ecdc/ecdc_model.py
+++ b/src/covid19/blueprints/ecdc/ecdc_model.py
@@ -148,7 +148,7 @@ class EcdcCountry(db.Model):
         )).one()
 
     @classmethod
-    def find_by(cls, countries_and_territories: str, geo_id: str, country_territory_code: str):
+    def find_by(cls, countries_and_territories: str = '', geo_id: str = '', country_territory_code: str = ''):
         return db.session.query(cls).filter(and_(
             (cls.countries_and_territories == countries_and_territories),
             (cls.geo_id == geo_id),
@@ -185,7 +185,7 @@ class EcdcData(db.Model):
     ecdc_country_id = db.Column(db.Integer, db.ForeignKey('ecdc_country.id'), nullable=False)
     ecdc_country = db.relationship(
         'EcdcCountry',
-        lazy='joined', cascade="all, delete",
+        lazy='subquery', cascade="all, delete",
         order_by='asc(EcdcCountry.countries_and_territories)'
     )
 
diff --git a/src/covid19/blueprints/ecdc/ecdc_model_import.py b/src/covid19/blueprints/ecdc/ecdc_model_import.py
index 402d4e80..010e1b12 100644
--- a/src/covid19/blueprints/ecdc/ecdc_model_import.py
+++ b/src/covid19/blueprints/ecdc/ecdc_model_import.py
@@ -109,8 +109,7 @@ class EcdcImport(db.Model):
         #return db.session.execute(sql, my_params).fetchall()
 
     @classmethod
-    def find_by_date_reported(cls, p_edcd_date_reported_str: str):
+    def find_by_date_reported(cls, p_edcd_date_reported_str: str = ''):
         return db.session.query(cls)\
             .filter(cls.date_rep == p_edcd_date_reported_str) \
-            .order_by(cls.countries_and_territories)\
             .all()
diff --git a/src/covid19/blueprints/ecdc/ecdc_service_update.py b/src/covid19/blueprints/ecdc/ecdc_service_update.py
index b6b09417..c10f311f 100644
--- a/src/covid19/blueprints/ecdc/ecdc_service_update.py
+++ b/src/covid19/blueprints/ecdc/ecdc_service_update.py
@@ -100,36 +100,35 @@ class EcdcServiceUpdate:
             geo_id=my_geo_id,
             country_territory_code=my_country_territory_code
         )
-        if ecdc_country in None:
+        if ecdc_country is None:
             my_continent = self.__get_continent_from_import(ecdc_import)
-            ecdc_country = EcdcCountry(
+            app.logger.info(my_continent.id + " "+my_continent.region)
+            o = EcdcCountry(
                 countries_and_territories=my_countries_and_territories,
                 pop_data_2019=my_pop_data_2019,
                 geo_id=my_geo_id,
                 country_territory_code=my_country_territory_code,
                 continent=my_continent
             )
-            db.session.add(ecdc_country)
+            db.session.add(o)
             db.session.commit()
-        ecdc_country = EcdcCountry.get_by(
-            countries_and_territories=my_countries_and_territories,
-            geo_id=my_geo_id,
-            country_territory_code=my_country_territory_code
-        )
+            ecdc_country = EcdcCountry.get_by(
+                countries_and_territories=my_countries_and_territories,
+                geo_id=my_geo_id,
+                country_territory_code=my_country_territory_code
+            )
         return ecdc_country
 
-    def __get_country_from_import(self, ecdc_import: EcdcImport):
-        ecdc_date_reported = None
-        return ecdc_date_reported
-
     def __get_date_reported_from_import(self):
         dict_date_reported_from_import = {}
         result_date_str_from_ecdc_import = EcdcImport.get_date_rep()
         for item_date_str_from_ecdc_import in result_date_str_from_ecdc_import:
-            item_date_str_from_ecdc_import_str = str(item_date_str_from_ecdc_import)
+            item_date_str_from_ecdc_import_str = str(item_date_str_from_ecdc_import[0])
+            app.logger.info(item_date_str_from_ecdc_import_str)
             my_date_reported_search_str = EcdcDateReported.get_date_format_from_ecdc_import_fomat(
                 date_reported_ecdc_import_fomat=item_date_str_from_ecdc_import_str
             )
+            app.logger.info(my_date_reported_search_str)
             my_ecdc_date_reported_obj = EcdcDateReported.find_by_date_reported(
                 p_date_reported=my_date_reported_search_str
             )
@@ -150,9 +149,10 @@ class EcdcServiceUpdate:
         app.logger.info("------------------------------------------------------------")
         EcdcData.remove_all()
         i = 0
-        for (my_date_reported, my_ecdc_date_reported) in self.__get_date_reported_from_import():
-            result_ecdc_data_import = EcdcImport.find_by_date_reported(my_date_reported)
-            for item_ecdc_data_import in result_ecdc_data_import:
+        dict_date_reported_from_import = self.__get_date_reported_from_import()
+        for my_date_reported in dict_date_reported_from_import.keys():
+            my_ecdc_date_reported = dict_date_reported_from_import[my_date_reported]
+            for item_ecdc_data_import in EcdcImport.find_by_date_reported(my_date_reported):
                 my_ecdc_country = self.__get_country_from_import(item_ecdc_data_import)
                 my_deaths_weekly = int(item_ecdc_data_import.deaths_weekly)
                 my_cases_weekly = int(item_ecdc_data_import.cases_weekly)
-- 
GitLab