예제 #1
0
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()
예제 #2
0
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()
예제 #3
0
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()
예제 #4
0
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
예제 #5
0
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}
예제 #6
0
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
예제 #7
0
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}
예제 #8
0
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()
예제 #9
0
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()
예제 #10
0
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()