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