annotate discusssions/model.py @ 0:c904249afb04

initial commit of discussions
author k0s <k0scist@gmail.com>
date Sat, 02 Jan 2010 13:36:23 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
1 import datetime
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
2 from discussions.utils import is_reply
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
3
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
4 class MessageContainer(object):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
5 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
6 something that can contain messages
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
7 JSON representation:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
8 {
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
9 'name': 'name of discussion',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
10 'subject': 'what its about',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
11 'subscribers': [ list of subsribers ],
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
12 'unsubscribers': [ list of unsubscribers ],
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
13 'moderated': False,
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
14 'options': { dict of options }, 'attachments': True # archive attachments?
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
15 'discussions': [ ordered list of discussions ids (either conferences or discussions)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
16 'date': creation date (format?),
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
17
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
18 }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
19 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
20
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
21 options = { 'archive': True,
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
22 'web': True,
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
23 'moderated': True,
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
24 'type': 'public',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
25 'post': 'web', 'email',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
26 'digest': (),
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
27 }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
28
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
29 def __init__(self, parent, name, subject, subscribers=(), locked=None):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
30 self.parent = parent
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
31
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
32 ### traverse to message
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
33 next = parent
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
34 self.path = [ name ]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
35 while next:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
36 self.path[:0] = next
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
37 next = next.parent
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
38
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
39 ### set options
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
40 if locked is not None:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
41 # lock the options
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
42
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
43 ### set subscribers
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
44
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
45 ### methods for messages
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
46
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
47 def message(self, id):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
48 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
49 retrieves a submessage given the id
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
50 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
51
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
52 def role(self, member, message):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
53
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
54
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
55 def messages(self): # future arguments == search criteria
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
56 """returns a list of top-level messages"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
57
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
58 def add_message(self, message):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
59 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
60 add a message to the container
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
61 message: an email message
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
62 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
63 for m in self.messages():
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
64 replied_to = m.reply_to(message):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
65 if replied_to:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
66 replied_to._add_message(message)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
67 # TODO: trigger
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
68 self._add_message(message)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
69
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
70 def search(self, **query):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
71 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
72 e.g. from='k0scist@example.com'
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
73 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
74
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
75 ### methods for members
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
76
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
77 def members(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
78 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
79 members of this discussion
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
80 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
81 return self.parent.members()
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
82
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
83 def subscribers(self, unsubscribers=()):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
84 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
85 return people watching these messages
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
86 walks up the chain
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
87 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
88 _subscribers = self['subscribers']
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
89
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
90
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
91
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
92 def subscribe(self, subscriber):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
93 """subscribe someone to the messages in the container"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
94
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
95 def unsubscribe(self, subscriber):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
96 """removes a member from this discussion"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
97
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
98 ### database functions
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
99
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
100 def _add_message(self, message):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
101 """add a message to the database"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
102 date = message.date()
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
103 if not date:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
104 date = datetime.datetime.now()
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
105
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
106 class Forum(MessageContainer):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
107 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
108 a mailing list and a forum
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
109 JSON same as above, but also with
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
110 { 'address': 'thisforum@example.org',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
111 'import': True # whether importing from an e.g. mbox is allowed or not
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
112 }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
113 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
114 def __init__(self, parent, name, subject, subscribers=(), options=None):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
115 MessageContainer(self, member, parent, name, subject, subscribers, options)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
116
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
117 # if name is None, it is the metacontainer
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
118 self.conferences = []
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
119
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
120
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
121 def reply_to(self, reply):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
122 if not is_reply(self.address, reply):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
123 return None
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
124 for _message in messages:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
125
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
126
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
127 def add_conference(self, options):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
128 """add a sub-forum to this forum"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
129
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
130 def members(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
131 """return members of this mailing list"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
132
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
133 class Discussion(MessageContainer):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
134 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
135 a mailing list thread
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
136 JSON representation: as for message, but also with
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
137 { 'headers': { dictionary of headers }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
138 }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
139 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
140
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
141 def reply_to(self, message):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
142 """TODO"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
143
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
144 def site(name):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
145 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
146 return the top-level site for the database 'name'
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
147 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
148
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
149