class Job(BaseModel): class Types: CATEGORY_LETTER = 'CATEGORY_LETTER' TOP_LEVEL_CATEGORIES = 'TOP_LEVEL_CATEGORIES' PODCAST_LOOKUP = 'PODCAST_LOOKUP' id = AutoField(primary_key=True) type = CharField(index=True) metadata = JSONField(null=False, default={}) interval = IntegerField() created_at = DateTimeField(default=datetime.datetime.now) last_success_at = DateTimeField(null=True) next_time_at = DateTimeField(index=True, default=datetime.datetime.now) class Meta: indexes = ( (('type', 'metadata'), True), ) def __str__(self): return "[{}] {} - {}".format(self.created_at, self.id, self.type) @staticmethod def take(): table = Job._meta.table_name job_id = db.execute_sql( ('SELECT j2.id FROM (SELECT * FROM {} as j ' + 'WHERE j.next_time_at <= %s ORDER BY next_time_at) as j2 ' + 'WHERE pg_try_advisory_lock(%s, j2.id) LIMIT 1').format(table), params=[datetime.datetime.now(), settings.JOB_LOCK_PREFIX] ) job_result = job_id.fetchall() if not job_result: return None return Job.get_by_id(job_result[0][0]) def release(self): db.execute_sql('SELECT pg_advisory_unlock(%s, %s)', [settings.JOB_LOCK_PREFIX, self.id])
class Company(BaseModel): """ Model class for the companies table. :param company_name: Name of the companies :param email: Email of the companies :param address_1: address_1 of the companies :param address_2: address_2 of the companies :param city: city of the companies :param province: province of the companies :param zipcode: zipcode of the companies :param country: country of the companies :param phone: phone of the companies :param password: Password of the companies :param state: State of the companies. Should be an enum value of the above :param created_at: Time of creation of the row :param modified_at: Time of last modification of the row """ id = AutoField() # Primary key company_name = CharField() email = CharField(unique=True) password = CharField() address_1 = CharField(null=True) address_2 = CharField(null=True) city = CharField(null=True) province = CharField(null=True) zipcode = CharField(null=True) country = CharField(null=True) phone = CharField(null=True) state = CharField(null=True) created_at = DateTimeField(default=datetime.now()) modified_at = DateTimeField(default=datetime.now()) def has_address(self): return (self.address_1 is not None and self.city is not None and self.province is not None and self.zipcode is not None and self.country is not None) def is_active(self): return self.state == 'ACTIVE'
class DbContractData(ModelBase): id = AutoField() symbol: str = CharField() exchange: str = CharField() name: str = CharField() size: float = FloatField() pricetick: float = FloatField() min_volume: float = FloatField( default=1) # minimum trading volume of the contract stop_supported: bool = BooleanField( default=False) # whether server supports stop order net_position: bool = BooleanField( default=False) # whether gateway uses net position volume history_data: bool = BooleanField( default=False) # whether gateway provides bar history data # option_strike: float = FloatField() # option_underlying: float = FloatField() # vt_symbol of underlying contract class Meta: database = db
class Mentor(BaseModel): id = AutoField(primary_key=True) first_name = TextField() last_name = TextField() email = TextField(index=True, unique=True) phone = TextField() created_on = DateTimeField(default=datetime.datetime.now) workplace = TextField(null=True) job_title = TextField(null=True) bio = TextField(null=True) academic_bio = TextField(null=True) job_search = TextField(null=True) availability = TextField(null=True) match_preferences = TextField(null=True) multiple_mentees = BooleanField(default=False) can_simulate = BooleanField(default=False) technologies = ArrayField(field_class=TextField, null=True) years_experience = TextField(null=True) comments = TextField(null=True) class Meta: indexes = ((('first_name', 'last_name'), True), )
class EventModel(BaseModel): id = AutoField() bucket = ForeignKeyField(BucketModel, backref='events', index=True) timestamp = DateTimeField(index=True, default=datetime.now) duration = DecimalField() datastr = CharField() @classmethod def from_event(cls, bucket_key, event: Event): return cls(bucket=bucket_key, id=event.id, timestamp=event.timestamp, duration=event.duration.total_seconds(), datastr=json.dumps(event.data)) def json(self): return { "id": self.id, "timestamp": self.timestamp, "duration": float(self.duration), "data": json.loads(self.datastr) }
class UtteranceCode(BaseModel): utterance_code_id = AutoField() utterance = ForeignKeyField(Utterance, index=True, null=False, backref="codes", on_update="CASCADE", on_delete="CASCADE", column_name='utterance_id') property_value = ForeignKeyField(PropertyValue, index=True, null=False, backref="codes", on_update="CASCADE", on_delete="CASCADE", column_name='property_value_id') class Meta: constraints = [ SQL('CONSTRAINT utt_id_pv_iq_unique UNIQUE(utterance_id, property_value_id)' ) ]
class Tag(Model): """ 博主个人文章分类表:Linux、python、面试心得、鸡汤 分类表和用户表是多对一的关系,由于用户和站点是一对一,分类表与站点也是多对一的关系 多 """ id = AutoField(primary_key=True) title = CharField(max_length=32, verbose_name='标签名称') blog_id = ForeignKeyField(BlogSite, backref='blog', verbose_name="多对一站点表") create_time = DateTimeField(default=datetime.datetime.now, verbose_name='创建时间') update_time = DateTimeField(null=False, default=datetime.datetime.now, verbose_name='修改时间') def save(self, *args, **kwargs): self.update_time = datetime.datetime.now() return super(Tag, self).save(*args, **kwargs) class Meta: database = database table_name = 'tag'
class Student(BaseModel): """ Model class for the students table. :param first_name: First name of the students :param last_name: Last name of the students :param email: Email of the students :param password: Password of the students :param state: State of the students. Should be an enum value of the above :param created: Time of creation of the row """ id = AutoField() # Primary key first_name = CharField() last_name = CharField() password = CharField() email = CharField(unique=True) state = CharField() created_at = DateTimeField(default=datetime.now()) modified_at = DateTimeField(default=datetime.now()) def is_active(self): return self.state == 'ACTIVE'
class User(BaseModel): id = AutoField() username = CharField(null=False, unique=True) password = CharField(null=False) @classmethod def create_user(cls, username, password): if not username or not password: raise ValueError user = cls.create(username=username, password=password) return user @classmethod def find_all(cls): users = cls.select() return users @classmethod def find_by_user_id(cls, user_id): user = cls.get_or_none(cls.id == user_id) return user @classmethod def update_by_id(cls, user_id, update_data): query = cls.update(**update_data).where(cls.id == user_id) query.execute() user = cls.get_or_none(cls.id == user_id) return user @classmethod def delete_by_id(cls, user_id): query = cls.delete().where(cls.id == user_id) query.execute() @classmethod def delete_by_username(cls, username): query = cls.delete().where(cls.username == username) query.execute()
class User(BaseModel): class Meta: table_name = 'user' user_id = AutoField(column_name='user_id') first_name = CharField(null=True) last_name = CharField(null=True) mail = CharField(null=False, unique=True) nickname = CharField() user_password = CharField() picture = CharField(default='default.png') role = ForeignKeyField(Role, field='role_id') date_registered = DateTimeField(default=datetime.now) @staticmethod def encode_password(password): if not isinstance(password, six.binary_type): password = password.encode('utf-8') m = hashlib.md5() m.update(password) return m.hexdigest() @classmethod def create(cls, **query): role = Role.get(Role.role == 'user') # default role is user # cls.role = role return super(User, cls).create(role=role, **query) def is_admin(self): return self.role.role == 'admin' def __repr__(self): date_registered_str = self.date_registered.strftime( config.DEFAULT_DATE_FORMAT) return '{nickname} [{mail}] ({date_registered})'.format( nickname=self.nickname, mail=self.mail, date_registered=date_registered_str)
class GlobalValue(BaseModel): global_value_id = AutoField() global_property = ForeignKeyField(GlobalProperty, backref="global_values", index=True, null=False, column_name="global_property_id", on_update="CASCADE", on_delete="CASCADE") gv_value = TextField(null=False, column_name="gv_value") gv_description = TextField(null=False, unique=False) class Meta: constraints = [ SQL('CONSTRAINT gv_value_gp_id_unique UNIQUE(gv_value, global_property_id)' ), SQL('CONSTRAINT gv_source_id_gp_id_unique UNIQUE(source_id, global_property_id)' ) ] def __repr__(self): return "<GlobalValue: {0} | {1}>".format(self.global_value_id, self.gv_value)
class Ads(BaseModel): id = AutoField() price = FloatField() coin = CharField(default='BIP') post_time = DateTimeField(null=True) advertiser = ForeignKeyField(Users, on_update='CASCADE', db_column='user_id', backref='ads') channel = ForeignKeyField(Channels, on_update='CASCADE', db_column='channel_id', backref='ads') uuid = CharField() text = TextField() photo_id = TextField(null=True) false_reason = TextField(null=True) is_declined = BooleanField(default=False) is_approved = BooleanField(default=False) is_paid = BooleanField(default=False) is_processed = BooleanField(default=False) message_id = IntegerField() created_dt = DateTimeField(default=dt.utcnow)
class Player(BaseModel): id = AutoField(primary_key=True) first_name = CharField(max_length=25) last_name = CharField(max_length=50) age = IntegerField(default=0) pas = IntegerField(default=0) # pass palavra reservada python finish = IntegerField(default=0) speed = IntegerField(default=0) dribbling = IntegerField(default=0) defensive = IntegerField(default=0) mentality = IntegerField(default=0) foot = IntegerField(default=0) position = EnumField(Position) time = ForeignKeyField(Time, backref='players') class Meta: table_name = 'player' @property def name(self): return f'{self.first_name} {self.last_name}'
class DbBarData(Model): """ Candlestick bar data for database storage. Index is defined unique with datetime, interval, symbol """ id = AutoField() symbol: str = CharField() exchange: str = CharField() datetime: datetime = DateTimeField() interval: str = CharField() volume: float = FloatField() open_interest: float = FloatField() open_price: float = FloatField() high_price: float = FloatField() low_price: float = FloatField() close_price: float = FloatField() class Meta: database = db indexes = ((("symbol", "exchange", "interval", "datetime"), True),)
class User(Model, UserMixin): pk = AutoField(primary_key=True) name = CharField(max_length=200, unique=True, **collate_kw) email = CharField(max_length=200, null=True) password = TextField(null=False) active = BooleanField(default=True, index=True) created = DateTimeField(default=datetime.utcnow) admin = BooleanField(default=False) def __int__(self) -> int: # pragma: nocover return self.pk def get_id(self) -> str: return str(self.pk) def is_active(self) -> bool: # pragma: nocover return self.active def set_password(self, password: str): self.password = generate_password_hash(password) def check_password(self, password: str) -> bool: return check_password_hash(self.password, password)
class SummaryModel(BaseModel): id = AutoField() bucket = ForeignKeyField(BucketModel, backref="summary", index=True) application = ForeignKeyField(ApplicationsModel, backref="application", index=True) timestamp = DateField(index=True, default=datetime.now) duration = DecimalField() def json(self): return { "id": self.id, "timestamp": iso8601.parse_date(str(self.timestamp)).astimezone( timezone.utc).isoformat(), "duration": float(self.duration), "application": self.application.applicaionname, "bucket": self.bucket.hostname, }
class CveMetadata(BaseModel): """cve_metadata table""" id = AutoField() cve = TextField(index=True, null=False, unique=True) cvss3_score = DecimalField(index=True, null=True) cvss3_metrics = TextField(null=True) description = TextField(null=False) impact_id = ForeignKeyField(column_name="impact_id", model=CveImpact, field="id") public_date = DateTimeField(null=False) modified_date = DateTimeField(null=False) cvss2_score = DecimalField(index=True, null=True) cvss2_metrics = TextField(null=True) redhat_url = TextField(null=True) secondary_url = TextField(null=True) advisories_list = BinaryJSONField(null=True) celebrity_name = TextField(column_name='celebrity_name', null=True) exploits = BooleanField(default=False, null=False) class Meta: """cve_metadata table metadata""" table_name = 'cve_metadata'
class Thermostat(BaseModel): id = AutoField() number = IntegerField(unique=True) name = CharField(default='Thermostat') sensor = IntegerField() pid_heating_p = FloatField(default=120) pid_heating_i = FloatField(default=0) pid_heating_d = FloatField(default=0) pid_cooling_p = FloatField(default=120) pid_cooling_i = FloatField(default=0) pid_cooling_d = FloatField(default=0) automatic = BooleanField(default=True) room = IntegerField() start = IntegerField() valve_config = CharField(default='cascade') # options: cascade, equal thermostat_group = ForeignKeyField(ThermostatGroup, backref='thermostats', on_delete='CASCADE', default=1)
class Fiber(BaseModel): actuator = ForeignKeyField(column_name='actuator_pk', field='pk', model=Actuator, null=True, backref='fibers') fiber_status = ForeignKeyField(column_name='fiber_status_pk', field='pk', model=FiberStatus, null=True, backref='fibers') fiberid = IntegerField(null=True) throughput = FloatField(null=True) pk = AutoField() spectrograph = ForeignKeyField(backref='fibers', column_name='spectrograph_pk', field='pk', model=Spectrograph, null=True) class Meta: table_name = 'fiber' schema = 'targetdb'
class DbCtaTrade(Model): """docstring for CtaTrade""" id = AutoField() strategyName: str = CharField() tradeid: str = CharField() datetime: datetime = DateTimeField() symbol: str = CharField() direction: str = CharField() offset: str = CharField() price: float = FloatField() volume: float = FloatField() class Meta: database = db indexes = ((("strategyName", "symbol", "datetime"), True), ) @staticmethod def from_ctatrade(ctatrade: CtaTrade): db_ctatrade = DbCtaTrade() db_ctatrade.strategyName = ctatrade.strategyName db_ctatrade.tradeid = ctatrade.tradeid db_ctatrade.datetime = ctatrade.datetime db_ctatrade.symbol = ctatrade.symbol db_ctatrade.direction = ctatrade.direction db_ctatrade.offset = ctatrade.offset db_ctatrade.price = ctatrade.price db_ctatrade.volume = ctatrade.volume return db_ctatrade @staticmethod def save_data(data): with db.atomic(): DbCtaTrade.insert( data.__data__).on_conflict_replace().execute()
class ObraPersonSnapshot(ObraModel): """ A point in time record of OBRA member data. The OBRA website doesn't make historical data available, so we store a timestamped copy every time we do a lookup. Doesn't help with really old upgrades, but it should be useful going forward. """ id = AutoField(verbose_name='Scrape ID', primary_key=True) date = DateField(verbose_name='Scrape Date') person = ForeignKeyField(verbose_name='Person', model=Person, backref='obra', on_update='RESTRICT', on_delete='RESTRICT') license = IntegerField(verbose_name='License', null=True) mtb_category = IntegerField(verbose_name='MTB Category', default=3) dh_category = IntegerField(verbose_name='DH Category', default=3) ccx_category = IntegerField(verbose_name='CX Category', default=5) road_category = IntegerField(verbose_name='Road Category', default=5) track_category = IntegerField(verbose_name='Track Category', default=5) class Meta: indexes = ((('date', 'person'), True), ) def category_for_discipline(self, discipline): discipline = discipline.replace('mountain_bike', 'mtb') discipline = discipline.replace('short_track', 'mtb') discipline = discipline.replace('cyclocross', 'ccx') discipline = discipline.replace('criterium', 'road') discipline = discipline.replace('time_trial', 'road') discipline = discipline.replace('circuit', 'road') discipline = discipline.replace('gran_fondo', 'road') discipline = discipline.replace('gravel', 'road') discipline = discipline.replace('tour', 'road') discipline = discipline.replace('downhill', 'dh') discipline = discipline.replace('super_d', 'dh') return getattr(self, discipline + '_category')
class Preset(BaseModel): class Types(object): MANUAL = 'manual' SCHEDULE = 'schedule' AWAY = 'away' VACATION = 'vacation' PARTY = 'party' ALL_TYPES = [Types.MANUAL, Types.SCHEDULE, Types.AWAY, Types.VACATION, Types.PARTY] DEFAULT_PRESET_TYPES = [Types.AWAY, Types.VACATION, Types.PARTY] DEFAULT_PRESETS = {ThermostatGroup.Modes.HEATING: dict(zip(DEFAULT_PRESET_TYPES, [16.0, 15.0, 22.0])), ThermostatGroup.Modes.COOLING: dict(zip(DEFAULT_PRESET_TYPES, [25.0, 38.0, 25.0]))} TYPE_TO_SETPOINT = {Types.AWAY: 3, Types.VACATION: 4, Types.PARTY: 5} SETPOINT_TO_TYPE = {setpoint: preset_type for preset_type, setpoint in TYPE_TO_SETPOINT.items()} id = AutoField() type = CharField() # Options: see Preset.Types heating_setpoint = FloatField(default=14.0) cooling_setpoint = FloatField(default=30.0) active = BooleanField(default=False) thermostat = ForeignKeyField(Thermostat, backref='presets', on_delete='CASCADE')
class MovCajaModel(ModeloBase): idmovcaja = AutoField() fecha = DateField(default="0000-00-00") idtipocomp = ForeignKeyField(TipoComprobante, default=0, db_column='idtipocomp') numcomp = CharField(max_length=12, default='000000000000') importe = DecimalField(max_digits=12, decimal_places=2, default=0) banco = IntegerField(default=0) sucursal = IntegerField(default=0) numche = CharField(max_length=15, default='') vence = DateField(default='0000-00-00') cuit = CharField(max_length=13, default='') estado = CharField(max_length=1, default='I') idcabfact = IntegerField(default=0) idproveedor = IntegerField(default=0) idcliente = IntegerField(default=0) usuario = CharField(max_length=30, default='', db_column='_usuario') fechagraba = DateField(default=datetime.now(), db_column='_fecha') hora = CharField(db_column='_hora', default='00:00:00') class Meta: table_name = 'movcaja'
class Command(BaseCommandsModel): uid = AutoField() target = ForeignKeyField(Target, backref="commands") name = TextField() value = TextField() def to_dict(self): return {"name": self.name, "value": self.value} def get_value(self): return self.value def update_name(self, new_name): check_command = Command.get_or_none(Command.name % new_name) if check_command: return False else: self.name = new_name self.save() return True class Meta: indexes = ((('target', 'name'), True), )
class SystemVulnerabilities(BaseModel): """system_vulnerabilities_active table""" id = AutoField() system_id = ForeignKeyField(column_name="system_id", model=SystemPlatform, field="id") cve_id = ForeignKeyField(column_name="cve_id", model=CveMetadata, field="id") first_reported = DateTimeField(null=False) when_mitigated = DateTimeField(null=True) status_id = ForeignKeyField(column_name="status_id", model=Status, field="id") status_text = TextField() rule_id = ForeignKeyField(column_name="rule_id", model=InsightsRule, field="id") rule_hit_details = TextField() mitigation_reason = TextField() class Meta: """system_vulnerabilities_active table metadata""" table_name = "system_vulnerabilities_active"
class DaySchedule(BaseModel): id = AutoField() index = IntegerField() content = TextField() mode = CharField(default=ThermostatGroup.Modes.HEATING) thermostat = ForeignKeyField(Thermostat, backref='day_schedules', on_delete='CASCADE') @property def schedule_data(self): # type: () -> Dict[int, float] return json.loads(self.content) @schedule_data.setter def schedule_data(self, value): # type: (Dict[int, float]) -> None self.content = json.dumps(value) def get_scheduled_temperature(self, seconds_in_day): # type: (int) -> float seconds_in_day = seconds_in_day % 86400 data = self.schedule_data last_value = data.get(0) for key in sorted(data): if key > seconds_in_day: break last_value = data[key] return last_value
class AccountGroupMap(BaseModel): map_id = AutoField() account_id = IntegerField() group_id = IntegerField() is_admin = IntegerField(default=0) def validate(self): if self.is_admin != 0 and self.is_admin != 1: raise ValueError("is_admin must be 1 or 0") # helper method for formatting API output def format_member(self, map, account): return { 'member_id': map.map_id, 'group_id': map.group_id, 'is_admin': map.is_admin, 'account': account.to_clean_dict() } class Meta(object): table_name = 'account_group_map' indexes = ( (('account_id', 'group_id'), True), )
class Refeicoes_Dieta(BaseModel): id_refeicoes_dieta = AutoField(primary_key=True, column_name="id_refeicoes_dieta") id_refeicao = ForeignKeyField(Refeicao, column_name="id_refeicao") id_dieta = ForeignKeyField(Dieta, column_name="id_dieta", backref="refeicoes_dieta") def nome(self): r = Refeicao.get_by_id(self.id_refeicao) return r.nome_refeicao def proteina(self): r = Refeicao.get_by_id(self.id_refeicao) prot = Refeicao.proteina(r) return prot def carboidrato(self): r = Refeicao.get_by_id(self.id_refeicao) carb = Refeicao.carboidrato(r) return carb def caloria(self): r = Refeicao.get_by_id(self.id_refeicao) cal = Refeicao.caloria(r) return cal def gordura(self): r = Refeicao.get_by_id(self.id_refeicao) gord = Refeicao.gordura(r) return gord def colesterol(self): r = Refeicao.get_by_id(self.id_refeicao) col = Refeicao.colesterol(r) return col
class Exposure(BaseModel): duration = FloatField(null=True) pk = AutoField() sn2_median = FloatField(null=True) start_mjd = IntegerField(null=True) tile = ForeignKeyField(column_name='tile_pk', field='pk', model=Tile, null=True, backref='exposures') weather = ForeignKeyField(column_name='weather_pk', field='pk', model=Weather, null=True, backref='exposures') simulation = ForeignKeyField(column_name='simulation_pk', field='pk', model=Simulation, null=True, backref='exposures') class Meta: table_name = 'exposure' schema = 'targetdb'
class Bairro(BaseModel): id_bairro = AutoField(primary_key=True) id_cidade = ForeignKeyField(Cidade, column_name="id_cidade") nome_bairro = CharField(200),