class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key = True, autoincrement=True) name = db.Column(db.String(20)) birth = db.Column(db.String(10), nullable = False) username = db.Column(db.String(64), unique=True, index=True) password = db.Column(db.String(128)) recipes = db.relationship('Recipe', backref='users', lazy=True) liked = db.relationship('LikePost', foreign_keys='LikePost.user_id', backref='users', lazy='dynamic') def __init__(self,name,birth,username,password): self.name = name self.birth = birth self.username = username self.password = password def __repr__(self): return '<User %r>' % self.username def like(self, recipe): if LikePost.query.filter(LikePost.user_id == self.id, LikePost.recipe_id == recipe.id).count() > 0: LikePost.query.filter_by(user_id=self.id, recipe_id=recipe.id).delete() recipe.removeLike() db.session.commit() else: like = LikePost(user_id=self.id, recipe_id=recipe.id) db.session.add(like) recipe.addLike() db.session.commit()
class User(Base): __tablename__ = 'user' # User Name name = db.Column(db.String(128), nullable=False) # Identification Data: email & password orcid = db.Column(db.String(128), nullable=False, unique=True) token = db.Column(db.String(192), nullable=False) aka = db.Column(db.String(1000), nullable=True) role = db.Column(db.String(128), nullable=True) repos = db.relationship('Repository', backref='user', lazy=True) def __init__(self, name, orcid, aka, token): self.name = name self.orcid = orcid self.token = token self.aka = aka def __repr__(self): return '<User %r>' % self.name def as_dict(self): # return {c.name: getattr(self, c.name) for c in self.__table__.columns} return { c.name: str(getattr(self, c.name)) for c in self.__table__.columns } # to support datetime
class Department(db.Model): """Model for a department.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) def __repr__(self): return f"Department('{self.id}','{self.name}')"
class ItemModel(db.Model): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) price = db.Column(db.Float(precision=2)) store_id = db.Column(db.Integer, db.ForeignKey('stores.id')) 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 {'name': self.name, 'price': self.price} @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 Key(db.Model): __tablename__ = 'keys' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) key_value = db.Column(db.String(50)) key_type = db.Column(db.String(25))
class User(db.Model): """Create a user table.""" __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(60), index=True, unique=True) password_hash = db.Column(db.Text()) @property def password(self): """Prevent password from being accessed.""" raise AttributeError('Password is not a readable attribute.') @password.setter def password(self, password): """Create password hash.""" self.password_hash = generate_password_hash(password) def verify_password(self, password): """Check if hashed password matches actual password.""" return check_password_hash(self.password_hash, password) @classmethod def find_user_in_db(cls, email): return User.query.filter_by(email=email).first() def __repr__(self): return '<User: {} with email: {}>'.format(self.username, self.email)
class UserModel(BaseModel, db.Model): __tablename__ = 'dripbox_user' 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) files = relationship("FileModel")
class Show(db.Model): __tablename__ = 'Show' venue_id = db.Column(db.Integer, db.ForeignKey('Venue.id'), primary_key=True) artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'), primary_key=True) starttime = db.Column(db.DateTime, primary_key=True) artist = db.relationship('Artist', lazy='joined') @property def info(self): return { 'venue_id': self.venue.id, 'venue_name': self.venue.name, 'artist_id': self.artist.id, 'artist_name': self.artist.name, 'artist_image_link': self.artist.image_link, 'start_time': self.starttime.isoformat() + 'Z' } def __init__(self, venue=None, artist=None, starttime=None): self.venue = venue self.artist = artist self.starttime = starttime def __repr__(self): return f'<{self.artist.name} @ {self.venue.name}: {self.starttime}>'
class Sale(db.Model): __tablename__ = 'sales' id_product = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=False, primary_key=True) date_sale = db.Column(db.DateTime, nullable=False) amount = db.Column(db.Integer, nullable=False) def __init__(self, **kwargs): super().__init(**kwargs) self.init_load() @reconstructor def init_load(self): self.id_product = self.id_product self.date_sale = self.encrypt(self.date_sale) self.amount = self.encrypt(self.amount) def encrypt(self, value): key = b'1234567890123456' value = bytes(str(value), 'utf-8') return aes.encrypt(key, value) def decrypt(self, value): key = b'1234567890123456' return aes.decrypt(key, value)
class StoreModel(db.Model): __tablename__ = 'stores' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) items = db.relationship('ItemModel', 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 ServiceForm(db.Model): __tablename__ = 'service_forms' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(50), nullable=False) def __str__(self): return self.name
class UserToken(db.Model): """ User Token Model for storing JWT tokens """ __tablename__ = 'user_token' id = db.Column(db.Integer, primary_key=True, autoincrement=True) token = db.Column(db.String(500), unique=True, nullable=False) created_on = db.Column(db.DateTime, nullable=False) def __init__(self, token): self.token = token self.created_on = datetime.datetime.now() @staticmethod def check_token(auth_token): # check whether auth token has been created res = UserToken.query.filter_by(token=str(auth_token)).first() if res: return True else: return False def __repr__(self): return '<token: {}'.format(self.token)
class History(Model): __tablename__ = "history" history_id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False) user = db.relationship('User', backref=db.backref('histories', lazy=True)) description = db.Column(db.String, nullable=False) date_created = db.Column(db.Date, nullable=False) time_created = db.Column(db.Time, nullable=False) def __init__(self, user_id, description, date_created=None, time_created=None): self.user_id = user_id self.description = description if date_created is None: date_created = datetime.datetime.today().date() if time_created is None: time_created = datetime.datetime.now().time() self.date_created = date_created self.time_created = time_created
class UserModel(BaseModel, db.Model): __tablename__ = 'warsaw_user' 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) is_admin = db.Column(db.Boolean)
class Driver(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(255), nullable=False) last_name = db.Column(db.String(255), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship("User", back_populates="driver") trips = db.relationship("Trip", backref="driver", lazy=True)
class Hint(db.Model): __tablename__ = 'hint' id = db.Column(db.Integer, primary_key=True) order = db.Column(db.Integer, nullable=False) content = db.Column(db.UnicodeText, nullable=False) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) def __serialize__(self): return { 'id': self.id, 'order': self.order, 'content': self.content, 'questionId': self.question_id } def set_question(self, question_id): question = Question.query.filter(Question.id == question_id).first() if not question: return not_found('Question with id ' + question_id + ' not found.') self.question = question def update(self, **args): if 'id' in args: raise TypeError('Cannot update id.') if 'content' in args: self.content = args['content'] if 'order' in args: self.order = args['order'] if 'questionId' in args: self.set_question(args['questionId']) return self
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) project_name = db.Column(db.String(50), index=True) dataset_location = db.Column(db.String(50), index=True) def __repr__(self): return "<Project name={}>".format(self.project_name)
class Port(Base): __tablename__ = "port" port_id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True) name = db.Column(db.String, unique=True) type = db.Column(db.String) description = db.Column(db.String) date_created = db.Column(db.DateTime, default=datetime.datetime.now())
class Region(Base): __tablename__ = 'region' region_id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) acronym = db.Column(db.String) description = db.Column(db.String) date_created = db.Column(db.DateTime, default=datetime.datetime.now())
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False) user = db.relationship('User', backref='roles') def __str__(self): return self.name
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) users = db.relationship('User', backref='role') def __str__(self): return self.name
class Resource(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), index=True, unique=True) home_page = db.Column(db.String(120), index=True, unique=True) news = db.relationship('News', backref='source', lazy='dynamic') def __repr__(self): return f'<Resource {self.title}>'
class Author(db.Model): __tablename__ = 'authors' id_presentation = db.Column(db.Integer, db.ForeignKey('presentations.id'), primary_key=True) id_user = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
class Room(db.Model): __tablename__ = 'rooms' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text) schedule = db.relationship('Schedule', backref='room') def __str__(self): return 'Room #' + str(self.id)
class Group(db.Model): """Create a test table.""" __tablename__ = 'group' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), index=True) spec_id = db.Column(db.Integer, db.ForeignKey('specification.id')) test_id = db.Column(db.Integer, db.ForeignKey('test.id'))
class Vessel(Model): __tablename__ = 'vessel' vessel_id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) acronym = db.Column(db.String) description = db.Column(db.String) date_created = db.Column(db.DateTime, default=datetime.datetime.now())
class Specification(db.Model): """Create a user table.""" __tablename__ = 'specification' id = db.Column(db.Integer, primary_key=True) spec_name = db.Column(db.String(60), index=True, unique=True) url = db.Column(db.Text()) group = db.relationship('Group')
class LikePost(db.Model): __tablename__ = 'likepost' id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id')) def __repr__(self): return '<Like id %r>' % self.id
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) def __repr__(self): return '<User {}>'.format(self.username)
class Language(db.Model): __tablename__ = 'language' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.UnicodeText, nullable=False) version = db.Column(db.UnicodeText, nullable=False) def __serialize__(self): return {'id': self.id, 'name': self.name, 'version': self.version}