class Url(db.Model): id_url = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(1000)) # 50 because of custom urls shortenend_url = db.Column(db.String(50), unique=True) # 1 = random, 2 = custom url_type = db.Column(db.Integer)
class Effect(db.Model): __tablename__ = 'effects' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) impactId = db.Column(db.Integer) calculationId = db.Column(db.Integer) def insert(effect_new): effect = Effect( name=effect_new['name'], impactId=effect_new['impactId'], calculationId=effect_new['calculationId'], ) db.session.add(effect) db.session.commit() return 'success' def update(effect_new): effect = db.session.query(Effect).filter( Effect.id == effect_new['id']).first() effect.name = effect_new['name'] effect.impactId = effect_new['impactId'] effect.calculationId = effect_new['calculationId'] db.session.add(effect) db.session.commit() return 'success'
class PlotModel(db.Model): __tablename__ = 'plot' id = db.Column(db.Integer, primary_key=True) cpm = db.Column(db.String(30)) values = db.Column(db.JSON) data = db.Column(db.JSON) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('UserModel') # type: src.UserModel def __init__(self, cpm: str, values, data, user_id): self.cpm = cpm self.values = values self.data = data self.user_id = user_id def json(self): return { 'plot': { 'user': self.user.username, 'cpm': self.cpm, 'values': self.values, 'data': self.data } } def save(self): db.session.add(self) db.session.commit() @classmethod def find_by_id(cls, _id) -> PlotModel: return cls.query.get(_id)
class Skill(db.Model): __tablename__ = 'skills' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) skillCategoryId = db.Column(db.Integer) elements = db.Column(db.String(100)) def insert(rowData): record = Skill( name = rowData['name'], skillCategoryId = rowData['skillCategoryId'], elements = rowData['elements'] ) db.session.add(record) db.session.commit() return 'success' def update(rowData): record = db.session.query(Skill).filter(Skill.id==rowData['id']).first() record.name = rowData['name'] record.skillCategoryId = rowData['skillCategoryId'] record.elements = rowData['elements'] db.session.add(record) db.session.commit() return 'success'
class EstabelecimentoModel(db.Model): __tablename__ = 'estabelecimentos' id = db.Column(db.Integer, primary_key=True) nome = db.Column(db.String()) cnpj = db.Column(db.String()) dono = db.Column(db.String()) telefone = db.Column(db.String()) def __repr__(self): return f"<Estabelecimento {self.nome}>" @property def serialize(self): cnpj = self.format_cnpj(self.cnpj) return { 'id': self.id, 'nome': self.nome, 'cnpj': cnpj, 'dono': self.dono, 'telefone': self.telefone, } def format_cnpj(self, cnpj): return cnpj[:2] + '.' + cnpj[2:5] + '.' + cnpj[5:8] + '/' + cnpj[ 8:12] + '-' + cnpj[12:]
class Power(db.Model): __tablename__ = 'powers' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) def get_list(): records = db.session.query(Power).order_by(Power.id.asc()).all() return list( map(lambda row: PowerValue(**to_dict_from_sql_record(row)), records)) def insert(rowData): record = Power(name=rowData['name']) db.session.add(record) db.session.commit() return 'success' def update(rowData): record = db.session.query(Power).filter( Power.id == rowData['id']).first() record.name = rowData['name'] db.session.add(record) db.session.commit() return 'success'
class UserModel(db.Model): __tablename__ = 'users' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def return_all(cls): def to_json(x): return {'username': x.username} return map(to_json, UserModel.query.all()) @staticmethod def generate_hash(password): return hasher.hash(password) @staticmethod def verifyHash(password, hash): return hasher.verify(password, hash)
class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) def get_list(): lists = db.session.query(Category).order_by(Category.id.asc()).all() return list( map(lambda row: CategoryValue(**to_dict_from_sql_record(row)), lists)) def insert(category_new): category = Category(name=category_new['name']) db.session.add(category) db.session.commit() return 'success' def update(category_new): category = db.session.query(Category).filter( Category.id == category_new['id']).first() category.name = category_new['name'] db.session.add(category) db.session.commit() return 'success'
class Recipe(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) name = db.Column(db.String(80)) ingredients = db.relationship('Ingredients', backref='recipe', lazy='dynamic') imageUrl = db.Column(db.String(80)) created = db.Column(db.DateTime) def __init__(self, user_id, name, ingredients, imageUrl, created=None): self.user_id = user_id self.name = name self.ingredients = [] for ing in ingredients: if 'name' in name: name = ing.name if name is None: name = ing.get('name') self.ingredients.append(Ingredients(self.id, name)) if created is None: created = datetime.utcnow() self.created = created self.imageUrl = imageUrl def __repr__(self): return '<Recipe: user_id: %s, name: %s, imageUrl: %s>' % ( self.user_id, self.name, self.imageUrl)
class Settlement(db.Model): id = db.Column(db.Integer, primary_key=True) ext_account_id_to_settle = db.Column(db.String(256)) sender_withdrawal_amount = db.Column(db.Float) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id_to_settle, sender_withdrawal_amount): self.ext_account_id_to_settle = ext_account_id_to_settle self.sender_withdrawal_amount = sender_withdrawal_amount def __repr__(self): return '<Settlement: %r>' % self.ext_account_id @classmethod def find_by_ext_account_id(cls, ext_account_id): records = None try: records = cls.query.filter_by(ext_account_id_to_settle=ext_account_id). \ order_by(Settlement.time_created).First() except: records = None return records def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Base: now = datetime.now() id = db.Column(db.Integer, primary_key=True, autoincrement=True) created_at = db.Column(db.DateTime, nullable=False, default=now) modified_at = db.Column(db.DateTime, nullable=False, default=now, onupdate=now)
class Like(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) value = db.Column(db.Boolean, nullable=True) def __repr__(self): return f"<Like id:{self.id} user_id:{self.user_id} value:{self.value}>"
class Ingredients(db.Model): id = db.Column(db.Integer, primary_key=True) recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id')) name = db.Column(db.String(200)) def __init__(self, recipe_id, name): self.recipe_id = recipe_id self.name = name
class SkillEffect(db.Model): __tablename__ = 'skill_effects' id = db.Column(db.Integer, primary_key=True, autoincrement=True) skillId = db.Column(db.Integer) effectId = db.Column(db.Integer) def insert(rowData): print(rowData['skillId']) record = SkillEffect( skillId = rowData['skillId'], effectId = rowData['effectId'], ) db.session.add(record) db.session.commit() print(record.skillId) print(record.effectId) return 'success' def update(rowData): record = db.session.query(SkillEffect).filter(SkillEffect.id==rowData['id']).first() record.skillId = rowData['skillId'] record.effectId = rowData['effectId'] db.session.add(record) db.session.commit() return 'success' def get_list(skillId): cmd = """select a.id, a.skillId, b.name as skillName, a.effectId, c.name as effectName from skill_effects a inner join skills b on a.skillId = b.id inner join effects c on a.effectId = c.id where skillId = :bindSkillId order by a.id""" records = db.session.connection().execute(text(cmd),bindSkillId = skillId) return list(map(lambda row: SkillEffectValue(**dict(row)), records)) def get_list_all(): cmd = """select a.id, a.skillId, b.name as skillName, a.effectId, c.name as effectName from skill_effects a inner join skills b on a.skillId = b.id inner join effects c on a.effectId = c.id order by a.id""" records = db.session.connection().execute(text(cmd)) return list(map(lambda row: SkillEffectValue(**dict(row)), records))
class User(db.Model): __tablename__ = "users" id = db.Column(db.String(64), primary_key=True, nullable=False, unique=True) pseudo = db.Column(db.String(64), primary_key=True, nullable=False) creation_date = db.Column(db.DateTime, nullable=False) update_date = db.Column(db.DateTime, nullable=True) tweets = db.relationship('Tweet', back_populates="user")
class WebmapImagesModel(db.Model): __tablename__ = 'webmap_images' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) image = db.Column(db.String(255)) def __init__(self, title, image): self.title = title self.image = image
class Tag(db.Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) id_report = db.Column(db.Integer, nullable=False) tag = db.Column(db.String(50), nullable=False) def __init__(self, id_report, tag): self.id_report = id_report self.tag = tag
class Tweet(db.Model): __tablename__ = "tweets" id = db.Column(db.String(64), primary_key=True, nullable=False, unique=True) message = db.Column(db.String(280), nullable=False) creation_date = db.Column(db.DateTime, nullable=False) update_date = db.Column(db.DateTime, nullable=True) user_id = db.Column(db.String(64), db.ForeignKey(user.User.id)) user = db.relationship('User', back_populates="tweets")
class Rarity(db.Model): __tablename__ = 'rarities' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) def get_list(): records = db.session.query(Rarity).order_by(Rarity.id.asc()).all() return list( map(lambda row: RarityValue(**to_dict_from_sql_record(row)), records))
class WebmapsModel(db.Model): __tablename__ = 'webmaps' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) description = db.Column(db.String) url = db.Column(db.String(255)) def __init__(self, title, description, url): self.title = title self.description = description self.url = url
class WebmapsWithImageModel(db.Model): __tablename__ = 'webmaps_with_image' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(1000)) description = db.Column(db.String) image = db.Column(db.String(1000)) def __init__(self, title, description, image): self.title = title self.description = description self.image = image
class Report(db.Model): __tablename__ = 'reports' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30), nullable=False) url = db.Column(db.String(50), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) def __init__(self, title, url, dt=datetime.now): self.title = title self.url = url
class Recommended(db.Model): __tablename__ = 'recommended' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True, unique=True) user_id = db.Column(db.Integer, nullable=False) anime_id = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
class CharacterModel(db.Model): __tablename__ = 'characters' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) race = db.Column(db.String(120), nullable=False) age = db.Column(db.String(120), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def to_json(cls, x): if not x: return return {'name': x.name, 'race': x.race, 'age': x.age} @classmethod def find_by_id(cls, id): return cls.to_json(cls.query.filter_by(id=id).first()) @classmethod def find_by_name(cls, name): search = "{}%".format(name) return map(cls.to_json, cls.query.filter(cls.name.ilike(search)).all()) @classmethod def return_all(cls): return map(cls.to_json, cls.query.all()) @classmethod def update(cls, id, data): character_query = cls.query.filter_by(id=id) if not character_query.first(): raise LookupError character_query.update(data) db.session.commit() @classmethod def delete(cls, id): character_query = cls.query.filter_by(id=id) character_query.delete() if not character_query.first(): raise LookupError db.session.commit()
class Client(db.Model): __tablename__ = 'client' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) created = db.Column(db.DateTime(), default=func.now()) last_connection = db.Column(db.DateTime(), default=func.now(), onupdate=datetime.now) def __repr__(self): return f'<Client(id, {self.name})>'
class ClientCommand(db.Model): __tablename__ = 'client_command' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) parameters = db.Column(JsonEncodedDict()) client_id = db.Column(db.Integer(), ForeignKey('client.id')) client = relationship('Client', backref='commands', cascade='all') def __repr__(self): return f'<ClientCommand(id, {self.client_id}, {self.name})>'
class Message(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) body = db.Column(db.String, nullable=False) author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) timestamp = db.Column(db.DateTime, nullable=False) def resp_dict(self): return { 'id': self.id, 'body': self.body, 'authorId': self.author_id, 'timestamp': int(time.mktime(self.timestamp.timetuple())) * 1000 }
class WeaponType(db.Model): __tablename__ = 'weapon_types' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) def get_list(): records = db.session.query(WeaponType).order_by( WeaponType.id.asc()).all() return list( map( lambda record: WeaponTypeValue(**to_dict_from_sql_record( record)), records))
class BCMonitor(db.Model): __tablename__ = 'monitor' id = db.Column(db.Integer, primary_key=True) latest_block = db.Column(db.Integer) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, latest_block): self.latest_block = latest_block def __repr__(self): return '<bc monitor: %r>' % self.latest_block # @classmethod # def find_by_last_block_by_number(cls, latest_block): # try: # return cls.query.filter_by(latest_block=latest_block).first() # except Exception as e: # print('Failed to find_by_last_block: ' + str(e)) # db.session.rollback() @classmethod def find_by_last_block(cls): try: return cls.query.order_by(None).first() except Exception as e: print('Failed to find_by_last_block: ' + str(e)) db.session.rollback() @classmethod def find_latest_block_by_number(cls, latest_block): try: return cls.query.filter_by(latest_block=latest_block).one() except: db.session.rollback() return None def update_db(self): db.session.merge(self) db.session.flush() db.session.commit() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class UserModel(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True) password = db.Column(db.String(30)) plots = db.relationship('PlotModel', lazy='dynamic') # type: list def __init__(self, username, password): self.username = username self.password = password def json(self): return { 'user': self.username, 'is_admin': (self.is_admin() is not None), 'plots': [plot.id for plot in self.plots] } def save(self): db.session.add(self) db.session.commit() def is_admin(self): return AdminModel.find_by_id(self.id) @classmethod def find_by_username(cls, username) -> UserModel: return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, _id) -> UserModel: return cls.query.get(_id) @classmethod def set_master_admin(cls, password, access_level) -> bool: _LOGGER.debug("Checking master admin..") master_admin = UserModel.find_by_username("admin") if not master_admin: _LOGGER.debug("No master admin set, creating new..") user = UserModel("admin", password) user.save() master_admin = AdminModel(user.id, access_level) master_admin.save() _LOGGER.debug(f"Admin 'admin' set with password '{password}'.") return True else: _LOGGER.debug("Already set.") return False