"""Model for the EUPointOfSaleByCountryView.""" from collections import OrderedDict from sqlalchemy import Column, Date, Integer, String from sqlalchemy.ext.declarative import declarative_base from reporting.models import persister BaseModel = declarative_base() class EUPointOfSaleByCountryView(BaseModel): """The EU point of sale View.""" __tablename__ = 'EU_POINT_OF_SALE_BY_COUNTRY_VIEW' subacct_id = Column( Integer, name='SUBACCT_ID', nullable=True, primary_key=True ) vendor_id = Column( Integer, name='VENDOR_ID', nullable=True, primary_key=True ) label_name = Column(String(250), nullable=True, name='Label Name') sub_label = Column(String(250), nullable=True, name='Sub Label') country = Column( String(45), nullable=False, name='Country', primary_key=True ) first_day_of_release_week = Column( Date, nullable=False, name='First Day of Release Week', primary_key=True, ) weekly_scans = Column(Integer, nullable=False, name='Weekly Scans') all_time_scans = Column(Integer, nullable=False, name='All Time Scans') @staticmethod def _group_by(model_object): return getattr(model_object, 'country') @classmethod def all(cls, params): """Get all for EUPointOfSaleByCountryView.""" filters = [ 'country', 'first_day_of_release_week', 'subacct_id', 'vendor_id', ] # order matters! order_by = [ 'vendor_id', 'subacct_id', 'country', 'first_day_of_release_week', ] return persister.get_all_point_of_sale_data( cls, params, filters, order_by, cls._group_by ) @classmethod def filter(cls, vendor_id, subacct_id): """Get filters for EUPointOfSaleByCountryView.""" filter_data = [ {'column': 'Country', 'alias': 'country'}, {'column': 'Sub Label', 'alias': 'sub_label'}, ] return persister.get_filter( cls.__tablename__, vendor_id, subacct_id, filter_data, filter_out_generic_products=False, ) @classmethod def headers(cls): """Get all headers.""" return [ 'Sub Label', 'Country', 'Scans by Weeks', 'Aggregated Scans', 'All Time Scans', ] def to_dict(self): """To dictionary.""" data = OrderedDict() data['Sub Label'] = self.sub_label data['Country'] = self.country return data