| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | """ |
|---|
| 3 | = Watchlist Plugin for Trac = |
|---|
| 4 | Plugin Website: http://trac-hacks.org/wiki/WatchlistPlugin |
|---|
| 5 | Trac website: http://trac.edgewall.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2008-2010 by Martin Scharrer <martin@scharrer-online.de> |
|---|
| 8 | All rights reserved. |
|---|
| 9 | |
|---|
| 10 | The i18n support was added by Steffen Hoffmann <hoff.st@web.de>. |
|---|
| 11 | |
|---|
| 12 | This program is free software: you can redistribute it and/or modify |
|---|
| 13 | it under the terms of the GNU General Public License as published by |
|---|
| 14 | the Free Software Foundation, either version 3 of the License, or |
|---|
| 15 | (at your option) any later version. |
|---|
| 16 | |
|---|
| 17 | This program is distributed in the hope that it will be useful, |
|---|
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | GNU General Public License for more details. |
|---|
| 21 | |
|---|
| 22 | For a copy of the GNU General Public License see |
|---|
| 23 | <http://www.gnu.org/licenses/>. |
|---|
| 24 | |
|---|
| 25 | $Id: api.py 15264 2016-02-11 04:22:34Z rjollos $ |
|---|
| 26 | """ |
|---|
| 27 | |
|---|
| 28 | __url__ = ur"$URL: //trac-hacks.org/svn/watchlistplugin/0.12/tracwatchlist/api.py $"[6:-2] |
|---|
| 29 | __author__ = ur"$Author: rjollos $"[9:-2] |
|---|
| 30 | __revision__ = int("0" + ur"$Rev: 15264 $"[6:-2].strip('M')) |
|---|
| 31 | __date__ = ur"$Date: 2016-02-11 04:22:34 +0000 (Thu, 11 Feb 2016) $"[7:-2] |
|---|
| 32 | |
|---|
| 33 | import copy |
|---|
| 34 | from trac.core import * |
|---|
| 35 | from tracwatchlist.translation import gettext |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | class IWatchlistProvider(Interface): |
|---|
| 39 | """Interface for watchlist providers.""" |
|---|
| 40 | def get_realms(): |
|---|
| 41 | """ Must return list or tuple of realms provided. """ |
|---|
| 42 | pass |
|---|
| 43 | |
|---|
| 44 | def get_realm_label(realm, n_plural=1, astitle=False): |
|---|
| 45 | pass |
|---|
| 46 | |
|---|
| 47 | def resources_exists(realm, resids): |
|---|
| 48 | """ Returns all existing resources described by `realm` and `resids`. |
|---|
| 49 | If `resids` is a list return all listed resources which exist. |
|---|
| 50 | If `resids` is a string, take it as a pattern and |
|---|
| 51 | list all resources which match it. |
|---|
| 52 | """ |
|---|
| 53 | pass |
|---|
| 54 | |
|---|
| 55 | def watched_resources(self, realm, resids, user, wl, fuzzy=0): |
|---|
| 56 | pass |
|---|
| 57 | |
|---|
| 58 | def unwatched_resources(self, realm, resids, user, wl, fuzzy=0): |
|---|
| 59 | pass |
|---|
| 60 | |
|---|
| 61 | #def res_list_exists(realm, reslist): |
|---|
| 62 | # pass |
|---|
| 63 | |
|---|
| 64 | #def res_pattern_exists(realm, pattern): |
|---|
| 65 | # pass |
|---|
| 66 | |
|---|
| 67 | def has_perm(realm, perm): |
|---|
| 68 | pass |
|---|
| 69 | |
|---|
| 70 | def get_list(realm, wl, req, fields=None): |
|---|
| 71 | """Returns list of watched elements as dictionaries plus an extra dictionary of extra |
|---|
| 72 | template data, which will be available in the template under the name "<realm>data" |
|---|
| 73 | Example: |
|---|
| 74 | data = [ {'name':'example', 'changetime': <DT Object> }, { ... } ] |
|---|
| 75 | extradict = { 'somethingspecial':42, ... } |
|---|
| 76 | return data, extradict |
|---|
| 77 | """ |
|---|
| 78 | pass |
|---|
| 79 | |
|---|
| 80 | def get_href(realm, resid=None): |
|---|
| 81 | pass |
|---|
| 82 | |
|---|
| 83 | def get_abs_href(realm, resid=None): |
|---|
| 84 | pass |
|---|
| 85 | |
|---|
| 86 | def get_fields(realm): |
|---|
| 87 | """ Returns fields (table columns) |
|---|
| 88 | Format: ( {Field:Label, Field:Label, ...}, (DEFAULT list) ) |
|---|
| 89 | """ |
|---|
| 90 | pass |
|---|
| 91 | |
|---|
| 92 | def get_sort_key(realm): |
|---|
| 93 | """Returns a sort `key` function for the argument of the same name of |
|---|
| 94 | `sorted` or `list.sort`. By default this can be `None` for normal |
|---|
| 95 | sorting. |
|---|
| 96 | Providers with numeric resource ids should return `int` or a similar |
|---|
| 97 | function to enable numeric sorting. |
|---|
| 98 | """ |
|---|
| 99 | pass |
|---|
| 100 | |
|---|
| 101 | def get_sort_cmp(realm): |
|---|
| 102 | """Returns a sort `cmp` function for the argument of the same name of |
|---|
| 103 | `sorted` or `list.sort`. By default this can be `None` for normal |
|---|
| 104 | sorting. """ |
|---|
| 105 | pass |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | class BasicWatchlist(Component): |
|---|
| 109 | """Base class for watchlist providers. |
|---|
| 110 | This class provides default implementations of all interface methods. |
|---|
| 111 | Watchlist provider can inherit from it to simply their implementation. |
|---|
| 112 | """ |
|---|
| 113 | implements( IWatchlistProvider ) |
|---|
| 114 | realms = [] |
|---|
| 115 | default_fields = {} |
|---|
| 116 | fields = {} |
|---|
| 117 | sort_key = {} |
|---|
| 118 | sort_cmp = {} |
|---|
| 119 | |
|---|
| 120 | def get_sort_key(self, realm): |
|---|
| 121 | return self.sort_key.get(realm, None) |
|---|
| 122 | |
|---|
| 123 | def get_sort_cmp(self, realm): |
|---|
| 124 | return self.sort_cmp.get(realm, None) |
|---|
| 125 | |
|---|
| 126 | def get_realms(self): |
|---|
| 127 | return self.realms |
|---|
| 128 | |
|---|
| 129 | def get_realm_label(self, realm, n_plural=1, astitle=False): |
|---|
| 130 | if astitle: |
|---|
| 131 | r = realm.capitalize() |
|---|
| 132 | else: |
|---|
| 133 | r = realm |
|---|
| 134 | if n_plural == 1: |
|---|
| 135 | return r |
|---|
| 136 | else: |
|---|
| 137 | return r + 's' |
|---|
| 138 | |
|---|
| 139 | def resources_exists(self, realm, resids): |
|---|
| 140 | if isinstance(resids,basestring): |
|---|
| 141 | return False |
|---|
| 142 | else: |
|---|
| 143 | return [] |
|---|
| 144 | |
|---|
| 145 | def watched_resources(self, realm, resids, user, wl, fuzzy=0): |
|---|
| 146 | return [] |
|---|
| 147 | |
|---|
| 148 | def unwatched_resources(self, realm, resids, user, wl, fuzzy=0): |
|---|
| 149 | return [] |
|---|
| 150 | |
|---|
| 151 | def has_perm(self, realm, perm): |
|---|
| 152 | if realm not in self.realms: |
|---|
| 153 | return False |
|---|
| 154 | return realm.upper() + '_VIEW' in perm |
|---|
| 155 | |
|---|
| 156 | def get_list(self, realm, wl, req, fields=None): |
|---|
| 157 | return [], {} |
|---|
| 158 | |
|---|
| 159 | def get_href(self, realm, resid=None, **kwargs): |
|---|
| 160 | if resid is None: |
|---|
| 161 | return self.env.href.__get_attr__(realm) |
|---|
| 162 | else: |
|---|
| 163 | return self.env.href(realm,resid,**kwargs) |
|---|
| 164 | |
|---|
| 165 | def get_abs_href(self, realm, resid=None, **kwargs): |
|---|
| 166 | if resid is None: |
|---|
| 167 | return self.env.abs_href.__get_attr__(realm) |
|---|
| 168 | else: |
|---|
| 169 | return self.env.abs_href(realm,resid,**kwargs) |
|---|
| 170 | |
|---|
| 171 | def get_fields(self, realm): |
|---|
| 172 | # Needed to re-localise after locale changed: |
|---|
| 173 | # See also ticket.api: get_ticket_fields |
|---|
| 174 | fields = copy.deepcopy(self.fields.get(realm,{})) |
|---|
| 175 | col = 'col' # workaround gettext extraction bug |
|---|
| 176 | for col in fields: |
|---|
| 177 | fields[col] = gettext(fields[col]) |
|---|
| 178 | return ( fields, self.default_fields.get(realm,[]) ) |
|---|
| 179 | |
|---|
| 180 | # EOF |
|---|