Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jun66j5/trac-tagsplugin
...
head fork: jun66j5/trac-tagsplugin
compare: ticket11147
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
2  tractags/tests/api.py
@@ -34,13 +34,13 @@ def setUp(self):
self.req = Mock()
self.actions = ['TAGS_ADMIN', 'TAGS_MODIFY', 'TAGS_VIEW']
- self.tag_s = tractags.api.TagSystem(self.env)
self.db = self.env.get_db_cnx()
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
# Revert these changes for getting default permissions inserted.
self._revert_tractags_schema_init()
setup.upgrade_environment(self.db)
+ self.tag_s = tractags.api.TagSystem(self.env)
def tearDown(self):
self.db.close()
View
7 tractags/tests/web_ui.py
@@ -27,10 +27,6 @@ def setUp(self):
self.env = EnvironmentStub(
enable=['trac.*', 'tractags.*'])
self.env.path = tempfile.mkdtemp()
-
- self.tag_s = TagSystem(self.env)
- self.tag_rh = TagRequestHandler(self.env)
-
self.db = self.env.get_db_cnx()
setup = TagSetup(self.env)
# Current tractags schema is setup with enabled component anyway.
@@ -38,6 +34,9 @@ def setUp(self):
self._revert_tractags_schema_init()
setup.upgrade_environment(self.db)
+ self.tag_s = TagSystem(self.env)
+ self.tag_rh = TagRequestHandler(self.env)
+
perms = PermissionSystem(self.env)
# Revoke default permissions, because more diversity is required here.
perms.revoke_permission('anonymous', 'TAGS_VIEW')
View
13 tractags/tests/wiki.py
@@ -13,8 +13,10 @@
try:
from babel import Locale
+ locale_en = Locale.parse('en_US')
except ImportError:
Locale = None
+ locale_en = None
from datetime import datetime
@@ -113,11 +115,6 @@ def setUp(self):
self.env = EnvironmentStub(default_data=True,
enable=['trac.*', 'tractags.*'])
self.env.path = tempfile.mkdtemp()
- self.perms = PermissionSystem(self.env)
-
- self.tag_s = TagSystem(self.env)
- self.tag_wp = WikiTagProvider(self.env)
-
self.db = self.env.get_db_cnx()
setup = TagSetup(self.env)
# Current tractags schema is partially setup with enabled component.
@@ -125,6 +122,10 @@ def setUp(self):
self._revert_tractags_schema_init()
setup.upgrade_environment(self.db)
+ self.perms = PermissionSystem(self.env)
+ self.tag_s = TagSystem(self.env)
+ self.tag_wp = WikiTagProvider(self.env)
+
cursor = self.db.cursor()
# Populate table with initial test data.
cursor.execute("""
@@ -212,7 +213,7 @@ def wiki_setup(tc):
req = Mock(href=Href('/'), abs_href=Href('http://www.example.com/'),
authname='anonymous', perm=MockPerm(), tz=utc, args={},
- locale=Locale.parse('en_US') if Locale else None)
+ locale=locale_en)
tc.env.href = req.href
tc.env.abs_href = req.abs_href
tc.context = Context.from_request(req)
View
41 tractags/ticket.py
@@ -21,7 +21,7 @@
from trac.util.text import to_unicode
from tractags.api import DefaultTagProvider, ITagProvider, _
-from tractags.util import split_into_tags
+from tractags.util import split_into_tags, get_db_exc
class TicketTagProvider(DefaultTagProvider):
@@ -50,7 +50,16 @@ class TicketTagProvider(DefaultTagProvider):
use_cache = False
def __init__(self):
- self._fetch_tkt_tags()
+ db = self.env.get_db_cnx()
+ try:
+ self._fetch_tkt_tags(db)
+ db.commit()
+ except get_db_exc(self.env).IntegrityError, e:
+ self.log.warn('tags for ticket already exist: %s', to_unicode(e))
+ db.rollback()
+ except:
+ db.rollback()
+ raise
cfg = self.config
cfg_key = 'permission_policies'
default_policies = cfg.defaults().get('trac', {}).get(cfg_key)
@@ -188,31 +197,40 @@ def ticket_deleted(self, ticket):
# Private methods
- def _fetch_tkt_tags(self):
+ def _fetch_tkt_tags(self, db):
"""Transfer all relevant ticket attributes to tags db table."""
# Initial sync is done by forced, stupid one-way mirroring.
# Data aquisition for this utilizes the known ticket tags query.
- db = self.env.get_db_cnx()
fields = ["COALESCE(%s, '')" % f for f in self.fields]
ignore = ''
if self.ignore_closed_tickets:
- ignore = " WHERE status != 'closed'"
+ ignore = " AND status != 'closed'"
sql = """
SELECT *
FROM (SELECT id, %s, %s AS std_fields
- FROM ticket%s) s
- WHERE std_fields != '' ORDER BY id
- """ % (','.join(self.fields), db.concat(*fields), ignore)
+ FROM ticket AS tkt
+ WHERE NOT EXISTS (SELECT * FROM tags
+ WHERE tagspace=%%s AND name=%s)
+ %s) AS s
+ WHERE std_fields != ''
+ ORDER BY id
+ """ % (','.join(self.fields), db.concat(*fields),
+ db.cast('tkt.id', 'text'), ignore)
self.env.log.debug(sql)
# Obtain cursors for reading tickets and altering tags db table.
# DEVEL: Use appropriate cursor typs from Trac 1.0 db API.
ro_cursor = db.cursor()
rw_cursor = db.cursor()
- # Delete all previous entries for 'ticket' tagspace.
- rw_cursor.execute('DELETE FROM tags WHERE tagspace=%s', (self.realm,))
+ # Delete tags for non-existent ticket
+ rw_cursor.execute("""
+ DELETE FROM tags
+ WHERE tagspace=%%s
+ AND NOT EXISTS (SELECT * FROM ticket AS tkt WHERE %s=tags.name)
+ """ % db.cast('tkt.id', 'text'),
+ (self.realm,))
self.log.debug('ENTER_TAG_DB_CHECKOUT')
- ro_cursor.execute(sql)
+ ro_cursor.execute(sql, (self.realm,))
self.log.debug('EXIT_TAG_DB_CHECKOUT')
self.log.debug('ENTER_TAG_SYNC')
@@ -224,7 +242,6 @@ def _fetch_tkt_tags(self):
(tagspace, name, tag)
VALUES (%s, %s, %s)
""", [(self.realm, str(tkt_id), tag) for tag in ticket_tags])
- db.commit()
self.log.debug('EXIT_TAG_SYNC')
try:
View
14 tractags/util.py
@@ -17,3 +17,17 @@ def split_into_tags(text):
return set([tag.strip() for tag in _TAG_SPLIT.split(text)
if tag.strip()])
+
+def get_db_exc(env):
+ if hasattr(env, 'db_exc'):
+ return env.db_exc
+ database = env.config.get('trac', 'database')
+ if database.startswith('sqlite:'):
+ from trac.db.sqlite_backend import sqlite
+ return sqlite
+ if database.startswith('postgres:'):
+ from trac.db.postgres_backend import psycopg
+ return psycopg
+ if database.startswith('mysql:'):
+ from trac.db.mysql_backend import MySQLdb
+ return MySQLdb

No commit comments for this range

Something went wrong with that request. Please try again.