|
1 | 1 | from trac.admin import IAdminCommandProvider |
| 2 | +from trac.attachment import Attachment |
2 | 3 | from trac.core import Component, implements |
3 | 4 | from trac.versioncontrol import RepositoryManager, NoSuchChangeset |
4 | 5 |
|
@@ -83,6 +84,26 @@ def _from_dict(cls, env, dict_): |
83 | 84 | subscription.insert() |
84 | 85 | return subscription |
85 | 86 |
|
| 87 | + @classmethod |
| 88 | + def from_attachment(cls, env, attachment): |
| 89 | + """ |
| 90 | + Creates a subscription from an Attachment object. |
| 91 | + """ |
| 92 | + _path = "/{0}/{1}/{2}".format(attachment.parent_realm, |
| 93 | + attachment.parent_id, |
| 94 | + attachment.filename) |
| 95 | + |
| 96 | + sub = { |
| 97 | + 'user': attachment.author, |
| 98 | + 'role': 'author', |
| 99 | + 'type': 'attachment', |
| 100 | + 'path': _path, |
| 101 | + 'repos': '', |
| 102 | + 'rev': '', |
| 103 | + 'notify': 'always', |
| 104 | + } |
| 105 | + return cls._from_dict(env, sub) |
| 106 | + |
86 | 107 | @classmethod |
87 | 108 | def from_changeset(cls, env, changeset): |
88 | 109 | """ |
@@ -156,19 +177,11 @@ def get_admin_commands(self): |
156 | 177 | def _do_seed(self): |
157 | 178 | # Create a subscription for all existing attachments |
158 | 179 | cursor = self.env.get_read_db().cursor() |
159 | | - cursor.execute("SELECT type, id, filename, author FROM attachment") |
160 | | - attachments = cursor.fetchall() |
161 | | - for attachment in attachments: |
162 | | - sub = { |
163 | | - 'user': attachment[3], |
164 | | - 'role': 'author', |
165 | | - 'type': 'attachment', |
166 | | - 'path': "/{0}/{1}/{2}".format(*attachment), |
167 | | - 'repos': '', |
168 | | - 'rev': '', |
169 | | - 'notify': 'always', |
170 | | - } |
171 | | - Subscription._from_dict(self.env, sub) |
| 180 | + cursor.execute("SELECT DISTINCT type, id FROM attachment") |
| 181 | + rows = cursor.fetchall() |
| 182 | + for row in rows: |
| 183 | + for attachment in Attachment.select(self.env, row[0], row[1]): |
| 184 | + Subscription.from_attachment(self.env, attachment) |
172 | 185 |
|
173 | 186 | # Create a subscription for all existing revisions |
174 | 187 | rm = RepositoryManager(self.env) |
|
0 commit comments