Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # Copyright: 2013 MoinMoin:AnaBalica | 1 # Copyright: 2013 MoinMoin:AnaBalica |
2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details. | 2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details. |
3 | 3 |
4 """ | 4 """ |
5 MoinMoin - Notifications | 5 MoinMoin - Notifications |
6 """ | 6 """ |
7 | 7 |
8 from urlparse import urljoin | 8 from urlparse import urljoin |
9 from whoosh.query import Term, And | 9 from whoosh.query import Term, And |
10 | 10 |
11 from flask import url_for, g as flaskg | 11 from flask import url_for, g as flaskg |
12 | 12 |
13 from MoinMoin.constants.keys import (ACTION_COPY, ACTION_RENAME, ACTION_REVERT, | 13 from MoinMoin.constants.keys import (ACTION_COPY, ACTION_RENAME, ACTION_REVERT, |
14 ACTION_SAVE, ACTION_TRASH, ALL_REVS, MTIME, | 14 ACTION_SAVE, ACTION_TRASH, ALL_REVS, MTIME, NAME_EXACT, WIKINAME) |
15 NAME_EXACT, WIKINAME) | |
16 from MoinMoin.i18n import _, L_, N_ | 15 from MoinMoin.i18n import _, L_, N_ |
17 from MoinMoin.themes import render_template | 16 from MoinMoin.themes import render_template |
18 from MoinMoin.util.diff_text import diff as text_diff | 17 from MoinMoin.util.diff_text import diff as text_diff |
19 from MoinMoin.util.diff_datastruct import make_text_diff, diff as dict_diff | 18 from MoinMoin.util.diff_datastruct import make_text_diff, diff as dict_diff |
20 | 19 |
21 from MoinMoin import log | 20 from MoinMoin import log |
22 logging = log.getLogger(__name__) | 21 logging = log.getLogger(__name__) |
23 | 22 |
24 # additional action values | 23 # additional action values |
25 ACTION_CREATE = u"CREATE" | 24 ACTION_CREATE = u"CREATE" |
(...skipping 12 matching lines...) Expand all Loading... | |
38 html_template = "mail/notification_main.html" | 37 html_template = "mail/notification_main.html" |
39 | 38 |
40 def __init__(self, app, item_name, revs, **kwargs): | 39 def __init__(self, app, item_name, revs, **kwargs): |
41 self.app = app | 40 self.app = app |
42 self.item_name = item_name | 41 self.item_name = item_name |
43 self.revs = revs | 42 self.revs = revs |
44 self.action = kwargs.get('action', None) | 43 self.action = kwargs.get('action', None) |
45 self.content = kwargs.get('content', None) | 44 self.content = kwargs.get('content', None) |
46 self.meta = kwargs.get('meta', None) | 45 self.meta = kwargs.get('meta', None) |
47 self.comment = kwargs.get('comment', None) | 46 self.comment = kwargs.get('comment', None) |
47 self.wiki_name = self.app.cfg.interwikiname | |
48 | 48 |
49 if self.action == ACTION_SAVE: | 49 if self.action == ACTION_SAVE: |
50 self.action = ACTION_CREATE if len(self.revs) == 1 else ACTION_MODIF Y | 50 self.action = ACTION_CREATE if len(self.revs) == 1 else ACTION_MODIF Y |
51 | 51 |
52 self.change_actions = {ACTION_CREATE: L_("has been created"), | 52 template_message = "The '%(item_name)s' item on '%(wiki_name)s' has {0} by %(user_name)s:" |
53 ACTION_MODIFY: L_("has been modified"), | 53 user_name = flaskg.user.name0 |
54 ACTION_RENAME: L_("has been renamed"), | 54 self.notification_sentence = {ACTION_CREATE: L_(template_message.format( |
55 ACTION_COPY: L_("has been copied"), | 55 "been created"), item_name=self.item_name, wiki_name=self.wiki_name, user_name=user_name), |
56 ACTION_REVERT: L_("has been reverted"), | 56 ACTION_MODIFY: L_(template_message.format( |
57 ACTION_TRASH: L_("has been deleted"), | 57 "been modified"), item_name=self.item_name, wiki_name=self.wiki_name , user_name=user_name), |
58 DESTROY_REV: L_("has one revision destroyed"), | 58 ACTION_RENAME: L_(template_message.format( |
59 DESTROY_ALL: L_("has been destroyed"), | 59 "been renamed"), item_name=self.item_name, wiki_name=self.wiki_name, user_name=user_name), |
Thomas.J.Waldmann
2013/08/19 21:41:10
that is unlikely to work for all languages.
you a
| |
60 ACTION_COPY: L_(template_message.format( | |
61 "been copied"), item_name=self.item_name, wiki_name=self.wiki_name, user_name=user_name), | |
62 ACTION_REVERT: L_(template_message.format( | |
63 "been reverted"), item_name=self.item_name, wiki_name=self.wiki_name , user_name=user_name), | |
64 ACTION_TRASH: L_(template_message.format( | |
65 "been deleted"), item_name=self.item_name, wiki_name=self.wiki_name, user_name=user_name), | |
66 DESTROY_REV: L_(template_message.format( | |
67 "one revision destroyed"), item_name=self.item_name, wiki_name=self. wiki_name, user_name=user_name), | |
68 DESTROY_ALL: L_(template_message.format( | |
69 "been destroyed"), item_name=self.item_name, wiki_name=self.wiki_nam e, user_name=user_name), | |
60 } | 70 } |
61 | 71 |
62 def get_content_diff(self): | 72 def get_content_diff(self): |
63 """ Create a content diff for the last item change | 73 """ Create a content diff for the last item change |
64 | 74 |
65 :return: list of diff lines | 75 :return: list of diff lines |
66 """ | 76 """ |
67 if self.action in (DESTROY_REV, DESTROY_ALL, ): | 77 if self.action in [DESTROY_REV, DESTROY_ALL, ]: |
Thomas.J.Waldmann
2013/08/19 21:41:10
well, this works, but i rather think of a list in
| |
68 content_diff = text_diff(self.content.get_data().splitlines(), []) | 78 content_diff = text_diff(self.content.get_data().splitlines(), []) |
69 elif self.action == ACTION_TRASH: | 79 elif self.action == ACTION_TRASH: |
70 content_diff = text_diff(self.revs[0].data.readlines(), []) | 80 content_diff = text_diff(self.revs[0].data.readlines(), []) |
Thomas.J.Waldmann
2013/08/19 21:41:10
where do you check that it really IS text?
should
ana.balica
2013/08/21 14:03:26
I almost forgot about other Content types.
What ab
| |
71 else: | 81 else: |
72 new_content = self.revs[0].data.readlines() | 82 new_content = self.revs[0].data.readlines() |
73 if len(self.revs) == 1: | 83 if len(self.revs) == 1: |
74 old_content = "" | 84 old_content = "" |
75 else: | 85 else: |
76 old_content = self.revs[1].data.readlines() | 86 old_content = self.revs[1].data.readlines() |
77 content_diff = text_diff(old_content, new_content) | 87 content_diff = text_diff(old_content, new_content) |
78 content_diff = [line.strip() for line in content_diff] | 88 content_diff = [line.strip() for line in content_diff] |
79 return content_diff | 89 return content_diff |
80 | 90 |
81 def get_meta_diff(self): | 91 def get_meta_diff(self): |
82 """ Create a meta diff for the last item change | 92 """ Create a meta diff for the last item change |
83 | 93 |
84 :return: a list of tuples of the format (<change type>, <basekeys>, <val ue>) | 94 :return: a list of tuples of the format (<change type>, <basekeys>, <val ue>) |
85 that can be used to format a diff | 95 that can be used to format a diff |
86 """ | 96 """ |
87 if self.action in (DESTROY_REV, DESTROY_ALL, ): | 97 if self.action in [DESTROY_REV, DESTROY_ALL, ]: |
88 meta_diff = dict_diff(dict(self.meta), dict()) | 98 meta_diff = dict_diff(dict(self.meta), dict()) |
89 elif self.action == ACTION_TRASH: | 99 elif self.action == ACTION_TRASH: |
90 meta_diff = dict_diff(self.revs[0].meta._meta, dict()) | 100 meta_diff = dict_diff(self.revs[0].meta._meta, dict()) |
Thomas.J.Waldmann
2013/08/19 21:41:10
looks a bit like these 2 cases could get implement
| |
91 else: | 101 else: |
92 new_meta = self.revs[0].meta._meta | 102 new_meta = self.revs[0].meta._meta |
93 if len(self.revs) == 1: | 103 if len(self.revs) == 1: |
94 old_meta = dict() | 104 old_meta = dict() |
95 else: | 105 else: |
96 old_meta = self.revs[1].meta._meta | 106 old_meta = self.revs[1].meta._meta |
97 meta_diff = dict_diff(old_meta, new_meta) | 107 meta_diff = dict_diff(old_meta, new_meta) |
Thomas.J.Waldmann
2013/08/19 21:41:10
even this...
| |
98 return meta_diff | 108 return meta_diff |
99 | 109 |
100 def generate_diff_url(self, domain): | 110 def generate_diff_url(self, domain): |
101 """ Generate the URL that leads to diff page of the last 2 revisions | 111 """ Generate the URL that leads to diff page of the last 2 revisions |
102 | 112 |
103 :param domain: domain name | 113 :param domain: domain name |
104 :return: the absolute URL to the diff page | 114 :return: the absolute URL to the diff page |
105 """ | 115 """ |
106 if not self.revs: | 116 if not self.revs: |
107 return u"" | 117 return u"" |
108 if len(self.revs) == 1: | 118 if len(self.revs) == 1: |
109 revid1 = revid2 = self.revs[0].revid | 119 revid1 = revid2 = self.revs[0].revid |
Thomas.J.Waldmann
2013/08/19 21:41:10
what does the diff show here?
ana.balica
2013/08/21 14:03:26
Hm... nothing. And there is no view that can show
| |
110 else: | 120 else: |
111 revid1 = self.revs[1].revid | 121 revid1 = self.revs[1].revid |
112 revid2 = self.revs[0].revid | 122 revid2 = self.revs[0].revid |
113 diff_rel_url = url_for('frontend.diff', item_name=self.item_name, rev1=r evid1, rev2=revid2) | 123 diff_rel_url = url_for('frontend.diff', item_name=self.item_name, rev1=r evid1, rev2=revid2) |
114 return urljoin(domain, diff_rel_url) | 124 return urljoin(domain, diff_rel_url) |
Thomas.J.Waldmann
2013/08/19 21:41:10
why is that needed?
ana.balica
2013/08/21 14:03:26
What exactly? The whole point of the function? url
| |
115 | 125 |
116 def render_templates(self): | 126 def render_templates(self): |
117 """ Render both plain text and HTML templates by providing all the | 127 """ Render both plain text and HTML templates by providing all the |
118 necessary arguments | 128 necessary arguments |
119 | 129 |
120 :return: tuple consisting of plain text and HTML notification message | 130 :return: tuple consisting of plain text and HTML notification message |
121 """ | 131 """ |
122 wiki_name = self.app.cfg.interwikiname | |
123 content_diff = self.get_content_diff() | 132 content_diff = self.get_content_diff() |
124 meta_diff = self.get_meta_diff() | 133 meta_diff = self.get_meta_diff() |
125 meta_diff_txt = list(make_text_diff(meta_diff)) | 134 meta_diff_txt = list(make_text_diff(meta_diff)) |
126 domain = self.app.cfg.interwiki_map[self.app.cfg.interwikiname] | 135 domain = self.app.cfg.interwiki_map[self.app.cfg.interwikiname] |
127 unsubscribe_url = urljoin(domain, url_for('subscribe_item', | 136 unsubscribe_url = urljoin(domain, url_for('frontend.subscribe_item', |
128 item_name=self.item_n ame)) | 137 item_name=self.item_name)) |
129 change_action = self.change_actions[self.action] | |
130 diff_url = self.generate_diff_url(domain) | 138 diff_url = self.generate_diff_url(domain) |
131 item_url = urljoin(domain, url_for('show_item', item_name=self.item_name )) | 139 item_url = urljoin(domain, url_for('frontend.show_item', item_name=self. item_name)) |
132 if self.comment: | 140 if self.comment: |
133 comment = self.meta["comment"] | 141 comment = self.meta["comment"] |
134 else: | 142 else: |
135 comment = self.revs[0].meta["comment"] | 143 comment = self.revs[0].meta["comment"] |
136 txt_template = render_template(Notification.txt_template, | 144 txt_template = render_template(Notification.txt_template, |
137 wiki_name=wiki_name, | 145 wiki_name=self.wiki_name, |
138 page_name=self.item_name, | 146 notification_sentence=self.notification_s entence[self.action], |
139 change_action=change_action, | |
140 user_name=flaskg.user.name0, | |
141 diff_url=diff_url, | 147 diff_url=diff_url, |
142 item_url=item_url, | 148 item_url=item_url, |
143 comment=comment, | 149 comment=comment, |
144 content_diff_=content_diff, | 150 content_diff_=content_diff, |
145 meta_diff_=meta_diff_txt, | 151 meta_diff_=meta_diff_txt, |
146 unsubscribe_url=unsubscribe_url, | 152 unsubscribe_url=unsubscribe_url, |
147 ) | 153 ) |
148 html_template = render_template(Notification.html_template, | 154 html_template = render_template(Notification.html_template, |
149 wiki_name=wiki_name, | 155 wiki_name=self.wiki_name, |
150 page_name=self.item_name, | 156 notification_sentence=self.notification_ sentence[self.action], |
151 change_action=change_action, | |
152 user_name=flaskg.user.name0, | |
153 diff_url=diff_url, | 157 diff_url=diff_url, |
154 item_url=item_url, | 158 item_url=item_url, |
155 comment=comment, | 159 comment=comment, |
156 content_diff_=content_diff, | 160 content_diff_=content_diff, |
157 meta_diff_=meta_diff, | 161 meta_diff_=meta_diff, |
158 unsubscribe_url=unsubscribe_url, | 162 unsubscribe_url=unsubscribe_url, |
159 ) | 163 ) |
160 return txt_template, html_template | 164 return txt_template, html_template |
161 | 165 |
162 | 166 |
163 def get_item_last_revisions(app, item_name): | 167 def get_item_last_revisions(app, item_name): |
164 """ Get 2 or less most recent item revisions from the index | 168 """ Get 2 or less most recent item revisions from the index |
165 | 169 |
166 :param app: local proxy app | 170 :param app: local proxy app |
167 :param item_name: the name of the item | 171 :param item_name: the name of the item |
168 :return: a list of revisions | 172 :return: a list of revisions |
169 """ | 173 """ |
170 terms = [Term(WIKINAME, app.cfg.interwikiname), Term(NAME_EXACT, item_name), ] | 174 terms = [Term(WIKINAME, app.cfg.interwikiname), Term(NAME_EXACT, item_name), ] |
171 query = And(terms) | 175 query = And(terms) |
172 return list( | 176 return list( |
173 flaskg.storage.search(query, idx_name=ALL_REVS, sortedby=[MTIME], | 177 flaskg.storage.search(query, idx_name=ALL_REVS, sortedby=[MTIME], |
174 reverse=True, limit=2)) | 178 reverse=True, limit=2)) |
LEFT | RIGHT |