class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) passwd = db.Column(db.String(80)) auth = db.Column(db.Integer) admin = db.Column(db.Integer) def __init__(self, name, passwd, auth=0, admin=0): self.name = name self.passwd = passwd self.auth = auth self.admin = admin def is_authenticated(self): return self.auth def is_admin(self): return self.admin def auth_toggle(self): if self.auth: self.auth = 0 else: self.auth = 1 db.session.commit()
class Product(db.Model): __tablename__ = 'Product' SKU = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(24), index=True, unique=True, nullable=False) category = db.Column(db.String(16), nullable=False) price = db.Column(db.Numeric(4,2), nullable=False) reorder_point = db.Column(db.Integer, nullable=False) stock_quantity = db.Column(db.Integer, default=0, nullable=False) storage_location = db.Column(db.String(24), nullable=False) batches = db.relationship('ProductBatch', backref='product', lazy=True) def __init__(self, SKU, name, category, price, reorder_point, storage_location): self.SKU = SKU self.name = name self.category = category self.price = price self.reorder_point = reorder_point self.storage_location = storage_location def __repr__(self): return '<Product {}, {}>'.format(self.SKU, self.name) def update_stock_quantity(self): total = 0 for batch in self.batches: total += batch.batch_quantity self.stock_quantity = int(total)
class Employee(db.Model): __tablename__ = 'employee' __table_args__ = {'mysql_engine': 'InnoDB'} id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64), nullable=False) salary = db.Column(db.Integer, nullable=True) department = db.Column(db.String(64), db.ForeignKey('department.name', ondelete='SET NULL', onupdate='CASCADE'), nullable=True) birth_date = db.Column(db.Date, nullable=False) hire_date = db.Column(db.Date, nullable=False, default=date.today()) dept = db.relationship('Department', backref=db.backref('employee', passive_deletes=True, passive_updates=True)) head = db.relationship('Head', backref='employee', passive_deletes=True, uselist=False) def serialize(self): return { 'id': self.id, 'first_name': self.first_name, 'last_name': self.last_name, 'salary': self.salary, 'department': self.department, 'birth_date': dump_date(self.birth_date), 'hire_date': dump_date(self.hire_date) }
class Employee(db.Model): __tablename__ = 'employee' __table_args__ = ( db.UniqueConstraint('cpf', 'role', 'condominium_id'), db.Index('employee_idx', 'cpf', 'role', 'condominium_id') ) id = db.Column(db.Integer, primary_key=True) cpf = db.Column(db.String(11), nullable=False) name = db.Column(db.String(50), nullable=False) birthday = db.Column(db.Date, nullable=False) photo_location = db.Column(db.String(200), nullable=True, default='data/photos/default/employee.png') role = db.Column(db.String(50), nullable=False) type = db.Column(db.SmallInteger, nullable=False) condominium_id = db.Column(db.Integer, db.ForeignKey('condominium.id', ondelete='CASCADE'), nullable=False) active = db.Column(db.SmallInteger, default=1) condominium = db.relationship('Condominium', backref=db.backref('employees', lazy=True, cascade='all, delete'), lazy=True, primaryjoin='and_(Employee.condominium_id == Condominium.id, Condominium.active == 1)') def __repr__(self): return f'Employee(id={self.id}, ' \ f'cpf={self.cpf}, ' \ f'name={self.name}, ' \ f'birthday={self.birthday}, ' \ f'role={self.role})'
class Resident(db.Model): __tablename__ = 'resident' __table_args__ = (db.UniqueConstraint('cpf', 'apartment_id'), db.Index('resident_idx', 'cpf', 'apartment_id')) id = db.Column(db.Integer, primary_key=True) cpf = db.Column(db.String(11), nullable=False) name = db.Column(db.String(50), nullable=False) birthday = db.Column(db.Date, nullable=False) photo_location = db.Column(db.String(200), nullable=True, default='data/photos/default/resident.png') apartment_id = db.Column(db.Integer, db.ForeignKey('apartment.id', ondelete='CASCADE'), nullable=False) apartment = db.relationship( 'Apartment', backref=db.backref('residents', lazy=True, cascade='all, delete'), lazy=True, primaryjoin= 'and_(Resident.apartment_id == Apartment.id, Apartment.active == 1)') def __repr__(self): return f'Resident(id={self.id}, ' \ f'name={self.name}, ' \ f'birthday={self.birthday})'
class SuperUser(db.Model): __tablename__ = 'super_user' __table_args__ = (db.UniqueConstraint('username'), db.UniqueConstraint('password')) username = db.Column(db.String(100), primary_key=True) password = db.Column(db.String(200), nullable=False) def __repr__(self): return f'SuperUser(username={self.username})'
class Room(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=True) description = db.Column(db.String(500)) reservable = db.Column(db.Boolean, nullable=False) reservations = db.relationship('Reservation', backref='room', lazy='dynamic') def __repr__(self): return '<Room: %s>' % self.name
class DataSet(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.String(80)) project_name = db.Column(db.String(80)) screen_img = db.Column(db.String(80)) camera_img = db.Column(db.String(80)) def __init__(self, date, project_name, screen_img, camera_img): self.date = date self.project_name = project_name self.screen_img = screen_img self.camera_img = camera_img
class Test(db.Model): #test table definitions id = db.Column(db.Integer, primary_key=True, autoincrement=True) value1 = db.Column(db.String(512), nullable=False) value2 = db.Column(db.String(512), nullable=False) def __init__(self, value1, value2): self.value1 = value1 self.value2 = value2 def __repr__(self): #this is what you get if you print Test Object return '<Test %r>' % self.value1 #db.create_all
class Movie(db.Model): __tablename__ = 'movie' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now) release_year = db.Column(db.Integer, nullable=False) director = db.Column(db.String(30), nullable=False) duration_hrs = db.Column(db.Integer, nullable=False) duration_mins = db.Column(db.Integer, nullable=False) rate = db.Column(db.Integer, nullable=False) storyline = db.Column(db.String(1000), nullable=False) poster = db.Column(db.String(1000), nullable=False, default='movie_poster_default.jpg') genre_id = db.Column(db.Integer, db.ForeignKey('genre.id'), nullable=False) genre = db.relationship('Genre') user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User') def __repr__(self): return ("Movie" f"('{self.title}'," f" '{self.director}'," f" '{self.rate}'," f" '{self.duration_hrs}:{self.duration_mins}'," f" '{self.release_year}'," f" '{self.genre.name}'" f" '{self.author.username}'" f" '{self.date_posted.strftime('%Y-%m-%d %I:%M%p')}')") @property def serialize(self): return { 'id': self.id, 'title': self.title, 'date_posted': self.date_posted, 'release_year': self.release_year, 'director': self.director, 'duration_hrs': self.duration_hrs, 'duration_mins': self.duration_mins, 'rate': self.rate, 'poster': self.poster, 'genre': self.genre.name, 'genre_id': self.genre_id, 'user': self.user.username, 'user_id': self.user_id, 'poster': self.poster }
class Customer(db.Model): __tablename__ = 'Customer' customerID = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), index=True, unique=True, nullable=False) name = db.Column(db.String(64), nullable=False) phoneNum = db.Column(db.String(64)) creditCardID = db.relationship('CreditCard', backref='customer', lazy=True) addressID = db.relationship('Address', backref='customer', lazy=True) def __init__(self, email, name): self.email = email self.name = name def __repr__(self): return '<Customer: {}, customer: {}>'.format(self.customerID, self.email)
class Flight(TimestampMixin, PersistableMixin, db.Model): id = db.Column(db.Integer, primary_key=True) destination_id = db.Column(db.Integer, db.ForeignKey('destination.id')) destination = relationship("Destination", back_populates="flights") beginning = db.Column(db.Date, nullable=False) end = db.Column(db.Date, nullable=False) cost = db.Column(db.Float, nullable=False) url = db.Column(db.String(200), nullable=False) def __repr__(self): return f"<Flight id='{self.id}' cost='{self.cost}'" def to_csv(self): return ", ".join([ str(self.id), self.created.strftime('%e %b %Y %H:%M:%S%p'), self.beginning.strftime('%e %b %Y'), self.end.strftime('%e %b %Y'), str(self.cost), self.url ]) @property def dates(self): return f"{self.beginning.strftime('%b %d')} - {self.end.strftime('%b %d')}" def num_days(self): return (self.end - self.beginning).days def month(self): return self.beginning.strftime("%B") def chart_timestamp(self): return self.created.strftime("%Y-%m-%d %H:%M:%S ") + "-0800"
class Guest(db.Model): __tablename__ = 'guest' __table_args__ = (db.UniqueConstraint('name', 'arrival', 'apartment_id'), db.Index('guest_idx', 'name', 'arrival', 'apartment_id')) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) arrival = db.Column(db.DateTime, nullable=False) active = db.Column(db.SmallInteger, default=1) apartment_id = db.Column(db.Integer, db.ForeignKey('apartment.id', ondelete='CASCADE'), nullable=False) apartment = db.relationship( 'Apartment', backref=db.backref( 'guests', lazy=True, cascade='all, delete', primaryjoin= 'and_(Guest.apartment_id == Apartment.id, Guest.active == 1)'), lazy=True, primaryjoin= 'and_(Guest.apartment_id == Apartment.id, Apartment.active == 1)') def __repr__(self): return f'Guest(id={self.id}, name={self.name}, arrival={self.arrival})'
class Rule(db.Model): __tablename__ = 'rule' __table_args = (db.UniqueConstraint('text', 'condominium_id'), db.Index('rule_idx', 'text')) id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(500), nullable=False) employee_id = db.Column(db.Integer, db.ForeignKey('employee.id', ondelete='CASCADE'), nullable=False) condominium_id = db.Column(db.Integer, db.ForeignKey('condominium.id', ondelete='CASCADE'), nullable=False) author = db.relationship('Employee', backref=db.backref('rules', lazy=True, cascade='all, delete'), lazy=False) condominium = db.relationship( 'Condominium', backref=db.backref('rules', lazy=True, cascade='all, delete'), lazy=True, primaryjoin= 'and_(Rule.condominium_id == Condominium.id, Condominium.active == 1)') def __repr__(self): return f'Rule(text={self.text})'
class Address(db.Model): __tablename__ = 'address' __table_args__ = (db.UniqueConstraint('street_name', 'city_id'), db.Index('address_idx', 'street_name', 'city_id')) id = db.Column(db.Integer, primary_key=True) street_name = db.Column(db.String(150), nullable=False) neighbourhood = db.Column(db.String(150), nullable=False) city_id = db.Column(db.Integer, db.ForeignKey('city.id', ondelete='CASCADE'), nullable=False) city = db.relationship('City', backref=db.backref('addresses', lazy=True, cascade='all, delete'), lazy=False) def __repr__(self): return f'Address(id={self.id}, ' \ f'street_name={self.street_name}, ' \ f'neighbourhood={self.neighbourhood}, ' \ f'city_name={self.city.name})'
class Company(db.Model): __tablename__ = 'company' c_id = db.Column(db.Integer, primary_key=True, autoincrement=True) c_name = db.Column(db.String(32)) c_legent = db.Column(db.String(32)) c_employed = db.Column(db.Integer) c_shacap = db.Column(db.Integer) c_other = db.Column(db.Text) c_cdate = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) notes = db.relationship('Note', backref='company', cascade='all, delete, delete-orphan', single_parent=True, order_by='desc(Note.timestamp)')
class Note(db.Model): __bind_key__ = 'notes' __tablename__ = 'note' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) datetime = db.Column(db.String) user = db.Column(db.String(100)) content = db.Column(db.String)
class Department(db.Model): __tablename__ = 'department' __table_args__ = {'mysql_engine': 'InnoDB'} name = db.Column(db.String(64), primary_key=True) head = db.relationship('Head', backref='department', passive_deletes=True, uselist=False)
class Country(db.Model): __tablename__ = 'country' __table_args__ = (db.UniqueConstraint('name'), ) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40), nullable=False, index=True) def __repr__(self): return f'Country(id={self.id}, name={self.name})'
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) profile_pic = db.Column(db.String(1000), nullable=False, default='op_profile_default.png') movies = db.relationship('Movie', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.movies}')" @property def serialize(self): return { 'id': self.id, 'uesrname': self.username, 'email': self.email, }
class User(db.Model): # columns # __tablename__ = "users" # to get old users id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) role_id = db.Column( db.Integer, nullable=False) # 1 for user, 2 for user manager and 3 for admin expected_calories = db.Column(db.Integer, nullable=False) password = db.Column(db.String(60), nullable=False) # password = db.Column(db.String(60), nullable=True) # hashing algorithm will make this string as 60 char long records = db.relationship('Record', backref='owner', lazy=True) # backref is similar to adding another column # Magic Method: How object is printed when we print it (also __scr__) def __repr__(self): return f"User('{self.id}, {self.name}, {self.email}, {self.role_id}')"
class Destination(TimestampMixin, PersistableMixin, db.Model): id = db.Column(db.Integer, primary_key=True) city = db.Column(db.String(50), nullable=False) country = db.Column(db.String(50), nullable=False) code = db.Column(db.String(10), nullable=False) flights = relationship("Flight", back_populates="destination") def __repr__(self): return f"<Destination id='{self.id}' city='{self.city}' code='{self.code}' />" @classmethod def find(cls, d): return cls.query.filter(cls.city == d['city']).one_or_none() def flights_to_chart_data(self): data = { 'name': self.code, 'data': {f.chart_timestamp(): f.cost for f in self.flights} } return data
class Genre(db.Model): __tablename__ = 'genre' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(1000), unique=True, nullable=False) movies = db.relationship('Movie') def __repr__(self): return f"Genre('{self.name}')" @property def serialize(self): return {'id': self.id, 'name': self.name}
class Users(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) password = db.Column(db.String(512), nullable=False) dob = db.Column(db.DateTime, nullable=False) email = db.Column(db.String(512), unique=True) name = db.Column(db.String(512), nullable=False) phone = db.Column(db.String(10), nullable=False, unique=True) image = db.Column(db.String(512)) role = db.Column(db.String(128)) def __init__(self, password, dob, email, name, phone, image, role): self.password = password self.dob = dob self.email = email self.name = name self.phone = phone self.image = image self.role = role @login_manager.user_loader def load_user(user_id): return Users.query.get(int(user_id)) def __repr__(self): return '<User %r>' % self.name
class Condominium(db.Model): __tablename__ = 'condominium' __table_args__ = (db.UniqueConstraint('street_number', 'address_id'), db.Index('cond_idx', 'street_number', 'address_id')) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), nullable=False) street_number = db.Column(db.Integer, nullable=False) photo_location = db.Column(db.String(200), nullable=True, default='data/photos/default/condominium.png') address_id = db.Column(db.Integer, db.ForeignKey('address.id', ondelete='CASCADE'), nullable=False) active = db.Column(db.SmallInteger, default=1) address = db.relationship('Address', backref=db.backref('condominiums', lazy=True, cascade='all, delete', primaryjoin='and_(Address.id == Condominium.address_id, Condominium.active == 1)'), lazy=True, primaryjoin='and_(Condominium.address_id == Address.id, Condominium.active == 1)') def __repr__(self): return f'Condominium(id={self.id}, ' \ f'condominium_name={self.name})'
class User(db.Model, UserMixin): id = db.Column(db.String(8), primary_key=True) # Andrew ID username = db.Column(db.String(100), nullable=False) # required for Social Flask admin = db.Column(db.Boolean, default=False, nullable=False) banned = db.Column(db.Boolean, default=False, nullable=False) last_login = db.Column(db.DateTime, default=func.now(), nullable=False) reservations = db.relationship('Reservation', backref='user', lazy='dynamic') def __init__(self, *args, **kwargs): self.id = kwargs.get('id', kwargs.get('email', '').split('@')[0]) self.username = self.id self.admin = kwargs.get('admin', self.id in admins) self.banned = kwargs.get('banned', False) def email(self, template='*****@*****.**'): return template % id def __repr__(self): return '<User: %s>' % self.id
class Notification(db.Model): __tablename__ = 'notification' __table_args__ = ( db.UniqueConstraint('title', 'condominium_id'), db.Index('date_idx', 'finish_date'), db.Index('notification_idx', 'title', 'condominium_id') ) id = db.Column(db.Integer, primary_key=True) type = db.Column(db.Integer, nullable=False) title = db.Column(db.String(50), nullable=False) text = db.Column(db.String(300), nullable=False) finish_date = db.Column(db.Date, nullable=True) active = db.Column(db.SmallInteger, default=1) employee_id = db.Column(db.Integer, db.ForeignKey('employee.id', ondelete='CASCADE'), nullable=False) condominium_id = db.Column(db.Integer, db.ForeignKey('condominium.id', ondelete='CASCADE'), nullable=False) author = db.relationship('Employee', backref=db.backref('notifications', lazy=True, cascade='all, delete', primaryjoin='and_(Notification.employee_id == Employee.id, Notification.active == 1)'), lazy=True) condominium = db.relationship('Condominium', backref=db.backref('notifications', lazy=True, cascade='all, delete', primaryjoin='and_(Notification.condominium_id == Condominium.id, Notification.active == 1)'), lazy=True, primaryjoin='and_(Notification.condominium_id == Condominium.id, Condominium.active == 1)') def __repr__(self): return f'Notification(id={self.id}, ' \ f'type={self.type}, ' \ f'title={self.title}, ' \ f'text={self.text}, ' \ f'finish_date={self.finish_date})'
class Record(db.Model): id = db.Column(db.Integer, primary_key=True) datetime = db.Column(db.DateTime, default=datetime.now, nullable=False) # time = db.Column(db.Text, nullable=True) text = db.Column(db.String(250), nullable=False) num_calories = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # In user model, Test is started with capital letter because we are using the actual Test class. # But here we use 'user.id' with small u because it is actual table in db, and actual column # table and column name are simply lowercase by default def __repr__(self): return f"Record('{self.id}, {self.datetime}, {self.text}, {self.num_calories}, {self.user_id}')"
class ResidentUser(db.Model): __tablename__ = 'resident_user' __table_args__ = (db.UniqueConstraint('apartment_id'), db.UniqueConstraint('username')) id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), db.ForeignKey('username.username', ondelete='CASCADE'), nullable=False) password = db.Column(db.String(200), nullable=False) apartment_id = db.Column(db.Integer, db.ForeignKey('apartment.id', ondelete='CASCADE'), nullable=False) apartment = db.relationship( 'Apartment', backref=db.backref('user', lazy=True, cascade='all, delete', uselist=False), lazy=True, uselist=False, primaryjoin= 'and_(ResidentUser.apartment_id == Apartment.id, Apartment.active == 1)' ) _username = db.relationship('Username', backref=db.backref('resident_user', lazy=True, cascade='all, delete', uselist=False), lazy=True, uselist=False) def __repr__(self): return f'ResidentUser(username={self.username})'
class CartItem(db.Model): __tablename__ = 'CartItem' cartItemID = db.Column(db.Integer, primary_key=True) SKU = db.Column(db.Integer, nullable=False) cartID = db.Column(db.Integer, nullable=False) quantity = db.Column(db.Integer, nullable=False) status = db.Column(db.String(15), nullable=False) def __init__(self, SKU, cartID, quantity): self.SKU = SKU self.cartID = cartID self.quantity = quantity self.status = 'UNFILLED' def __repr__(self): return '<CartItem {}, cartID: {}>'.format(self.SKU, self.cartID)