Skip to content
Snippets Groups Projects
europe_model.py 5.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • thomaswoehlke's avatar
    thomaswoehlke committed
    from sqlalchemy import and_
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    from database import db, ITEMS_PER_PAGE
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    from covid19.blueprints.common.common_model import CommonDateReported, CommonRegion
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    class EuropeDateReported(CommonDateReported):
        __mapper_args__ = {'polymorphic_identity': 'europe'}
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    class EuropeContinent(CommonRegion):
        __mapper_args__ = {'polymorphic_identity': 'europe'}
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    class EuropeCountry(db.Model):
        __tablename__ = 'europe_country'
    
        id = db.Column(db.Integer, primary_key=True)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        countries_and_territories = db.Column(db.String(255), nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        pop_data_2019 = db.Column(db.String(255), nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        geo_id = db.Column(db.String(255), nullable=False)
        country_territory_code = db.Column(db.String(255), nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        continent_id = db.Column(db.Integer, db.ForeignKey('common_region.id'), nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        continent = db.relationship('CommonRegion', lazy='subquery', order_by='CommonRegion.region')
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
        @classmethod
        def remove_all(cls):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            # TODO: SQLalchemy instead of SQL
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            db.session.execute("delete from " + cls.__tablename__ + " cascade")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            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).limit(500)
    
        @classmethod
        def get_by_id(cls, other_id):
            return db.session.query(cls).filter(cls.id == other_id).one()
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        @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)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            )).one()
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        @classmethod
        def find_by_continent(cls, continent, page):
    
            return db.session.query(cls)\
                .filter(cls.continent_id == continent.id)\
                .paginate(page, per_page=ITEMS_PER_PAGE)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        @classmethod
        def get_germany(cls):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            return db.session.query(cls) \
                .filter(cls.country_territory_code == 'DEU') \
    
    thomaswoehlke's avatar
    thomaswoehlke committed
                .one_or_none()
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
    class EuropeData(db.Model):
        __tablename__ = 'europe_data'
    
        id = db.Column(db.Integer, primary_key=True)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        deaths_weekly = db.Column(db.Integer, nullable=False)
        cases_weekly = db.Column(db.Integer, nullable=False)
        notification_rate_per_100000_population_14days = db.Column(db.Float, nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
        europe_country_id = db.Column(db.Integer, db.ForeignKey('europe_country.id'), nullable=False)
        europe_country = db.relationship('EuropeCountry', lazy='joined')
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        europe_date_reported_id = db.Column(db.Integer, db.ForeignKey('common_date_reported.id'), nullable=False)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        europe_date_reported = db.relationship('EuropeDateReported', lazy='joined')
    
        @classmethod
        def remove_all(cls):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            # TODO: SQLalchemy instead of SQL
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            db.session.execute("delete from " + cls.__tablename__ + " cascade")
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            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).limit(500)
    
        @classmethod
        def get_by_id(cls, other_id):
            return db.session.query(cls).filter(cls.id == other_id).one()
    
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        @classmethod
    
    thomaswoehlke's avatar
    thomaswoehlke committed
        def find_by_date_reported(cls, europe_date_reported, page):
    
            #TODO: * Issue #43 /europe/date_reported
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            return db.session.query(cls).filter(
    
                cls.europe_date_reported_id == europe_date_reported.id)\
                .order_by(cls.notification_rate_per_100000_population_14days.desc())\
                .paginate(page, per_page=ITEMS_PER_PAGE)
    
        @classmethod
        def find_by_date_reported_notification_rate(cls, europe_date_reported, page):
            # TODO: * Issue #43 /europe/date_reported
            return db.session.query(cls).filter(
                cls.europe_date_reported_id == europe_date_reported.id) \
                .order_by(cls.notification_rate_per_100000_population_14days.desc()) \
                .paginate(page, per_page=ITEMS_PER_PAGE)
    
        @classmethod
        def find_by_date_reported_deaths_weekly(cls, europe_date_reported, page):
            # TODO: * Issue #43 /europe/date_reported
            return db.session.query(cls).filter(
                cls.europe_date_reported_id == europe_date_reported.id) \
                .order_by(cls.deaths_weekly.desc()) \
                .paginate(page, per_page=ITEMS_PER_PAGE)
    
        @classmethod
        def find_by_date_reported_cases_weekly(cls, europe_date_reported, page):
            # TODO: * Issue #43 /europe/date_reported
            return db.session.query(cls).filter(
                cls.europe_date_reported_id == europe_date_reported.id) \
                .order_by(cls.cases_weekly.desc()) \
                .paginate(page, per_page=ITEMS_PER_PAGE)
    
    thomaswoehlke's avatar
    thomaswoehlke committed
    
        @classmethod
        def find_by_country(cls, europe_country, page):
    
    thomaswoehlke's avatar
    thomaswoehlke committed
            return db.session.query(cls).filter(
    
    thomaswoehlke's avatar
    thomaswoehlke committed
                cls.europe_country_id == europe_country.id).paginate(page, per_page=ITEMS_PER_PAGE)