class Game(Base): __tablename__ = 'game' game_id = db.Column(db.Integer, primary_key=True) game_name = db.Column(db.String(80), unique=True, nullable=False) def __repr__(self): return '<Game %r (id:$d)>' % self.game_name, self.game_id
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) name = db.Column(db.String(120), nullable=False) @classmethod def find_by_email(cls, email): return cls.query.filter_by(email=email).first() #generating hash for password @staticmethod def generate_hash(password): return sha256.hash(password) #check if hashes match @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) #saving user to database def save_to_db(self): db.session.add(self) db.session.commit()
class RoomType(db.Model): __tablename__ = 'RoomTypes' id = db.Column(db.Integer, name='Id', primary_key=True) name = db.Column(db.String(255), name='Name') guid = db.Column(db.String(255), name='GUID') image_path = db.Column(db.String(255), name='Image_Path', nullable=True) formulas = db.relationship('Formula', back_populates='room_type') def to_json(self): return { 'id': self.id, 'name': self.name, 'guid': self.guid, 'imagePath': self.image_path, 'formulas': self.formulas } def to_view_json(self): return {'id': self.id, 'name': self.name} @classmethod def from_json(cls, data): return cls(id=data.get('id', None), name=data['name'], guid=data['guid'], image_path=data['imagePath'], formulas=list(map(Formula.from_json, data['formulas'])))
class Email(db.Model): id: Optional[int] email: str url: str search_query: str sent_posts: Optional[List[Post]] id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(120)) url = db.Column(db.String(500)) search_query = db.Column(db.String(200)) sent_posts = db.relationship("Post", secondary=posts_identifier, cascade="all, delete") def __repr__(self): return "<Email %r>" % self.email def subscribe(self): db.session.add(self) db.session.commit() def unsubscribe(self): db.session.delete(self) db.session.commit()
class StoreModel(db.Model): __tablename__ = 'stores' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) items = db.relationship('InventoryModel', lazy='dynamic') def __init__(self, name): self.name = name def json(self): return {'name': self.name, 'items': [item.json() for item in self.items.all()]} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() 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 Application(ModelBase): user_id = db.Column( db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False, ) company_name = db.Column(db.String(255), nullable=False, index=True) application_date = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) application_notes = db.Column(db.Text, nullable=True) position = db.Column(db.String(255), nullable=False, index=True) user = db.relationship( 'User', backref=db.backref('applications', lazy='dynamic'), ) events = db.relationship( 'ApplicationEvent', backref='application', lazy='dynamic', ) contacts = db.relationship( 'ApplicationContact', backref='application', lazy='dynamic', ) keywords = db.relationship('Keyword', secondary=application_keyword, backref=db.backref('applications', lazy='dynamic'), lazy='dynamic')
class UserModel(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), nullable=False) posts = db.relationship('Post', backref='user', lazy=True) def __init__(self, name): self.name = name def json(self): return {'id': self.id, 'name': self.name} @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() 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 TicketRecords(db.Model): __tablename__ = 'TICKET_RECORDS' id_ticket = db.Column(db.Integer, primary_key=True) id_ticket_hash = db.Column(db.String(255), unique=True, default='') id_creator = db.Column( db.Integer, db.ForeignKey('USERS.id_user', ondelete='RESTRICT', onupdate='RESTRICT')) id_admin = db.Column(db.Integer, db.ForeignKey('USERS.id_user', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=True) id_channel = db.Column(db.String(255), unique=True, default='') # status: -1 => No admin has taken the ticket # status: 0 => Ticket is assigned to an admin # status: 1 => Ticket is marked as closed status = db.Column(TINYINT(1), default=-1) title = db.Column(db.String(255), default='') category = db.Column(db.String(100), default='') create_timestamp = db.Column( TIMESTAMP, default=datetime.utcnow().replace(microsecond=0)) last_activity_timestamp = db.Column( TIMESTAMP, default=datetime.utcnow().replace(microsecond=0))
class Group(db.Model): __tablename__ = 'db_group' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(32), unique=True, nullable=False) users = db.relation('User', secondary=user_group_table, backref='groups') def __repr__(self): return '%r' % self.name
class Req(Base): __tablename__ = 'req' req_id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=True) game_id = db.Column(db.Integer, db.ForeignKey('game.game_id'), nullable=True) def __repr__(self): return '<Req $d (id:$d)>' % self.req_id
class Topics(db.Model): id = db.Column(db.Integer, primary_key=True) topic = db.Column(db.String, nullable=False, unique=True) vocabularys = db.relationship('Vocabularys') progress = db.relationship('Progress') # Create function to return String def __repr__(self): return self.id
class WorkType(db.Model): __tablename__ = 'WorkTypes' id = db.Column(db.Integer, name='Id', primary_key=True) guid = db.Column(db.String(255), name='GUID') name = db.Column(db.String(255), name='Name') price_value = db.Column(db.Float, name='PriceValue', nullable=False) salary = db.Column(db.Float, name='Salary', nullable=False) time = db.Column(db.Float, name='Time', nullable=False) order = db.Column(db.Integer, name='Order', nullable=False) description = db.Column(db.Text, name='Description') materials_count = db.Column(db.Text, name='MaterialsCount') formula_id = db.Column(db.ForeignKey('Formulae.Id'), name='Formula_Id', index=True) formula = db.relationship('Formula') categories = db.relationship('Category', secondary='WorkTypeCategories') def to_json(self): return { 'id': self.id, 'name': self.name, 'GUID': self.guid, 'priceValue': self.price_value, 'salary': self.salary, 'time': self.time, 'order': self.order, 'descriptions': self.description, 'materialsCount': self.materials_count, 'formula': self.formula, 'categories': list(map(lambda c: {'id': c.id}, self.categories)) } @classmethod def from_json(cls, data): return WorkType(id=data.get('id', None), name=data['name'], guid=data['GUID'], price_value=data['priceValue'], salary=data['salary'], time=data['time'], order=data['order'], description=data['description'], materials_count=data['materialsCount'], formula=Formula.from_json(data['formula']))
class GameChar(Base): __tablename__ = 'game_char' char_id = db.Column(db.Integer, primary_key=True) char_name = db.Column(db.String(80), unique=True, nullable=False) life = db.Column(db.Integer, nullable=True) traits = db.Column(utils.TextPickleType(), nullable=False) traits_blurb = db.Column(db.String(TRAITS_BLURB_LENGTH), nullable=False) def __repr__(self): return '<Character %r (id:$d)>' % self.char_name, char_id
class News(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) title = db.Column(db.String, nullable=False, unique=True) text = db.Column(db.String, nullable=False) # Create function to return String def __repr__(self): return self.id
class Recipes(db.Model): """This is Recipes class for table structure.""" __tablename__ = 'recipes' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40), nullable=False) ingredients = db.Column(db.Text, nullable=False) description = db.Column(db.Text, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) date_updated = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False) category = db.relationship('Categories', foreign_keys=[category_id]) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship('Users', foreign_keys=[user_id]) def __rerp__(self): """Representation string of an Recipes object.""" return f"Recipes('{self.name}', " \ f"'{self.category_id}', " \ f"'{self.user_id}', " \ f"'{self.date_posted}', " \ f"'{self.date_updated}', " \ f"'{self.image_file}')"
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(80), unique=True, nullable=False) def __init__(self, username, email, password): self.username = username self.email = email self.password = password
class Role(db.Model): __tablename__ = 'role' id = db.Column(db.Integer, name='id', nullable=False, primary_key=True) name = db.Column(db.Enum(RoleName), name='name', nullable=False) @staticmethod def get_by_name(name): return Role.query.filter(Role.name == name).first()
class Permission(db.Model): __tablename__ = 'db_permission' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(32), unique=True, nullable=False) # description = Column(Unicode(255)) groups = db.relation(Group, secondary=group_permission_table, backref='permissions') def __repr__(self): return '%r' % self.name
class Data(db.Model): __tablename__ = "Data" Id = db.Column(db.Integer, primary_key=True) Created_date = db.Column(db.Date) Name = db.Column(db.String(128)) Value = db.Column(db.Float) def __init__(self, Id, Created_date, Name, Value): self.Id = Id self.Created_date = Created_date self.Name = Name self.Value = Value
class Hat(db.Model): __tablename__ = "hat" id = db.Column(db.Integer, primary_key=True) colour = db.Column(db.Enum(Colour)) char_id = db.Column(db.Integer, db.ForeignKey('character.id')) # Creates the one to one relationship character = db.relationship("Character", backref=db.backref("children", cascade="all,delete", passive_deletes=True)) def __init__(self, id, colour): self.id = id self.colour = colour
class Product(db.Model): __tablename__ = "db_product" id = db.Column(db.Integer, autoincrement=True, primary_key=True) vendor_id = db.Column(db.Integer, db.ForeignKey('db_vendor.id')) name = db.Column(db.String(42), nullable=False) price = db.Column(db.Float, nullable=False) vendor = db.relation(Vendor, backref='products') purchases = db.relationship('Purchase', secondary=product_purchase_table, back_populates="products") def __repr__(self): return self.name
class Vendor(db.Model): __tablename__ = "db_vendor" id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('db_user.id'), nullable=False) user = db.relation(User, backref='vendor') name = db.Column(db.String(42), nullable=False) isFoundation = db.Column(db.Boolean, default=False, nullable=False) events = db.relationship(Event, secondary=vendor_event_table) def __repr__(self): return self.name
class Post(db.Model): id: int url: str title: str time: datetime id = db.Column(db.Integer, primary_key=True, autoincrement=False) url = db.Column(db.String(1000)) title = db.Column(db.String(200)) time = db.Column(db.DateTime) def __repr__(self): return "<Post %r>" % self.id
class Movement(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), unique=True) def __init__(self, title): self.title = title def __repr__(self): return '<Movement %r>' % self.title def __iter__(self): return self.__dict__.iteritems()
class Users(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(100), nullable=False, unique=True) name = db.Column(db.String, nullable=False) password = db.Column(db.String, nullable=False) vocabularys = db.relationship('Vocabularys') progress = db.relationship('Progress') news = db.relationship('News') topics = db.relationship('Topics', secondary=topic_identifier) # Create function to return String def __repr__(self): return self.id
class FollowerCountDataPoint(db.Model): id = db.Column(db.Integer, primary_key=True) num_followers = db.Column(db.String(256), nullable=False) timestamp = db.Column(db.DateTime, nullable=True) contestant_id = db.Column( db.Integer, db.ForeignKey('contestant.id'), nullable=False ) def __unicode__(self): return '<User %r>' % self.id def __repr__(self): return '<User %r>' % self.id
class Tag(db.Model): __tablename__ = 'Tags' Id = db.Column(db.Integer, primary_key=True) Name = db.Column(db.String(255)) WorkTypes = relationship('WorkType', secondary='TagWorkTypes') def to_json(self): return { 'id': self.Id, 'name': self.Name, 'workTypes': list(map(lambda wt: wt.id, self.WorkTypes)) }
class ItemModel(db.Model): __tablename__ = "items" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) # precision=2, stands for the number of numbers after the decimal point. price = db.Column(db.Float(precision=2)) # stores is the table name of the store .id - column # foreign key reference is sort of "subsidiary" key, which has limited credentials. # basically now we give ID additional parameter, what store do we belong to? store_id = db.Column(db.Integer, db.ForeignKey("stores.id")) # Creates relationship between store id and every item in our database. store = db.relationship("StoreModel") def __init__(self, name, price, store_id): self.name = name self.price = price self.store_id = store_id def json(self): return { "id": self.id, "name": self.name, "price": self.price, "store_id": self.store_id } @classmethod def find_by_name(cls, name): # query is not something we have defined, it something that comes from db.Model # We have class. Then we say we want to query the model. And then we say filter by. # In other words SELECT * FROM items WHERE name=name(second name is our arg inside of find_by_name method) # Translates this code onto SQLCode, this data is also being converted to an item model object. return cls.query.filter_by(name=name).first() # SELECT * FROM items WHERE name=name LIMIT 1, basically returns only @classmethod def find_all(cls): return cls.query.all() def save_to_db(self): # it is saving the model to the data base, SQLAlchemy can directly transform object to a row. # we are adding the collection of objects # this method is useful for both update and insert db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class MultiProject(db.Model): __tablename__ = 'MultiProjects' id = db.Column(db.Integer, name='Id', primary_key=True) name = db.Column(db.String(255), name='Name') documents = db.relationship('Document') def to_json(self): return {'id': self.id, 'name': self.name} @classmethod def from_json(cls, data): return cls(name=data['name'])
class Keyword(ModelBase): __table_args__ = (db.UniqueConstraint('user_id', 'keyword', name='keyword_user_unique'), ) user_id = db.Column( db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False, ) keyword = db.Column(db.String(50), nullable=False, index=True) user = db.relationship( 'User', backref=db.backref('keywords', lazy='dynamic'), )