| 17 | | from tracdiscussion.notification import * |
|---|
| | 17 | class IDiscussionFilter(Interface): |
|---|
| | 18 | """Extension point interface for components that want to filter discussion |
|---|
| | 19 | topics and messages before their addition.""" |
|---|
| | 20 | |
|---|
| | 21 | def filter_topic(req, topic): |
|---|
| | 22 | """ Called before new topic creation. May return tuple (False, |
|---|
| | 23 | <error_message>) or (True, <topic>) where <error_message> is a message |
|---|
| | 24 | that will be displayed when topic creation will be canceled and <topic> |
|---|
| | 25 | is modified topic that will be added.""" |
|---|
| | 26 | |
|---|
| | 27 | def filter_message(req, message): |
|---|
| | 28 | """ Called before new message creation. May return tuple (False, |
|---|
| | 29 | <error_message>) or (True, <message>) where <error_message> is a |
|---|
| | 30 | message that will be displayed when message creation will be canceled |
|---|
| | 31 | and <message> is modified message that will be added.""" |
|---|
| | 32 | |
|---|
| | 33 | |
|---|
| | 34 | class ITopicChangeListener(Interface): |
|---|
| | 35 | """Extension point interface for components that require notification |
|---|
| | 36 | when new forum topics are created, modified or deleted.""" |
|---|
| | 37 | |
|---|
| | 38 | def topic_created(topic): |
|---|
| | 39 | """Called when a topic is created. Only argument `topic` is |
|---|
| | 40 | a dictionary with topic attributes.""" |
|---|
| | 41 | |
|---|
| | 42 | def topic_changed(topic, old_topic): |
|---|
| | 43 | """Called when a topic is modified. `old_topic` is a dictionary |
|---|
| | 44 | containing the previous values of the topic attributes and `topic` is |
|---|
| | 45 | a dictionary with new values that has changed.""" |
|---|
| | 46 | |
|---|
| | 47 | def topic_deleted(topic): |
|---|
| | 48 | """Called when a topic is deleted. `topic` argument is a dictionary |
|---|
| | 49 | with values of attributes of just deleted topic.""" |
|---|
| | 50 | |
|---|
| | 51 | class IMessageChangeListener(Interface): |
|---|
| | 52 | """Extension point interface for components that require notification |
|---|
| | 53 | when new forum messages are created, modified or deleted.""" |
|---|
| | 54 | |
|---|
| | 55 | def message_created(message): |
|---|
| | 56 | """Called when a message is created. Only argument `message` is |
|---|
| | 57 | a dictionary with message attributes.""" |
|---|
| | 58 | |
|---|
| | 59 | def message_changed(message, old_message): |
|---|
| | 60 | """Called when a message is modified. `old_message` is a dictionary |
|---|
| | 61 | containing the previous values of the message attributes and `message` |
|---|
| | 62 | is a dictionary with new values that has changed.""" |
|---|
| | 63 | |
|---|
| | 64 | def message_deleted(message): |
|---|
| | 65 | """Called when a message is deleted. `message` argument is a dictionary |
|---|
| | 66 | with values of attributes of just deleted message.""" |
|---|
| 552 | | new_subject = context.req.args.get('subject') |
|---|
| 553 | | new_author = context.req.args.get('author') |
|---|
| 554 | | new_body = context.req.args.get('body') |
|---|
| 555 | | new_time = to_timestamp(datetime.now(utc)) |
|---|
| 556 | | |
|---|
| 557 | | self.log.debug((new_subject, new_body)) |
|---|
| 558 | | |
|---|
| 559 | | # Add topic. |
|---|
| 560 | | self.add_topic(context, forum['id'], new_subject, new_time, |
|---|
| 561 | | new_author, new_body) |
|---|
| 562 | | |
|---|
| 563 | | # Get new popic and notify about creation. |
|---|
| 564 | | new_topic = self.get_topic_by_time(context, new_time) |
|---|
| 565 | | to = self.get_topic_to_recipients(context, new_topic['id']) |
|---|
| 566 | | cc = self.get_topic_cc_recipients(context, new_topic['id']) |
|---|
| 567 | | notifier = DiscussionNotifyEmail(self.env) |
|---|
| 568 | | notifier.notify(context, mode, forum, new_topic, None, to, cc) |
|---|
| | 605 | topic = {'forum' : forum['id'], |
|---|
| | 606 | 'subject' : context.req.args.get('subject'), |
|---|
| | 607 | 'author' : context.req.args.get('author'), |
|---|
| | 608 | 'body' : context.req.args.get('body'), |
|---|
| | 609 | 'time': to_timestamp(datetime.now(utc))} |
|---|
| | 610 | |
|---|
| | 611 | # Add new topic. |
|---|
| | 612 | self.add_topic(context, topic) |
|---|
| | 613 | |
|---|
| | 614 | # Get inserted topic with new ID. |
|---|
| | 615 | topic = self.get_topic_by_time(context, topic) |
|---|
| | 616 | |
|---|
| | 617 | #Â Notify change listeners. |
|---|
| | 618 | for listener in self.listeners: |
|---|
| | 619 | listener.topic_created(topic) |
|---|
| 766 | | if display == 'flat-asc': |
|---|
| 767 | | self.data['messages'] = self.get_flat_messages(context, |
|---|
| 768 | | topic['id'], visit_time) |
|---|
| 769 | | elif display == 'flat-desc': |
|---|
| 770 | | self.data['messages'] = self.get_flat_messages(context, |
|---|
| 771 | | topic['id'], visit_time, 'ORDER BY time DESC') |
|---|
| 772 | | else: |
|---|
| 773 | | self.data['messages'] = self.get_messages(context, topic['id'], |
|---|
| 774 | | visit_time) |
|---|
| | 829 | self.data['messages'] = messages |
|---|
| | 830 | |
|---|
| | 831 | def _format_message(self, context, time, message): |
|---|
| | 832 | message['author'] = format_to_oneliner(self.env, context, |
|---|
| | 833 | message['author']) |
|---|
| | 834 | message['body'] = format_to_html(self.env, context, message['body']) |
|---|
| | 835 | message['new'] = int(message['time']) > time |
|---|
| | 836 | message['time'] = format_datetime(message['time']) |
|---|
| | 837 | return message |
|---|
| 778 | | def get_message(self, context, id): |
|---|
| 779 | | columns = ('id', 'forum', 'topic', 'replyto', 'time', 'author', 'body') |
|---|
| 780 | | sql = "SELECT id, forum, topic, replyto, time, author, body FROM" \ |
|---|
| 781 | | " message WHERE id = %s" |
|---|
| 782 | | self.env.log.debug(sql % (to_unicode(id),)) |
|---|
| 783 | | context.cursor.execute(sql, (to_unicode(id),)) |
|---|
| | 841 | def _get_item(self, context, table, columns, where = '', values = ()): |
|---|
| | 842 | sql = 'SELECT ' + ', '.join(columns) + ' FROM ' + table + (where |
|---|
| | 843 | and (' WHERE ' + where) or '') |
|---|
| | 844 | self.log.debug(sql, values) |
|---|
| | 845 | context.cursor.execute(sql, values) |
|---|
| 790 | | columns = ('id', 'forum', 'topic', 'replyto', 'time', 'author', 'body') |
|---|
| 791 | | sql = "SELECT id, forum, topic, replyto, time, author, body FROM" \ |
|---|
| 792 | | " message WHERE time = %s" |
|---|
| 793 | | self.env.log.debug(sql % (time,)) |
|---|
| 794 | | context.cursor.execute(sql, (time,)) |
|---|
| | 857 | #Â Get message by time of creation. |
|---|
| | 858 | return self._get_item(context, 'message', ('id', 'forum', 'topic', |
|---|
| | 859 | 'replyto', 'time', 'author', 'body'), 'time = %s', (time,)) |
|---|
| | 860 | |
|---|
| | 861 | def get_topic(self, context, id): |
|---|
| | 862 | # Get topic by ID. |
|---|
| | 863 | return self._get_item(context, 'topic', ('id', 'forum', 'subject', |
|---|
| | 864 | 'time', 'author', 'body'), 'id = %s', (id,)) |
|---|
| | 865 | |
|---|
| | 866 | def get_topic_by_time(self, context, time): |
|---|
| | 867 | # Get topic by time of creation. |
|---|
| | 868 | return self._get_item(context, 'topic', ('id', 'forum', 'subject', |
|---|
| | 869 | 'time', 'author', 'body'), 'time = %s', (time,)) |
|---|
| | 870 | |
|---|
| | 871 | def get_topic_by_subject(self, context, subject): |
|---|
| | 872 | # Get topic by subject. |
|---|
| | 873 | return self._get_item(context, 'topic', ('id', 'forum', 'subject', |
|---|
| | 874 | 'time', 'author', 'body'), 'subject = %s', (subject,)) |
|---|
| | 875 | |
|---|
| | 876 | def get_forum(self, context, id): |
|---|
| | 877 | # Get forum by ID. |
|---|
| | 878 | forum = self._get_item(context, 'forum', ('id', 'group', 'name', |
|---|
| | 879 | 'subject', 'time', 'moderators', 'description'), 'id = %s', (id,)) |
|---|
| | 880 | |
|---|
| | 881 | # Fix list of moderators. |
|---|
| | 882 | if forum: |
|---|
| | 883 | forum['moderators'] = forum['moderators'].split(' ') |
|---|
| | 884 | |
|---|
| | 885 | return forum |
|---|
| | 886 | |
|---|
| | 887 | def get_group(self, context, id): |
|---|
| | 888 | #Â Get forum group or none group. |
|---|
| | 889 | return self.get_item(context, 'group', ('id', 'name', 'description'), |
|---|
| | 890 | 'id = %s', (id,)) or {'id' : 0, 'name': 'None', 'description': |
|---|
| | 891 | 'No Group'} |
|---|
| | 892 | |
|---|
| | 893 | # Get list functions. |
|---|
| | 894 | |
|---|
| | 895 | def _get_items(self, context, table, columns, where = '', values = (), |
|---|
| | 896 | order_by = '', desc = False): |
|---|
| | 897 | sql = 'SELECT ' + ', '.join(columns) + ' FROM ' + table + (where |
|---|
| | 898 | and (' WHERE ' + where) or '') + (order_by and (' ORDER BY ' + |
|---|
| | 899 | order_by + (' ASC', ' DESC')[bool(desc)]) or '') |
|---|
| | 900 | self.log.debug(sql % values) |
|---|
| | 901 | context.cursor.execute(sql, values) |
|---|
| | 902 | items = [] |
|---|
| 797 | | return row |
|---|
| 798 | | return None |
|---|
| 799 | | |
|---|
| 800 | | def get_topic(self, context, id): |
|---|
| 801 | | columns = ('id', 'forum', 'subject', 'time', 'author', 'body') |
|---|
| 802 | | sql = "SELECT id, forum, subject, time, author, body FROM topic WHERE" \ |
|---|
| 803 | | " id = %s" |
|---|
| 804 | | self.env.log.debug(sql % (to_unicode(id),)) |
|---|
| 805 | | context.cursor.execute(sql, (to_unicode(id),)) |
|---|
| 806 | | for row in context.cursor: |
|---|
| 807 | | row = dict(zip(columns, row)) |
|---|
| 808 | | return row |
|---|
| 809 | | return None |
|---|
| 810 | | |
|---|
| 811 | | def get_topic_by_time(self, context, time): |
|---|
| 812 | | columns = ('id', 'forum', 'subject', 'time', 'author', 'body') |
|---|
| 813 | | sql = "SELECT id, forum, subject, time, author, body FROM topic WHERE" \ |
|---|
| 814 | | " time = %s" |
|---|
| 815 | | self.env.log.debug(sql % (time,)) |
|---|
| 816 | | context.cursor.execute(sql, (time,)) |
|---|
| 817 | | for row in context.cursor: |
|---|
| 818 | | row = dict(zip(columns, row)) |
|---|
| 819 | | return row |
|---|
| 820 | | return None |
|---|
| 821 | | |
|---|
| 822 | | def get_topic_by_subject(self, context, subject): |
|---|
| 823 | | columns = ('id', 'forum', 'subject', 'time', 'author', 'body') |
|---|
| 824 | | sql = "SELECT id, forum, subject, time, author, body FROM topic WHERE" \ |
|---|
| 825 | | " subject = %s" |
|---|
| 826 | | self.env.log.debug(sql % (subject,)) |
|---|
| 827 | | context.cursor.execute(sql, (subject,)) |
|---|
| 828 | | for row in context.cursor: |
|---|
| 829 | | row = dict(zip(columns, row)) |
|---|
| 830 | | return row |
|---|
| 831 | | return None |
|---|
| 832 | | |
|---|
| 833 | | def get_topic_to_recipients(self, context, id): |
|---|
| 834 | | sql = "SELECT t.author FROM topic t WHERE t.id = %s UNION SELECT" \ |
|---|
| 835 | | " m.author FROM message m WHERE m.topic = %s" |
|---|
| 836 | | self.env.log.debug(sql % (to_unicode(id), to_unicode(id))) |
|---|
| 837 | | context.cursor.execute(sql, (to_unicode(id), to_unicode(id))) |
|---|
| 838 | | to_recipients = [] |
|---|
| 839 | | for row in context.cursor: |
|---|
| 840 | | to_recipients.append(row[0]) |
|---|
| 841 | | return to_recipients |
|---|
| 842 | | |
|---|
| 843 | | def get_topic_cc_recipients(self, context, id): |
|---|
| 844 | | return [] |
|---|
| 845 | | |
|---|
| 846 | | def get_forum(self, context, id): |
|---|
| 847 | | columns = ('id', 'group', 'name', 'subject', 'time', 'moderators', |
|---|
| 848 | | 'description') |
|---|
| 849 | | sql = "SELECT id, forum_group, name, subject, time, moderators," \ |
|---|
| 850 | | " description FROM forum WHERE id = %s" |
|---|
| 851 | | self.env.log.debug(sql % (to_unicode(id),)) |
|---|
| 852 | | context.cursor.execute(sql, (to_unicode(id),)) |
|---|
| 853 | | for row in context.cursor: |
|---|
| 854 | | row = dict(zip(columns, row)) |
|---|
| 855 | | row['moderators'] = row['moderators'].split(' ') |
|---|
| 856 | | return row |
|---|
| 857 | | return None |
|---|
| 858 | | |
|---|
| 859 | | def get_group(self, context, id): |
|---|
| 860 | | columns = ('id', 'name', 'description') |
|---|
| 861 | | sql = "SELECT id, name, description FROM forum_group WHERE id = %s" |
|---|
| 862 | | self.env.log.debug(sql % (to_unicode(id),)) |
|---|
| 863 | | context.cursor.execute(sql, (to_unicode(id),)) |
|---|
| 864 | | for row in context.cursor: |
|---|
| 865 | | row = dict(zip(columns, row)) |
|---|
| 866 | | return row |
|---|
| 867 | | return {'id' : 0, 'name': 'None', 'description': 'No Group'} |
|---|
| 868 | | |
|---|
| 869 | | # Get list functions. |
|---|
| | 905 | items.append(row) |
|---|
| | 906 | return items |
|---|
| 1056 | | def get_flat_messages(self, context, topic_id, time, order_by = |
|---|
| 1057 | | 'ORDER BY time ASC'): |
|---|
| 1058 | | columns = ('id', 'replyto', 'time', 'author', 'body') |
|---|
| 1059 | | sql = "SELECT m.id, m.replyto, m.time, m.author, m.body FROM message m" \ |
|---|
| 1060 | | " WHERE m.topic = %s " + order_by |
|---|
| 1061 | | self.env.log.debug(sql % (to_unicode(topic_id),)) |
|---|
| 1062 | | context.cursor.execute(sql, (to_unicode(topic_id),)) |
|---|
| 1063 | | messages = [] |
|---|
| 1064 | | for row in context.cursor: |
|---|
| 1065 | | row = dict(zip(columns, row)) |
|---|
| 1066 | | row['author'] = format_to_oneliner(self.env, context, row['author']) |
|---|
| 1067 | | row['body'] = format_to_html(self.env, context, row['body']) |
|---|
| 1068 | | if int(row['time']) > time: |
|---|
| 1069 | | row['new'] = True |
|---|
| 1070 | | row['time'] = format_datetime(row['time']) |
|---|
| 1071 | | messages.append(row) |
|---|
| 1072 | | return messages |
|---|
| | 1088 | def get_flat_messages(self, context, id, order_by = 'time', desc = False): |
|---|
| | 1089 | # Return messages of specified topic. |
|---|
| | 1090 | return self._get_items(context, 'message', ('id', 'replyto', 'time', |
|---|
| | 1091 | 'author', 'body'), 'topic = %s', (id,), order_by, desc) |
|---|
| | 1092 | |
|---|
| | 1093 | def get_replies(self, context, id, order_by = 'time', desc = False): |
|---|
| | 1094 | # Return replies of specified message. |
|---|
| | 1095 | return self._get_items(context, 'message', ('id', 'replyto', 'time', |
|---|
| | 1096 | 'author', 'body'), where = 'replyto = %s', values = (id,), order_by |
|---|
| | 1097 | = order_by, desc = desc) |
|---|
| 1082 | | def add_group(self, context, name, description): |
|---|
| 1083 | | sql = "INSERT INTO forum_group (name, description) VALUES (%s, %s)" |
|---|
| 1084 | | self.env.log.debug(sql % (name, description)) |
|---|
| 1085 | | context.cursor.execute(sql, (name, description)) |
|---|
| 1086 | | |
|---|
| 1087 | | def add_forum(self, context, name, author, subject, description, moderators, |
|---|
| 1088 | | group): |
|---|
| 1089 | | moderators = ' '.join(moderators) |
|---|
| 1090 | | sql = "INSERT INTO forum (name, author, time, moderators, subject," \ |
|---|
| 1091 | | " description, forum_group) VALUES (%s, %s, %s, %s, %s, %s, %s)" |
|---|
| 1092 | | self.env.log.debug(sql % (name, author, to_timestamp(datetime.now(utc)), |
|---|
| 1093 | | moderators, subject, description, group)) |
|---|
| 1094 | | context.cursor.execute(sql, (name, author, to_timestamp(datetime.now(utc)), |
|---|
| 1095 | | moderators, subject, description, group)) |
|---|
| 1096 | | |
|---|
| 1097 | | def add_topic(self, context, forum, subject, time, author, body): |
|---|
| 1098 | | sql = "INSERT INTO topic (forum, subject, time, author, body) VALUES" \ |
|---|
| 1099 | | " (%s, %s, %s, %s, %s)" |
|---|
| 1100 | | self.env.log.debug(sql % (forum, subject, time, author, body)) |
|---|
| 1101 | | context.cursor.execute(sql, (forum, subject, time, author, body)) |
|---|
| 1102 | | |
|---|
| 1103 | | def add_message(self, context, forum, topic, replyto, time, author, body): |
|---|
| 1104 | | sql = "INSERT INTO message (forum, topic, replyto, time, author," \ |
|---|
| 1105 | | " body) VALUES (%s, %s, %s, %s, %s, %s)" |
|---|
| 1106 | | self.env.log.debug(sql % (forum, topic, replyto, time, author, body)) |
|---|
| 1107 | | context.cursor.execute(sql, (forum, topic, replyto, time, author, body)) |
|---|
| | 1108 | def _add_item(self, context, table, item): |
|---|
| | 1109 | fields = item.keys() |
|---|
| | 1110 | values = item.values() |
|---|
| | 1111 | sql = "INSERT INTO %s (" % (table,) + ", ".join(fields) + ") VALUES (" \ |
|---|
| | 1112 | + ", ".join(["%s" for I in xrange(len(fields))]) + ")" |
|---|
| | 1113 | self.log.debug(sql % tuple(values)) |
|---|
| | 1114 | context.cursor.execute(sql, tuple(values)) |
|---|
| | 1115 | |
|---|
| | 1116 | def add_group(self, context, group): |
|---|
| | 1117 | self._add_item(context, 'group', group) |
|---|
| | 1118 | |
|---|
| | 1119 | def add_forum(self, context, forum): |
|---|
| | 1120 | forum['moderators'] = ' '.join(forum['moderators']) |
|---|
| | 1121 | self._add_item(context, 'forum', forum) |
|---|
| | 1122 | |
|---|
| | 1123 | def add_topic(self, context, topic): |
|---|
| | 1124 | self._add_item(context, 'topic', topic) |
|---|
| | 1125 | |
|---|
| | 1126 | def add_message(self, context, message): |
|---|
| | 1127 | self._add_item(context, 'message', message) |
|---|
| 1111 | | def delete_group(self, context, group): |
|---|
| 1112 | | sql = "DELETE FROM forum_group WHERE id = %s" |
|---|
| 1113 | | self.env.log.debug(sql % (to_unicode(group),)) |
|---|
| 1114 | | context.cursor.execute(sql, (to_unicode(group),)) |
|---|
| 1115 | | sql = "UPDATE forum SET forum_group = 0 WHERE forum_group = %s" |
|---|
| 1116 | | self.env.log.debug(sql % (to_unicode(group),)) |
|---|
| 1117 | | context.cursor.execute(sql, (to_unicode(group),)) |
|---|
| 1118 | | |
|---|
| 1119 | | def delete_forum(self, context, forum): |
|---|
| 1120 | | sql = "DELETE FROM message WHERE forum = %s" |
|---|
| 1121 | | self.env.log.debug(sql % (to_unicode(forum),)) |
|---|
| 1122 | | context.cursor.execute(sql, (to_unicode(forum),)) |
|---|
| 1123 | | sql = "DELETE FROM topic WHERE forum = %s" |
|---|
| 1124 | | self.env.log.debug(sql % (to_unicode(forum),)) |
|---|
| 1125 | | context.cursor.execute(sql, (to_unicode(forum),)) |
|---|
| 1126 | | sql = "DELETE FROM forum WHERE id = %s" |
|---|
| 1127 | | self.env.log.debug(sql % (to_unicode(forum),)) |
|---|
| 1128 | | context.cursor.execute(sql, (to_unicode(forum),)) |
|---|
| 1129 | | |
|---|
| 1130 | | def delete_topic(self, context, topic): |
|---|
| 1131 | | sql = "DELETE FROM message WHERE topic = %s" |
|---|
| 1132 | | self.env.log.debug(sql % (to_unicode(topic),)) |
|---|
| 1133 | | context.cursor.execute(sql, (to_unicode(topic),)) |
|---|
| 1134 | | sql = "DELETE FROM topic WHERE id = %s" |
|---|
| 1135 | | self.env.log.debug(sql % (to_unicode(topic),)) |
|---|
| 1136 | | context.cursor.execute(sql, (to_unicode(topic),)) |
|---|
| 1137 | | |
|---|
| 1138 | | def delete_message(self, context, message): |
|---|
| 1139 | | # Get message replies. |
|---|
| 1140 | | sql = "SELECT m.id FROM message m WHERE m.replyto = %s" |
|---|
| 1141 | | self.env.log.debug(sql % (to_unicode(message),)) |
|---|
| 1142 | | context.cursor.execute(sql, (to_unicode(message),)) |
|---|
| 1143 | | replies = [] |
|---|
| 1144 | | |
|---|
| 1145 | | # Get all replies first. |
|---|
| 1146 | | for row in context.cursor: |
|---|
| 1147 | | replies.append(row[0]) |
|---|
| 1148 | | |
|---|
| 1149 | | # Delete all replies. |
|---|
| 1150 | | for reply in replies: |
|---|
| 1151 | | self.delete_message(context, reply) |
|---|
| | 1131 | def _delete_item(self, context, table, where = '', values = ()): |
|---|
| | 1132 | sql = 'DELETE FROM ' + table + (where and (' WHERE ' + where) or '') |
|---|
| | 1133 | self.env.log.debug(sql % values) |
|---|
| | 1134 | context.cursor.execute(sql, values) |
|---|
| | 1135 | |
|---|
| | 1136 | def delete_group(self, context, id): |
|---|
| | 1137 | #Â Delete group. |
|---|
| | 1138 | self._delete_item(context, 'forum_group', 'id = %s', (id,)) |
|---|
| | 1139 | |
|---|
| | 1140 | # Assing forums of this group to none group. |
|---|
| | 1141 | self._set_item(context, 'forum', 'forum_group', '0', 'forum_group = %s', |
|---|
| | 1142 | (id,)) |
|---|
| | 1143 | |
|---|
| | 1144 | def delete_forum(self, context, id): |
|---|
| | 1145 | #Â Delete all messages of this forum. |
|---|
| | 1146 | self._delete_item(context, 'message', 'forum = %s', (id,)) |
|---|
| | 1147 | |
|---|
| | 1148 | # Delete all topics of this forum. |
|---|
| | 1149 | self._delete_item(context, 'topic', 'forum = %s', (id,)) |
|---|
| | 1150 | |
|---|
| | 1151 | # Finally delete forum. |
|---|
| | 1152 | self._delete_item(context, 'forum', 'id = %s', (id,)) |
|---|
| | 1153 | |
|---|
| | 1154 | def delete_topic(self, context, id): |
|---|
| | 1155 | #Â Delete all messages of this topic. |
|---|
| | 1156 | self._delete_item(context, 'message', 'topic = %s', (id,)) |
|---|
| | 1157 | |
|---|
| | 1158 | # Delete topic itself. |
|---|
| | 1159 | self._delete_item(context, 'topic', 'id = %s', (id,)) |
|---|
| | 1160 | |
|---|
| | 1161 | def delete_message(self, context, id): |
|---|
| | 1162 | # Delete all replies of this message. |
|---|
| | 1163 | for reply in self.get_replies(context, id): |
|---|
| | 1164 | self.delete_message(context, reply['id']) |
|---|
| 1160 | | def set_group(self, context, forum, group): |
|---|
| 1161 | | if not group: |
|---|
| 1162 | | group = '0' |
|---|
| 1163 | | sql = "UPDATE forum SET forum_group = %s WHERE id = %s" |
|---|
| 1164 | | self.env.log.debug(sql % (group, forum)) |
|---|
| 1165 | | context.cursor.execute(sql, (group, forum)) |
|---|
| 1166 | | |
|---|
| 1167 | | def set_forum(self, context, topic, forum): |
|---|
| 1168 | | sql = "UPDATE topic SET forum = %s WHERE id = %s" |
|---|
| 1169 | | self.env.log.debug(sql % (forum, topic)) |
|---|
| 1170 | | context.cursor.execute(sql, (forum, topic)) |
|---|
| 1171 | | sql = "UPDATE message SET forum = %s WHERE topic = %s" |
|---|
| 1172 | | self.env.log.debug(sql % (forum, topic)) |
|---|
| 1173 | | context.cursor.execute(sql, (forum, topic)) |
|---|
| | 1171 | def _set_item(self, context, table, field, value, where = '', values = ()): |
|---|
| | 1172 | sql = 'UPDATE ' + table + ' SET ' + field + ' = "' + to_unicode(value) \ |
|---|
| | 1173 | + '"' + (where and (' WHERE ' + where) or '') |
|---|
| | 1174 | self.env.log.debug(sql % values) |
|---|
| | 1175 | context.cursor.execute(sql, values) |
|---|
| | 1176 | |
|---|
| | 1177 | def set_group(self, context, forum_id, group_id): |
|---|
| | 1178 | #Â Change group of specified forum. |
|---|
| | 1179 | self._set_item(context, 'forum', 'forum_group', group_id or '0', |
|---|
| | 1180 | 'id = %s', (forum_id,)) |
|---|
| | 1181 | |
|---|
| | 1182 | def set_forum(self, context, topic_id, forum_id): |
|---|
| | 1183 | # Change forum of all topics and messages. |
|---|
| | 1184 | self._set_item(context, 'topic', 'forum', forum_id, 'id = %s', |
|---|
| | 1185 | (topic_id,)) |
|---|
| | 1186 | self._set_item(context, 'message', 'forum', forum_id, 'topic = %s', |
|---|
| | 1187 | (topic_id,)) |
|---|
| 1177 | | def edit_group(self, context, group, name, description): |
|---|
| 1178 | | sql = "UPDATE forum_group SET name = %s, description = %s WHERE id = %s" |
|---|
| 1179 | | self.env.log.debug(sql % (name, description, group)) |
|---|
| 1180 | | context.cursor.execute(sql, (name, description, group)) |
|---|
| 1181 | | |
|---|
| 1182 | | def edit_forum(self, context, forum, name, subject, description, moderators, |
|---|
| 1183 | | group): |
|---|
| 1184 | | moderators = ' '.join(moderators) |
|---|
| 1185 | | if not group: |
|---|
| 1186 | | group = '0' |
|---|
| 1187 | | sql = "UPDATE forum SET name = %s, subject = %s, description = %s," \ |
|---|
| 1188 | | " moderators = %s, forum_group = %s WHERE id = %s" |
|---|
| 1189 | | self.env.log.debug(sql % (name, subject, description, moderators, |
|---|
| 1190 | | group, forum)) |
|---|
| 1191 | | context.cursor.execute(sql, (name, subject, description, moderators, |
|---|
| 1192 | | group, forum)) |
|---|
| 1193 | | |
|---|
| 1194 | | def edit_topic(self, context, topic, forum, subject, body): |
|---|
| 1195 | | sql = "UPDATE topic SET forum = %s, subject = %s, body = %s WHERE id" \ |
|---|
| 1196 | | " = %s" |
|---|
| 1197 | | self.env.log.debug(sql % (forum, subject, body, topic)) |
|---|
| 1198 | | context.cursor.execute(sql, (forum, subject, body, topic)) |
|---|
| 1199 | | |
|---|
| 1200 | | def edit_message(self, context, message, forum, topic, replyto, body): |
|---|
| 1201 | | sql = "UPDATE message SET forum = %s, topic = %s, replyto = %s, body" \ |
|---|
| 1202 | | " = %s WHERE id = %s" |
|---|
| 1203 | | self.env.log.debug(sql % (forum, topic, replyto, body, message)) |
|---|
| 1204 | | context.cursor.execute(sql, (forum, topic, replyto, body, message)) |
|---|
| | 1191 | def _edit_item(self, context, table, id, item): |
|---|
| | 1192 | fields = item.keys() |
|---|
| | 1193 | values = item.values() |
|---|
| | 1194 | sql = "UPDATE %s SET " % (table,) + ", ".join([("%s = %%s" % (field)) |
|---|
| | 1195 | for field in fields]) + " WHERE id = %s" |
|---|
| | 1196 | self.log.debug(sql % tuple(values + [id])) |
|---|
| | 1197 | context.cursor.execute(sql, tuple(values + [id])) |
|---|
| | 1198 | |
|---|
| | 1199 | def edit_group(self, context, id, group): |
|---|
| | 1200 | # Edit froum group. |
|---|
| | 1201 | self._edit_item(context, 'forum_group', id, group) |
|---|
| | 1202 | |
|---|
| | 1203 | def edit_forum(self, context, id, forum): |
|---|
| | 1204 | # Fix forum fields. |
|---|
| | 1205 | forum['moderators'] = ' '.join(forum['moderators']) |
|---|
| | 1206 | forum['group'] = forum['group'] or '0' |
|---|
| | 1207 | |
|---|
| | 1208 | # Edit forum. |
|---|
| | 1209 | self._edit_item(context, 'forum', id, forum) |
|---|
| | 1210 | |
|---|
| | 1211 | def edit_topic(self, context, id, topic): |
|---|
| | 1212 | # Edit topic. |
|---|
| | 1213 | self._edit_item(context, 'topic', id, topic) |
|---|
| | 1214 | |
|---|
| | 1215 | def edit_message(self, context, id, message): |
|---|
| | 1216 | #Â Edit message, |
|---|
| | 1217 | self._edit_item(context, 'message', id, message) |
|---|