84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
from trac.core import *
|
|
from tractags.api import TagSystem, ITagProvider
|
|
from trac.util.text import to_unicode
|
|
from trac.util.compat import set, sorted
|
|
from trac.config import *
|
|
from trac.resource import Resource
|
|
|
|
from model import RendezVous
|
|
|
|
__all__ = ['RendezVousTagProvider',]
|
|
|
|
class RendezVousTagProvider(Component):
|
|
"""A tag provider using rendezvous tag field as sources of tags.
|
|
"""
|
|
implements(ITagProvider)
|
|
|
|
ignore_closed_rendezvous = BoolOption('tags', 'ignore_closed_rendezvous', True,
|
|
'Do not collect tags from closed rendezvous.')
|
|
|
|
# ITagProvider methods
|
|
def get_taggable_realm(self):
|
|
return 'rendezvous'
|
|
|
|
def get_tagged_resources(self, req, tags):
|
|
if 'RENDEZVOUS_VIEW' not in req.perm:
|
|
return
|
|
|
|
split_into_tags = TagSystem(self.env).split_into_tags
|
|
db = self.env.get_db_cnx()
|
|
cursor = db.cursor()
|
|
args = []
|
|
ignore = ''
|
|
if self.ignore_closed_rendezvous:
|
|
ignore = " WHERE status != 'closed'"
|
|
sql = "SELECT * FROM (SELECT rendezvous_id, tags, COALESCE(tags, '') as fields FROM rendezvous%s)" % ignore
|
|
constraints = []
|
|
if tags:
|
|
constraints.append(
|
|
"(" + ' OR '.join(["fields LIKE %s" for t in tags]) + ")")
|
|
args += ['%' + t + '%' for t in tags]
|
|
else:
|
|
constraints.append("fields != ''")
|
|
|
|
if constraints:
|
|
sql += " WHERE " + " AND ".join(constraints)
|
|
sql += " ORDER BY rendezvous_id"
|
|
self.env.log.debug(sql)
|
|
cursor.execute(sql, args)
|
|
for row in cursor:
|
|
id, ttags = row[0], ' '.join([f for f in row[1:-1] if f])
|
|
rendezvous_tags = split_into_tags(ttags)
|
|
tags = set([to_unicode(x) for x in tags])
|
|
if (not tags or rendezvous_tags.intersection(tags)):
|
|
yield Resource('rendezvous', id), rendezvous_tags
|
|
|
|
def get_resource_tags(self, req, resource):
|
|
if 'RENDEZVOUS_VIEW' not in req.perm(resource):
|
|
return
|
|
rendezvous = RendezVous.fetch_one(self.env, resource.id)
|
|
tags = self._rendezvous_tags(rendezvous)
|
|
return tags
|
|
|
|
def set_resource_tags(self, req, resource, tags):
|
|
#req.perm.require('RENDEZVOUS_MODIFY', resource)
|
|
split_into_tags = TagSystem(self.env).split_into_tags
|
|
rendezvous = RendezVous.fetch_one(self.env, resource.id)
|
|
all = self._rendezvous_tags(rendezvous)
|
|
tags.difference_update(all.difference(rendezvous.tags))
|
|
rendezvous.tags = u' '.join(sorted(map(to_unicode, tags)))
|
|
rendezvous.update()
|
|
|
|
def remove_resource_tags(self, req, resource):
|
|
req.perm.require('RENDEZVOUS_MODIFY', resource)
|
|
rendezvous = RendezVous.fetch_one(self.env, resource.id)
|
|
rendezvous.tags = None
|
|
rendezvous.update()
|
|
|
|
# Private methods
|
|
def _rendezvous_tags(self, rendezvous):
|
|
return TagSystem(self.env).split_into_tags(rendezvous.tags)
|