class RoleModel(db.Model, CRUDMixin): __tablename__ = "api_auth_role" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) def __repr__(self): return '<id {}>'.format(self.id)
class User(BaseModel, db.Model): """ Table Users """ __tablename__ = 'users' name = db.Column(db.String(100), nullable=False) client = relationship('Cliente', uselist=False, back_populates='user') email = db.Column(db.String(100), nullable=False) password = db.Column(db.String(100), nullable=False) is_active = db.Column(TINYINT(1), default=1)
class Cliente(BaseModel, db.Model): """ Table Clientes """ __tablename__ = 'clientes' user_id = db.Column(db.String(36), ForeignKey('users.id')) user = relationship('User', back_populates='client') profile_picture = db.Column(db.String(255), nullable=True) is_facebook = db.Column(TINYINT(1), default=0) is_twitter = db.Column(TINYINT(1), default=0) is_google = db.Column(TINYINT(1), default=0)
class Bike(db.Model): id = db.Column(db.Integer, primary_key=True) make = db.Column(db.String(120)) year = db.Column(db.String(4)) liters_in_tank = db.Column(db.Float) average_liter_per_100_km = db.Column(db.Float) manufacturer = db.Column(db.String(120)) def __rep__(self): return self.make
class UserModel(db.Model): __tablename__ = "api_auth_user" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), nullable=False, unique=True) password = db.Column(db.String, nullable=False, default='SOME_SECRET') first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) email = db.Column(db.String(255), unique=True) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) creation_date = db.Column(db.DateTime, default=datetime.utcnow) modification_date = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) roles = db.relationship('RoleModel', secondary=api_auth_roles_users, lazy='subquery', backref=db.backref('users', lazy=True)) company_id = db.Column(db.Integer, db.ForeignKey('api_auth_company.id')) # Each user has one company company = db.relationship("CompanyModel") @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() @classmethod def find_all(cls): return cls.query.all() @classmethod def find_by_company(cls, _id): return cls.query.filter_by(company_id=_id).all() def set_password(self, pw): pwhash = bcrypt.generate_password_hash(pw.encode('utf8')) self.password = pwhash.decode('utf8') def check_password(self, pw): return bcrypt.check_password_hash(self.password, pw) def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit() def update(self): return db.session.commit()
class Author(db.Model): __tablename__ = "authors" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False) birth = db.Column(db.String(10), nullable=False) books = db.relationship("Book", backref=db.backref('author')) def __init__(self, name: str, birth: str): self.name = name self.birth = birth def serialize(self): return {"id": self.id, "name": self.name, "birth": self.birth}
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255)) picture = db.Column(db.String(255)) company = db.Column(db.String(255)) email = db.Column(db.String(255), index=True) phone = db.Column(db.String(255)) skills = db.relationship("Skill") # one to many def update(self, data): for k, v in data.items(): setattr(self, k, v) return self
class User(db.Model, Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(50), nullable=False) email = db.Column(db.String(255), nullable=False, unique=True) wins = db.Column(db.Integer, default=0) losses = db.Column(db.Integer, default=0) ties = db.Column(db.Integer, default=0) unfinished = db.Column(db.Integer, default=0) is_active = db.Column(db.Boolean(), nullable=False, default=False) is_anonymous = db.Column(db.Boolean(), nullable=False, default=False) is_authenticated = db.Column(db.Boolean(), nullable=False, default=False) @hybrid_property def game_count(self): return self.wins + self.losses + self.ties + self.unfinished @hybrid_property def serialized(self): return { 'id': self.id, 'username': self.username, 'email': self.email, 'wins': self.wins, 'losses': self.losses, 'ties': self.ties, 'unfinished': self.unfinished, 'games': self.game_count } @classmethod def exists_by_id(cls, id): return cls.query.filter(cls.id == id).count() > 0 @classmethod def exists_by_username(cls, username): return cls.query.filter(cls.username == username).count() > 0 @classmethod def exists_by_email(cls, email): return cls.query.filter(cls.email == email).count() > 0 def get_id(self): return self.id
class Review(db.Model): '''Review Model''' __tablename__ = "reviews" id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(250), index=False, nullable=False) business_id = db.Column(db.Integer, db.ForeignKey('businesses.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) created_at = db.Column(db.DateTime, default=db.func.now(), nullable=False) updated_at = db.Column(db.DateTime, default=db.func.now(), server_onupdate=db.func.now(), nullable=False) @classmethod def save(cls, data): ''' Save method ''' review = cls(user_id=data['user_id'], description=data['description'], business_id=data['business_id']) db.session.add(review) db.session.commit() return review @classmethod def serializer(cls, datum): ''' Serialize model object array (Convert into a list) ''' results = [] for data in datum: obj = { 'id': hashid(data.id), 'user': User.query.get(data.user_id).username.capitalize(), 'description': data.description, 'created_at': data.created_at, } results.append(obj) return results @property def serialize_one(self): ''' Serialize model object array (Convert into a list) ''' obj = { 'id': hashid(self.id), 'user': User.query.get(self.user_id).username.capitalize(), 'description': self.description, 'created_at': self.created_at, } return obj @classmethod def delete_all(cls, business_id): ''' Delete All reviews about business ''' cls.query.filter_by(business_id=business_id).delete()
class CompanyModel(db.Model, CRUDMixin): __tablename__ = "api_auth_company" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True) active = db.Column(db.Boolean()) address = db.Column(db.String(255)) city = db.Column(db.String(255)) zip = db.Column(db.Integer) creation_date = db.Column(db.DateTime, default=datetime.utcnow) modification_date = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) def __str__(self): return self.name @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first()
class Blood(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(100), unique=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) phone = db.Column(db.String(15), unique=True) lat = db.Column(db.String(6)) lon = db.Column(db.String(6)) bloodgroup = db.Column(db.String(3)) gender = db.Column(db.String(1)) age = db.Column(db.String(3)) created = db.Column(db.String(20)) def __init__( self, first_name, last_name, phone, location, bloodgroup, gender, age, created, ): self.public_id = uuid.uuid4() self.first_name = first_name self.last_name = last_name self.phone = phone self.lat = location[0] self.lon = location[1] self.bloodgroup = bloodgroup self.gender = gender self.age = age self.created = created def __repr__(self): return f"<Blood(name={self.first_name}, blood={self.bloodgroup})>"
class Book(db.Model): __tablename__ = "books" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), nullable=False) yearOfPublication = db.Column(db.String(4), nullable=False) genre = db.Column(db.String(64), nullable=False) authorID = db.Column(db.Integer, db.ForeignKey("authors.id")) def __init__(self, title: str, yearOfPublication: str, genre: str, authorID: int): self.title = title self.yearOfPublication = yearOfPublication self.genre = genre self.authorID = authorID def serialize(self): return { "id": self.id, "yearOfPublication": self.yearOfPublication, "genre": self.genre, "author": self.author.serialize() }
class Skill(db.Model): __tablename__ = 'skill' id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), index=True) rating = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class Business(db.Model): '''Business Model''' __tablename__ = "businesses" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(180), index=False, nullable=False) description = db.Column(db.Text, index=False, nullable=False) country = db.Column(db.String(128), index=False, nullable=False) city = db.Column(db.String(128), index=False, nullable=False) category = db.Column(db.String(128), index=False, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) created_at = db.Column(db.DateTime, default=db.func.now(), nullable=False) updated_at = db.Column(db.DateTime, default=db.func.now(), server_onupdate=db.func.now(), nullable=False) def hashid(self): ''' Generate hashid ''' return hashid(self.id) @classmethod def get(cls, business_id): ''' Get business by hashid ''' found_id = get_id(business_id) if found_id is not None: return cls.query.get(found_id) @classmethod def serializer(cls, datum): ''' Serialize model object array (Convert into a list ''' results = [] for data in datum: obj = { 'id': hashid(data.id), 'user_id': hashid(data.user_id), 'name': data.name, 'description': data.description, 'category': data.category, 'country': data.country, 'city': data.city, 'reviews_count': Review.query.filter_by(business_id=data.id).count(), 'created_at': data.created_at, } results.append(obj) return results @classmethod def get_by_user(cls, business_id, user_id): ''' Get user businesses ''' found_business_id = get_id(business_id) if get_id(business_id) is not None: return cls.query.filter_by(user_id=user_id, id=found_business_id).first() @classmethod def serialize_obj(cls, data): ''' Convert model object to dictionary ''' return { 'id': hashid(data.id), 'user_id': hashid(data.user_id), 'name': data.name, 'description': data.description, 'category': data.category, 'country': data.country, 'city': data.city, 'reviews_count': Review.query.filter_by(business_id=data.id).count(), 'created_at': data.created_at, } @classmethod def has_two_same_business(cls, user_id, business_name, business_id): ''' Check if the user has the two same busines name #nt from the one to update ''' if cls.query.filter(cls.user_id == user_id, func.lower(cls.name) == func.lower(business_name), cls.id != get_id(business_id)).first() is not None: return True return False @classmethod def update(cls, business_id, data): ''' Update business''' business = cls.query.filter_by(id=get_id(business_id)).first() business.name = data['name'] business.description = data['description'] business.category = data['category'] business.city = data['city'] business.country = data['country'] db.session.add(business) db.session.commit() @classmethod def save(cls, data): ''' Save method ''' business = cls(user_id=data['user_id'], name=data['name'], description=data['description'], category=data['category'], country=data['country'], city=data['city']) db.session.add(business) db.session.commit() @classmethod def delete(cls, business_id): ''' Delete method ''' business = cls.query.get(business_id) db.session.delete(business) db.session.commit()
class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(100), unique=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) email = db.Column(db.String(100), unique=True) phone = db.Column(db.String(15), unique=True) password = db.Column(db.String(100)) lat = db.Column(db.String(6)) lon = db.Column(db.String(6)) bloodgroup = db.Column(db.String(3)) dob = db.Column(db.String(11)) weight = db.Column(db.String(3)) def __init__( self, first_name, last_name, email, phone, password, location, bloodgroup, dob, weight, ): self.public_id = str(uuid.uuid4()) self.first_name = first_name self.last_name = last_name self.email = email self.phone = phone self.password = bcrypt.generate_password_hash( password, app.config.get("BCRYPT_LOG_ROUNDS")).decode() self.lat = location[0] self.lon = location[1] self.bloodgroup = bloodgroup self.dob = dob self.weight = weight def __repr__(self): return f"<User(name={self.first_name})>" @property def serialize(self): return { "firstName": self.first_name, "lastName": self.last_name, "email": self.email, "phone": self.phone, "location": (self.lat, self.lon), "dob": self.dob, "weight": self.weight, }