-
Notifications
You must be signed in to change notification settings - Fork 1
/
blog.py
52 lines (44 loc) · 1.57 KB
/
blog.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
import uuid
import datetime
from models.database import Database
from models.post import Post
class Blog(object):
def __init__(self, author, title, description, id=None):
self.author = author
self.title = title
self.description = description
self.id = uuid.uuid4().hex if id is None else id
def new_post(self):
title = input("Enter Post Title: ")
content = input("Enter Post content: ")
date = input("Enter date, or leave blank for today (in format DDMMYYYY): ")
if date == "":
date = datetime.datetime.utcnow()
else:
date = datetime.datetime.strptime(date, "%d%m%Y")
post = Post(blog_id=self.id,
title=title,
content=content,
author=self.author,
date=date)
post.save_to_mongo()
def get_posts(self):
return Post.from_blog(self.id)
def save_to_mongo(self):
Database.insert(collection='blogs',
data=self.json())
def json(self):
return {
'author': self.author,
'title': self.title,
'description': self.description,
'id': self.id
}
@classmethod
def get_from_mongo(cls, id):
blog_data = Database.find_one(collection='blogs',
query={'id':id})
return cls(author=blog_data['author'],
title=blog_data['title'],
description=blog_data['description'],
id=blog_data['id'])