diff --git a/covid19/blueprints/common/common_model.py b/covid19/blueprints/common/common_model.py index 21f91c3c81cb013cf8c766bfa6108fededf783ef..3e059f6854659ea04c8ed40fc16ef9e227beff17 100644 --- a/covid19/blueprints/common/common_model.py +++ b/covid19/blueprints/common/common_model.py @@ -101,3 +101,48 @@ class CommonDateReported(db.Model): @classmethod def find_by_year_week(cls, year_week): return db.session.query(cls).filter(cls.year_week == year_week).one() + + +class CommonRegion(db.Model): + __tablename__ = 'common_region' + discriminator = db.Column('type', db.String(50)) + __mapper_args__ = {'polymorphic_on': discriminator} + + id = db.Column(db.Integer, primary_key=True) + region = db.Column(db.String(255), unique=True) + + @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(cls): + return db.session.query(cls).all() + + @classmethod + def get_all_as_page(cls, page): + return db.session.query(cls)\ + .order_by(cls.region)\ + .paginate(page, per_page=ITEMS_PER_PAGE) + + @classmethod + def get_all_as_dict(cls): + regions = {} + for my_region in cls.get_all(): + regions[my_region.region] = my_region + return regions + + @classmethod + def get_by_id(cls, other_id): + return db.session.query(cls)\ + .filter(cls.id == other_id)\ + .one() + + @classmethod + def find_by_region(cls, i_who_region): + return db.session.query(cls)\ + .filter(cls.region == i_who_region)\ + .one_or_none() diff --git a/covid19/blueprints/europe/europe_model.py b/covid19/blueprints/europe/europe_model.py index facde0e2734098411a250ca8ccee27c05fd6ac6c..af568535f3b3602eeb73445849bb63aae0daa449 100644 --- a/covid19/blueprints/europe/europe_model.py +++ b/covid19/blueprints/europe/europe_model.py @@ -1,36 +1,14 @@ from sqlalchemy import and_ from database import db, ITEMS_PER_PAGE -from covid19.blueprints.common.common_model import CommonDateReported +from covid19.blueprints.common.common_model import CommonDateReported, CommonRegion class EuropeDateReported(CommonDateReported): __mapper_args__ = {'polymorphic_identity': 'europe'} -class EuropeContinent(db.Model): - __tablename__ = 'europe_continent' - - id = db.Column(db.Integer, primary_key=True) - continent_exp = db.Column(db.String(255), nullable=False) - - @classmethod - def remove_all(cls): - # TODO: SQLalchemy instead of SQL - db.session.execute("delete from " + cls.__tablename__ + " cascade") - db.session.commit() - return None - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls).paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all(cls): - return db.session.query(cls).all() - - @classmethod - def get_by_id(cls, other_id): - return db.session.query(cls).filter(cls.id == other_id).one() +class EuropeContinent(CommonRegion): + __mapper_args__ = {'polymorphic_identity': 'europe'} class EuropeCountry(db.Model): diff --git a/covid19/blueprints/rki/rki_model.py b/covid19/blueprints/rki/rki_model.py index 3780638b29bd00935550dfa9b024ef74ded11302..46208f87a325aebbe08afd94911b84f55d415302 100644 --- a/covid19/blueprints/rki/rki_model.py +++ b/covid19/blueprints/rki/rki_model.py @@ -3,48 +3,21 @@ from sqlalchemy.orm import joinedload from covid19.blueprints.common.common_model import CommonDateReported from database import db, ITEMS_PER_PAGE +from covid19.blueprints.common.common_model import CommonDateReported, CommonRegion class RkiDateReported(CommonDateReported): __mapper_args__ = {'polymorphic_identity': 'rki'} -class RkiRegion(db.Model): +class RkiRegion(CommonRegion): + __mapper_args__ = {'polymorphic_identity': 'rki'} + __tablename__ = 'rki_region' id = db.Column(db.Integer, primary_key=True) region = db.Column(db.String(255), unique=True) - @classmethod - def remove_all(cls): - db.session.execute("delete from " + cls.__tablename__) - db.session.commit() - return None - - @classmethod - def get_all(cls): - return db.session.query(cls).all() - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls).order_by(cls.region).paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all_as_dict(cls): - regions = {} - for my_region in cls.get_all(): - regions[my_region.region] = my_region - return regions - - @classmethod - def get_by_id(cls, other_id): - return db.session.query(cls).filter(cls.id == other_id).one() - - @classmethod - def find_by_region(cls, i_who_region): - my_region = db.session.query(cls).filter(cls.region == i_who_region).one_or_none() - return my_region - class RkiCountry(db.Model): __tablename__ = 'rki_country' @@ -52,7 +25,7 @@ class RkiCountry(db.Model): id = db.Column(db.Integer, primary_key=True) country_code = db.Column(db.String(255), unique=True, nullable=False) country = db.Column(db.String(255), unique=True, nullable=False) - region_id = db.Column(db.Integer, db.ForeignKey('who_region.id'), nullable=False) + region_id = db.Column(db.Integer, db.ForeignKey('common_region.id'), nullable=False) region = db.relationship( 'WhoRegion', lazy='subquery', diff --git a/covid19/blueprints/who/who_model.py b/covid19/blueprints/who/who_model.py index 1cbdf51f1ab66f51a746b1bd8fa36f0ebabaa589..d536b4a779a6383948cfea3df142c1a5f0021be5 100644 --- a/covid19/blueprints/who/who_model.py +++ b/covid19/blueprints/who/who_model.py @@ -1,54 +1,15 @@ from sqlalchemy import and_, func from database import db, ITEMS_PER_PAGE from sqlalchemy.orm import joinedload -from covid19.blueprints.common.common_model import CommonDateReported +from covid19.blueprints.common.common_model import CommonDateReported, CommonRegion class WhoDateReported(CommonDateReported): __mapper_args__ = {'polymorphic_identity': 'who'} -class WhoRegion(db.Model): - __tablename__ = 'who_region' - - id = db.Column(db.Integer, primary_key=True) - region = db.Column(db.String(255), unique=True) - - @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(cls): - return db.session.query(cls).all() - - @classmethod - def get_all_as_page(cls, page): - return db.session.query(cls)\ - .order_by(cls.region)\ - .paginate(page, per_page=ITEMS_PER_PAGE) - - @classmethod - def get_all_as_dict(cls): - regions = {} - for my_region in cls.get_all(): - regions[my_region.region] = my_region - return regions - - @classmethod - def get_by_id(cls, other_id): - return db.session.query(cls)\ - .filter(cls.id == other_id)\ - .one() - - @classmethod - def find_by_region(cls, i_who_region): - return db.session.query(cls)\ - .filter(cls.region == i_who_region)\ - .one_or_none() +class WhoRegion(CommonRegion): + __mapper_args__ = {'polymorphic_identity': 'who'} class WhoCountry(db.Model):