Skip to content

Commit ffd97bb

Browse files
committed
Make compatible with Trac 1.2
1 parent fa7cdff commit ffd97bb

File tree

10 files changed

+141
-221
lines changed

10 files changed

+141
-221
lines changed

HACKING

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ YOU WANT TO CONTRIBUTE BUT YOU DON'T KNOW HOW? NO MORE SUCH EXCUSES
44

55
In most cases this can be done via easy_install:
66

7-
# easy_install Trac==0.12
7+
# easy_install Trac==1.2
88

99
If you run into problems, see Trac documentation [1][2].
1010

@@ -42,7 +42,7 @@ repository_type = svn
4242
Remember the file location.
4343

4444
6. Add yourself as admin:
45-
45+
4646
$ trac-admin <trac-name> permission add <username> TRAC_ADMIN
4747

4848
6. Run trac server:
@@ -67,7 +67,7 @@ You should now have a working vanilla Trac at http://localhost:8000/, where you
6767

6868
# Footnotes
6969

70-
[1] http://trac.edgewall.org/wiki/0.12/TracInstall
70+
[1] http://trac.edgewall.org/wiki/1.2/TracInstall
7171
[2] http://trac.edgewall.org/wiki/TracDev/DevelopmentEnvironmentSetup
7272
[3] http://trac.edgewall.org/wiki/TracSubversion#GettingSubversion
73-
[4] http://stackoverflow.com/questions/14538468/how-to-install-python-subversion-bindings-needed-by-hg-convert-on-mac-os-x-10-8/16950786#16950786
73+
[4] http://stackoverflow.com/questions/14538468/how-to-install-python-subversion-bindings-needed-by-hg-convert-on-mac-os-x-10-8/16950786#16950786

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Alternatively build your own egg by checking out the repository and running
2828
`python setup.py bdist_egg` in your working copy. Copy the resultant .egg to
2929
your `plugins` directory.
3030

31-
Trac Code Comments plugin requres at least python 2.4 and runs on Trac 0.12.
31+
Trac Code Comments plugin requires at least Trac 1.2.
3232

3333
Enable all the modules through the admin web UI or by editing `trac.ini`.
3434

code_comments/comment.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def md5_hexdigest(s):
2121
VERSION = 1
2222

2323

24-
class Comment:
24+
class Comment(object):
2525
columns = [column.name for column in db.schema['code_comments'].columns]
2626

2727
required = 'text', 'author'
@@ -101,7 +101,7 @@ def link_text(self):
101101

102102
def changeset_link_text(self):
103103
if 0 != self.line:
104-
return 'Changeset @%d#L%d (in %s)' % (self.revision, self.line,
104+
return 'Changeset @%s#L%d (in %s)' % (self.revision, self.line,
105105
self.path)
106106
else:
107107
return 'Changeset @%s' % self.revision
@@ -142,25 +142,17 @@ def get_ticket_relations(self):
142142
VALUE LIKE '%%,%(comment_id)d' OR
143143
VALUE LIKE '%%,%(comment_id)d,%%')
144144
""" % {'comment_id': self.id}
145-
result = {}
146-
147-
@self.env.with_transaction()
148-
def get_ticket_ids(db):
149-
cursor = db.cursor()
150-
cursor.execute(query)
151-
result['tickets'] = cursor.fetchall()
152-
return set([int(row[0]) for row in result['tickets']])
145+
return set([int(row[0]) for row in self.env.db_query(query)])
153146

154147
def get_ticket_links(self):
155148
relations = self.get_ticket_relations()
156149
links = ['[[ticket:%s]]' % relation for relation in relations]
157150
return format_to_html(self.req, self.env, ', '.join(links))
158151

159152
def delete(self):
160-
@self.env.with_transaction()
161-
def delete_comment(db):
162-
cursor = db.cursor()
163-
cursor.execute("DELETE FROM code_comments WHERE id=%s", [self.id])
153+
self.env.db_transaction("""
154+
DELETE FROM code_comments WHERE id=%s
155+
""", (self.id,))
164156

165157

166158
class CommentJSONEncoder(json.JSONEncoder):

code_comments/comments.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from code_comments.comment import Comment
88

99

10-
class Comments:
10+
class Comments(object):
1111

1212
FILTER_MAX_PATH_DEPTH = 2
1313

@@ -39,14 +39,8 @@ def get_all_comment_authors(self, comments):
3939
return sorted(list(set([comment.author for comment in comments])))
4040

4141
def select(self, *query):
42-
result = {}
43-
44-
@self.env.with_transaction()
45-
def get_comments(db):
46-
cursor = db.cursor()
47-
cursor.execute(*query)
48-
result['comments'] = cursor.fetchall()
49-
return [self.comment_from_row(row) for row in result['comments']]
42+
return [self.comment_from_row(row)
43+
for row in self.env.db_query(*query)]
5044

5145
def count(self, args={}):
5246
conditions_str, values = \
@@ -57,11 +51,9 @@ def count(self, args={}):
5751
query = 'SELECT COUNT(*) FROM code_comments ' + where
5852
result = {}
5953

60-
@self.env.with_transaction()
61-
def get_comment_count(db):
62-
cursor = db.cursor()
63-
cursor.execute(query, values)
64-
result['count'] = cursor.fetchone()[0]
54+
for count, in self.env.db_query(query, values):
55+
result['count'] = count
56+
6557
return result['count']
6658

6759
def all(self):
@@ -131,14 +123,12 @@ def create(self, args):
131123
values = [getattr(comment, n) for n in column_names_to_insert]
132124
comment_id = [None]
133125

134-
@self.env.with_transaction()
135-
def insert_comment(db):
126+
with self.env.db_transaction as db:
136127
cursor = db.cursor()
137-
sql = """
128+
cursor.execute("""
138129
INSERT INTO code_comments (%s) VALUES(%s)
139130
""" % (', '.join(column_names_to_insert),
140-
', '.join(['%s'] * len(values)))
141-
cursor.execute(sql, values)
131+
', '.join(['%s'] * len(values))), values)
142132
comment_id[0] = db.get_last_id(cursor, 'code_comments')
143133

