Skip to content

Commit f844604

Browse files
committed
Added from_attachment() method
Use Attachment objects instead of manually querying the database. (We still need the initial manual query as there's no way to get all attachments.)
1 parent 880ea0e commit f844604

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

code_comments/subscription.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from trac.admin import IAdminCommandProvider
2+
from trac.attachment import Attachment
23
from trac.core import Component, implements
34
from trac.versioncontrol import RepositoryManager, NoSuchChangeset
45

@@ -83,6 +84,26 @@ def _from_dict(cls, env, dict_):
8384
subscription.insert()
8485
return subscription
8586

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+
86107
@classmethod
87108
def from_changeset(cls, env, changeset):
88109
"""
@@ -156,19 +177,11 @@ def get_admin_commands(self):
156177
def _do_seed(self):
157178
# Create a subscription for all existing attachments
158179
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)
172185

173186
# Create a subscription for all existing revisions
174187
rm = RepositoryManager(self.env)

todo.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ Need to figure out how to handle blanket subscriptions.
2222

2323
## Subscriptions
2424

25-
- [ ] model
25+
- [x] model
26+
- [x] create
27+
- [ ] update
28+
- [ ] delete
29+
- [ ] query
2630
- [x] db
27-
- [ ] api - create, update, delete, query
31+
- [ ] api
2832

2933
## Listeners
3034

0 commit comments

Comments
 (0)