diff --git a/README.md b/README.md index 6bc673472b8ac256052c006c590cb42c371441fc..26ef15422c420655e68575a15d28df3f49bec045 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,10 @@ class WhoGlobalDataImportTable(db.Model): ### 0.0.11 Release * Issue #28 /admin/database/import +* Issue #28 SQLalchemy instead of SQL: AllModelClasses.remove_all() +* Issue #29 SQLalchemy instead of SQL: EuropeDataImportTable.get_date_rep() +* Issue #30 SQLalchemy instead of SQL: EuropeDataImportTable.get_countries_of_continent() +* Issue #31 SQLalchemy instead of SQL: WhoGlobalDataImportTable.get_new_dates_as_array() * Issue #5 Visual Graphs for Data per Countries order by Date diff --git a/org/woehlke/covid19/europe/europe_model.py b/org/woehlke/covid19/europe/europe_model.py index 1da7b845d4aa1f3cc0214830b6fd6ea97a1fbfa7..6f875d3e9c89439e83ceea00331fbef38134c242 100644 --- a/org/woehlke/covid19/europe/europe_model.py +++ b/org/woehlke/covid19/europe/europe_model.py @@ -1,3 +1,4 @@ +from sqlalchemy import and_ from database import db, ITEMS_PER_PAGE @@ -15,9 +16,11 @@ class EuropeDataImportTable(db.Model): country_territory_code = db.Column(db.String(255), nullable=False) continent_exp = db.Column(db.String(255), nullable=False) notification_rate_per_100000_population_14days = db.Column(db.String(255), nullable=False) + row_imported = db.Column(db.Boolean, nullable=False, default=False) @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__ + " cascade") db.session.commit() return None @@ -36,11 +39,13 @@ class EuropeDataImportTable(db.Model): @classmethod def get_date_rep(cls): + #TODO: SQLalchemy instead of SQL sql = "select distinct date_rep, year_week from europe_data_import order by year_week desc" return db.session.execute(sql).fetchall() @classmethod def get_continent(cls): + # TODO: SQLalchemy instead of SQL sql = "select distinct continent_exp from europe_data_import order by continent_exp asc" return db.session.execute(sql).fetchall() @@ -49,6 +54,7 @@ class EuropeDataImportTable(db.Model): my_continent_exp = my_continent.continent_exp my_params = {} my_params['my_continent_param'] = my_continent_exp + # TODO: SQLalchemy instead of SQL sql = """ select distinct countries_and_territories, @@ -71,16 +77,21 @@ class EuropeDataImportTable(db.Model): """ return db.session.execute(sql, my_params).fetchall() + @classmethod + def find_by_date_reported(cls, europe_date_reported): + return db.session.query(cls).filter(cls.year_week == europe_date_reported.year_week).all() + class EuropeDateReported(db.Model): __tablename__ = 'europe_date_reported' id = db.Column(db.Integer, primary_key=True) date_rep = db.Column(db.String(255), nullable=False) - year_week = db.Column(db.String(255), nullable=False) + year_week = db.Column(db.String(255), nullable=False, unique=True) @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__ + " cascade") db.session.commit() return None @@ -97,6 +108,10 @@ class EuropeDateReported(db.Model): def get_by_id(cls, other_id): return db.session.query(cls).filter(cls.id == other_id).one() + @classmethod + def find_by(cls, year_week): + return db.session.query(cls).filter(cls.year_week == year_week).one() + class EuropeContinent(db.Model): __tablename__ = 'europe_continent' @@ -106,6 +121,7 @@ class EuropeContinent(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__ + " cascade") db.session.commit() return None @@ -127,16 +143,17 @@ class EuropeCountry(db.Model): __tablename__ = 'europe_country' id = db.Column(db.Integer, primary_key=True) - countries_and_territories = db.Column(db.String(255), nullable=False) + countries_and_territories = db.Column(db.String(255), nullable=False, unique=True) pop_data_2019 = 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) + geo_id = db.Column(db.String(255), nullable=False, unique=True) + country_territory_code = db.Column(db.String(255), nullable=False, unique=True) continent_id = db.Column(db.Integer, db.ForeignKey('europe_continent.id'), nullable=False) continent = db.relationship('EuropeContinent', lazy='subquery', order_by='EuropeContinent.continent_exp') @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__ + " cascade") db.session.commit() return None @@ -153,6 +170,14 @@ class EuropeCountry(db.Model): def get_by_id(cls, other_id): return db.session.query(cls).filter(cls.id == other_id).one() + @classmethod + def find_by(cls, countries_and_territories, geo_id, country_territory_code): + return db.session.query(cls).filter(and_( + (cls.countries_and_territories == countries_and_territories), + (cls.geo_id == geo_id), + (cls.country_territory_code == country_territory_code) + )).order_by(cls.countries_and_territories.asc()).one() + class EuropeData(db.Model): __tablename__ = 'europe_data' @@ -170,6 +195,7 @@ class EuropeData(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__ + " cascade") db.session.commit() return None diff --git a/org/woehlke/covid19/europe/europe_service_update.py b/org/woehlke/covid19/europe/europe_service_update.py index e0175d6e68f70d7825185eaaeb4b1d79d0b41e91..2486fcd4b58ca3e4487ba5dbc86a208567f32869 100644 --- a/org/woehlke/covid19/europe/europe_service_update.py +++ b/org/woehlke/covid19/europe/europe_service_update.py @@ -1,5 +1,4 @@ import os -import psycopg2 from database import db, app from org.woehlke.covid19.europe.europe_model import EuropeDataImportTable, \ EuropeDateReported, EuropeContinent, EuropeCountry, EuropeData @@ -75,34 +74,41 @@ class EuropeServiceUpdate: def __update_data(self): app.logger.info(" __update_data [begin]") app.logger.info("------------------------------------------------------------") - app.logger.info(" ... ") + result_date_rep = EuropeDataImportTable.get_date_rep() + for item_date_rep in result_date_rep: + europe_date_reported = EuropeDateReported.find_by( + year_week=item_date_rep['year_week'] + ) + result_europe_data_import = EuropeDataImportTable.find_by_date_reported(europe_date_reported) + for item_europe_data_import in result_europe_data_import: + europe_country = EuropeCountry.find_by( + countries_and_territories=item_europe_data_import['countries_and_territories'], + geo_id=item_europe_data_import['geo_id'], + country_territory_code=item_europe_data_import['country_territory_code'] + ) + o = EuropeData( + europe_country=europe_country, + europe_date_reported=europe_date_reported, + deaths_weekly=item_europe_data_import['notification_rate_per_100000_population_14days'], + cases_weekly=item_europe_data_import['deaths_weekly'], + notification_rate_per_100000_population_14days=item_europe_data_import[ + 'notification_rate_per_100000_population_14days'] + ) + db.session.add(o) + item_europe_data_import.row_imported = True + db.session.add(item_europe_data_import) + db.session.commit() app.logger.info(" __update_data [done]") app.logger.info("------------------------------------------------------------") return self - def __delete_data(self): - EuropeData.remove_all() - return self - - def __delete_continent(self): - EuropeContinent.remove_all() - return self - - def __delete_country(self): - EuropeCountry.remove_all() - return self - - def __delete_date_reported(self): - EuropeDateReported.remove_all() - return self - def update_db(self): app.logger.info(" update_db [begin]") app.logger.info("------------------------------------------------------------") - self.__delete_data() - self.__delete_country() - self.__delete_continent() - self.__delete_date_reported() + EuropeData.remove_all() + EuropeCountry.remove_all() + EuropeContinent.remove_all() + EuropeDateReported.remove_all() self.__update_date_reported() self.__update_continent() self.__update_country() diff --git a/org/woehlke/covid19/who/who_model.py b/org/woehlke/covid19/who/who_model.py index f8b8f8a0e094e15a087cbccf77d269f12adfa834..7b1856c8c73b1260e6acbbbdef1e7cbf14293835 100644 --- a/org/woehlke/covid19/who/who_model.py +++ b/org/woehlke/covid19/who/who_model.py @@ -12,6 +12,7 @@ class WhoDateReported(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__) db.session.commit() return None @@ -48,6 +49,7 @@ class WhoRegion(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__) db.session.commit() return None @@ -91,6 +93,7 @@ class WhoCountry(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__) db.session.commit() return None @@ -160,6 +163,7 @@ class WhoGlobalData(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__) db.session.commit() return None @@ -225,6 +229,7 @@ class WhoGlobalDataImportTable(db.Model): @classmethod def remove_all(cls): + # TODO: SQLalchemy instead of SQL db.session.execute("delete from " + cls.__tablename__) db.session.commit() return None @@ -255,6 +260,7 @@ class WhoGlobalDataImportTable(db.Model): @classmethod def get_new_dates_as_array(cls): + # TODO: SQLalchemy instead of SQL sql_query = """ select date_reported