示例#1
0
class Comment(Model):
    """ Comment Model for storing comment related details """

    # Basic details
    id = Column(db.Integer, primary_key=True)
    public_id = Column(db.String(15))
    owner_id = Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    creator_public_id = Column(db.String(15))
    on_post = Column(db.Integer, db.ForeignKey("post.id"))

    # Comment content and details
    content = Column(db.Text)
    image_file = Column(db.String(40), default=None, nullable=True)
    created = Column(db.DateTime, default=datetime.utcnow)
    edited = Column(db.Boolean, default=False)

    likes = relationship("CommentLike",
                         backref="comment",
                         cascade="all, delete-orphan",
                         lazy=True)
    replies = relationship("Reply",
                           backref="comment",
                           cascade="all, delete-orphan",
                           lazy=True)

    def __repr__(self):
        return f"<Comment '{self.id}'>"
示例#2
0
class Role(Model):
    """ Role Model for storing role related details """

    __tablename__ = "role"

    id = Column(db.Integer, primary_key=True)
    name = Column(db.String(20), unique=True)
    description = Column(db.String(50))

    def __repr__(self):
        return f"<{self.name} - {self.id}>"
示例#3
0
class Notification(Model):
    id = Column(db.Integer, primary_key=True)
    # The user that committed the action, takes public_id.
    actor = Column(db.String(15))
    # Target owner is the user receiving the notification.
    target_owner = Column(db.Integer, db.ForeignKey("user.id"))
    # Example actions: 'liked', 'replied', 'commented', etc.
    action = Column(db.String(10))

    timestamp = Column(db.DateTime, default=datetime.utcnow)
    read = Column(db.Boolean, default=False)

    # Object type: post, comment, reply ...
    object_type = Column(db.String(20))
    object_public_id = Column(db.String(15))

    def is_read(self, read):
        self.read = True
示例#4
0
class Reply(Model):
    """ Reply Model for storing reply related details """

    # Basic details
    id = Column(db.Integer, primary_key=True)
    public_id = Column(db.String(15))
    owner_id = Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    creator_public_id = Column(db.String(15))
    on_comment = Column(db.Integer, db.ForeignKey("comment.id"))

    # Reply content and details
    content = Column(db.Text)
    image_file = Column(db.String(40), default=None, nullable=True)
    created = Column(db.DateTime, default=datetime.utcnow)
    edited = Column(db.Boolean, default=False)

    likes = relationship("ReplyLike",
                         backref="reply",
                         cascade="all, delete-orphan")

    def __repr__(self):
        return "<Reply {self.id}>"
示例#5
0
class User(Model):
    """ User Model for storing user related details """

    # Basic details
    id = Column(db.Integer, primary_key=True)
    public_id = Column(db.String(15), unique=True)
    email = Column(db.String(255), unique=True, nullable=False)
    username = Column(db.String(15), unique=True)
    password_hash = Column(db.String(255))

    private = Column(db.Boolean, default=False)

    # Optional
    full_name = Column(db.String(50), nullable=True)
    bio = Column(db.String(150), nullable=True)
    orientation = Column(db.String(30), nullable=True)

    ## Media
    profile_picture = Column(db.String(40), nullable=True)
    background_cover = Column(db.String(40), nullable=True)

    # Relationships
    notifications = relationship("Notification", backref="user")

    posts = relationship("Post", backref="author", lazy="dynamic")
    comments = relationship("Comment", backref="author", lazy="dynamic")
    replies = relationship("Reply", backref="author", lazy="dynamic")

    post_likes = relationship("PostLike", backref="user")
    comment_likes = relationship("CommentLike", backref="user")
    reply_likes = relationship("ReplyLike", backref="user")

    # Status
    joined_date = Column(db.DateTime)
    roles = relationship("Role",
                         secondary=roles_users,
                         backref=db.backref("users"),
                         lazy="dynamic")

    @property
    def password(self):
        raise AttributeError("Password: Write-Only field")

    @password.setter
    def password(self, password):
        self.password_hash = bcrypt.generate_password_hash(password).decode(
            "utf-8")

    def check_password(self, password):
        return bcrypt.check_password_hash(self.password_hash, password)

    # Checks if data can be accessed in relation with private mode.
    def is_accessible(self, current_user_id):
        # if user is private
        if self.private:
            # Check if current_user is in followers.
            return False

        return True

    def __repr__(self):
        return f"<User '{self.username}'>"