Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better patch for #13, #14, #15 #24

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions code_comments/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def validate(self):

def href(self):
if self.is_comment_to_file:
href = self.req.href.browser(self.path, rev=self.revision, codecomment=self.id)
href = self.req.href.browser(self.repository, self.path, rev=self.revision, codecomment=self.id)
elif self.is_comment_to_changeset:
href = self.req.href.changeset(self.revision, codecomment=self.id)
href = self.req.href.changeset(self.revision, self.repository, codecomment=self.id)
elif self.is_comment_to_attachment:
href = self.req.href('/attachment/ticket/%d/%s' % (self.attachment_ticket, self.attachment_filename), codecomment=self.id)
if self.line:
Expand Down
29 changes: 25 additions & 4 deletions code_comments/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from trac.db.api import DatabaseManager

# Database version identifier for upgrades.
db_version = 1
db_version = 3

# Database schema
schema = {
Expand All @@ -13,10 +13,11 @@
Column('version', type='int'),
Column('text'),
Column('path'),
Column('revision', type='int'),
Column('revision'),
Column('line', type='int'),
Column('author'),
Column('time', type='int'),
Column('repository'),
Index(['path']),
Index(['author']),
],
Expand All @@ -36,10 +37,30 @@ def create_tables(env, db):
str(db_version))
# Upgrades
def upgrade_from_1_to_2(env, db):
pass
columns = [c.name for c in schema['code_comments'].columns]
cursor = db.cursor()
values = cursor.execute('PRAGMA INDEX_LIST(`code_comments`)')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not for mysql, could you please provide a solution for mysql?

Thanks

indexes = values.fetchall()
for index in indexes:
if index[1].startswith('code_comments'):
cursor.execute('DROP INDEX IF EXISTS %s' % index[1])
values = cursor.execute('SELECT %s FROM code_comments' % ','.join(columns))
lines = values.fetchall()
cursor.execute('ALTER TABLE code_comments RENAME TO tmp_code_comments')
for stmt in to_sql(env, schema['code_comments']):
cursor.execute(stmt)
for line in lines:
ins = 'INSERT INTO code_comments (%s) VALUES (%s)' % (','.join(columns), ','.join(['\'%s\'' % str(v).replace('\'','\'\'') for v in line]))
cursor.execute(ins)
cursor.execute('DROP TABLE tmp_code_comments')

def upgrade_from_2_to_3(env, db):
cursor = db.cursor()
cursor.execute('ALTER TABLE code_comments ADD COLUMN repository text')

upgrade_map = {
2: upgrade_from_1_to_2
2: upgrade_from_1_to_2,
3: upgrade_from_2_to_3,
}


Expand Down
2 changes: 1 addition & 1 deletion code_comments/htdocs/code-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ jQuery(function($) {
self.$el.dialog('close');
}
};
this.collection.create({text: text, author: CodeComments.username, path: CodeComments.path, revision: CodeComments.revision, line: line}, options);
this.collection.create({text: text, author: CodeComments.username, path: CodeComments.path, repository: CodeComments.repository, revision: CodeComments.revision, line: line}, options);
},
previewThrottled: $.throttle(1500, function(e) { return this.preview(e); }),
preview: function(e) {
Expand Down
22 changes: 13 additions & 9 deletions code_comments/web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import copy
from trac.core import *
from trac.perm import IPermissionRequestor
from trac.web.chrome import INavigationContributor, ITemplateProvider, add_script, add_script_data, add_stylesheet, add_notice, add_link
from trac.web.main import IRequestHandler, IRequestFilter
from trac.util import Markup
Expand All @@ -19,7 +20,7 @@ class CodeComments(Component):
implements(ITemplateProvider, IRequestFilter)

href = 'code-comments'

# ITemplateProvider methods
def get_templates_dirs(self):
return [self.get_template_dir()]
Expand All @@ -41,14 +42,17 @@ def post_process_request(self, req, template, data, content_type):
return template, data, content_type

class MainNavigation(CodeComments):
implements(INavigationContributor)
implements(INavigationContributor, IPermissionRequestor)

def get_permission_actions(self):
return ['CODE_COMMENTS_LIST']

# INavigationContributor methods
def get_active_navigation_item(self, req):
return self.href

def get_navigation_items(self, req):
if 'TRAC_ADMIN' in req.perm:
if 'CODE_COMMENTS_LIST' in req.perm:
yield 'mainnav', 'code-comments', Markup('<a href="%s">Code Comments</a>' % (
req.href(self.href) ) )

Expand All @@ -73,7 +77,7 @@ def post_process_request(self, req, template, data, content_type):
'templates': self.templates_js_data(),
'active_comment_id': req.args.get('codecomment'),
'username': req.authname,
'is_admin': 'TRAC_ADMIN' in req.perm,
'is_admin': 'CODE_COMMENTS_LIST' in req.perm,
}

original_return_value = template, data, content_type
Expand Down Expand Up @@ -105,10 +109,10 @@ def templates_js_data(self):
return data

def changeset_js_data(self, req, data):
return {'page': 'changeset', 'revision': data['new_rev'], 'path': '', 'selectorToInsertBefore': 'div.diff:first'}
return {'page': 'changeset', 'repository': data['reponame'], 'revision': data['new_rev'], 'path': '', 'selectorToInsertBefore': 'div.diff:first'}

def browser_js_data(self, req, data):
return {'page': 'browser', 'revision': data['rev'], 'path': data['path'], 'selectorToInsertBefore': 'table#info'}
return {'page': 'browser', 'repository': data['reponame'], 'revision': data['rev'], 'path': data['path'], 'selectorToInsertBefore': 'table#info'}

def attachment_js_data(self, req, data):
path = req.path_info.replace('/attachment/', 'attachment:/')
Expand All @@ -130,7 +134,7 @@ def match_request(self, req):
return req.path_info == '/' + self.href

def process_request(self, req):
req.perm.require('TRAC_ADMIN')
req.perm.require('CODE_COMMENTS_LIST')

self.data = {}
self.args = {}
Expand All @@ -146,7 +150,7 @@ def process_request(self, req):
self.comments = Comments(req, self.env);
self.data['comments'] = self.comments.search(self.args, self.order, self.per_page, self.page, self.order_by)
self.data['reponame'], repos, path = RepositoryManager(self.env).get_repository_by_path('/')
self.data['can_delete'] = 'TRAC_ADMIN' in req.perm
self.data['can_delete'] = 'CODE_COMMENTS_LIST' in req.perm
self.data['paginator'] = self.get_paginator()
self.data['current_sorting_method'] = self.order_by
self.data['current_order'] = self.order
Expand Down Expand Up @@ -219,7 +223,7 @@ def match_request(self, req):
return req.path_info == '/' + self.href

def process_request(self, req):
req.perm.require('TRAC_ADMIN')
req.perm.require('CODE_COMMENTS_LIST')
if 'GET' == req.method:
return self.form(req)
else:
Expand Down