Modified post model.
[TownSquare.git] / townsquare / model / post.py
bloba90ca3bd2373649c754aa1ba710096d872b16a4b
1 import datetime
3 from fma import SuperDoc
4 from fma.orm import relation
5 from townsquare.model.user import User
6 from townsquare.model.channel import Channel
8 class Post(SuperDoc):
9 _collection_name = 'posts'
11 @property
12 def creator(self):
13 #TODO: optimize
14 try:
15 if self.creator_id == self._creator._id:
16 return self._creator
17 except: pass
18 if not hasattr(self, 'creator_id') or len(self.creator_id) is 0:
19 return None
20 #TODO: should be something like: account.get_account(creator_id)
21 self._creator = self._monga.col(User).find_one(_id=self.creator_id)
22 return self._creator
24 @property
25 def url(self):
26 return '/post/' + str(self._id)
28 def get_host(self):
29 try:
30 if self._host_classname == self.host.__class__.__name__ and self._host_id == self.host._id:
31 return self.host
32 except: pass
33 if not hasattr(self, '_host_classname') or len(self._host_classname) is 0:
34 return None
35 if not hasattr(self, '_host_id') or len(self._host_id) is 0:
36 return None
37 cl = globals()[self._host_classname]
38 self.host = self._monga.col(cl).find_one(_id=self._host_id)
39 return self.host
41 @property
42 def parent(self):
43 #TODO: check for loop
44 #TODO: optimize
45 try:
46 if self.parent_id == self._parent._id:
47 return self._parent
48 except: pass
49 if not hasattr(self, 'parent_id') or len(self.parent_id) is 0:
50 return None
51 self._parent = self._monga.col(Post).find_one(_id=self.parent_id)
52 return self._parent
54 def children_count(self):
55 try:
56 return self._children_count
57 except: pass
58 return 0
60 def touch(self, toucher_id, timestamp=None, updatestats=False, recursive=True):
61 self.update_user_id = toucher_id
62 if timestamp is None:
63 timestamp = datetime.datetime.utcnow()
64 self.update_timestamp = timestamp
65 if updatestats is True:
66 #CHECK: saved?
67 self._children_count = self._monga.col(Post).find(parent_id=self._id).count()
68 #TODO: calculate total children (including grand children recursively)
69 if recursive is True:
70 try:
71 self.parent.touch(toucher_id, timestamp, updatestats)
72 except: pass
73 self.save()
75 def save(self):
76 #TODO: check attributes
77 #if self.name is None or len(self.name) is 0:
78 #TODO: take the name from the few first words of the content (or bail out?)
79 # pass
80 #if self.idname is None or len(self.idname) is 0:
81 #TODO: generate idname (case-insensitive unique url-safe name) from title (name)
82 # if the idname is not provided generate it from name
83 # if the generated idname conflicts with existing one, add serial (dunno how to track the existing serials)
84 # if the idname is provided but that idname is already exists, bail out
85 # pass
87 #TODO: limit content
89 if hasattr(self, 'host'):
90 #TODO: check the validity of the object (e.g. has a valid id)
91 if isinstance(self.host, SuperDoc):
92 self._host_classname = self.host.__class__.__name__
93 self._host_id = str(self.host._id)
94 else:
95 if hasattr(self, '_host_metaname'):
96 self._host_classname = '' #TODO: delete property
97 if hasattr(self, '_host_id'):
98 self._host_id = '' #TODO: delete property
100 saved = self._saved()
101 timestamp = datetime.datetime.utcnow()
103 if not saved:
104 if hasattr(self, 'creation_timestamp') is not True or len(self.creation_timestamp) is 0:
105 self.creation_timestamp = timestamp
106 else:
107 #TODO: check whether the content has been modified
108 #TODO: save revision
109 #self.modification_user_id
110 #self.modification_timestamp = timestamp
111 #self.modification_count = self.modification_count + 1
112 pass
114 super(Post, self).save()
116 #TODO: need to care about edit
117 if not saved:
118 try:
119 self.parent.touch(self.creator_id, timestamp, updatestats=True)
120 except: pass