-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
42 lines (34 loc) · 990 Bytes
/
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
from collections import namedtuple
from datetime import datetime
from app import db
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
reddit_id = db.Column(db.String(16), unique=True)
subreddit = db.Column(db.String(205))
title = db.Column(db.String(300))
url = db.Column(db.String(300))
author = db.Column(db.String(40))
timestamp = db.Column(db.DateTime)
@property
def reddit_link(self):
return "http://redd.it/%s" % (self.reddit_id)
@classmethod
def create_from_praw(cls, post):
instance = cls(
reddit_id = post.id,
subreddit = post.subreddit.display_name,
title = post.title,
url = post.url,
author = post.author.name if post.author else "",
timestamp = datetime.fromtimestamp(post.created_utc)
)
db.session.add(instance)
db.session.commit()
return instance
DATA_MODEL = Post
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "--setup":
db.create_all()
print("Work complete.")
exit()