class Submission(CRUDMixin, db.Model): __tablename__ = 'submission' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(140), unique=False, nullable=False) url = db.Column(db.String(399), unique=False, nullable=True) ups = db.Column(db.Integer, default=1) downs = db.Column(db.Integer, default=0) timestamp = db.Column(db.DateTime, nullable=False) author = db.Column(db.String, nullable=False) comment_count = db.Column(db.Integer, default=0) comments = db.relationship('Comment', backref='thread', lazy='dynamic') def __init__(self, title, url, timestamp, author): self.title = title self.url = url self.timestamp = timestamp self.author = author def post_comment(self, author, content, nested_limit, timestamp, parent_id=None): if parent_id: if parent_id.isdigit(): parent = fpage.comment.models.Comment.query.filter_by(id=int(parent_id)).first() if parent is None: return False elif parent.level >= nested_limit: return "max depth exceeded" else: user=fpage.user.models.User.query.filter_by(username=parent.author).first() user.update(unread_count=0 if user.unread_count is None else user.unread_count+1) session['unread']=user.unread_count fpage.message.models.Message.create(sender=author.username, reciever=user.username, content=content, description="Comment reply", thread_url="comments/{}".format(self.id)) else: return False comment = fpage.comment.models.Comment(self.id, author.username, content, timestamp, parent) else: comment = fpage.comment.models.Comment(self.id, author.username, content, timestamp) self.comment_count += 1 db.session.add(comment) db.session.commit() return True def get_comments(self): return self.comments.filter_by(level=0).all() def get_timestamp(self): return self.timestamp.isoformat() def __repr__(self): return '<Submission {id} "{title} >"'.format(id=self.id, title=self.title)
class ThreadVotes(CRUDMixin, db.Model): __tablename__ = 'thread_votes' id = db.Column(db.Integer, primary_key=True) thread_id = db.Column(db.Integer, db.ForeignKey('submission.id')) user = db.Column(db.Integer, db.ForeignKey('users.id')) vote_value = db.Column(db.Integer) def __init__(self, user, thread, value): self.user = user.id self.thread_id = thread.id self.vote_value = value def __repr__(self): return "<Thread Vote {vote_id} by user id {vote_user}>".format( vote_id=self.id, vote_user=self.user)
class CommentVotes(CRUDMixin, db.Model): __tablename__ = 'comment_votes' id = db.Column(db.Integer, primary_key=True) comment_id = db.Column(db.Integer, db.ForeignKey('comment.id')) user = db.Column(db.Integer, db.ForeignKey('users.id')) vote_value = db.Column(db.Integer) def __init__(self, user, comment, value): self.user = user.id self.comment_id = comment.id self.vote_value = value def __repr__(self): return "<Comment Vote {vote_id} by user id:{vote_user}>".format( vote_id=self.id, vote_user=self.user)
class User(UserMixin, CRUDMixin, db.Model): __tablename__ = 'users' username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String, nullable=False) # The hashed password created_at = db.Column(db.DateTime(), nullable=False) first_name = db.Column(db.String(30), nullable=True) last_name = db.Column(db.String(30), nullable=True) active = db.Column(db.Boolean()) is_admin = db.Column(db.Boolean()) unread_count = db.Column(db.Integer, nullable=True) def __init__(self, username=None, email=None, password=None, first_name=None, last_name=None, active=True, is_admin=False): self.username = username self.email = email if password: self.set_password(password) self.active = active self.is_admin = is_admin self.created_at = dt.datetime.utcnow() self.first_name = first_name self.last_name = last_name def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, password): return bcrypt.check_password_hash(self.password, password) @property def iso_time(self): return self.created_at.isoformat() @property def full_name(self): return "{0} {1}".format(self.first_name, self.last_name) def __repr__(self): return '<User "{username}">'.format(username=self.username)
class Comment(CRUDMixin, db.Model): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True) thread_id = db.Column(db.Integer, db.ForeignKey('submission.id')) author = db.Column(db.String, nullable=False) #todo: rel to author content = db.Column(db.String, nullable=False) ups = db.Column(db.Integer, default=1) downs = db.Column(db.Integer, default=0) parent_id = db.Column(db.Integer, db.ForeignKey('comment.id')) children = db.relationship('Comment', backref=db.backref('parent', remote_side=[id]), lazy='dynamic') timestamp = db.Column(db.DateTime, nullable=False) level = db.Column(db.Integer, default=0) def __init__(self, thread_id, author, content, timestamp, parent=None): self.thread_id = thread_id self.author = author self.content = content self.timestamp = timestamp if parent is not None: self.parent_id = parent.id self.level = parent.level + 1 else: self.parent_id = None self.level = 0 def get_comments(self): return self.children.all() def get_timestamp(self): return self.timestamp.isoformat() def __repr__(self): return "<Comment {} >".format(self.id)
class Message(CRUDMixin, db.Model): __tablename__ = 'message' id = db.Column(db.Integer, primary_key=True) sender = db.Column(db.String, nullable=False) reciever = db.Column(db.String, nullable=False) content = db.Column(db.String, nullable=False) timestamp = db.Column(db.DateTime, nullable=False) description = db.Column(db.String, nullable=False) thread_url = db.Column(db.String) def __init__(self, sender, reciever, content, description, thread_url): self.sender = sender self.reciever = reciever self.content = content self.timestamp = dt.datetime.now() self.description = description self.thread_url = thread_url @property def iso_time(self): return self.timestamp.isoformat()