Mercurial > genshi > mirror
comparison examples/tutorial/geddit/model.py @ 611:236c351928a2 trunk
Add current code for GenshiTutorial to the `examples` directory.
author | cmlenz |
---|---|
date | Wed, 29 Aug 2007 17:51:45 +0000 |
parents | |
children | b6706f9346ac |
comparison
equal
deleted
inserted
replaced
610:5e358de79e4c | 611:236c351928a2 |
---|---|
1 from datetime import datetime | |
2 | |
3 | |
4 class Submission(object): | |
5 | |
6 def __init__(self, username, url, title): | |
7 self.username = username | |
8 self.url = url | |
9 self.title = title | |
10 self.time = datetime.utcnow() | |
11 self.comments = [] | |
12 | |
13 def __repr__(self): | |
14 return '<%s %r>' % (type(self).__name__, self.title) | |
15 | |
16 def add_comment(self, username, content): | |
17 comment = Comment(username, content, in_reply_to=self) | |
18 self.comments.append(comment) | |
19 return comment | |
20 | |
21 @property | |
22 def code(self): | |
23 uid = tuple([self.username, self.url, self.title, self.time]) | |
24 return hex(hash(uid))[2:] | |
25 | |
26 @property | |
27 def total_comments(self): | |
28 retval = [] | |
29 for comment in self.comments: | |
30 retval.append(comment) | |
31 retval.extend(comment.total_replies) | |
32 return retval | |
33 | |
34 | |
35 class Comment(object): | |
36 | |
37 def __init__(self, username, content, in_reply_to=None): | |
38 self.username = username | |
39 self.content = content | |
40 self.in_reply_to = in_reply_to | |
41 self.time = datetime.utcnow() | |
42 self.replies = [] | |
43 | |
44 def __repr__(self): | |
45 return '<%s>' % (type(self).__name__) | |
46 | |
47 def add_reply(self, username, content): | |
48 reply = Comment(username, content, in_reply_to=self) | |
49 self.replies.append(reply) | |
50 return reply | |
51 | |
52 @property | |
53 def code(self): | |
54 uid = tuple([self.in_reply_to.code, self.username, self.time]) | |
55 return hex(hash(uid))[2:] | |
56 | |
57 @property | |
58 def submission(self): | |
59 ref = self.in_reply_to | |
60 while ref: | |
61 if isinstance(ref, Submission): | |
62 return ref | |
63 ref = ref.in_reply_to | |
64 | |
65 @property | |
66 def total_replies(self): | |
67 retval = [] | |
68 for reply in self.replies: | |
69 retval.append(reply) | |
70 retval.extend(reply.total_replies) | |
71 return retval |