class HomeHub(Model): __tablename__ = 'home_hubs' home_hub_id = Column(db.Integer, autoincrement=True, primary_key=True, nullable=False) service_location_id = Column( db.Integer, db.ForeignKey('service_locations.service_location_id'), unique=True, nullable=False) market_id = Column(db.Integer, db.ForeignKey('markets.market_id'), nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) # Methods def __repr__(self): return f'<HomeHub home_hub_id={self.home_hub_id} service_location_id={self.service_location_id} created_at={self.created_at}>' # Relationships pv = relationship('Pv', backref=db.backref('home_hub'), uselist=False)
class Pv(Model): __tablename__ = 'pvs' pv_id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) home_hub_id = Column(db.Integer, db.ForeignKey('home_hubs.home_hub_id'), unique=True, nullable=False) meter_id = Column(db.Integer, db.ForeignKey('meters.meter_id'), unique=True, nullable=False) q_rated = Column(db.Float, nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) def __repr__(self): return f'<Pv pv_id={self.pv_id} home_hub_id={self.home_hub_id} created_at={self.created_at}>'
class MeterInterval(Model): __tablename__ = 'meter_intervals' meter_interval_id = Column(db.Integer, autoincrement=True, primary_key=True, nullable=False) meter_id = Column(db.Integer, db.ForeignKey('meters.meter_id'), nullable=False) rate_id = Column(db.Integer, db.ForeignKey('rates.rate_id'), nullable=False) start_time = Column(TIMESTAMP, nullable=False) end_time = Column(TIMESTAMP, nullable=False) e = Column(db.Float, nullable=False) qmtp = Column(db.Float, nullable=False) p_bid = Column(db.Float, default=0, nullable=False) q_bid = Column(db.Float, default=0, nullable=False) mode = Column(db.Boolean(create_constraint=True), default=0, nullable=False) is_bid = Column(db.Boolean(), default=False, nullable=False) @staticmethod def get_interval_coverage(interval_id_list): '''Takes in list of meter interval ids, returns list of tuples for start and end times in ISO8601 format''' selected_intervals = MeterInterval.query.filter( MeterInterval.meter_interval_id.in_(interval_id_list)).all() start_end_tuples_list = [] for meter_interval in selected_intervals: start_end_tuples_list.append( (meter_interval.start_time, meter_interval.end_time)) return start_end_tuples_list # Methods def __repr__(self): return f'<MeterInterval meter_interval_id={self.meter_interval_id} meter_id={self.meter_id} end_time={self.end_time} e={self.e}>'
class User(UserMixin, SurrogatePK, Model): """A user of the app.""" __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False) email = Column(db.String(80), unique=True, nullable=False) #: The hashed password password = Column(db.Binary(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) first_name = Column(db.String(30), nullable=True) last_name = Column(db.String(30), nullable=True) active = Column(db.Boolean(), default=False) is_admin = Column(db.Boolean(), default=False) def __init__(self, username, email, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def full_name(self): """Full user name.""" return '{0} {1}'.format(self.first_name, self.last_name) def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username)
class Group(Model): __tablename__ = 'groups' group_id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) role_id = Column(db.Integer, db.ForeignKey('roles.role_id'), primary_key=True, nullable=False) user_id = Column(db.Integer, db.ForeignKey('users.id'), primary_key=True, nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) # Unique constraint for role_id and user_id __table_args__ = (UniqueConstraint('role_id', 'user_id', name='_role_user_uc'), ) # Methods def __repr__(self): return f'<Group group_id={self.group_id} role_id={self.role_id} user_id={self.user_id}>'
class ServiceLocation(Model): __tablename__ = 'service_locations' service_location_id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) alternate_service_location_id = Column(db.String(64), unique=True) address_id = Column(db.Integer, db.ForeignKey('addresses.address_id'), unique=True, nullable=False) map_location = Column(db.String(64), nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) def __repr__(self): return f'<ServiceLocation service_location_id={self.service_location_id} address_id={self.address_id}>' # Relationships home_hub = relationship('HomeHub', backref=db.backref('service_location'), uselist=False) meters = relationship('Meter', backref=db.backref('service_location'))
class User(db.Model, UserMixin): __tablename__ = "web_users" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) last_login_at = db.Column(db.DateTime()) current_login_at = db.Column(db.DateTime()) last_login_ip = db.Column(db.String(100)) current_login_ip = db.Column(db.String(100)) login_count = db.Column(db.Integer) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) timezone = db.Column(db.String(25)) steamid = db.Column(db.String(17), unique=True) roles = db.relationship("Role", secondary=roles_users, backref=db.backref("users", lazy="dynamic")) def __repr__(self): return "%s - %s" % (self.code, self.name)
class HceBids(Model): __tablename__ = 'hce_bids' bid_id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) start_time = Column(TIMESTAMP, nullable=False) end_time = Column(TIMESTAMP, nullable=False) p_bid = Column(db.Float, default=0, nullable=False) q_bid = Column(db.Float, default=0, nullable=False) is_supply = Column(db.Boolean(), default=True, nullable=False) comment = Column(db.String(512), nullable=False) market_id = Column(db.Integer, db.ForeignKey('markets.market_id'), nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) def __repr__(self): return f'<HceBids bid_id={self.bid_id}>'
class Meter(Model): __tablename__ = 'meters' # Composite primary key: meter_id, utility_id, and service_location_id meter_id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) utility_id = Column(db.Integer, db.ForeignKey('utilities.utility_id'), primary_key=True, nullable=False) service_location_id = Column( db.Integer, db.ForeignKey('service_locations.service_location_id'), primary_key=True, nullable=False) home_hub_id = Column(db.Integer, db.ForeignKey('home_hubs.home_hub_id'), nullable=False) transformer_id = Column(db.Integer, db.ForeignKey('transformers.transformer_id'), nullable=True) alternate_meter_id = Column(db.String(64), unique=True) feeder = Column(db.String(45), nullable=False) substation = Column(db.String(45), nullable=False) meter_type = Column(db.Enum(MeterType), nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) # Methods def get_interval_count(self, start, end): '''Takes in start and end ISO8601 times, returns the interval count (integer) between start / end times, inclusively''' self_intervals = self.meter_intervals selected_intervals = [] if start == None: start = datetime.now() - timedelta(days=1) if end == None: end = datetime.now() for meter_interval in self_intervals: if meter_interval.start_time >= start and meter_interval.end_time <= end: selected_intervals.append(meter_interval) return len(selected_intervals) def get_rates(self): '''Returns meter instance's rates as a list''' rates = [] for meter_interval in self.meter_intervals: if meter_interval.rate.description not in rates: rates.append(meter_interval.rate.description) return rates def get_channels(self): '''Returns meter instance's channel settings as a list''' channels = Meter.query.\ join(Meter.channels). \ all() return channels def get_all_intervals(self): '''Returns all meter instances's intervals in a list''' intervals_list = [] for meter_interval in self.meter_intervals: intervals_list.append(meter_interval.meter_interval_id) return intervals_list def __repr__(self): return f'<Meter meter_id={self.meter_id} is_active={self.is_active}>' # Relationships channels = relationship('Channel', backref=db.backref('meter')) meter_intervals = relationship('MeterInterval', backref=db.backref('meter'))
class User(UserMixin, Model): __tablename__ = 'users' id = Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) # User email information email = Column(db.String(255), unique=True, nullable=False) email_confirmed_at = Column(TIMESTAMP) # User information first_name = Column(db.String(64), nullable=False) last_name = Column(db.String(64), nullable=False) address_id = Column(db.Integer, db.ForeignKey('addresses.address_id'), nullable=False) utility_id = Column(db.Integer, db.ForeignKey('utilities.utility_id'), nullable=False) is_active = Column(db.Boolean(), default=False, nullable=False) is_archived = Column(db.Boolean(), default=False, nullable=False) updated_at = Column( TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) created_at = Column(TIMESTAMP, server_default=func.now()) # Methods def get_roles(self): '''Returns list of user role objects''' roles = [] for group in self.groups: roles.append(group.role) return roles def does_user_role_exist(self, role_name): '''Returns true or false whether user has assigned role''' for group in self.groups: if group.role.name.value == role_name: return True return False def __repr__(self): return f'<User id={self.id} email_id={self.email}>' # Relationships login = relationship('Login', backref=db.backref('user'), uselist=False) notifications = relationship('Notification', backref=db.backref('user')) alerts = relationship('Alert', backref=db.backref('user'))