-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
110 lines (86 loc) · 3.12 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask.ext.sqlalchemy import SQLAlchemy
from flask import Flask
from settings import app, db
import sys
class TimeStampedModel(db.Model):
__abstract__ = True
created = db.Column(db.DateTime, default=db.func.now())
modified = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())
class User(TimeStampedModel):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
password = db.Column(db.Text)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=True)
post_ids = db.Column(db.String, default=None)
is_deleted = db.Column(db.Boolean, default=False)
posts = db.relationship('Post', backref='users', lazy='dynamic')
comments = db.relationship('Comment', backref='users', lazy='dynamic')
def __init__(self, password, username, email=None, post_ids=None):
self.password = password
self.username = username
self.email = email
self.post_ids = post_ids
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_username(self):
return self.username
def get_id(self):
try:
return unicode(self.id) # python 2
except NameError:
return str(self.id) # python 3
def __repr__(self):
return '<User %r>' % (self.username)
class Post(TimeStampedModel):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text)
image = db.Column(db.Text, nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
upvotes = db.Column(db.Integer, default=0)
upvoted_by = db.Column(db.String, default=None)
is_deleted = db.Column(db.Boolean, default=False)
comments = db.relationship('Comment', backref='posts', lazy='dynamic')
def __init__(self, content, user_id, image=None, upvotes=0, upvoted_by=None):
self.content = content
self.user_id = user_id
self.image = image
self.upvotes = upvotes
self.upvoted_by = upvoted_by
# def __repr__(self):
# return 'content %s' % self.content
def get_all_posts(self):
return Post.query.all()
def get_all_posts_user(self, user_id):
return Post.query.filter_by(user_id=user_id)
class Comment(TimeStampedModel):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
content = db.Column(db.Text)
image = db.Column(db.Text, nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
upvotes = db.Column(db.Integer, default=0)
upvoted_by = db.Column(db.String, default=None)
is_deleted = db.Column(db.Boolean, default=False)
def __init__(self, post_id, content, user_id, image=None, upvotes=0, upvoted_by=None):
self.post_id = post_id
self.content = content
self.user_id = user_id
self.image = image
self.upvotes = upvotes
self.upvoted_by = upvoted_by
def __repr__(self):
return 'content %s' % self.content
def get_all_comments_post(self, post_id):
return Comment.query.filter_by(post_id=post_id)
def get_all_comments_user(self, user_id):
return Comment.query.filter_by(user_id=user_id)
if __name__ == '__main__':
if sys.argv[1] == 'init':
db.create_all()