-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.py
45 lines (36 loc) · 1.31 KB
/
post.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
import uuid
from models.database import Database
import datetime
__author__ = 'Libère'
class Post(object):
def __init__(self,blog_id, title, content, author, date=datetime.datetime.utcnow(), id=None):
self.blog_id = blog_id
self.title = title
self.content = content
self.author = author
self.created_date = date
self.id = uuid.uuid4().hex if id is None else id
def save_to_mongo(self):
Database.insert(collection='post',
data=self.json())
def json(self):
return {
'id' : self.id,
'blog_id' : self.blog_id,
'author' : self.author,
'content' : self.content,
'title' : self.title,
'created_date' : self.created_date
}
@classmethod
def from_mongo(cls, id):
post_data = Database.find_one(collection='posts', query={'id':id})
return cls(blog_id=post_data['blog_id'],
title=post_data['title'],
content=post_data['content'],
author=post_data['author'],
date=post_data['created_date'],
id=post_data['id'])
@staticmethod
def from_blog(id):
return [post for post in Database.find(collection='posts', query={'blog_id':id})]