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
9 _collection_name
= 'posts'
15 if self
.creator_id
== self
._creator
._id
:
18 if not hasattr(self
, 'creator_id') or len(self
.creator_id
) is 0:
20 #TODO: should be something like: account.get_account(creator_id)
21 self
._creator
= self
._monga
.col(User
).find_one(_id
=self
.creator_id
)
26 return '/post/' + str(self
._id
)
30 if self
._host
_classname
== self
.host
.__class
__.__name
__ and self
._host
_id
== self
.host
._id
:
33 if not hasattr(self
, '_host_classname') or len(self
._host
_classname
) is 0:
35 if not hasattr(self
, '_host_id') or len(self
._host
_id
) is 0:
37 cl
= globals()[self
._host
_classname
]
38 self
.host
= self
._monga
.col(cl
).find_one(_id
=self
._host
_id
)
46 if self
.parent_id
== self
._parent
._id
:
49 if not hasattr(self
, 'parent_id') or len(self
.parent_id
) is 0:
51 self
._parent
= self
._monga
.col(Post
).find_one(_id
=self
.parent_id
)
54 def children_count(self
):
56 return self
._children
_count
60 def touch(self
, toucher_id
, timestamp
=None, updatestats
=False, recursive
=True):
61 self
.update_user_id
= toucher_id
63 timestamp
= datetime
.datetime
.utcnow()
64 self
.update_timestamp
= timestamp
65 if updatestats
is True:
67 self
._children
_count
= self
._monga
.col(Post
).find(parent_id
=self
._id
).count()
68 #TODO: calculate total children (including grand children recursively)
71 self
.parent
.touch(toucher_id
, timestamp
, updatestats
)
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?)
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
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
)
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()
104 if hasattr(self
, 'creation_timestamp') is not True or len(self
.creation_timestamp
) is 0:
105 self
.creation_timestamp
= timestamp
107 #TODO: check whether the content has been modified
109 #self.modification_user_id
110 #self.modification_timestamp = timestamp
111 #self.modification_count = self.modification_count + 1
114 super(Post
, self
).save()
116 #TODO: need to care about edit
119 self
.parent
.touch(self
.creator_id
, timestamp
, updatestats
=True)