diff --git a/src/flask_covid19/blueprints/app_all/all_model.py b/src/flask_covid19/blueprints/app_all/all_model.py index fdd3ee5794e18a41da0d85674b9115b4e013649f..0ff9734c116bab4f6930576237a49d519a5869a8 100644 --- a/src/flask_covid19/blueprints/app_all/all_model.py +++ b/src/flask_covid19/blueprints/app_all/all_model.py @@ -1,10 +1,11 @@ from datetime import date from database import db, ITEMS_PER_PAGE from sqlalchemy.orm import subqueryload -from sqlalchemy import not_ +from sqlalchemy import not_, and_ class BlueprintEntity(db.Model): + __tablename__ = 'all_entity' id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) @@ -79,13 +80,14 @@ class BlueprintEntity(db.Model): class BlueprintDateReported(BlueprintEntity): - __tablename__ = 'blueprint_date_reported' + __tablename__ = 'all_date_reported' + __mapper_args__ = {'concrete': True} __table_args__ = ( db.UniqueConstraint( 'date_reported_import_str', 'datum', 'year_day_of_year', - name="uix_blueprint_date_reported" + name="uix_all_date_reported" ), ) @@ -169,9 +171,10 @@ class BlueprintDateReported(BlueprintEntity): class BlueprintLocationGroup(BlueprintEntity): - __tablename__ = 'blueprint_location_group' + __tablename__ = 'all_location_group' + __mapper_args__ = {'concrete': True} __table_args__ = ( - db.UniqueConstraint('location_group', name='uix_blueprint_location_group'), + db.UniqueConstraint('location_group', name='uix_all_location_group'), ) def __str__(self): @@ -201,9 +204,10 @@ class BlueprintLocationGroup(BlueprintEntity): class BlueprintLocation(BlueprintEntity): - __tablename__ = 'blueprint_location' + __tablename__ = 'all_location' + __mapper_args__ = {'concrete': True} __table_args__ = ( - db.UniqueConstraint('location', name='uix_blueprint_location'), + db.UniqueConstraint('location', name='uix_all_location'), ) def __str__(self): @@ -214,7 +218,7 @@ class BlueprintLocation(BlueprintEntity): processed_full_update = db.Column(db.Boolean, nullable=False) location_code = db.Column(db.String(255), unique=True, nullable=True) location = db.Column(db.String(255), nullable=False, unique=True) - location_group_id = db.Column(db.Integer, db.ForeignKey('blueprint_location_group.id'), nullable=False) + location_group_id = db.Column(db.Integer, db.ForeignKey('all_location_group.id'), nullable=False) location_group = db.relationship( 'BlueprintLocationGroup', lazy='joined', @@ -246,16 +250,61 @@ class BlueprintLocation(BlueprintEntity): ).one() @classmethod - def find_by_location_group(cls, location_group, page: int): + def find_by_location_group(cls, location_group): + return db.session.query(cls).filter( + cls.location_group == location_group + ).order_by(cls.location).all() + + @classmethod + def get_by_location_group(cls, location_group, page: int): return db.session.query(cls).filter( cls.location_group == location_group ).order_by(cls.location).paginate(page, per_page=ITEMS_PER_PAGE) + @classmethod + def find_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group): + return db.session.query(cls).filter( + and_( + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id + ) + ).one_or_none() + + @classmethod + def get_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group): + return db.session.query(cls).filter( + and_( + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id + ) + ).one() + + @classmethod + def find_by_location_code_and_location(cls, location_code: str, location: str): + return db.session.query(cls).filter( + and_( + cls.location_code == location_code, + cls.location == location + ) + ).one_or_none() + + @classmethod + def get_by_location_code_and_location(cls, location_code: str, location: str): + return db.session.query(cls).filter( + and_( + cls.location_code == location_code, + cls.location == location + ) + ).one() + class BlueprintFactTableTimeSeries(BlueprintEntity): - __tablename__ = 'blueprint_data_timeline' + __tablename__ = 'all_data_timeline' + __mapper_args__ = {'concrete': True} __table_args__ = ( - db.UniqueConstraint('date_reported_id', name='uix_blueprint_data_timeline'), + db.UniqueConstraint('date_reported_id', name='uix_all_data_timeline'), ) def __str__(self): @@ -264,7 +313,7 @@ class BlueprintFactTableTimeSeries(BlueprintEntity): id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) - date_reported_id = db.Column(db.Integer, db.ForeignKey('blueprint_date_reported.id'), nullable=False) + date_reported_id = db.Column(db.Integer, db.ForeignKey('all_date_reported.id'), nullable=False) date_reported = db.relationship( 'BlueprintDateReported', lazy='joined', @@ -282,7 +331,7 @@ class BlueprintFactTableTimeSeries(BlueprintEntity): return date_reported_list @classmethod - def get_joungest_datum(cls): + def get_joungest_date_reported(cls): data = cls.get_date_reported_list() if len(data) > 0: return data.pop() @@ -303,9 +352,10 @@ class BlueprintFactTableTimeSeries(BlueprintEntity): class BlueprintFactTable(BlueprintFactTableTimeSeries): - __tablename__ = 'blueprint_data' + __tablename__ = 'all_data' + __mapper_args__ = {'concrete': True} __table_args__ = ( - db.UniqueConstraint('location_id', 'date_reported_id', name='uix_blueprint_data'), + db.UniqueConstraint('location_id', 'date_reported_id', name='uix_all_data'), ) def __str__(self): @@ -314,15 +364,31 @@ class BlueprintFactTable(BlueprintFactTableTimeSeries): id = db.Column(db.Integer, primary_key=True) processed_update = db.Column(db.Boolean, nullable=False) processed_full_update = db.Column(db.Boolean, nullable=False) - date_reported_id = db.Column(db.Integer, db.ForeignKey('blueprint_date_reported.id'), nullable=False) + date_reported_id = db.Column(db.Integer, db.ForeignKey('all_date_reported.id'), nullable=False) date_reported = db.relationship( 'BlueprintDateReported', lazy='joined', cascade='save-update', order_by='desc(BlueprintDateReported.datum)') - location_id = db.Column(db.Integer, db.ForeignKey('blueprint_location.id'), nullable=False) + location_id = db.Column(db.Integer, db.ForeignKey('all_location.id'), nullable=False) location = db.relationship( 'BlueprintLocation', lazy='joined', cascade='save-update', order_by='asc(BlueprintLocation.location)') + + @classmethod + def get_by_location(cls, location, page: int): + pass + + @classmethod + def find_by_location(cls, location): + pass + + @classmethod + def find_by_date_reported_and_location(cls, date_reported, location): + pass + + @classmethod + def get_by_date_reported_and_location(cls, date_reported, location, page: int): + pass diff --git a/src/flask_covid19/blueprints/data_who/who_model.py b/src/flask_covid19/blueprints/data_who/who_model.py index c03fa1b54c6dc9b9e9063a1a5371cb0656a1b60f..ee3884f110f3ef9712943bbe3158925973033f6a 100644 --- a/src/flask_covid19/blueprints/data_who/who_model.py +++ b/src/flask_covid19/blueprints/data_who/who_model.py @@ -75,54 +75,53 @@ class WhoCountry(BlueprintLocation): return self.location_group.__str__() + " : "+ self.location_code + " | " + self.location @classmethod - def find_by_country_code_and_country_and_who_region_id(cls, i_country_code, i_country, my_region): + def find_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: WhoCountryRegion): return db.session.query(cls).filter( and_( - cls.location_code == i_country_code, - cls.location == i_country, - cls.location_group_id == my_region.id + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id ) ).one_or_none() @classmethod - def get_by_country_code_and_country(cls, s_country_code: str, s_country: str): + def get_by_location_code_and_location_and_location_group(cls, location_code: str, location: str, location_group: WhoCountryRegion): return db.session.query(cls).filter( and_( - cls.location_code == s_country_code, - cls.location == s_country + cls.location_code == location_code, + cls.location == location, + cls.location_group_id == location_group.id ) ).one() @classmethod - def find_by_country_code(cls, i_country_code): + def find_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls).filter( - cls.location_code == i_country_code + and_( + cls.location_code == location_code, + cls.location == location + ) ).one_or_none() @classmethod - # @cache.memoize(50) - def get_by_country_code(cls, i_country_code): + def get_by_location_code_and_location(cls, location_code: str, location: str): return db.session.query(cls).filter( - cls.location_code == i_country_code + and_( + cls.location_code == location_code, + cls.location == location + ) ).one() @classmethod - def find_by_country(cls, i_country): + def find_by_location_group(cls, location_group: WhoCountryRegion, page: int): return db.session.query(cls).filter( - cls.location == i_country - ).one_or_none() + cls.location_group == location_group + ).order_by(cls.location).all() @classmethod - # @cache.memoize(50) - def get_by_country(cls, i_country): + def get_by_location_group(cls, location_group: WhoCountryRegion, page: int): return db.session.query(cls).filter( - cls.location == i_country - ).one() - - @classmethod - def get_who_countries_for_region(cls, region, page): - return db.session.query(cls).filter( - cls.location_group == region + cls.location_group == location_group ).order_by(cls.location).paginate(page, per_page=ITEMS_PER_PAGE) @@ -159,9 +158,9 @@ class WhoData(BlueprintFactTable): return None @classmethod - def find_by_datum(cls, datum: WhoDateReported): + def __query_by_date_reported(cls, date_reported: WhoDateReported): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.date_reported_id == date_reported.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) @@ -170,62 +169,54 @@ class WhoData(BlueprintFactTable): cls.cases_new.desc(), cls.deaths_cumulative.desc(), cls.cases_cumulative.desc() - ).all() + ) @classmethod - def find_page_by_datum(cls, datum: WhoDateReported, page): + def __query_by_location(cls, location: WhoCountry): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.location_id == location.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) - ).order_by( - cls.deaths_new.desc(), - cls.cases_new.desc(), - cls.deaths_cumulative.desc(), - cls.cases_cumulative.desc() - ).paginate(page, per_page=ITEMS_PER_PAGE) + ) @classmethod - def find_page_by_location(cls, location: WhoCountry, page): + def __query_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): return db.session.query(cls).filter( - cls.location_id == location.id - ).populate_existing().options( - joinedload(cls.location).joinedload(WhoCountry.location_group), - joinedload(cls.date_reported) - ).paginate(page, per_page=ITEMS_PER_PAGE) + and_( + cls.date_reported_id == date_reported.id, + cls.location_id == location.id + ) + ) + + @classmethod + def find_by_date_reported(cls, date_reported: WhoDateReported): + return cls.__query_by_date_reported(date_reported).all() + + @classmethod + def get_by_date_reported(cls, date_reported: WhoDateReported, page: int): + return cls.__query_by_date_reported(date_reported).paginate(page, per_page=ITEMS_PER_PAGE) + + @classmethod + def get_by_location(cls, location: WhoCountry, page: int): + return cls.__query_by_location(location).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod def find_by_location(cls, location: WhoCountry): - return db.session.query(cls).filter( - cls.location_id == location.id - ).populate_existing().options( - joinedload(cls.location).joinedload(WhoCountry.location_group), - joinedload(cls.date_reported) - ).all() + return cls.__query_by_location(location).all() @classmethod - def find_by_datum_and_location(cls, datum: WhoDateReported, location: WhoCountry): - return db.session.query(cls).filter( - and_( - cls.date_reported_id == datum.id, - cls.location_id == location.id - ) - ).one_or_none() + def find_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): + return cls.__query_by_date_reported_and_location(date_reported, location).one_or_none() @classmethod - def get_by_datum_and_location(cls, datum: WhoDateReported, location: WhoCountry): - return db.session.query(cls).filter( - and_( - cls.date_reported_id == datum.id, - cls.location_id == location.id - ) - ).one() + def get_by_date_reported_and_location(cls, date_reported: WhoDateReported, location: WhoCountry): + return cls.__query_by_date_reported_and_location(date_reported, location).one() @classmethod - def find_by_datum_order_by_cases_new(cls, datum: WhoDateReported, page): + def get_by_date_reported_order_by_cases_new(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.date_reported_id == date_reported.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) @@ -234,9 +225,9 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_datum_order_by_cases_cumulative(cls, datum: WhoDateReported, page): + def get_by_date_reported_order_by_cases_cumulative(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.date_reported_id == date_reported.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) @@ -245,9 +236,9 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_datum_order_by_deaths_new(cls, datum: WhoDateReported, page): + def get_by_date_reported_order_by_deaths_new(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.date_reported_id == date_reported.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) @@ -256,9 +247,9 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_datum_order_by_deaths_cumulative(cls, datum: WhoDateReported, page): + def get_by_date_reported_order_by_deaths_cumulative(cls, date_reported: WhoDateReported, page): return db.session.query(cls).filter( - cls.date_reported_id == datum.id + cls.date_reported_id == date_reported.id ).populate_existing().options( joinedload(cls.location).joinedload(WhoCountry.location_group), joinedload(cls.date_reported) @@ -267,7 +258,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_location_order_by_cases_new(cls, location: WhoCountry, page): + def get_by_location_order_by_cases_new(cls, location: WhoCountry, page): return db.session.query(cls).filter( cls.location_id == location.id ).populate_existing().options( @@ -278,7 +269,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_location_order_by_cases_cumulative(cls, location: WhoDateReported, page): + def get_by_location_order_by_cases_cumulative(cls, location: WhoDateReported, page): return db.session.query(cls).filter( cls.location_id == location.id ).populate_existing().options( @@ -289,7 +280,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_location_order_by_deaths_new(cls, location: WhoDateReported, page): + def get_by_location_order_by_deaths_new(cls, location: WhoDateReported, page): return db.session.query(cls).filter( cls.location_id == location.id ).populate_existing().options( @@ -300,7 +291,7 @@ class WhoData(BlueprintFactTable): ).paginate(page, per_page=ITEMS_PER_PAGE) @classmethod - def find_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported, page): + def __query_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported): return db.session.query(cls).filter( cls.location_id == location.id ).populate_existing().options( @@ -308,4 +299,12 @@ class WhoData(BlueprintFactTable): joinedload(cls.date_reported) ).order_by( cls.deaths_cumulative.desc() - ).paginate(page, per_page=ITEMS_PER_PAGE) + ) + + @classmethod + def get_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported, page): + return cls.__query_by_location_order_by_deaths_cumulative(location).paginate(page, per_page=ITEMS_PER_PAGE) + + @classmethod + def find_by_location_order_by_deaths_cumulative(cls, location: WhoDateReported): + return cls.__query_by_location_order_by_deaths_cumulative(location).all()