| 6 | | # Get one item functions |
|---|
| 7 | | |
|---|
| 8 | | def get_message(cursor, env, req, log, id): |
|---|
| 9 | | columns = ('id', 'forum', 'topic', 'replyto', 'time', 'author', 'body') |
|---|
| 10 | | sql = "SELECT id, forum, topic, replyto, time, author, body FROM message" \ |
|---|
| 11 | | " WHERE id = %s" |
|---|
| 12 | | log.debug(sql) |
|---|
| 13 | | cursor.execute(sql, (id,)) |
|---|
| 14 | | for row in cursor: |
|---|
| 15 | | row = dict(zip(columns, row)) |
|---|
| 16 | | row['author'] = wiki_to_oneliner(row['author'], env) |
|---|
| 17 | | row['body'] = wiki_to_html(row['body'], env, req) |
|---|
| 18 | | return row |
|---|
| 19 | | return None |
|---|
| 20 | | |
|---|
| 21 | | def get_topic(cursor, env, req, log, id): |
|---|
| 22 | | columns = ('id', 'forum', 'time', 'subject', 'body', 'author') |
|---|
| 23 | | sql = "SELECT id, forum, time, subject, body, author FROM topic WHERE id =" \ |
|---|
| 24 | | " %s" |
|---|
| 25 | | log.debug(sql) |
|---|
| 26 | | cursor.execute(sql, (id,)) |
|---|
| 27 | | for row in cursor: |
|---|
| 28 | | row = dict(zip(columns, row)) |
|---|
| 29 | | row['author'] = wiki_to_oneliner(row['author'], env) |
|---|
| 30 | | row['body'] = wiki_to_html(row['body'], env, req) |
|---|
| 31 | | row['time'] = format_datetime(row['time']) |
|---|
| 32 | | return row |
|---|
| 33 | | return None |
|---|
| 34 | | |
|---|
| 35 | | def get_topic_by_subject(cursor, env, req, log, subject): |
|---|
| 36 | | columns = ('id', 'forum', 'time', 'subject', 'body', 'author') |
|---|
| 37 | | sql = "SELECT id, forum, time, subject, body, author FROM topic WHERE subject =" \ |
|---|
| 38 | | " %s" |
|---|
| 39 | | log.debug(sql) |
|---|
| 40 | | cursor.execute(sql, (subject,)) |
|---|
| 41 | | for row in cursor: |
|---|
| 42 | | row = dict(zip(columns, row)) |
|---|
| 43 | | row['author'] = wiki_to_oneliner(row['author'], env) |
|---|
| 44 | | row['body'] = wiki_to_html(row['body'], env, req) |
|---|
| 45 | | row['time'] = format_datetime(row['time']) |
|---|
| 46 | | return row |
|---|
| 47 | | return None |
|---|
| 48 | | |
|---|
| 49 | | def get_forum(cursor, env, req, log, id): |
|---|
| 50 | | columns = ('name', 'moderators', 'id', 'time', 'subject', 'description', |
|---|
| 51 | | 'group') |
|---|
| 52 | | sql = "SELECT name, moderators, id, time, subject, description, forum_group" \ |
|---|
| 53 | | " FROM forum WHERE id = %s" |
|---|
| 54 | | log.debug(sql) |
|---|
| 55 | | cursor.execute(sql, (id,)) |
|---|
| 56 | | for row in cursor: |
|---|
| 57 | | row = dict(zip(columns, row)) |
|---|
| 58 | | row['moderators'] = row['moderators'].split(' ') |
|---|
| 59 | | row['description'] = wiki_to_oneliner(row['description'], env) |
|---|
| 60 | | return row |
|---|
| 61 | | return None |
|---|
| 62 | | |
|---|
| 63 | | def get_group(cursor, env, req, log, id): |
|---|
| 64 | | columns = ('id', 'name', 'description') |
|---|
| 65 | | sql = "SELECT id, name, description FROM forum_group WHERE id = %s" |
|---|
| 66 | | log.debug(sql) |
|---|
| 67 | | cursor.execute(sql, (id,)) |
|---|
| 68 | | for row in cursor: |
|---|
| 69 | | row = dict(zip(columns, row)) |
|---|
| 70 | | row['name'] = wiki_to_oneliner(row['name'], env) |
|---|
| 71 | | row['description'] = wiki_to_oneliner(row['description'], env) |
|---|
| 72 | | return row |
|---|
| 73 | | return None |
|---|
| 74 | | |
|---|
| 75 | | # Set item functions |
|---|
| 76 | | |
|---|
| 77 | | def set_group(cursor, log, forum, group): |
|---|
| 78 | | if not group: |
|---|
| 79 | | group = '0' |
|---|
| 80 | | sql = "UPDATE forum SET forum_group = %s WHERE id = %s" |
|---|
| 81 | | log.debug(sql) |
|---|
| 82 | | cursor.execute(sql, (group, forum)) |
|---|
| 83 | | |
|---|
| 84 | | def set_forum(cursor, log, topic, forum): |
|---|
| 85 | | sql = "UPDATE topic SET forum = %s WHERE id = %s" |
|---|
| 86 | | log.debug(sql) |
|---|
| 87 | | cursor.execute(sql, (forum, topic)) |
|---|
| 88 | | sql = "UPDATE message SET forum = %s WHERE topic = %s" |
|---|
| 89 | | log.debug(sql) |
|---|
| 90 | | cursor.execute(sql, (forum, topic)) |
|---|
| 91 | | |
|---|
| 92 | | # Edit all functons |
|---|
| 93 | | def edit_group(cursor, log, group, name, description): |
|---|
| 94 | | sql = "UPDATE forum_group SET name = %s, description = %s WHERE id = %s" |
|---|
| 95 | | log.debug(sql) |
|---|
| 96 | | cursor.execute(sql, (name, description, group)) |
|---|
| 97 | | |
|---|
| 98 | | def edit_forum(cursor, log, forum, name, subject, description, moderators, group): |
|---|
| 99 | | moderators = ' '.join(moderators) |
|---|
| 100 | | if not group: |
|---|
| 101 | | group = '0' |
|---|
| 102 | | sql = "UPDATE forum SET name = %s, subject = %s, description = %s," \ |
|---|
| 103 | | " moderators = %s, forum_group = %s WHERE id = %s" |
|---|
| 104 | | log.debug(sql) |
|---|
| 105 | | cursor.execute(sql, (name, subject, description, moderators, group, forum)) |
|---|
| 106 | | |
|---|
| 107 | | # Get list functions |
|---|
| 108 | | |
|---|
| 109 | | def get_groups(cursor, env, req, log): |
|---|
| 110 | | # Get count of forums without group |
|---|
| 111 | | sql = "SELECT COUNT(id) FROM forum WHERE forum_group = 0" |
|---|
| 112 | | log.debug(sql) |
|---|
| 113 | | cursor.execute(sql) |
|---|
| 114 | | no_group_forums = 0 |
|---|
| 115 | | for row in cursor: |
|---|
| 116 | | no_group_forums = row[0] |
|---|
| 117 | | groups = [{'id' : 0, 'name' : 'None', 'description' : 'No Group', |
|---|
| 118 | | 'forums' : no_group_forums}] |
|---|
| 119 | | |
|---|
| 120 | | # Get forum groups |
|---|
| 121 | | columns = ('id', 'name', 'description', 'forums') |
|---|
| 122 | | sql = "SELECT id, name, description, (SELECT COUNT(id) FROM forum f WHERE" \ |
|---|
| 123 | | " f.forum_group = forum_group.id) FROM forum_group" |
|---|
| 124 | | log.debug(sql) |
|---|
| 125 | | cursor.execute(sql) |
|---|
| 126 | | for row in cursor: |
|---|
| 127 | | row = dict(zip(columns, row)) |
|---|
| 128 | | row['name'] = wiki_to_oneliner(row['name'], env) |
|---|
| 129 | | row['description'] = wiki_to_oneliner(row['description'], env) |
|---|
| 130 | | groups.append(row) |
|---|
| 131 | | return groups |
|---|
| 132 | | |
|---|
| 133 | | def get_forums(cursor, env, req, log): |
|---|
| 134 | | columns = ('id', 'name', 'author', 'time', 'moderators', 'group', 'subject', |
|---|
| 135 | | 'description', 'topics', 'replies', 'lastreply', 'lasttopic') |
|---|
| 136 | | sql = "SELECT id, name, author, time, moderators, forum_group, subject," \ |
|---|
| 137 | | " description, (SELECT COUNT(id) FROM topic t WHERE t.forum = forum.id)," \ |
|---|
| 138 | | " (SELECT COUNT(id) FROM message m WHERE m.forum = forum.id), (SELECT" \ |
|---|
| 139 | | " MAX(time) FROM message m WHERE m.forum = forum.id), (SELECT MAX(time)" \ |
|---|
| 140 | | " FROM topic t WHERE t.forum = forum.id) FROM forum ORDER BY subject" |
|---|
| 141 | | log.debug(sql) |
|---|
| 142 | | cursor.execute(sql) |
|---|
| 143 | | forums = [] |
|---|
| 144 | | for row in cursor: |
|---|
| 145 | | row = dict(zip(columns, row)) |
|---|
| 146 | | #row['name'] = wiki_to_oneliner(row['name'], env) |
|---|
| 147 | | #row['subject'] = wiki_to_oneliner(row['subject'], env) |
|---|
| 148 | | row['moderators'] = wiki_to_oneliner(row['moderators'], env) |
|---|
| 149 | | row['description'] = wiki_to_oneliner(row['description'], env) |
|---|
| 150 | | if row['lastreply']: |
|---|
| 151 | | row['lastreply'] = pretty_timedelta(row['lastreply']) |
|---|
| | 9 | class DiscussionApi(object): |
|---|
| | 10 | def __init__(self, component, req): |
|---|
| | 11 | self.env = component.env |
|---|
| | 12 | self.log = component.log |
|---|
| | 13 | self.req = req |
|---|
| | 14 | self.db = self.env.get_db_cnx() |
|---|
| | 15 | self.cursor = self.db.cursor() |
|---|
| | 16 | |
|---|
| | 17 | # Main request processing function |
|---|
| | 18 | |
|---|
| | 19 | def render_discussion(self): |
|---|
| | 20 | # Get request mode |
|---|
| | 21 | group, forum, topic, message = self._get_items() |
|---|
| | 22 | modes = self._get_modes(group, forum, topic, message) |
|---|
| | 23 | |
|---|
| | 24 | # Determine moderator rights. |
|---|
| | 25 | if forum: |
|---|
| | 26 | is_moderator = (self.req.authname in forum['moderators']) or \ |
|---|
| | 27 | self.req.perm.has_permission('DISCUSSION_ADMIN') |
|---|
| 157 | | row['lasttopic'] = 'No topics' |
|---|
| 158 | | row['time'] = format_datetime(row['time']) |
|---|
| 159 | | forums.append(row) |
|---|
| 160 | | return forums |
|---|
| 161 | | |
|---|
| 162 | | def get_topics(cursor, env, req, log, forum): |
|---|
| 163 | | columns = ('id', 'forum', 'time', 'subject', 'body', 'author', |
|---|
| 164 | | 'replies', 'lastreply') |
|---|
| 165 | | sql = "SELECT id, forum, time, subject, body, author, (SELECT COUNT(id)" \ |
|---|
| 166 | | " FROM message m WHERE m.topic = topic.id), (SELECT MAX(time) FROM" \ |
|---|
| 167 | | " message m WHERE m.topic = topic.id) FROM topic WHERE forum = %s ORDER" \ |
|---|
| 168 | | " BY time" |
|---|
| 169 | | log.debug(sql) |
|---|
| 170 | | cursor.execute(sql, (forum,)) |
|---|
| 171 | | topics = [] |
|---|
| 172 | | for row in cursor: |
|---|
| 173 | | row = dict(zip(columns, row)) |
|---|
| 174 | | row['author'] = wiki_to_oneliner(row['author'], env) |
|---|
| 175 | | row['body'] = wiki_to_html(row['body'], env, req) |
|---|
| 176 | | if row['lastreply']: |
|---|
| 177 | | row['lastreply'] = pretty_timedelta(row['lastreply']) |
|---|
| | 101 | self.req.hdf['discussion.href'] = self.env.href.discussion() |
|---|
| | 102 | self.req.hdf['discussion.component'] = component |
|---|
| | 103 | |
|---|
| | 104 | # Determine mode |
|---|
| | 105 | if message: |
|---|
| | 106 | if component == 'admin': |
|---|
| | 107 | pass |
|---|
| | 108 | elif component == 'wiki': |
|---|
| | 109 | if action == 'add': |
|---|
| | 110 | return ['message-list'] |
|---|
| | 111 | elif action == 'quote': |
|---|
| | 112 | return ['message-quote', 'message-list'] |
|---|
| | 113 | elif action == 'post-add': |
|---|
| | 114 | if preview: |
|---|
| | 115 | return ['message-list'] |
|---|
| | 116 | else: |
|---|
| | 117 | return ['message-post-add', 'message-list'] |
|---|
| | 118 | elif action == 'delete': |
|---|
| | 119 | return ['message-delete', 'message-list'] |
|---|
| | 120 | else: |
|---|
| | 121 | return ['message-list'] |
|---|
| | 122 | else: |
|---|
| | 123 | if action == 'add': |
|---|
| | 124 | return ['message-list'] |
|---|
| | 125 | elif action == 'quote': |
|---|
| | 126 | return ['message-quote', 'message-list'] |
|---|
| | 127 | elif action == 'post-add': |
|---|
| | 128 | if preview: |
|---|
| | 129 | return ['message-list'] |
|---|
| | 130 | else: |
|---|
| | 131 | return ['message-post-add', 'message-list'] |
|---|
| | 132 | elif action == 'delete': |
|---|
| | 133 | return ['message-delete', 'message-list'] |
|---|
| | 134 | else: |
|---|
| | 135 | return ['message-list'] |
|---|
| | 136 | if topic: |
|---|
| | 137 | if component == 'admin': |
|---|
| | 138 | pass |
|---|
| | 139 | elif component == 'wiki': |
|---|
| | 140 | if action == 'add': |
|---|
| | 141 | return ['message-list'] |
|---|
| | 142 | elif action == 'quote': |
|---|
| | 143 | return ['message-quote', 'message-list'] |
|---|
| | 144 | elif action == 'post-add': |
|---|
| | 145 | if preview: |
|---|
| | 146 | return ['message-list'] |
|---|
| | 147 | else: |
|---|
| | 148 | return ['message-post-add', 'message-list'] |
|---|
| | 149 | elif action == 'delete': |
|---|
| | 150 | self.req.hdf['discussion.no_display'] = True |
|---|
| | 151 | return ['topic-delete', 'message-list'] |
|---|
| | 152 | else: |
|---|
| | 153 | return ['message-list'] |
|---|
| | 154 | else: |
|---|
| | 155 | if action == 'add': |
|---|
| | 156 | return ['message-list'] |
|---|
| | 157 | elif action == 'quote': |
|---|
| | 158 | return ['message-quote', 'message-list'] |
|---|
| | 159 | elif action == 'post-add': |
|---|
| | 160 | if preview: |
|---|
| | 161 | return ['message-list'] |
|---|
| | 162 | else: |
|---|
| | 163 | return ['message-post-add', 'message-list'] |
|---|
| | 164 | elif action == 'delete': |
|---|
| | 165 | return ['topic-delete', 'topic-list'] |
|---|
| | 166 | elif action == 'move': |
|---|
| | 167 | return ['topic-move'] |
|---|
| | 168 | elif action == 'post-move': |
|---|
| | 169 | return ['topic-post-move', 'topic-list'] |
|---|
| | 170 | else: |
|---|
| | 171 | return ['message-list'] |
|---|
| | 172 | elif forum: |
|---|
| | 173 | if component == 'admin': |
|---|
| | 174 | if action == 'post-edit': |
|---|
| | 175 | return ['forum-post-edit', 'admin-forum-list'] |
|---|
| | 176 | else: |
|---|
| | 177 | return ['admin-forum-list'] |
|---|
| | 178 | elif component == 'wiki': |
|---|
| | 179 | pass |
|---|
| | 180 | else: |
|---|
| | 181 | if action == 'add': |
|---|
| | 182 | return ['topic-add'] |
|---|
| | 183 | elif action == 'post-add': |
|---|
| | 184 | if preview: |
|---|
| | 185 | return ['topic-add'] |
|---|
| | 186 | else: |
|---|
| | 187 | return ['topic-post-add', 'topic-list'] |
|---|
| | 188 | elif action == 'delete': |
|---|
| | 189 | return ['forum-delete', 'forum-list'] |
|---|
| | 190 | else: |
|---|
| | 191 | return ['topic-list'] |
|---|
| | 192 | elif group: |
|---|
| | 193 | if component == 'admin': |
|---|
| | 194 | if action == 'post-add': |
|---|
| | 195 | return ['forum-post-add', 'admin-forum-list'] |
|---|
| | 196 | elif action == 'post-edit': |
|---|
| | 197 | return ['group-post-edit', 'admin-group-list'] |
|---|
| | 198 | elif action == 'delete': |
|---|
| | 199 | return ['forums-delete', 'admin-forum-list'] |
|---|
| | 200 | else: |
|---|
| | 201 | if group['id']: |
|---|
| | 202 | return ['admin-group-list'] |
|---|
| | 203 | else: |
|---|
| | 204 | return ['admin-forum-list'] |
|---|
| | 205 | elif component == 'wiki': |
|---|
| | 206 | pass |
|---|
| | 207 | else: |
|---|
| | 208 | if action == 'post-add': |
|---|
| | 209 | return ['forum-post-add', 'forum-list'] |
|---|
| | 210 | else: |
|---|
| | 211 | return ['forum-list'] |
|---|
| 179 | | row['lastreply'] = 'No replies' |
|---|
| 180 | | row['time'] = format_datetime(row['time']) |
|---|
| 181 | | topics.append(row) |
|---|
| 182 | | return topics |
|---|
| 183 | | |
|---|
| 184 | | def get_messages(cursor, env, req, log, topic): |
|---|
| 185 | | columns = ('id', 'replyto', 'time', 'author', 'body') |
|---|
| 186 | | sql = "SELECT id, replyto, time, author, body FROM message WHERE topic =" \ |
|---|
| 187 | | " %s ORDER BY time" |
|---|
| 188 | | log.debug(sql) |
|---|
| 189 | | cursor.execute(sql, (topic,)) |
|---|
| 190 | | |
|---|
| 191 | | messagemap = {} |
|---|
| 192 | | messages = [] |
|---|
| 193 | | |
|---|
| 194 | | for row in cursor: |
|---|
| 195 | | row = dict(zip(columns, row)) |
|---|
| 196 | | row['author'] = wiki_to_oneliner(row['author'], env) |
|---|
| 197 | | row['body'] = wiki_to_html(row['body'], env, req) |
|---|
| 198 | | row['time'] = format_datetime(row['time']) |
|---|
| 199 | | messagemap[row['id']] = row |
|---|
| 200 | | |
|---|
| 201 | | # Add top-level messages to the main list, in order of time |
|---|
| 202 | | if row['replyto'] == -1: |
|---|
| 203 | | messages.append(row) |
|---|
| 204 | | |
|---|
| 205 | | # Second pass, add replies |
|---|
| 206 | | for message in messagemap.values(): |
|---|
| 207 | | if message['replyto'] != -1: |
|---|
| 208 | | parent = messagemap[message['replyto']] |
|---|
| 209 | | if 'replies' in parent: |
|---|
| 210 | | parent['replies'].append(message) |
|---|
| 211 | | else: |
|---|
| 212 | | parent['replies'] = [message] |
|---|
| 213 | | return messages; |
|---|
| 214 | | |
|---|
| 215 | | def get_users(env, log): |
|---|
| 216 | | users = [] |
|---|
| 217 | | for user in env.get_known_users(): |
|---|
| 218 | | users.append(user[0]) |
|---|
| 219 | | return users |
|---|
| 220 | | |
|---|
| 221 | | # Add items functions |
|---|
| 222 | | |
|---|
| 223 | | def add_group(cursor, log, name, description): |
|---|
| 224 | | sql = "INSERT INTO forum_group (name, description) VALUES (%s, %s)" |
|---|
| 225 | | log.debug(sql) |
|---|
| 226 | | cursor.execute(sql, (escape(name), escape(description))) |
|---|
| 227 | | |
|---|
| 228 | | |
|---|
| 229 | | def add_forum(cursor, log, name, author, subject, description, moderators, |
|---|
| 230 | | group): |
|---|
| 231 | | moderators = ' '.join(moderators) |
|---|
| 232 | | if not group: |
|---|
| 233 | | group = '0' |
|---|
| 234 | | sql = "INSERT INTO forum (name, author, time, moderators, subject," \ |
|---|
| 235 | | " description, forum_group) VALUES (%s, %s, %s, %s, %s, %s, %s)" |
|---|
| 236 | | log.debug(sql) |
|---|
| 237 | | cursor.execute(sql, (escape(name), escape(author), str(int(time.time())), |
|---|
| 238 | | escape(moderators), escape(subject), escape(description), group)) |
|---|
| 239 | | |
|---|
| 240 | | def add_topic(cursor, log, forum, subject, author, body): |
|---|
| 241 | | sql = "INSERT INTO topic (forum, time, author, subject, body) VALUES" \ |
|---|
| 242 | | " (%s, %s, %s, %s, %s)" |
|---|
| 243 | | log.debug(sql) |
|---|
| 244 | | cursor.execute(sql, (forum, int(time.time()), escape(author), |
|---|
| 245 | | escape(subject), escape(body))) |
|---|
| 246 | | |
|---|
| 247 | | def add_message(cursor, log, forum, topic, replyto, author, body): |
|---|
| 248 | | sql = "INSERT INTO message (forum, topic, replyto, time, author, body)" \ |
|---|
| 249 | | " VALUES (%s, %s, %s, %s, %s, %s)" |
|---|
| 250 | | log.debug(sql) |
|---|
| 251 | | log.debug(body) |
|---|
| 252 | | cursor.execute(sql, (forum, topic, replyto, int(time.time()), |
|---|
| 253 | | escape(author), escape(Markup(body)))) |
|---|
| 254 | | |
|---|
| 255 | | # Delete items functions |
|---|
| 256 | | |
|---|
| 257 | | def delete_group(cursor, log, group): |
|---|
| 258 | | sql = "DELETE FROM forum_group WHERE id = %s" |
|---|
| 259 | | log.debug(sql) |
|---|
| 260 | | cursor.execute(sql, (group,)) |
|---|
| 261 | | sql = "UPDATE forum SET forum_group = 0 WHERE forum_group = %s" |
|---|
| 262 | | log.debug(sql) |
|---|
| 263 | | cursor.execute(sql, (group,)) |
|---|
| 264 | | |
|---|
| 265 | | def delete_forum(cursor, log, forum): |
|---|
| 266 | | sql = "DELETE FROM message WHERE forum = %s" |
|---|
| 267 | | log.debug(sql) |
|---|
| 268 | | cursor.execute(sql, (forum,)) |
|---|
| 269 | | sql = "DELETE FROM topic WHERE forum = %s" |
|---|
| 270 | | log.debug(sql) |
|---|
| 271 | | cursor.execute(sql, (forum,)) |
|---|
| 272 | | sql = "DELETE FROM forum WHERE id = %s" |
|---|
| 273 | | log.debug(sql) |
|---|
| 274 | | cursor.execute(sql, (forum,)) |
|---|
| 275 | | |
|---|
| 276 | | def delete_topic(cursor, log, topic): |
|---|
| 277 | | sql = "DELETE FROM message WHERE topic = %s" |
|---|
| 278 | | log.debug(sql) |
|---|
| 279 | | cursor.execute(sql, (topic,)) |
|---|
| 280 | | sql = "DELETE FROM topic WHERE id = %s" |
|---|
| 281 | | log.debug(sql) |
|---|
| 282 | | cursor.execute(sql, (topic,)) |
|---|
| 283 | | |
|---|
| 284 | | def delete_message(cursor, log, message): |
|---|
| 285 | | # Get message replies |
|---|
| 286 | | sql = "SELECT id FROM message WHERE replyto = %s" |
|---|
| 287 | | log.debug(sql) |
|---|
| 288 | | cursor.execute(sql, (message,)) |
|---|
| 289 | | replies = [] |
|---|
| 290 | | for row in cursor: |
|---|
| 291 | | replies.append(row[0]) |
|---|
| 292 | | |
|---|
| 293 | | # Delete all replies |
|---|
| 294 | | for reply in replies: |
|---|
| 295 | | delete_message(cursor, log, reply) |
|---|
| 296 | | |
|---|
| 297 | | # Delete message itself |
|---|
| 298 | | sql = "DELETE FROM message WHERE id = %s" |
|---|
| 299 | | log.debug(sql) |
|---|
| 300 | | cursor.execute(sql, (message,)) |
|---|
| | 213 | if component == 'admin': |
|---|
| | 214 | if action == 'post-add': |
|---|
| | 215 | return ['group-post-add', 'admin-group-list'] |
|---|
| | 216 | elif action == 'delete': |
|---|
| | 217 | return ['groups-delete', 'admin-group-list'] |
|---|
| | 218 | else: |
|---|
| | 219 | return ['admin-group-list'] |
|---|
| | 220 | elif component == 'wiki': |
|---|
| | 221 | pass |
|---|
| | 222 | else: |
|---|
| | 223 | if action == 'add': |
|---|
| | 224 | return ['forum-add'] |
|---|
| | 225 | elif action == 'post-add': |
|---|
| | 226 | return ['forum-post-add', 'forum-list'] |
|---|
| | 227 | else: |
|---|
| | 228 | return ['forum-list'] |
|---|
| | 229 | |
|---|
| | 230 | def _do_action(self, modes, group, forum, topic, message, is_moderator): |
|---|
| | 231 | for mode in modes: |
|---|
| | 232 | if mode == 'group-list': |
|---|
| | 233 | self.req.perm.assert_permission('DISCUSSION_VIEW') |
|---|
| | 234 | self.req.hdf['discussion.groups'] = self.get_groups() |
|---|
| | 235 | elif mode == 'admin-group-list': |
|---|
| | 236 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 237 | self.req.hdf['discussion.groups'] = self.get_groups() |
|---|
| | 238 | elif mode == 'group-add': |
|---|
| | 239 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 240 | elif mode == 'group-post-add': |
|---|
| | 241 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 242 | name = Markup(self.req.args.get('name')) |
|---|
| | 243 | description = Markup(self.req.args.get('description')) |
|---|
| | 244 | self.add_group(name, description) |
|---|
| | 245 | elif mode == 'group-post-edit': |
|---|
| | 246 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 247 | group = self.req.args.get('group') |
|---|
| | 248 | name = Markup(self.req.args.get('name')) |
|---|
| | 249 | description = Markup(self.req.args.get('description')) |
|---|
| | 250 | self.edit_group(group, name, description) |
|---|
| | 251 | elif mode == 'group-delete': |
|---|
| | 252 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 253 | elif mode == 'groups-delete': |
|---|
| | 254 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 255 | selection = self.req.args.get('selection') |
|---|
| | 256 | if isinstance(selection, (str, unicode)): |
|---|
| | 257 | selection = [selection] |
|---|
| | 258 | if selection: |
|---|
| | 259 | for group in selection: |
|---|
| | 260 | self.delete_group(group) |
|---|
| | 261 | elif mode == 'forum-list': |
|---|
| | 262 | self.req.perm.assert_permission('DISCUSSION_VIEW') |
|---|
| | 263 | self.req.hdf['discussion.groups'] = self.get_groups() |
|---|
| | 264 | self.req.hdf['discussion.forums'] = self.get_forums() |
|---|
| | 265 | elif mode == 'admin-forum-list': |
|---|
| | 266 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 267 | self.req.hdf['discussion.users'] = self.get_users() |
|---|
| | 268 | self.req.hdf['discussion.groups'] = self.get_groups() |
|---|
| | 269 | self.req.hdf['discussion.forums'] = self.get_forums() |
|---|
| | 270 | elif mode == 'forum-add': |
|---|
| | 271 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 272 | self.req.hdf['discussion.users'] = self.get_users() |
|---|
| | 273 | self.req.hdf['discussion.groups'] = self.get_groups() |
|---|
| | 274 | elif mode == 'forum-post-add': |
|---|
| | 275 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 276 | name = Markup(self.req.args.get('name')) |
|---|
| | 277 | author = self.req.authname |
|---|
| | 278 | subject = Markup(self.req.args.get('subject')) |
|---|
| | 279 | description = Markup(self.req.args.get('description')) |
|---|
| | 280 | moderators = self.req.args.get('moderators') |
|---|
| | 281 | group = self.req.args.get('group') |
|---|
| | 282 | if not moderators: |
|---|
| | 283 | moderators = [] |
|---|
| | 284 | if not isinstance(moderators, list): |
|---|
| | 285 | moderators = [moderators] |
|---|
| | 286 | self.add_forum(name, author, subject, description, moderators, |
|---|
| | 287 | group) |
|---|
| | 288 | elif mode == 'forum-post-edit': |
|---|
| | 289 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 290 | forum = self.req.args.get('forum') |
|---|
| | 291 | name = Markup(self.req.args.get('name')) |
|---|
| | 292 | subject = Markup(self.req.args.get('subject')) |
|---|
| | 293 | description = Markup(self.req.args.get('description')) |
|---|
| | 294 | moderators = self.req.args.get('moderators') |
|---|
| | 295 | group = self.req.args.get('group') |
|---|
| | 296 | if not moderators: |
|---|
| | 297 | moderators = [] |
|---|
| | 298 | if not isinstance(moderators, list): |
|---|
| | 299 | moderators = [moderators] |
|---|
| | 300 | self.edit_forum(forum, name, subject, description, moderators, |
|---|
| | 301 | group) |
|---|
| | 302 | elif mode == 'forum-delete': |
|---|
| | 303 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 304 | self.delete_forum(forum['id']) |
|---|
| | 305 | elif mode == 'forums-delete': |
|---|
| | 306 | self.req.perm.assert_permission('DISCUSSION_ADMIN') |
|---|
| | 307 | selection = self.req.args.get('selection') |
|---|
| | 308 | if isinstance(selection, (str, unicode)): |
|---|
| | 309 | selection = [selection] |
|---|
| | 310 | if selection: |
|---|
| | 311 | for forum in selection: |
|---|
| | 312 | self.delete_forum(forum) |
|---|
| | 313 | elif mode == 'topic-list': |
|---|
| | 314 | self.req.perm.assert_permission('DISCUSSION_VIEW') |
|---|
| | 315 | self.req.hdf['discussion.topics'] = self.get_topics(forum['id']) |
|---|
| | 316 | elif mode == 'topic-add': |
|---|
| | 317 | self.req.perm.assert_permission('DISCUSSION_APPEND') |
|---|
| | 318 | author = Markup(self.req.args.get('author')) |
|---|
| | 319 | body = Markup(self.req.args.get('body')) |
|---|
| | 320 | if author: |
|---|
| | 321 | self.req.hdf['discussion.author'] = wiki_to_oneliner(author, |
|---|
| | 322 | self.env) |
|---|
| | 323 | if body: |
|---|
| | 324 | self.req.hdf['discussion.body'] = wiki_to_html(body, |
|---|
| | 325 | self.env, self.req) |
|---|
| | 326 | self.req.hdf['discussion.time'] = format_datetime(time.time()) |
|---|
| | 327 | elif mode == 'topic-post-add': |
|---|
| | 328 | self.req.perm.assert_permission('DISCUSSION_APPEND') |
|---|
| | 329 | subject = Markup(self.req.args.get('subject')) |
|---|
| | 330 | author = Markup(self.req.args.get('author')) |
|---|
| | 331 | body = Markup(self.req.args.get('body')) |
|---|
| | 332 | self.add_topic(forum['id'], subject, author, body) |
|---|
| | 333 | elif mode == 'topic-move': |
|---|
| | 334 | self.req.perm.assert_permission('DISCUSSION_MODERATE') |
|---|
| | 335 | if not is_moderator: |
|---|
| | 336 | raise PermissionError('Forum moderate') |
|---|
| | 337 | self.req.hdf['discussion.forums'] = self.get_forums() |
|---|
| | 338 | elif mode == 'topic-post-move': |
|---|
| | 339 | self.req.perm.assert_permission('DISCUSSION_MODERATE') |
|---|
| | 340 | if not is_moderator: |
|---|
| | 341 | raise PermissionError('Forum moderate') |
|---|
| | 342 | new_forum = self.req.args.get('new_forum') |
|---|
| | 343 | self.set_forum(topic['id'], new_forum) |
|---|
| | 344 | elif mode == 'topic-delete': |
|---|
| | 345 | self.req.perm.assert_permission('DISCUSSION_MODERATE') |
|---|
| | 346 | if not is_moderator: |
|---|
| | 347 | raise PermissionError('Forum moderate') |
|---|
| | 348 | self.delete_topic(topic['id']) |
|---|
| | 349 | elif mode == 'message-list': |
|---|
| | 350 | self.req.perm.assert_permission('DISCUSSION_VIEW') |
|---|
| | 351 | author = Markup(self.req.args.get('author')) |
|---|
| | 352 | body = Markup(self.req.args.get('body')) |
|---|
| | 353 | if author: |
|---|
| | 354 | self.req.hdf['discussion.author'] = wiki_to_oneliner(author, |
|---|
| | 355 | self.env) |
|---|
| | 356 | if body: |
|---|
| | 357 | self.req.hdf['discussion.body'] = wiki_to_html(body, |
|---|
| | 358 | self.env, self.req) |
|---|
| | 359 | self.req.hdf['discussion.time'] = format_datetime(time.time()) |
|---|
| | 360 | self.req.hdf['discussion.messages'] = self.get_messages(topic['id']) |
|---|
| | 361 | elif mode == 'message-quote': |
|---|
| | 362 | self.req.perm.assert_permission('DISCUSSION_APPEND') |
|---|
| | 363 | lines = message['body'].splitlines() |
|---|
| | 364 | for I in xrange(len(lines)): |
|---|
| | 365 | lines[I] = '> %s' % (lines[I]) |
|---|
| | 366 | self.req.hdf['args.body'] = '\n'.join(lines) |
|---|
| | 367 | elif mode == 'message-post-add': |
|---|
| | 368 | self.req.perm.assert_permission('DISCUSSION_APPEND') |
|---|
| | 369 | author = Markup(self.req.args.get('author')) |
|---|
| | 370 | body = Markup(self.req.args.get('body')) |
|---|
| | 371 | if message: |
|---|
| | 372 | self.add_message(forum['id'], topic['id'], message['id'], |
|---|
| | 373 | author, body) |
|---|
| | 374 | else: |
|---|
| | 375 | self.add_message(forum['id'], topic['id'], '-1', author, body) |
|---|
| | 376 | elif mode == 'message-delete': |
|---|
| | 377 | self.req.perm.assert_permission('DISCUSSION_MODERATE') |
|---|
| | 378 | if not is_moderator: |
|---|
| | 379 | raise PermissionError('Forum moderate') |
|---|
| | 380 | self.delete_message(message['id']) |
|---|
| | 381 | |
|---|
| | 382 | # Get one item functions |
|---|
| | 383 | |
|---|
| | 384 | def get_message(self, id): |
|---|
| | 385 | columns = ('id', 'forum', 'topic', 'replyto', 'time', 'author', 'body') |
|---|
| | 386 | sql = "SELECT id, forum, topic, replyto, time, author, body FROM" \ |
|---|
| | 387 | " message WHERE id = %s" |
|---|
| | 388 | self.log.debug(sql % (id,)) |
|---|
| | 389 | self.cursor.execute(sql, (id,)) |
|---|
| | 390 | for row in self.cursor: |
|---|
| | 391 | row = dict(zip(columns, row)) |
|---|
| | 392 | return row |
|---|
| | 393 | return None |
|---|
| | 394 | |
|---|
| | 395 | def get_topic(self, id): |
|---|
| | 396 | columns = ('id', 'forum', 'time', 'subject', 'body', 'author') |
|---|
| | 397 | sql = "SELECT id, forum, time, subject, body, author FROM topic WHERE" \ |
|---|
| | 398 | " id = %s" |
|---|
| | 399 | self.log.debug(sql % (id,)) |
|---|
| | 400 | self.cursor.execute(sql, (id,)) |
|---|
| | 401 | for row in self.cursor: |
|---|
| | 402 | row = dict(zip(columns, row)) |
|---|
| | 403 | row['author'] = wiki_to_oneliner(row['author'], self.env) |
|---|
| | 404 | row['body'] = wiki_to_html(row['body'], self.env, self.req) |
|---|
| | 405 | row['time'] = format_datetime(row['time']) |
|---|
| | 406 | return row |
|---|
| | 407 | return None |
|---|
| | 408 | |
|---|
| | 409 | def get_topic_by_subject(self, subject): |
|---|
| | 410 | columns = ('id', 'forum', 'time', 'subject', 'body', 'author') |
|---|
| | 411 | sql = "SELECT id, forum, time, subject, body, author FROM topic WHERE" \ |
|---|
| | 412 | " subject = '%s'" % (subject) |
|---|
| | 413 | self.log.debug(sql) |
|---|
| | 414 | self.cursor.execute(sql) |
|---|
| | 415 | for row in self.cursor: |
|---|
| | 416 | row = dict(zip(columns, row)) |
|---|
| | 417 | row['author'] = wiki_to_oneliner(row['author'], self.env) |
|---|
| | 418 | row['body'] = wiki_to_html(row['body'], self.env, self.req) |
|---|
| | 419 | row['time'] = format_datetime(row['time']) |
|---|
| | 420 | return row |
|---|
| | 421 | return None |
|---|
| | 422 | |
|---|
| | 423 | def get_forum(self, id): |
|---|
| | 424 | columns = ('name', 'moderators', 'id', 'time', 'subject', |
|---|
| | 425 | 'description', 'group') |
|---|
| | 426 | sql = "SELECT name, moderators, id, time, subject, description," \ |
|---|
| | 427 | " forum_group FROM forum WHERE id = %s" |
|---|
| | 428 | self.log.debug(sql % (id,)) |
|---|
| | 429 | self.cursor.execute(sql, (id,)) |
|---|
| | 430 | for row in self.cursor: |
|---|
| | 431 | row = dict(zip(columns, row)) |
|---|
|
|---|