From 49b04028cd2caa4e2dd2fd7ddd9f7e3701c6a85c Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Tue, 19 Jan 2021 15:25:00 +0100
Subject: [PATCH] ### 0.0.9 Release * Fixed #18 /europe/update: Download *
 Fixed #19 /europe/update: Import File to DB * Issue #20 /europe/update:
 Update DB * Issue #21 update_date_reported * Issue #22 update_continent *
 Issue #23 update_country * Issue #24 update_data * Issue #3 ORM: 3NF for
 ecdc_europa_data_import * Issue #4 data update for 3NF
 ecdc_europa_data_import

---
 org/woehlke/covid19/europe/europe_model.py    | 29 +++++++++++++++++--
 .../covid19/europe/europe_service_update.py   | 17 +++++++++--
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/org/woehlke/covid19/europe/europe_model.py b/org/woehlke/covid19/europe/europe_model.py
index 858f5ba6..faf1ab57 100644
--- a/org/woehlke/covid19/europe/europe_model.py
+++ b/org/woehlke/covid19/europe/europe_model.py
@@ -44,6 +44,31 @@ class EuropeDataImportTable(db.Model):
         sql = "select distinct continent_exp from europe_data_import order by continent_exp asc"
         return db.session.execute(sql)
 
+    @classmethod
+    def get_countries_of_continent(cls, my_continent):
+        my_continent_exp = my_continent.continent_exp
+        sql = """
+        select distinct
+            countries_and_territories,
+            geo_id,
+            country_territory_code,
+            pop_data_2019,
+            continent_exp
+        from
+            europe_data_import
+        group by
+            countries_and_territories,
+            geo_id,
+            country_territory_code,
+            pop_data_2019,
+            continent_exp
+        having
+            continent_exp = :my_continent_exp
+        order by
+            countries_and_territories
+        """
+        return db.session.execute(sql, my_continent_exp=my_continent_exp)
+
 
 class EuropeDateReported(db.Model):
     __tablename__ = 'europe_date_reported'
@@ -105,8 +130,8 @@ class EuropeCountry(db.Model):
     geo_id = db.Column(db.String(255), nullable=False)
     country_territory_code = db.Column(db.String(255), nullable=False)
 
-    europe_continent_id = db.Column(db.Integer, db.ForeignKey('europe_continent.id'), nullable=False)
-    europe_continent = db.relationship('EuropeContinent', lazy='joined')
+    continent_id = db.Column(db.Integer, db.ForeignKey('europe_continent.id'), nullable=False)
+    continent = db.relationship('EuropeContinent', lazy='joined')
 
     @classmethod
     def remove_all(cls):
diff --git a/org/woehlke/covid19/europe/europe_service_update.py b/org/woehlke/covid19/europe/europe_service_update.py
index 43e2fb41..a818f8bf 100644
--- a/org/woehlke/covid19/europe/europe_service_update.py
+++ b/org/woehlke/covid19/europe/europe_service_update.py
@@ -1,7 +1,8 @@
 import os
 import psycopg2
 from database import db, app
-from org.woehlke.covid19.europe.europe_model import EuropeDataImportTable, EuropeDateReported, EuropeContinent
+from org.woehlke.covid19.europe.europe_model import EuropeDataImportTable, \
+    EuropeDateReported, EuropeContinent, EuropeCountry
 
 
 class EuropeServiceUpdate:
@@ -57,7 +58,19 @@ class EuropeServiceUpdate:
     def __update_country(self):
         app.logger.info(" __update_country [begin]")
         app.logger.info("------------------------------------------------------------")
-        app.logger.info(" ... ")
+        EuropeCountry.remove_all()
+        all_continents = EuropeContinent.get_all()
+        for my_continent in all_continents:
+            result_countries_of_continent = EuropeDataImportTable.get_countries_of_continent(my_continent)
+            for c in result_countries_of_continent:
+                o = EuropeCountry(
+                    countries_and_territories=c['countries_and_territories'],
+                    geo_id=c['geo_id'],
+                    country_territory_code=c['country_territory_code'],
+                    pop_data_2019=c['pop_data_2019'],
+                    continent=my_continent)
+                db.session.add(o)
+            db.session.commit()
         app.logger.info(" __update_country [done]")
         app.logger.info("------------------------------------------------------------")
         return self
-- 
GitLab