From fbb2c47b9b5ac8d9ba29bb07375f17b5fd1a5183 Mon Sep 17 00:00:00 2001 From: thomaswoehlke <thomas.woehlke@gmail.com> Date: Mon, 25 Jan 2021 11:46:45 +0100 Subject: [PATCH] work --- app.py | 7 -- org/woehlke/covid19/common/__init__.py | 0 org/woehlke/covid19/common/common_model.py | 72 ++++++++++++++++++++ org/woehlke/covid19/common/common_service.py | 24 +++++++ org/woehlke/covid19/who/who_service.py | 8 --- 5 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 org/woehlke/covid19/common/__init__.py create mode 100644 org/woehlke/covid19/common/common_model.py create mode 100644 org/woehlke/covid19/common/common_service.py diff --git a/app.py b/app.py index 1a189826..6452f141 100644 --- a/app.py +++ b/app.py @@ -491,13 +491,6 @@ def url_who_update_initial_run(): return redirect(url_for('home')) -@app.route('/test/who/update/countries') -def url_update_data_countries(): - who_service.run_update_countries() - flash("who_service.run_update_countries started") - return redirect(url_for('home')) - - ################################################################################################################## # # Europe diff --git a/org/woehlke/covid19/common/__init__.py b/org/woehlke/covid19/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/org/woehlke/covid19/common/common_model.py b/org/woehlke/covid19/common/common_model.py new file mode 100644 index 00000000..1b92a315 --- /dev/null +++ b/org/woehlke/covid19/common/common_model.py @@ -0,0 +1,72 @@ +from flask_sqlalchemy import Pagination +from sqlalchemy import and_, func +from database import db, ITEMS_PER_PAGE +from sqlalchemy.orm import joinedload, raiseload + + +class CommonDatum(db.Model): + __tablename__ = 'common_datum' + + id = db.Column(db.Integer, primary_key=True) + date_string = db.Column(db.String(255), nullable=False, unique=True, primary_key=True) + year_week = db.Column(db.String(255), nullable=True, unique=True) + year_day_of_year = db.Column(db.String(255), nullable=True, unique=True) + year = db.Column(db.Integer, nullable=True) + month = db.Column(db.Integer, nullable=True) + day_of_week = db.Column(db.Integer, nullable=True) + day_of_month = db.Column(db.Integer, nullable=True) + day_of_year = db.Column(db.Integer, nullable=True) + week_of_year = db.Column(db.Integer, nullable=True) + + @classmethod + def create_rw_datum_factory(cls, date_string): + # check date_string syntax + # load if already exists + old = db.session.query(cls).filter(cls.date_string == date_string).one_or_none() + if old is None: + o = CommonDatum(date_string=date_string) + # put year + # put month + # day_of_month + # compute day_of_year + # compute week_of_year + # put year_week + db.session.add(o) + db.session.commit() + return o + + @classmethod + def remove_all(cls): + # TODO: SQLalchemy instead of SQL + db.session.execute("delete from " + cls.__tablename__) + db.session.commit() + return None + + @classmethod + def get_all_as_page(cls, page): + return db.session.query(cls)\ + .order_by(cls.date_string.desc())\ + .paginate(page, per_page=ITEMS_PER_PAGE) + + @classmethod + def get_all(cls): + return db.session.query(cls).all() + + @classmethod + def get_all_as_dict(cls): + common_dates = {} + for my_common_datum in cls.get_all(): + common_dates[my_common_datum.date_string] = my_common_datum + return common_dates + + @classmethod + def get_by_id(cls, other_id): + return db.session.query(cls)\ + .filter(cls.id == other_id)\ + .one() + + @classmethod + def find_by_id(cls, other_id): + return db.session.query(cls)\ + .filter(cls.id == other_id)\ + .one_or_none() diff --git a/org/woehlke/covid19/common/common_service.py b/org/woehlke/covid19/common/common_service.py new file mode 100644 index 00000000..0ccae753 --- /dev/null +++ b/org/woehlke/covid19/common/common_service.py @@ -0,0 +1,24 @@ +import os +from database import app +from org.woehlke.covid19.vaccination.vaccination_service_download import VaccinationServiceDownload +from org.woehlke.covid19.vaccination.vaccination_service_import import VaccinationServiceImport +from org.woehlke.covid19.vaccination.vaccination_service_update import VaccinationServiceUpdate + +common_service = None + + +class CommonService: + def __init__(self, database): + app.logger.info("------------------------------------------------------------") + app.logger.info(" Common Service [init]") + app.logger.info("------------------------------------------------------------") + self.__database = database + self.limit_nr = 20 + self.vaccination_service_download = VaccinationServiceDownload(database) + self.vaccination_service_import = VaccinationServiceImport(database) + self.vaccination_service_update = VaccinationServiceUpdate(database) + app.logger.info("------------------------------------------------------------") + app.logger.info(" Vaccination Service [ready]") + + def add_new_datum(self, date_string): + return self \ No newline at end of file diff --git a/org/woehlke/covid19/who/who_service.py b/org/woehlke/covid19/who/who_service.py index 754d35f5..761b42fd 100644 --- a/org/woehlke/covid19/who/who_service.py +++ b/org/woehlke/covid19/who/who_service.py @@ -66,11 +66,3 @@ class WhoService: app.logger.info("------------------------------------------------------------") return self - def run_update_countries(self): - app.logger.info(" run update countries [begin]") - app.logger.info("------------------------------------------------------------") - self.who_service_update.update_who_country() - app.logger.info("") - app.logger.info(" run update countries [done]") - app.logger.info("------------------------------------------------------------") - return self -- GitLab