class VideoModel(db.Model): # Specifying table __tablename__ = "videos" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text()) url = db.Column(db.Text()) preview_url = db.Column(db.Text()) title = db.Column(db.String(200)) published = db.Column(db.Integer) source = db.Column(db.String(50)) channel = db.Column(db.String(100)) duration = db.Column(db.Integer) archived = db.Column(db.Boolean) free_to_reuse = db.Column(db.Boolean) authorised_to_reuse = db.Column(db.Boolean) def json(self): '''Returns VideoModel object in json format.''' return { "id": self.id, "name": self.name, "url": self.url, "preview_url": self.preview_url, "title": self.title, "published": self.published, "source": self.source, "channel": self.channel, "duration": self.duration, "archived": self.archived, "free_to_reuse": self.free_to_reuse, "authorised_to_reuse": self.authorised_to_reuse } def find_by_id(cls, _id: int) -> 'VideoModel': '''Returns a particular video of given id.''' return cls.query.filter_by(id=_id).first() def save_to_db(self) -> None: '''Add video to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes video from the database.''' db.session.delete(self) db.session.commit()
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 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 InstitutionModel(db.Model): # Specifying table __tablename__ = "institution" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text()) district = db.Column(db.String(50)) state = db.Column(db.String(50)) affiliation_code = db.Column(db.String(10)) active_applicant = db.Column(db.Integer) total_applicant = db.Column(db.Integer) is_school = db.Column(db.Boolean) is_college = db.Column(db.Boolean) def __init__(self, name, institute_type, state, district, affiliation_code): self.name = name self.state = state self.district = district self.affiliation_code = affiliation_code self.is_college = True if institute_type == 1 else False self.is_school = True if institute_type == 0 else False def save_to_db(self) -> None: '''Add institution to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes institution 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) # 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 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 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.Text()) videos = db.relationship('VideoModel', secondary=section_video, backref=db.backref('sections', lazy='dynamic')) 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()
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 VideoModel(db.Model): # Specifying table __tablename__ = "videos" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text()) url = db.Column(db.Text(), unique=True) preview_url = db.Column(db.Text()) title = db.Column(db.String(200)) date_published = db.Column(db.Date) source = db.Column(db.String(50), default="Youtube") channel = db.Column(db.String(100)) duration = db.Column(db.Integer) archived = db.Column(db.Boolean) free_to_reuse = db.Column(db.Boolean) authorized_to_reuse = db.Column(db.Boolean) def json(self): '''Returns VideoModel object in json format.''' return { "id": self.id, "name": self.name, "url": self.url, "preview_url": self.preview_url, "title": self.title, "date_published": datetime.strftime(self.date_published, '%Y-%m-%d'), "source": self.source, "channel": self.channel, "duration": self.duration, "archived": self.archived, "free_to_reuse": self.free_to_reuse, "authorized_to_reuse": self.authorized_to_reuse } def find_by_id(cls, _id: int) -> 'VideoModel': '''Returns a particular video of given id.''' return cls.query.filter_by(id=_id).first() def save_to_db(self) -> None: '''Add video to database''' db.session.add(self) db.session.commit() def update(self, new_data: Dict[str, object]) -> None: """Update existing video with new data""" for field in new_data: if hasattr(self, field): setattr(self, field, new_data[field]) db.session.commit() def delete_from_db(self) -> None: '''Deletes video from the database.''' db.session.delete(self) db.session.commit() # authors association def add_authors(self, authors: List['AuthorModel']) -> None: """ Add video authors entities to associated m2m model. :param authors: list of authors to add :return: None """ self.authors.extend(authors) db.session.commit() def update_authors(self, authors: List['AuthorModel']) -> None: """ Replace authors of video -> m2m records :param authors: list of new authors :return: None """ self.authors = authors db.session.commit() def remove_all_authors(self): """ Remove all authors of video (clean m2m records) :return: None """ self.authors = [] db.session.commit() # authors association def add_sections(self, sections: List['SectionModel']) -> None: """ Add video sections entities to associated m2m model. :param sections: list of sections to add :return: None """ self.sections.extend(sections) db.session.commit() def update_sections(self, sections: List['SectionModel']) -> None: """ Replace sections of video -> m2m records :param sections: list of new sections :return: None """ self.sections = sections db.session.commit() def remove_all_sections(self): """ Remove all sections of video (clean m2m records) :return: None """ self.sections = [] db.session.commit()
class UserModel(db.Model): # Specifying table __tablename__ = "user" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) firebase_id = db.Column(db.String(100)) name = db.Column(db.String(70)) email = db.Column(db.String(100)) password_hash = db.Column(db.String(100)) is_email_verified = db.Column(db.Boolean) profile_image = db.Column(db.Text()) occupation = db.Column(db.Text()) is_donor = db.Column(db.Boolean) is_recipient = db.Column(db.Boolean) is_moderator = db.Column(db.Boolean) address = db.Column(db.Text()) location = db.Column(db.Text()) def __init__(self, firebase_id, name, email, password, role): self.name = name self.email = email self.firebase_id = firebase_id # saving hash of password self.set_password(password) # Setting role of a user if role == 0: self.is_donor = True self.is_recipient = False self.is_moderator = False elif role == 1: self.is_donor = False self.is_recipient = True self.is_moderator = False elif role == 2: self.is_donor = False self.is_recipient = False self.is_moderator = True self.is_email_verified = False def json(self): '''UserModel object in json format.''' return { "id": self.id, "firebase_id": self.firebase_id, "name": self.name, "email": self.email, "is_email_verified": self.is_email_verified, "profile_image": self.profile_image, "address": self.address, "location": self.location, "occupation": self.occupation, "is_donor": self.is_donor, "is_recipient": self.is_recipient, "is_moderator": self.is_moderator, "profile_image": self.profile_image } @classmethod def find_by_email(cls, email: str) -> 'UserModel': return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, _id: int) -> 'UserModel': '''Returns user of given id.''' return cls.query.filter_by(id=_id).first() @classmethod def find_by_firebase_id(cls, firebase_id: str) -> 'UserModel': '''Returns user of given firebase_id.''' return cls.query.filter_by(firebase_id=firebase_id).first() def set_password(self, password_plain_text: str) -> None: """Sets user password""" self.password_hash = generate_password_hash(password_plain_text) def check_password(self, password_plain_text: str) -> bool: """Returns a boolean if password is the same as it's hash.""" return check_password_hash(self.password_hash, password_plain_text) def save_to_db(self) -> None: '''Add user to database''' db.session.add(self) db.session.commit() def delete_from_db(self) -> None: '''Deletes user from the database.''' db.session.delete(self) db.session.commit()