-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
57 lines (47 loc) · 1.63 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
import datetime
import json
from mongoengine import \
Document, StringField, DateTimeField, ReferenceField, ListField, CASCADE
class Post(Document):
title = StringField(required=True, max_length=200)
content = StringField(required=True)
author = StringField(required=True, max_length=50)
published = DateTimeField(default=datetime.datetime.now)
def to_dict(self, path=None):
url = "/api/posts/" + str(self.pk) + "/"
if path:
url = path + url
return {
"id": str(self.pk),
"title": self.title,
"content": self.content,
"author": self.author,
"published": self.published.isoformat(),
"url": url
}
def to_json(self, path=None):
return json.dumps(self.to_dict(path=path))
class Comment(Document):
comment = StringField(required=True)
author = StringField(required=True, max_length=50)
post = ReferenceField('Post', reverse_delete_rule=CASCADE)
def to_dict(self, path=None):
d = {
"id": str(self.pk),
"comment": self.comment,
"author": self.author,
"post_id": str(self.post.pk)
}
if path:
server_url = path + str(self.pk) + '/'
d.update({
'url': server_url
})
return d
def to_json(self, path=None):
return json.dumps(self.to_dict())
# from .db import db
# class Movie(Document):
# name = StringField(required=True, unique=True)
# casts = ListField(StringField(), required=True)
# genres =ListField(StringField(), required=True)