class Loss(db.Model): """ Keeps track of losers in each game. """ __tablename__ = "losses" user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('games.id'), primary_key=True)
class PolicyBenefits(db.Model): """ Links benefits offered by the insurance company to a particular child policy. Also contains the limit and the premium paid by the customer for the benefit """ __tablename__ = 'policy_benefits' id = db.Column(db.Integer, autoincrement=True, primary_key=True) policy_id = db.Column( db.Integer, db.ForeignKey('child_policy.id', ondelete='CASCADE', onupdate='CASCADE')) ic_benefit_id = db.Column( db.Integer, db.ForeignKey('ic_benefit.id', ondelete='CASCADE', onupdate='CASCADE')) amount = db.Column(db.Float, nullable=False) def __init__(self, policy_id, ic_benefit_id, amount): self.policy_id = policy_id self.ic_benefit_id = ic_benefit_id self.amount = amount def serialize(self): data = self.ic_benefit.serialize() # We need the actual policy_id for this object, so pop the ic_benefit id and add the policy_benefits id instead data.pop("id") data.update({"amount_paid": self.amount, "id": self.id}) return data def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.remove(self) db.session.commit() @classmethod def get_policy_benefit_by_policy(cls, child_policy_id): benefit_set = set() benefits = cls.query.filter_by(policy_id=child_policy_id) for benefit in benefits: benefit_set.add(benefit.ic_benefit_id) return benefit_set @classmethod def get_policy_benefit_by_id(cls, id): benefit = cls.query.filter_by(id=id).first() return benefit
class GamePlayed(db.Model): """ Keeps track of every game a user has joined. """ __tablename__ = 'games_played' user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('games.id'), primary_key=True) join_position = db.Column(db.Integer, nullable=False)
class PresentationModel(db.Model, Model): """ PresentationModel Class This class contains only PresentationModel methods and represents the presentations table in database. """ __tablename__ = 'presentations' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) performed_at = db.Column(db.Date(), nullable=False, default=datetime.date.today()) city = db.Column(db.String(250), nullable=False) state = db.Column(db.String(250)) country = db.Column(db.String(250), nullable=False) segment_id = db.Column(db.Integer, db.ForeignKey('segments.id'), nullable=False, default=1) segment = db.relationship('SegmentModel') company_id = db.Column(db.Integer, db.ForeignKey('companies.id')) company = db.relationship('CompanyModel') def __init__(self, name: str, performed_at: datetime, city: str, country: str, segment_id: int, state: str = None, company_id: int = None, _id: int = None) -> None: """ Loads a PresentationModel. """ self.id = _id self.name = name self.performed_at = string_to_date(performed_at) self.city = city self.state = state if state else None self.country = country self.segment_id = segment_id self.company_id = company_id def json(self) -> dict: """ Retruns a PresentationModel as a json format. """ return { 'id': self.id, 'name': self.name, 'performed_at': self.performed_at.isoformat(), 'city': self.city, 'state': self.state if self.state else None, 'country': self.country, 'segment': {'id': self.segment.id, 'name': self.segment.name}, 'company': {'id': self.company.id, 'name': self.company.name} if self.company else None } def curriculum_json(self) -> dict: """ Retruns a reduced PresentationModel as a json format. """ return {'id': self.id, 'name': self.name, 'performed_at': self.performed_at.isoformat()}
class UserPermissions(db.Model): __tablename__ = 'user_permission' id = db.Column(db.Integer, primary_key=True, auto_increment=True) user_id = db.Column(db.Integer, db.ForeignKey( 'user.id', onupdate='CASCADE', ondelete='CASCADE')) permission_id = db.Column(db.Integer, db.ForeignKey( 'permission.id', onupdate='CASCADE', ondelete='CASCADE')) def __init__(self, user_id, permission_id): self.user_id = user_id self.permission_id = permission_id def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_permission_by_user_id(cls, uid): permissions_list = cls.query.filter_by(user_id=uid).all() permission_ids = [] for i in permissions_list: permission_ids.append(str(i.permission_id)) return ''.join(permission_ids) @classmethod def get_specific_permission(cls, permission_id, user_id): permission = cls.query.filter_by( user_id=user_id, permission_id=permission_id).first() return permission @classmethod def delete_user_permissions(cls, user_id, permissions): """ delete user permissions where id matches in array :param user_id {Int} user id whom we are deleting permissions :param permissions {IntSet} """ user_acc = cls.query.filter(cls.permission_id.in_(permissions), cls.user_id==user_id)\ .delete(synchronize_session='fetch') db.session.commit()
class ICProducts(db.Model): """ Stores the company and the products it sells """ __tablename__ = 'ic_products' id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) company = db.Column( db.Integer, db.ForeignKey('insurance_company.id', onupdate='CASCADE', ondelete='CASCADE')) insurance_class = db.Column( db.Integer, db.ForeignKey('insurance_class.class_id', onupdate='CASCADE', ondelete='CASCADE')) sub_class = db.Column( db.Integer, db.ForeignKey('insurance_subclass.class_code', onupdate='CASCADE', ondelete='CASCADE')) def __init__(self, company, insurance_class, sub_class): self.company = company self.insurance_class = insurance_class self.sub_class = sub_class def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_products_by_company(cls, company_id): products = cls.query.filter_by(company=company_id).all() return products
class PolicyExtensions(db.Model): """ Links benefits offered by the insurance company to a particular child policy. Also contains the limit and the premium paid by the customer for the benefit """ __tablename__ = 'policy_extension' id = db.Column(db.Integer, autoincrement=True, primary_key=True) policy_id = db.Column(db.Integer, db.ForeignKey( 'child_policy.id', ondelete='CASCADE', onupdate='CASCADE')) ic_extension_id = db.Column(db.Integer, db.ForeignKey( 'ic_extension.id', ondelete='CASCADE', onupdate='CASCADE')) amount = db.Column(db.Float, nullable=False) def __init__(self, policy_id, ic_extension_id, amount): self.policy_id = policy_id self.ic_extension_id = ic_extension_id self.amount = amount def serialize(self): data = self.ic_extension.serialize() data.update({"amount_paid": self.amount}) return data def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.remove(self) db.session.commit() @classmethod def get_policy_ext_by_policy(cls, child_policy_id): extension_set = set() extensions = cls.query.filter_by(policy_id=child_policy_id) if extensions is None: return None for ext in extensions: extensions.add(ext.ic_extension_id) return extension_set
class InsuranceSubclass(db.Model): __tablename__ = 'insurance_subclass' class_code = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(50), nullable=False) parent_class = db.Column(db.Integer, db.ForeignKey( 'insurance_class.class_id', ondelete='CASCADE', onupdate='CASCADE')) acronym = db.Column(db.String(50), nullable=True) def __init__(self, class_code, name, parent_class, acronym): self.name = name self.class_code = class_code self.parent_class = parent_class self.acronym = acronym def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() @classmethod def get_class_by_id(cls, code): subclass = cls.query.filter_by(class_code=code).first() return subclass @classmethod def get_acronym(cls, id): subclass = cls.query.filter_by(class_code=id).first() return subclass.acronym
class PolicyPayments(db.Model): """ Describes all the transactions for a particular child policy, for both credit(payments) and debit(refunds) """ __tablename__ = 'policy_payments' id = db.Column(db.Integer, primary_key=True, autoincrement=True) # transaction type can either be mpesa, bankers cheque, refund transaction_type = db.Column(db.String(10)) amount = db.Column(db.Float, nullable=False) customer_no = db.Column(db.String(50)) child_policy_id = db.Column( db.Integer, db.ForeignKey('child_policy.id', ondelete='CASCADE', onupdate='CASCADE')) next_date = db.Column(db.DateTime) amount_due = db.Column(db.Float, nullable=False, default=0) # could be the mpesa code, or cheque number transaction_code = db.Column(db.String(25)) date_paid = db.Column(db.DateTime, default=db.func.now()) def __init__(self, transaction_type, amount, customer_no, child_policy_id, next_date, amount_due, transaction_code): self.transaction_type = transaction_type self.amount = amount self.customer_no = customer_no self.child_policy_id = child_policy_id self.next_date = next_date self.amount_due = amount_due self.transaction_code = transaction_code def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.remove(self) db.session.commit() @classmethod def total_amount_paid(cls, child_id): """ Get total amount paid for a particular child policy """ amounts = [ t.amount for t in cls.query.filter_by(child_policy=child_id).all() ] return sum(amounts) @classmethod def get_payments_history(cls, child_id): return [ installment.serialize() for installment in cls.query.filter_by(child_id=child_id).all() ]
class CompanyModel(db.Model, Model): """ CompanyModel Class This class contains only CompanyModel methods and represents the companies table in database. """ __tablename__ = 'companies' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) position = db.Column(db.String(100), nullable=False) assignments = db.Column(db.String(1000)) started_at = db.Column(db.Date(), nullable=False, default=datetime.date.today()) ended_at = db.Column(db.Date()) segment_id = db.Column(db.Integer, db.ForeignKey('segments.id'), nullable=False, default=1) segment = db.relationship('SegmentModel') products = db.relationship('ProductModel', lazy='dynamic') presentations = db.relationship('PresentationModel', lazy='dynamic') def __init__(self, name: str, position: str, assignments: str, started_at: datetime, segment_id: int, ended_at: datetime = None, _id: int = None) -> None: """ Loads a CompanyModel. """ self.id = _id self.name = name self.position = position self.assignments = assignments self.started_at = string_to_date(started_at) self.ended_at = string_to_date(ended_at) if ended_at else None self.segment_id = segment_id def json(self) -> dict: """ Retruns a CompanyModel as a json format. """ return { 'id': self.id, 'name': self.name, 'position': self.position, 'assignments': self.assignments, 'started_at': self.started_at.isoformat(), 'ended_at': self.ended_at.isoformat() if self.ended_at else None, 'segment': {'id': self.segment.id, 'name': self.segment.name}, 'products': [product.json() for product in self.products.all()], 'presentations': [presentation.json() for presentation in self.presentations.all()] } def curriculum_json(self) -> dict: """ Retruns a reduced CompanyModel as a json format. """ return { 'id': self.id, 'name': self.name, 'position': self.position, 'started_at': self.started_at.isoformat(), 'ended_at': self.ended_at.isoformat() if self.ended_at else None }
class Posts(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String()) description = db.Column(db.String(), nullable=True) content = db.Column(db.String(), nullable=True) tags = db.relationship('Tags', secondary=relations, backref=db.backref('tag')) categories = db.Column(db.Integer(), db.ForeignKey('categories.id'), nullable=True) author = db.Column(db.Integer(), db.ForeignKey('users.id'), nullable=True) def __init__(self, name, description, content): self.name = name self.description = description self.content = content
class UserInfo(db.Model): id = db.Column(db.Integer, primary_key=True) phone_number = db.Column(db.Integer, nullable=True) email = db.Column(db.String, nullable=True,unique=True) Address = db.Column(db.String, nullable=True) Address1 = db.Column(db.String, nullable=True) City = db.Column(db.String, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True,unique=False)
class SkillModel(db.Model, Model): """ SkillModel Class This class contains only SkillModel methods and represents the skills table in database. """ __tablename__ = 'skills' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False, unique=True) description = db.Column(db.String(1000), nullable=False) level = db.Column(db.Integer, nullable=False) created_at = db.Column(db.Date(), nullable=False, default=datetime.date.today(), unique=True) segment_id = db.Column(db.Integer, db.ForeignKey('segments.id'), nullable=False, default=1) segment = db.relationship('SegmentModel') def __init__(self, name: str, description: str, level: int, segment_id: int, _id: int = None) -> None: """ Loads a SkillModel. """ self.id = _id self.name = name self.description = description self.level = level self.segment_id = segment_id def json(self) -> dict: """ Retruns a SkillModel as a json format. """ return { 'id': self.id, 'name': self.name, 'description': self.description, 'level': self.level, 'created_at': self.created_at.isoformat(), 'segment': { 'id': self.segment.id, 'name': self.segment.name } } def curriculum_json(self) -> dict: """ Retruns a reduced SkillModel as a json format. """ return {'id': self.id, 'name': self.name, 'level': self.level}
class ProgressModel(db.Model): __tablename__ = "progress" progress_id = db.Column(db.String(36), primary_key=True) allocation_id = db.Column(db.String(36), db.ForeignKey('allocations.allocation_id', onupdate="CASCADE", ondelete="CASCADE")) date = db.Column(db.Date) document = db.Column(db.String(50)) comments = db.Column(db.Text) marks = db.Column(db.Integer) def __init__(self, progress_id, allocation_id, date, document, comments, marks): self.progress_id = progress_id self.allocation_id = allocation_id self.date = date self.document = document self.comments = comments self.marks = marks def serialize(self): """return json formatted data""" return { "progress_id": self.progress_id, "allocation_id": self.allocaiton_id, "date": self.date, "document": self.document, "comments": self.comments, "marks": self.marks }, 200 def add_progress(self): db.session.add(self) db.session.commit() @staticmethod def update_progress(): db.session.commit() def remove_progress(self): db.session.delete(self) db.session.commit() @classmethod def fetch_progress_by_id(cls, allocation_id): progress_rows = cls.query.filter_by(allocation_id=allocation_id) all_progress = [{ "progress_id": progress.progress_id, "allocation_id": progress.allocation_id, "date": progress.date, "document": progress.document, "comments": progress.comments, "marks": progress.marks } for progress in progress_rows] return all_progress @classmethod def fetch_progress_by_progress_id(cls, progress_id): return cls.query.filter_by(progress_id=progress_id).first()
class Item(db.Model): id = db.Column(db.Integer, primary_key=True) store_id = db.Column(db.Integer, db.ForeignKey('store.id')) item_image = db.Column(db.String, nullable=True) item_cover = db.Column(db.String, nullable=True) name = db.Column(db.String(20), nullable=False) detail = db.Column(db.String(300), nullable=True) date_Added = db.Column(db.String, nullable=True) cost = db.Column(db.Integer, nullable=False) number_of_items = db.Column(db.Integer, nullable=False)
class Store(db.Model): id = db.Column(db.Integer, primary_key=True) store_name = db.Column(db.String, nullable=False, unique=True) store_cover = db.Column(db.String, nullable=True) store_photo = db.Column(db.String, nullable=True) store_category = db.Column(db.String, nullable=True) store_description = db.Column(db.String, nullable=True) items = db.relationship('Item', backref='store', lazy=True) owner_id = db.Column(db.Integer, db.ForeignKey('user.id')) dropers = db.relationship('StoreRelation', backref='store', lazy=True)
class GraduationModel(db.Model, Model): """ GraduationModel Class This class contains only GraduationModel methods and represents the graduations table in database. """ __tablename__ = 'graduations' id = db.Column(db.Integer, primary_key=True) course = db.Column(db.String(50), nullable=False, unique=True) institution = db.Column(db.String(100), nullable=False, unique=True) started_at = db.Column(db.Date(), nullable=False, default=datetime.date.today(), unique=True) ended_at = db.Column(db.Date()) segment_id = db.Column(db.Integer, db.ForeignKey('segments.id'), nullable=False, default=1) segment = db.relationship('SegmentModel') def __init__(self, course: str, institution: str, started_at: datetime, segment_id: int, ended_at: datetime = None, _id: int = None) -> None: """ Loads a GraduationModel. """ self.id = _id self.course = course self.institution = institution self.started_at = string_to_date(started_at) self.ended_at = string_to_date(ended_at) if ended_at else None self.segment_id = segment_id def json(self) -> dict: """ Retruns a GraduationModel as a json format. """ return { 'id': self.id, 'course': self.course, 'institution': self.institution, 'started_at': self.started_at.isoformat(), 'ended_at': self.ended_at.isoformat() if self.ended_at else None, 'segment': { 'id': self.segment.id, 'name': self.segment.name } }
class Game(db.Model): __tablename__ = 'games' id = db.Column(db.Integer, primary_key=True) game_creator = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) num_players = db.Column(db.Integer, nullable=False) started = db.Column(db.Boolean, nullable=False) cancelled = db.Column(db.Boolean, nullable=False) completed = db.Column(db.Boolean, nullable=False) players_joined = db.Column(db.Integer, nullable=False)
class Posts(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(300), nullable=False) posted_date = db.Column(db.DateTime, nullable=False, default=datetime.now()) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def __repr__(self): return f'Users("{self.title}", "{self.posted_date}")'
class Ward(db.Model): __tablename__ = 'ward' id = db.Column(db.Integer, primary_key=True, auto_increment=True, nullable=False) name = db.Column(db.String(50), nullable=False) constituency_id = db.Column( db.Integer, db.ForeignKey('constituency.id', ondelete='CASCADE', onupdate='CASCADE')) county_id = db.Column( db.Integer, db.ForeignKey('county.id', ondelete='CASCADE', onupdate='CASCADE')) def __init__(self, name, constituency, county): self.name = name self.constituency = constituency self.county = county def serialize(self): return { "constituency": self.constituency.name, "id": self.id, "name": self.name } def save(self): db.session.add(self) db.session.commit() @classmethod def get_ward_id_by_name(cls, name): ward = cls.query.filter_by(name=name).first() return ward.id @classmethod def get_all_wards(cls): return [ward.serialize() for ward in cls.query.all()]
class LicencedClasses(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False) company = db.Column( db.Integer, db.ForeignKey('company_details.id', onupdate='CASCADE', ondelete='CASCADE')) insurance_class = db.Column( db.Integer, db.ForeignKey('insurance_class.class_id', onupdate='CASCADE', ondelete='CASCADE')) def __init__(self, company, insurance_class): self.company = company self.insurance_class = insurance_class def __repr__(self): return self.insurance_class def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_company_classes(cls, company_id): classes = cls.query.filter_by(company=company_id).all() class_list = [i.insurance_class for i in classes] return class_list
class TiedAgents(db.Model): """ Tied Agents model """ __tablename__ = 'tied_agent' id = db.Column(db.Integer, autoincrement=True, primary_key=True) # The tied agent is linked to a user profile since they have common details user_id = db.Column(db.Integer, db.ForeignKey( 'user.id', ondelete='CASCADE', onupdate='CASCADE')) ta_customer = db.relationship("TACustomer", backref="ta_affiliation") ta_staff = db.relationship("TAStaff", backref="ta_member") def __init__(self, user_id): self.user_id = user_id def __repr__(self): return f"{self.id}" def serialize(self): return self.user.serialize() def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_all_tied_agents(cls): tied_agents_rows = cls.query.all() tied_agents = [{ "id": agent.id, "user": agent.user } for agent in tied_agents_rows] return tied_agents @classmethod def get_tied_agent_by_id(cls, agent_id): return cls.query.filter_by(id=agent_id).first() @classmethod def get_tied_agent_by_user_id(cls, user_id): return cls.query.filter_by(user_id=user_id).first()
class Awards(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE")) award_title = db.Column(db.String(50), nullable=False) award_desc = db.Column(db.String(400)) create_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) updated_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, user_id, award_title="", award_desc=""): self.user_id = user_id self.award_title = award_title self.award_desc = award_desc
class IndividualCustomer(db.Model): """ Individual customer user linked to the UserProfile """ # ToDo: Serialize the individual customer data __tablename__ = 'individual_customer' id = db.Column(db.Integer, autoincrement=True, primary_key=True) customer_number = db.Column(db.String(50)) user_id = db.Column(db.Integer, db.ForeignKey( 'user.id', ondelete='CASCADE', onupdate='CASCADE')) salutation = db.Column(db.String(10), nullable=False) def __repr__(self): return f"{self.user}" def __init__(self, user_id, customer_number, salutation): self.user_id = user_id self.salutation = salutation self.customer_number = customer_number def serialize(self): customer_profile = { "customer_number": self.customer_number, "salutation": self.salutation } customer_profile.update(self.user.serialize()) return customer_profile def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_customer_by_user_id(cls, uid): return cls.query.filter_by(user_id=uid).first() @classmethod def get_customer_number(cls, uid): customer = cls.query.filter_by(user_id=uid).first() return customer.customer_number
class AttendanceModel(db.Model): __tablename__ = "attendance" id = db.Column(db.Integer, primary_key=True) user_id = db.Column( db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), nullable=False, ) course_name = db.Column(db.String()) percentage = db.Column(db.String()) ratio = db.Column(db.String()) def __init__(self, user_id, course_name, percentage, ratio): self.user_id = user_id self.course_name = course_name self.percentage = percentage self.ratio = ratio def json(self): return { "id": self.id, "user_id": self.user_id, "course_name": self.course_name, "percentage": self.percentage, "ratio": self.ratio }, 200 # Method to save user to DB def save_to_db(self): db.session.add(self) db.session.commit() # Method to remove user from DB def remove_from_db(self): db.session.delete(self) db.session.commit() # Class method which finds user from DB by username @classmethod def find_course_by_userid(cls, user_id): return cls.query.filter_by(user_id=user_id).all() # Class method which finds user from DB by id @classmethod def find_user_by_id(cls, _id): return cls.query.filter_by(id=_id).first() @classmethod def deleteall(cls): records = cls.query.all() db.session.delete(records) db.session.commit()
class UserRolePlacement(db.Model): """ Place users to a specified role """ __tablename__ = 'user_role' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column( db.Integer, db.ForeignKey('user.id', onupdate='CASCADE', ondelete='CASCADE')) role_id = db.Column( db.Integer, db.ForeignKey('role.id', onupdate='CASCADE', ondelete='CASCADE')) def __init__(self, user_id, role_id): self.user_id = user_id self.role_id = role_id def __repr__(self): return f"{self.user_id}" def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def fetch_role_by_user_id(cls, user_id): role = cls.query.filter_by(user_id=user_id).first() return role.role_id
class LocationData(db.Model): """ Stores ward, constituency and the relativity """ __tablename__ = "location_data" id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False) constituency = db.Column( db.Integer, db.ForeignKey('constituency.id', onupdate='CASCADE', ondelete='CASCADE')) ward = db.Column( db.Integer, db.ForeignKey('ward.id', onupdate='CASCADE', ondelete='CASCADE')) relativity = db.Column(db.Float, nullable=False) def __init__(self, constituency, ward, relativity): self.constituency = constituency self.ward = ward self.relativity = relativity def save(self): db.session.add(self) db.session.commit() def update(self, data): for key, item in data.items(): setattr(self, key, item) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False, unique=True) comment = db.Column(db.String(200)) price = db.Column(db.Float, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False) category = db.relationship('Category', backref=db.backref('products', lazy=True)) def __init__(self, name, comment, price, category_id): self.name = name self.comment = comment self.price = price self.category_id = category_id
class Education(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE")) school_name = db.Column(db.String(100), nullable=False) major = db.Column(db.String(50), nullable=False) status = db.Column(db.String(20), nullable=False) create_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) updated_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, user_id, school_name="", major="", status="attending"): self.user_id = user_id self.school_name = school_name self.major = major self.status = status
class PassesModel(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey(UsersModel.id), unique=True) display_name = db.Column(db.String(80), nullable=False) entries = db.Column(db.Text, nullable=False) def __init__(self, user_id, display_name, entries): self.user = user_id self.display_name = display_name self.entries = entries def save_to_db(self): db.session.add(self) db.session.commit() def remove_from_db(self): db.session.delete(self) db.session.commit() @classmethod def get_display_name_by_user_id(cls, user_id): target_pass = cls.query.filter_by(user_id=user_id).first() return target_pass.display_name @classmethod def get_string_pass_by_user_id(cls,user_id): target_pass = cls.query.filter_by(user_id=user_id).first() return target_pass.entries @classmethod def update_display_name(cls, user_id, new_name): target_pass = cls.query.filter_by(user_id=user_id).first() target_pass.display_name = new_name db.session.commit() @classmethod def update_entries(cls, user_id, new_entries): target_pass = cls.query.filter_by(user_id=user_id).first() target_pass.entries = new_entries db.session.commit()