class AuthorModel(db.Model): # Specifying table __tablename__ = "author" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) profile_image = db.Column(db.Text()) videos = db.relationship('VideoModel', secondary=video_author, backref=db.backref('authors', lazy='dynamic')) def save_to_db(self) -> None: '''Add author to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes author from the database.''' db.session.delete(self) db.session.commit() @classmethod def find_by_name(cls, name: str) -> 'AuthorModel': return cls.query.filter_by(name=name).first()
class SectionModel(db.Model): # Specifying table __tablename__ = "section" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) videos = db.relationship('VideoModel', secondary=section_video, backref=db.backref('sections', lazy='dynamic')) def init(self, title): self.title = title def save_to_db(self) -> None: '''Add section to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes section from the database.''' db.session.delete(self) db.session.commit() def add_video(self, video: 'VideoModel') -> None: self.videos.append(video) db.session.add(self) db.session.commit()
class CategoryModel(db.Model): # Specifying table __tablename__ = "category" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.Text()) section = db.relationship('SectionModel', secondary=category_section, backref=db.backref('category', lazy='dynamic')) def __init__(self, title): self.title = title @classmethod def find_by_title(cls, title: str) -> 'CategoryModel': return cls.query.filter_by(title=title.capitalize()).first() def save_to_db(self) -> None: '''Add category to database''' db.session.add(self) db.session.commit() def add_category_section(self, section) -> None: self.section.append(section) db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes category from the database.''' db.session.delete(self) db.session.commit()
class HistoryModel(db.Model): # Specifying table __tablename__ = "history" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) donor_id = db.Column(db.Integer, db.ForeignKey("user.id")) donor = db.relationship( UserModel, backref="history", primaryjoin="HistoryModel.donor_id == UserModel.id", ) recipient_id = db.Column(db.Integer, db.ForeignKey("user.id")) recipient = db.relationship( UserModel, backref="history", primaryjoin="HistoryModel.recipient_id == UserModel.id", ) application_id = db.Column(db.Integer, db.ForeignKey("application.id")) application = db.relationship( ApplicationModel, backref="history", primaryjoin="HistoryModel.application_id == ApplicationModel.id", ) institution_id = db.Column(db.Integer, db.ForeignKey("institution.id")) institution = db.relationship( InstitutionModel, backref="history", primaryjoin="HistoryModel.institution_id == InstitutionModel.id", ) amount = db.Column(db.String(15)) def save_to_db(self) -> None: '''Add history to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes history from the database.''' db.session.delete(self) db.session.commit()
class ModeratorRequestModel(db.Model): # Specifying table __tablename__ = "moderator_request" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) donor_id = db.Column(db.Integer, db.ForeignKey("user.id")) donor = db.relationship( UserModel, backref="moderator_request", primaryjoin="ModeratorRequestModel.donor_id == UserModel.id", ) application_id = db.Column(db.Integer, db.ForeignKey("application.id")) application = db.relationship( ApplicationModel, backref="moderator_request", primaryjoin= "ModeratorRequestModel.application_id == ApplicationModel.id", ) accepted = db.Column(db.Boolean) mod_email = db.Column(db.String(70)) def __init__(self, donor, invitee_email, unique_code): self.invitee_email = invitee_email self.unique_code = unique_code self.donor = donor @classmethod def find_by_mod_email(cls, invitee_email: str) -> 'InvitesModel': return cls.query.filter_by(invitee_email=invitee_email).first() def save_to_db(self) -> None: '''Add invite details to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes invite details from the database.''' db.session.delete(self) db.session.commit()
class SectionModel(db.Model): # Specifying table __tablename__ = "section" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) # NOTE: won't have much sections (compared to videos), not need index at this point title = db.Column(db.String(64), unique=True, nullable=False) videos = db.relationship( "VideoModel", secondary=section_video, backref=db.backref("sections", lazy="dynamic", cascade="all, delete"), ) def init(self, title): self.title = title def save_to_db(self) -> None: """Add section to database""" try: db.session.add(self) db.session.commit() except Exception: db.rollback() raise def delete_from_db(self) -> None: """Deletes section from the database.""" db.session.delete(self) db.session.commit() def update(self, **kwargs) -> None: """Updates section""" for field, field_value in kwargs.items(): if hasattr(self, field): setattr(self, field, field_value) try: db.session.commit() return self except Exception: db.session.rollback() raise def add_video(self, video: "VideoModel") -> None: self.videos.append(video) try: db.session.add(self) db.session.commit() except Exception: db.rollback() raise
class CategoryModel(db.Model): # Specifying table __tablename__ = "category" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) # NOTE: won't have much categories (compared to videos), not need index at this point title = db.Column(db.Text(), unique=True, nullable=False) section = db.relationship( "SectionModel", secondary=category_section, backref=db.backref("category", lazy="dynamic"), cascade="all, delete", passive_deletes=True, ) def __init__(self, title): self.title = title @classmethod def find_by_title(cls, title: str) -> "CategoryModel": return cls.query.filter_by(title=title.capitalize()).first() def save_to_db(self) -> None: """Add category to database""" self._add_and_commit() def add_sections(self, sections) -> None: """Add category section instance""" self.section.extend(sections) self._add_and_commit() def add_category_section(self, section) -> None: self.section.append(section) self._add_and_commit() def delete_from_db(self) -> None: """Deletes category from the database.""" db.session.delete(self) db.session.commit() def _add_and_commit(self): try: db.session.add(self) db.session.commit() except Exception as e: db.session.rollback() raise e def json(self): return {"id": self.id, "title": self.title}
class AuthorModel(db.Model): # Specifying table __tablename__ = "author" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), index=True) profile_image = db.Column(db.Text()) videos = db.relationship( "VideoModel", secondary=video_author, backref=db.backref("authors", lazy="dynamic"), ) def json(self): return { "id": self.id, "name": self.name, "profile_image": self.profile_image } @classmethod def find_by_name(cls, name: str) -> "AuthorModel": return cls.query.filter_by(name=name).first() def save_to_db(self) -> None: """Add author to database""" try: db.session.add(self) db.session.commit() except Exception: db.session.rollback() raise def delete_from_db(self) -> None: """Deletes author from the database.""" db.session.delete(self) db.session.commit() def add_video(self, video: "VideoModel") -> None: try: self.videos.append(video) db.session.add(self) db.session.commit() except Exception: db.session.rollback() raise
class StageModel(db.Model): # Specifying database table used for GoalModel __tablename__ = "stages" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30)) goal_id = db.Column(db.Integer, db.ForeignKey('goals.id'), nullable=False) tasks = db.relationship('TaskModel', backref='stages', lazy=True) def __init__(self, goal_id, title='mystage'): """Initialises userModel class with name. """ self.title = title self.goal_id = goal_id def json(self): """Returns Usermodel object in json format.""" return {"id": self.id, "name": self.title, "goal_id": self.goal_id} def __repr__(self): """Returns the user's name and username. """ return f"Stage {self.title}" @classmethod def find_by_id(cls, _id: int) -> 'StageModel': """Returns the goal that has the id we searched for. """ return cls.query.filter_by(id=_id).first() @classmethod def is_empty(cls) -> bool: """Returns a boolean if the GoalModel is empty or not. """ return cls.query.first() is None def save_to_db(self) -> int: """Adds a goal to the database. """ db.session.add(self) db.session.flush() id = self.id db.session.commit() return id def delete_from_db(self) -> None: """Deletes a user from the database. """ db.session.delete(self) db.session.commit()
class CategoryModel(db.Model): # Specifying table __tablename__ = "category" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.Text()) section = db.relationship( "SectionModel", secondary=category_section, backref=db.backref("category", lazy="dynamic"), ) def __init__(self, title): self.title = title @classmethod def find_by_title(cls, title: str) -> "CategoryModel": return cls.query.filter_by(title=title.capitalize()).first() def save_to_db(self) -> None: """Add category to database""" db.session.add(self) db.session.commit() def add_sections(self, sections) -> None: """Add category section instance""" self.section.extend(sections) db.session.add(self) db.session.commit() def add_category_section(self, section) -> None: self.section.append(section) db.session.add(self) db.session.commit() def delete_from_db(self) -> None: """Deletes category from the database.""" db.session.delete(self) db.session.commit() def json(self): return {"id": self.id, "title": self.title}
class PreferredLocationModel(db.Model): # Specifying table __tablename__ = "preferred_location" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) user = db.relationship( UserModel, backref=db.backref('preferred_location', uselist=False), primaryjoin="PreferredLocationModel.user_id == UserModel.id", ) state = db.Column(db.String(50)) district = db.Column(db.String(50)) sub_district = db.Column(db.String(50)) area = db.Column(db.String(50)) def __init__(self, user_id, state, district, sub_district, area): self.user_id = user_id self.state = state self.district = district self.sub_district = sub_district self.area = area def json(self): '''Preferred Location object in json format.''' return { "state": self.state, "district": self.district, "sub_district": self.sub_district, "area": self.area } @classmethod def find_by_user_id(cls, _user_id: int) -> 'PreferredLocationModel': '''Returns user preferred location of given user id.''' return cls.query.filter_by(user_id=_user_id).first() def save_to_db(self) -> None: """Saves the model to the database.""" db.session.add(self) db.session.commit()
class CategoryModel(db.Model): # Specifying table __tablename__ = "category" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.Text()) section = db.relationship('SectionModel', secondary=category_section, backref=db.backref('category', lazy='dynamic')) def save_to_db(self) -> None: '''Add category to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes category from the database.''' db.session.delete(self) db.session.commit()
class SectionModel(db.Model): # Specifying table __tablename__ = "section" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) videos = db.relationship( "VideoModel", secondary=section_video, backref=db.backref("sections", lazy="dynamic"), ) def init(self, title): self.title = title def save_to_db(self) -> None: """Add section to database""" db.session.add(self) db.session.commit() def delete_from_db(self) -> None: """Deletes section from the database.""" db.session.delete(self) db.session.commit() def update(self, **kwargs) -> None: """Updates section""" for field, field_value in kwargs.items(): if hasattr(self, field): setattr(self, field, field_value) db.session.commit() return self def add_video(self, video: "VideoModel") -> None: self.videos.append(video) db.session.add(self) db.session.commit()
class DocumentsModel(db.Model): # Specifying table __tablename__ = "documents" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) application_id = db.Column(db.Integer, db.ForeignKey("application.id")) application = db.relationship( ApplicationModel, backref="documents", primaryjoin="DocumentsModel.application_id == ApplicationModel.id", ) offer_letter = db.Column(db.Text()) fee_structure = db.Column(db.Text()) bank_statement = db.Column(db.Text()) affiliation_letter = db.Column(db.Text()) scholarship_letter = db.Column(db.Text()) additional_doc1 = db.Column(db.Text()) additional_doc2 = db.Column(db.Text()) additional_doc3 = db.Column(db.Text()) def __init__(self, offer_letter, fee_structure, bank_statement): self.offer_letter = offer_letter self.fee_structure = fee_structure self.bank_statement = bank_statement def save_to_db(self) -> None: '''Add document details to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes document details from the database.''' db.session.delete(self) db.session.commit()
class MentorshipRelationModel(db.Model): """Data Model representation of a mentorship relation. Attributes: id: integer primary key that defines the mentorships. mentor_id: integer indicates the id of the mentor. mentee_id: integer indicates the id of the mentee. action_user_id: integer indicates id of action user. mentor: relationship between UserModel and mentorship_relation. mentee: relationship between UserModel and mentorship_relation. creation_date: float that defines the date of creation of the mentorship. accept_date: float that indicates the date of acceptance of mentorship. start_date: float that indicates the starting date of mentorship. end_date: float that indicates the ending date of mentorship. state: enumeration that indicates state of mentorship. notes: string that indicates any notes. tasks_list_id: integer indicates the id of the tasks_list tasks_list: relationship between TasksListModel and mentorship_relation. """ # Specifying database table used for MentorshipRelationModel __tablename__ = "mentorship_relations" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) # personal data mentor_id = db.Column(db.Integer, db.ForeignKey("users.id")) mentee_id = db.Column(db.Integer, db.ForeignKey("users.id")) action_user_id = db.Column(db.Integer, nullable=False) mentor = db.relationship( UserModel, backref="mentor_relations", primaryjoin="MentorshipRelationModel.mentor_id == UserModel.id", ) mentee = db.relationship( UserModel, backref="mentee_relations", primaryjoin="MentorshipRelationModel.mentee_id == UserModel.id", ) creation_date = db.Column(db.Float, nullable=False) accept_date = db.Column(db.Float) start_date = db.Column(db.Float) end_date = db.Column(db.Float) state = db.Column(db.Enum(MentorshipRelationState), nullable=False) notes = db.Column(db.String(400)) tasks_list_id = db.Column(db.Integer, db.ForeignKey("tasks_list.id")) tasks_list = db.relationship(TasksListModel, uselist=False, backref="mentorship_relation") def __init__( self, action_user_id, mentor_user, mentee_user, creation_date, end_date, state, notes, tasks_list, ): self.action_user_id = action_user_id self.mentor = mentor_user self.mentee = mentee_user # same as mentee_user.mentee_relations.append(self) self.creation_date = creation_date self.end_date = end_date self.state = state self.notes = notes self.tasks_list = tasks_list def json(self): """Returns information of mentorship as a json object.""" return { "id": self.id, "action_user_id": self.action_user_id, "mentor_id": self.mentor_id, "mentee_id": self.mentee_id, "creation_date": self.creation_date, "accept_date": self.accept_date, "start_date": self.start_date, "end_date": self.end_date, "state": self.state, "notes": self.notes, } # def __repr__(self): # return f"Mentorship Relation with id = {self.id}, Mentor has id = {self.mentor_id} and Mentee has id = {self.mentee_id}" @classmethod def find_by_id(cls, _id) -> 'MentorshipRelationModel': """Returns the mentorship that has the passed id. Args: _id: The id of a mentorship. """ return cls.query.filter_by(id=_id).first() @classmethod def is_empty(cls) -> bool: """Returns True if the mentorship model is empty, and False otherwise.""" return cls.query.first() is None def save_to_db(self) -> None: """Saves the model to the database.""" db.session.add(self) db.session.commit() def delete_from_db(self) -> None: """Deletes the record of mentorship relation from the database.""" self.tasks_list.delete_from_db() db.session.delete(self) db.session.commit()
class ReservedApplicationModel(db.Model): # Specifying table __tablename__ = "reserved_application" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) application_id = db.Column(db.Integer, db.ForeignKey("application.id")) application = db.relationship( ApplicationModel, backref='reserved', primaryjoin= "ReservedApplicationModel.application_id == ApplicationModel.id", ) donor_id = db.Column(db.Integer, db.ForeignKey("user.id")) donor = db.relationship( UserModel, backref="reserved_as_donor", primaryjoin="ReservedApplicationModel.donor_id == UserModel.id", ) moderator_id = db.Column(db.Integer, db.ForeignKey("user.id")) moderator = db.relationship( UserModel, backref="reserved_as_moderator", primaryjoin="ReservedApplicationModel.moderator_id == UserModel.id", ) is_active = db.Column(db.Boolean) verified = db.Column(db.Boolean) verification_date = db.Column(db.String(20)) donation_date = db.Column(db.String(20)) amount = db.Column(db.Integer) def save_to_db(self) -> None: '''Add application to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes application from the database.''' db.session.delete(self) db.session.commit() @classmethod def find_by_id(cls, id: int) -> 'ReservedApplicationModel': '''Returns reserved application of given id.''' return cls.query.filter_by(id=id).first() @classmethod def find_by_application_id( cls, application_id: int) -> 'ReservedApplicationModel': '''Returns reserved application of given donor id.''' return cls.query.filter_by(application_id=application_id).first() @classmethod def find_by_donor_id(cls, donor_id: int) -> 'ReservedApplicationModel': '''Returns reserved application of given donor id.''' return cls.query.filter_by(donor_id=donor_id).first() @classmethod def find_by_moderator_id(cls, moderator_id: int) -> 'ReservedApplicationModel': '''Returns reserved application of given moderator id.''' return cls.query.filter_by(moderator_id=moderator_id).first() @classmethod def find_reserved_application( cls, application_id: int, donor_id: int, moderator_id: int) -> 'ReservedApplicationModel': '''Returns reserved application of given moderator id.''' return cls.query.filter_by(application_id=application_id, donor_id=donor_id, moderator_id=moderator_id).first()