144134
CodeCommentSystem(self.env).comment_created(

code_comments/db.py

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# Database version identifier for upgrades.
99
db_version = 3
10+
db_version_key = 'code_comments_schema_version'
1011

1112
# Database schema
1213
schema = {
@@ -30,7 +31,6 @@
3031
Column('user'),
3132
Column('type'),
3233
Column('path'),
33-
Column('repos'),
3434
Column('rev'),
3535
Column('notify', type='bool'),
3636
Index(['user']),
@@ -39,48 +39,24 @@
3939
}
4040

4141

42-
def to_sql(env, table):
43-
""" Convenience function to get the to_sql for the active connector."""
44-
dc = DatabaseManager(env)._get_connector()[0]
45-
return dc.to_sql(table)
46-
47-
48-
def create_tables(env, db):
49-
cursor = db.cursor()
50-
for table_name in schema:
51-
for stmt in to_sql(env, schema[table_name]):
52-
cursor.execute(stmt)
53-
cursor.execute(
54-
"INSERT INTO system VALUES ('code_comments_schema_version', %s)",
55-
str(db_version))
56-
57-
5842
# Upgrades
5943

60-
def upgrade_from_1_to_2(env, db):
61-
# Add the new column "type"
62-
@env.with_transaction()
63-
def add_type_column(db):
64-
cursor = db.cursor()
65-
cursor.execute('ALTER TABLE code_comments ADD COLUMN type TEXT')
66-
67-
# Convert all the current comments to the new schema
68-
@env.with_transaction()
69-
def convert_comments(db):
70-
comments = {}
71-
cursor = db.cursor()
72-
cursor.execute('SELECT id, path FROM code_comments')
73-
comments = cursor.fetchall()
44+
def upgrade_from_1_to_2(env):
45+
with env.db_transaction as db:
46+
# Add the new column "type"
47+
db('ALTER TABLE code_comments ADD COLUMN type TEXT')
48+
# Convert all the current comments to the new schema
7449
# options:
7550
# 1: comment on file (path != "" && path != "attachment")
7651
# 2: comment on changeset (path == "")
7752
# 3: comment on attachment (path == "attachment")
78-
for comment in comments:
53+
for comment in db("""
54+
SELECT id, path FROM code_comments
55+
"""):
7956
path = comment[1]
8057
is_comment_to_attachment = path.startswith('attachment')
8158
is_comment_to_file = not is_comment_to_attachment and '' != path
8259
is_comment_to_changeset = '' == path
83-
cursor = db.cursor()
8460
update = 'UPDATE code_comments SET type={0} WHERE id={1}'
8561
sql = ''
8662

@@ -91,16 +67,13 @@ def convert_comments(db):
9167
elif is_comment_to_file:
9268
sql = update.format("'browser'", str(comment[0]))
9369

94-
cursor.execute(sql)
70+
db(sql)
9571

9672

97-
def upgrade_from_2_to_3(env, db):
73+
def upgrade_from_2_to_3(env):
9874
# Add the new table
99-
@env.with_transaction()
100-
def add_subscriptions_table(db):
101-
cursor = db.cursor()
102-
for stmt in to_sql(env, schema['code_comments_subscriptions']):
103-
cursor.execute(stmt)
75+
dbm = DatabaseManager(env)
76+
dbm.create_tables((schema['code_comments_subscriptions'],))
10477

10578

10679
upgrade_map = {
@@ -118,40 +91,25 @@ def environment_created(self):
11891
"""Called when a new Trac environment is created."""
11992
pass
12093

121-
def environment_needs_upgrade(self, db):
94+
def environment_needs_upgrade(self):
12295
"""
12396
Called when Trac checks whether the environment needs to be upgraded.
12497
Returns `True` if upgrade is needed, `False` otherwise.
12598
"""
126-
return self._get_version(db) != db_version
99+
dbm = DatabaseManager(self.env)
100+
return dbm.get_database_version(db_version_key) != db_version
127101

128-
def upgrade_environment(self, db):
102+
def upgrade_environment(self):
129103
"""
130104
Actually perform an environment upgrade, but don't commit as
131105
that is done by the common upgrade procedure when all plugins are done.
132106
"""
133-
current_ver = self._get_version(db)
107+
dbm = DatabaseManager(self.env)
108+
current_ver = dbm.get_database_version(db_version_key)
134109
if current_ver == 0:
135-
create_tables(self.env, db)
110+
dbm.create_tables(schema.values())
136111
else:
137112
while current_ver + 1 <= db_version:
138-
upgrade_map[current_ver + 1](self.env, db)
113+
upgrade_map[current_ver + 1](self.env)
139114
current_ver += 1
140-
cursor = db.cursor()
141-
cursor.execute("""
142-
UPDATE system SET value=%s
143-
WHERE name='code_comments_schema_version'
144-
""", str(db_version))
145-
146-
def _get_version(self, db):
147-
cursor = db.cursor()
148-
try:
149-
cursor.execute("""
150-
SELECT value FROM system
151-
WHERE name='code_comments_schema_version'
152-
""")
153-
for row in cursor:
154-
return int(row[0])
155-
return 0
156-
except:
157-
return 0
115+
dbm.set_database_version(db_version, db_version_key)

code_comments/htdocs/code-comments.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var underscore = _.noConflict();
2929
defaultFetchParams: {
3030
path: CodeComments.path || undefined,
3131
revision: CodeComments.revision,
32-
type: CodeComments.page
32+
type: CodeComments.page,
3333
},
3434
fetchPageComments: function() {
3535
return this.fetch( { data: _.extend( { line: 0 }, this.defaultFetchParams ) } );
@@ -213,7 +213,14 @@ var underscore = _.noConflict();
213213
},
214214
wait: true
215215
};
216-
this.collection.create({text: text, author: CodeComments.username, path: this.path, revision: CodeComments.revision, line: line, type: CodeComments.page}, options);
216+
this.collection.create({
217+
text: text,
218+
author: CodeComments.username,
219+
path: this.path,
220+
revision: CodeComments.revision,
221+
line: line,
222+
type: CodeComments.page
223+
}, options);
217224
},
218225
previewThrottled: $.throttle(1500, function(e) { return this.preview(e); }),
219226
preview: function(e) {
@@ -326,7 +333,9 @@ var underscore = _.noConflict();
326333
initialize: function(){
327334
_.bindAll(this, "render");
328335
this.model.listenTo(this.model, 'change', this.render);
329-
this.model.url = $(this.$el).data('baseUrl') + '/subscription' + $(this.$el).data('path');
336+
var rev = $(this.$el).data('rev');
337+
this.model.url = $(this.$el).data('baseUrl') + '/subscription' +
338+
$(this.$el).data('path') + (rev ? '?rev=' + rev : '');
330339
this.render();
331340
},
332341

@@ -341,7 +350,7 @@ var underscore = _.noConflict();
341350
label: 'Unsubscribe',
342351
icons: {primary: 'ui-icon-check'}
343352
};
344-
var title = 'You receive notifications for comments';
353+
var title = 'You receive notifications for comments';
345354
} else {
346355
var options = {
347356
disabled: false,
@@ -364,7 +373,7 @@ var underscore = _.noConflict();
364373

365374
window.subscription = new Subscription();
366375
window.subscriptionView = new SubscriptionView({model: subscription});
367-
if (subscriptionView.el) {
368-
subscription.fetch();
369-
}
376+
if (subscriptionView.el) {
377+
subscription.fetch();
378+
}
370379
}); }( jQuery.noConflict( true ) ) );

code_comments/htdocs/jquery-1.11.1.min.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)