class SaleType(db.Model, ModelIter): __tablename__ = 'sale_types' id = db.Column(SmallInteger, primary_key=True) label = db.Column(db.String(30, collation=configs.DB_COLLATION), unique=True, nullable=False)
class Transformer(db.Model, ModelIter): __tablename__ = 'transformers' id = db.Column(SmallInteger, primary_key=True) label = db.Column(db.String(30, collation=configs.DB_COLLATION), unique=True, nullable=False)
class UserGroup(db.Model, ModelIter): __tablename__ = 'user_groups' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) users = relationship(User, secondary=user_user_groups, backref=db.backref('user_groups'))
class CompanyProfile(db.Model, ModelIter): __tablename__ = 'company_profile' allowed_widget = True fillable = ['name', 'address', 'contact', 'logo'] id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30, collation=configs.DB_COLLATION), unique=True, nullable=False) address = db.Column(db.Text(collation=configs.DB_COLLATION), nullable=False) contact = db.Column(db.String(10, collation=configs.DB_COLLATION), nullable=False) logo = db.Column(db.LargeBinary) settings = db.Column(MutableDict.as_mutable(db.JSON), comment='A JSON schema for global settings', nullable=False, server_default='{}')
class InverterModel(db.Model, ModelIter): __tablename__ = 'inverter_models' id = db.Column(SmallInteger, primary_key=True) label = db.Column(db.String(255, collation=configs.DB_COLLATION), unique=True, nullable=False) details = relationship( InstallationInverterModel, backref='inverter_model', primaryjoin=id == InstallationInverterModel.model_id, )
class Integration(db.Model, ModelIter): __tablename__ = 'integrations' fillable = ['service', 'data'] id = db.Column(db.Integer, primary_key=True) service = db.Column(db.String(30, collation=configs.DB_COLLATION), unique=True, nullable=False) data = db.Column(MutableList.as_mutable(db.JSON), comment='A JSON schema of integration service', nullable=False, server_default='[]')
class Audit(db.Model, ModelIter): __tablename__ = 'audits' id = db.Column(BigInteger, primary_key=True) date = db.Column(db.DateTime(), nullable=False, index=True, default=datetime.utcnow) user_id = db.Column(BigInteger, db.ForeignKey('users.id'), index=True, nullable=True) ip = db.Column(db.String(15), nullable=False) endpoint = db.Column(db.String(255, collation=configs.DB_COLLATION), nullable=False) method = db.Column(db.String(7, collation=configs.DB_COLLATION), nullable=False) headers = db.Column(db.Text(collation=configs.DB_COLLATION)) payload = db.Column(db.Text(collation=configs.DB_COLLATION)) response = db.Column(db.Text(collation=configs.DB_COLLATION)) user = relationship(User, uselist=False)
class Customer(db.Model, ModelIter): __tablename__ = 'customers' allowed_widget = True fillable = [ 'first_name', 'last_name', 'identification_number', 'primary_email', 'secondary_email', 'primary_phone', 'secondary_phone', 'address', 'source_project_id', 'province_id', ] id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(30, collation=configs.DB_COLLATION), nullable=False) last_name = db.Column(db.String(30, collation=configs.DB_COLLATION), nullable=False, index=True) identification_number = db.Column( db.String(25, collation=configs.DB_COLLATION), comment='National ID. i.e. Cedula, License', unique=True) primary_email = db.Column(db.String(50, collation=configs.DB_COLLATION), nullable=False, unique=True) secondary_email = db.Column(db.String(50, collation=configs.DB_COLLATION), nullable=True) primary_phone = db.Column(db.String(10, collation=configs.DB_COLLATION), nullable=False, unique=True) secondary_phone = db.Column(db.String(10, collation=configs.DB_COLLATION), nullable=True) address = db.Column(db.Text(collation=configs.DB_COLLATION), nullable=False) source_project = relationship(SourceProject, uselist=False, lazy='joined') province = relationship(Province, uselist=False, lazy='joined') created_on = db.Column(db.DateTime(), nullable=False, default=datetime.utcnow) updated_on = db.Column(db.DateTime(), nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) province_id = deferred( db.Column(db.Integer, db.ForeignKey('provinces.id'), nullable=False)) source_project_id = db.Column(db.Integer, db.ForeignKey('source_projects.id'))
class Province(db.Model, ModelIter): __tablename__ = 'provinces' # AKA states id = db.Column(db.Integer, primary_key=True) country = relationship(Country, uselist=False, backref='countries') name = db.Column(db.String(30, collation=configs.DB_COLLATION), unique=True, nullable=False) __table_args__ = (UniqueConstraint('country_id', 'name', name='province_country_id'), ) country_id = db.Column(db.Integer, db.ForeignKey('countries.id'), index=True)
class UserMessage(db.Model, ModelIter): __tablename__ = 'user_messages' allowed_widget = True id = db.Column(BigInteger, primary_key=True) user = relationship(User, uselist=False) date = db.Column(db.DateTime(), nullable=False, default=datetime.utcnow) read = db.Column(db.Boolean, nullable=False, index=True, server_default='0') subject = db.Column(db.String(255, collation=configs.DB_COLLATION), nullable=False) message = db.Column(db.Text(collation=configs.DB_COLLATION)) user_id = db.Column(BigInteger, db.ForeignKey('users.id'), index=True, nullable=True)
class Role(db.Model, ModelIter): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30, collation=configs.DB_COLLATION), index=True) permissions = db.Column(db.Text(collation=configs.DB_COLLATION)) @property def get_permissions(self): combined_permissions = default_access.copy() if self.permissions: for key, userGrants in json.loads(self.permissions).items(): for defaultKey, defaultGrants in default_access.items(): if key == defaultKey: for grant in defaultGrants: if grant not in userGrants: userGrants.append(grant) combined_permissions.update({key: userGrants}) return combined_permissions
class Commentable(db.Model, ModelIter): __tablename__ = 'comments' allowed_widget = True id = db.Column(db.Integer, primary_key=True) user = relationship(User, backref='notes') comment = db.Column(db.String) date = db.Column(db.DateTime(), default=datetime.utcnow) commentable_id = deferred(db.Column(db.Integer, index=True, nullable=False)) commentable_name = db.Column(db.String(96, collation=configs.DB_COLLATION), nullable=False) user_id = deferred( db.Column(BigInteger, db.ForeignKey('users.id'), index=True, nullable=True)) #__table_args__ = (db.Index('note_model_name_id_idx', commentable_name, commentable_id), ) __mapper_args__ = { 'polymorphic_on': commentable_name, 'polymorphic_identity': 'comment' }
class Installation(db.Model, ModelIter): __tablename__ = 'installations' allowed_widget = True fillable = [ 'installed_capacity', # kWp 'egauge_url', 'egauge_serial', 'egauge_mac', 'start_date', 'specific_yield', # kWh/kWp/Year 'project_id', 'sale_type_id', 'price_per_kwp', 'responsible_party', 'setup_summary' ] id = db.Column(db.Integer, primary_key=True) # project can have multiple installations because customers may ask to add more equipment after project is done project = relationship(CustomerProject, uselist=False, backref='installations', cascade='all, delete') responsible_party = db.Column(db.String(32)) installed_capacity = db.Column(db.Numeric(8, 3), nullable=False) sale_type = relationship(SaleType, uselist=False, lazy='joined') price_per_kwp = db.Column(db.Numeric(10, 2), nullable=False) setup_summary = db.Column( MutableDict.as_mutable(db.JSON), comment= 'A JSON schema that allows free form data, i.e. historical consumption data', server_default=('{' '"historical_consumption": [],' '"historical_power": [],' '"expected_generation": []' '}')) panels = relationship( InstallationPanelModel, backref='installations', primaryjoin=id == InstallationPanelModel.installation_id, lazy='joined') inverters = relationship( InstallationInverterModel, backref='installations', primaryjoin=id == InstallationInverterModel.installation_id, lazy='joined') egauge_url = db.Column(db.String(255, collation=configs.DB_COLLATION)) egauge_serial = db.Column(db.String(255, collation=configs.DB_COLLATION)) egauge_mac = db.Column(MacAddress) start_date = db.Column(db.DateTime()) specific_yield = db.Column(db.SmallInteger) @property def installation_size(self): if self.installed_capacity < 50: return 'Pequeño' elif 51 <= self.installed_capacity <= 200: return 'Mediano' elif 201 <= self.installed_capacity <= 500: return 'Comercial Pequeño' elif 501 <= self.installed_capacity <= 1000: return 'Comercial Mediano' elif 1001 <= self.installed_capacity <= 1500: return 'Comercial Grande' else: return 'Utilidad' @property def total_investment(self): return self.installed_capacity * self.price_per_kwp @property def annual_production(self): return self.installed_capacity * self.specific_yield sale_type_id = deferred( db.Column(db.Integer, db.ForeignKey('sale_types.id'), index=True, nullable=False)) project_id = deferred( db.Column(db.Integer, db.ForeignKey('customer_projects.id'), index=True, nullable=False))
class CustomerProject(db.Model, ModelIter): __tablename__ = 'customer_projects' allowed_widget = True fillable = [ 'name', 'address', 'lat', 'long', 'coordinates', 'nic', 'nic_title', 'circuit', 'ct', 'project_type_id', 'customer_id', 'province_id', 'distributor_id', 'rate_id', 'transformer_id', 'tr_capacity_id', 'phase_id', 'tension_id', ] id = db.Column(db.Integer, primary_key=True) customer = relationship(Customer, uselist=False, backref='customer_projects', cascade='all, delete') project_type = relationship(ProjectType, uselist=False, lazy='joined') name = db.Column(db.String(30, collation=configs.DB_COLLATION), nullable=False) address = db.Column(db.Text(collation=configs.DB_COLLATION), nullable=False) province = relationship(Province, uselist=False, lazy='joined') lat = db.Column(db.Float) long = db.Column(db.Float) coordinates = composite(Point, lat, long) distributor = relationship(Distributor, uselist=False, lazy='joined') nic = db.Column(db.Integer) nic_title = db.Column(db.String(64, collation=configs.DB_COLLATION)) rate = relationship(Rate, uselist=False, lazy='joined') circuit = db.Column(db.String(30, collation=configs.DB_COLLATION)) transformer = relationship(Transformer, uselist=False, lazy='joined') ct = db.Column(db.String(32)) # transformer ID number capacity = relationship(TrCapacity, uselist=False, lazy='joined') phase = relationship(Phase, uselist=False, lazy='joined') tension = relationship(Tension, uselist=False, lazy='joined') project_type_id = deferred( db.Column(db.Integer, db.ForeignKey('project_types.id'))) customer_id = deferred( db.Column(db.Integer, db.ForeignKey('customers.id'), index=True, nullable=False)) province_id = deferred( db.Column(db.Integer, db.ForeignKey('provinces.id'), nullable=False)) distributor_id = deferred( db.Column(db.Integer, db.ForeignKey('distributors.id'))) rate_id = deferred(db.Column(db.Integer, db.ForeignKey('rates.id'))) transformer_id = deferred( db.Column(db.Integer, db.ForeignKey('transformers.id'))) tr_capacity_id = deferred( db.Column(db.Integer, db.ForeignKey('tr_capacities.id'))) phase_id = deferred(db.Column(db.Integer, db.ForeignKey('phases.id'))) tension_id = deferred(db.Column(db.Integer, db.ForeignKey('tensions.id')))