"""Implements the API used in the HTML templating for the web interface.
"""
todo = '''
- Most methods should have a "default" arg to supply a value
when none appears in the hyperdb or request.
- Multilink property additions: change_note and new_upload
- Add class.find() too
- NumberHTMLProperty should support numeric operations
- LinkHTMLProperty should handle comparisons to strings (cf. linked name)
- HTMLRequest.default(self, sort, group, filter, columns, **filterspec):
"""Set the request's view arguments to the given values when no
values are found in the CGI environment.
"""
- have menu() methods accept filtering arguments
'''
__docformat__ = 'restructuredtext'
from __future__ import nested_scopes
import sys, cgi, urllib, os, re, os.path, time, errno, mimetypes, csv
from roundup import hyperdb, date, support
from roundup import i18n
from roundup.i18n import _
try:
import cPickle as pickle
except ImportError:
import pickle
try:
import cStringIO as StringIO
except ImportError:
import StringIO
try:
import StructuredText
except ImportError:
StructuredText = None
# bring in the templating support
from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService
from roundup.cgi.PageTemplates.Expressions import getEngine
from roundup.cgi.TAL import TALInterpreter
from roundup.cgi import TranslationService, ZTUtils
### i18n services
# this global translation service is not thread-safe.
# it is left here for backward compatibility
# until all Web UI translations are done via client.translator object
translationService = TranslationService.get_translation()
GlobalTranslationService.setGlobalTranslationService(translationService)
### templating
class NoTemplate(Exception):
pass
class Unauthorised(Exception):
def __init__(self, action, klass, translator=None):
self.action = action
self.klass = klass
if translator:
self._ = translator.gettext
else:
self._ = TranslationService.get_translation().gettext
def __str__(self):
return self._('You are not allowed to %(action)s '
'items of class %(class)s') % {
'action': self.action, 'class': self.klass}
def find_template(dir, name, view):
''' Find a template in the nominated dir
'''
# find the source
if view:
filename = '%s.%s'%(name, view)
else:
filename = name
# try old-style
src = os.path.join(dir, filename)
if os.path.exists(src):
return (src, filename)
# try with a .html or .xml extension (new-style)
for extension in '.html', '.xml':
filename = filename + extension
src = os.path.join(dir, filename)
if os.path.exists(src):
return (src, filename)
# no view == no generic template is possible
if not view:
raise NoTemplate, 'Template file "%s" doesn\'t exist'%name
# try for a _generic template
generic = '_generic.%s'%view
src = os.path.join(dir, generic)
if os.path.exists(src):
return (src, generic)
# finally, try _generic.html
generic = generic + '.html'
src = os.path.join(dir, generic)
if os.path.exists(src):
return (src, generic)
raise NoTemplate, 'No template file exists for templating "%s" '\
'with template "%s" (neither "%s" nor "%s")'%(name, view,
filename, generic)
class Templates:
templates = {}
def __init__(self, dir):
self.dir = dir
def precompileTemplates(self):
''' Go through a directory and precompile all the templates therein
'''
for filename in os.listdir(self.dir):
# skip subdirs
if os.path.isdir(filename):
continue
# skip files without ".html" or ".xml" extension - .css, .js etc.
for extension in '.html', '.xml':
if filename.endswith(extension):
break
else:
continue
# remove extension
filename = filename[:-len(extension)]
# load the template
if '.' in filename:
name, extension = filename.split('.', 1)
self.get(name, extension)
else:
self.get(filename, None)
def get(self, name, extension=None):
''' Interface to get a template, possibly loading a compiled template.
"name" and "extension" indicate the template we're after, which in
most cases will be "name.extension". If "extension" is None, then
we look for a template just called "name" with no extension.
If the file "name.extension" doesn't exist, we look for
"_generic.extension" as a fallback.
'''
# default the name to "home"
if name is None:
name = 'home'
elif extension is None and '.' in name:
# split name
name, extension = name.split('.')
# find the source
src, filename = find_template(self.dir, name, extension)
# has it changed?
try:
stime = os.stat(src)[os.path.stat.ST_MTIME]
except os.error, error:
if error.errno != errno.ENOENT:
raise
if self.templates.has_key(src) and \
stime <= self.templates[src].mtime:
# compiled template is up to date
return self.templates[src]
# compile the template
self.templates[src] = pt = RoundupPageTemplate()
# use pt_edit so we can pass the content_type guess too
content_type = mimetypes.guess_type(filename)[0] or 'text/html'
pt.pt_edit(open(src).read(), content_type)
pt.id = filename
pt.mtime = stime
return pt
def __getitem__(self, name):
name, extension = os.path.splitext(name)
if extension:
extension = extension[1:]
try:
return self.get(name, extension)
except NoTemplate, message:
raise KeyError, message
def context(client, template=None, classname=None, request=None):
"""Return the rendering context dictionary
The dictionary includes following symbols:
*context*
this is one of three things:
1. None - we're viewing a "home" page
2. The current class of item being displayed. This is an HTMLClass
instance.
3. The current item from the database, if we're viewing a specific
item, as an HTMLItem instance.
*request*
Includes information about the current request, including:
- the url
- the current index information (``filterspec``, ``filter`` args,
``properties``, etc) parsed out of the form.
- methods for easy filterspec link generation
- *user*, the current user node as an HTMLItem instance
- *form*, the current CGI form information as a FieldStorage
*config*
The current tracker config.
*db*
The current database, used to access arbitrary database items.
*utils*
This is a special class that has its base in the TemplatingUtils
class in this file. If the tracker interfaces module defines a
TemplatingUtils class then it is mixed in, overriding the methods
in the base class.
*templates*
Access to all the tracker templates by name.
Used mainly in *use-macro* commands.
*template*
Current rendering template.
*true*
Logical True value.
*false*
Logical False value.
*i18n*
Internationalization service, providing string translation
methods ``gettext`` and ``ngettext``.
"""
# construct the TemplatingUtils class
utils = TemplatingUtils
if (hasattr(client.instance, 'interfaces') and
hasattr(client.instance.interfaces, 'TemplatingUtils')):
class utils(client.instance.interfaces.TemplatingUtils, utils):
pass
# if template, classname and/or request are not passed explicitely,
# compute form client
if template is None:
template = client.template
if classname is None:
classname = client.classname
if request is None:
request = HTMLRequest(client)
c = {
'context': None,
'options': {},
'nothing': None,
'request': request,
'db': HTMLDatabase(client),
'config': client.instance.config,
'tracker': client.instance,
'utils': utils(client),
'templates': client.instance.templates,
'template': template,
'true': 1,
'false': 0,
'i18n': client.translator
}
# add in the item if there is one
if client.nodeid:
c['context'] = HTMLItem(client, classname, client.nodeid,
anonymous=1)
elif client.db.classes.has_key(classname):
c['context'] = HTMLClass(client, classname, anonymous=1)
return c
class RoundupPageTemplate(PageTemplate.PageTemplate):
'''A Roundup-specific PageTemplate.
Interrogate the client to set up Roundup-specific template variables
to be available. See 'context' function for the list of variables.
'''
# 06-jun-2004 [als] i am not sure if this method is used yet
def getContext(self, client, classname, request):
return context(client, self, classname, request)
def render(self, client, classname, request, **options):
"""Render this Page Template"""
if not self._v_cooked:
self._cook()
__traceback_supplement__ = (PageTemplate.PageTemplateTracebackSupplement, self)
if self._v_errors:
raise PageTemplate.PTRuntimeError, \
'Page Template %s has errors.'%self.id
# figure the context
c = context(client, self, classname, request)
c.update({'options': options})
# and go
output = StringIO.StringIO()
TALInterpreter.TALInterpreter(self._v_program, self.macros,
getEngine().getContext(c), output, tal=1, strictinsert=0)()
return output.getvalue()
def __repr__(self):
return ''%self.id
class HTMLDatabase:
''' Return HTMLClasses for valid class fetches
'''
def __init__(self, client):
self._client = client
self._ = client._
self._db = client.db
# we want config to be exposed
self.config = client.db.config
def __getitem__(self, item, desre=re.compile(r'(?P\w+)(?P[-\d]+)')):
# check to see if we're actually accessing an item
m = desre.match(item)
if m:
cl = m.group('cl')
self._client.db.getclass(cl)
return HTMLItem(self._client, cl, m.group('id'))
else:
self._client.db.getclass(item)
return HTMLClass(self._client, item)
def __getattr__(self, attr):
try:
return self[attr]
except KeyError:
raise AttributeError, attr
def classes(self):
l = self._client.db.classes.keys()
l.sort()
m = []
for item in l:
m.append(HTMLClass(self._client, item))
return m
def lookupIds(db, prop, ids, fail_ok=0, num_re=re.compile('^-?\d+$')):
''' "fail_ok" should be specified if we wish to pass through bad values
(most likely form values that we wish to represent back to the user)
'''
cl = db.getclass(prop.classname)
l = []
for entry in ids:
if num_re.match(entry):
l.append(entry)
else:
try:
l.append(cl.lookup(entry))
except (TypeError, KeyError):
if fail_ok:
# pass through the bad value
l.append(entry)
return l
def lookupKeys(linkcl, key, ids, num_re=re.compile('^-?\d+$')):
''' Look up the "key" values for "ids" list - though some may already
be key values, not ids.
'''
l = []
for entry in ids:
if num_re.match(entry):
l.append(linkcl.get(entry, key))
else:
l.append(entry)
return l
def input_html4(**attrs):
"""Generate an 'input' (html4) element with given attributes"""
return ''%' '.join(['%s="%s"'%item for item in attrs.items()])
def input_xhtml(**attrs):
"""Generate an 'input' (xhtml) element with given attributes"""
return ''%' '.join(['%s="%s"'%item for item in attrs.items()])
class HTMLInputMixin:
''' requires a _client property '''
def __init__(self):
html_version = 'html4'
if hasattr(self._client.instance.config, 'HTML_VERSION'):
html_version = self._client.instance.config.HTML_VERSION
if html_version == 'xhtml':
self.input = input_xhtml
else:
self.input = input_html4
# self._context is used for translations.
# will be initialized by the first call to .gettext()
self._context = None
def gettext(self, msgid):
"""Return the localized translation of msgid"""
if self._context is None:
self._context = context(self._client)
return self._client.translator.translate(domain="roundup",
msgid=msgid, context=self._context)
_ = gettext
class HTMLPermissions:
def view_check(self):
''' Raise the Unauthorised exception if the user's not permitted to
view this class.
'''
if not self.is_view_ok():
raise Unauthorised("view", self._classname,
translator=self._client.translator)
def edit_check(self):
''' Raise the Unauthorised exception if the user's not permitted to
edit items of this class.
'''
if not self.is_edit_ok():
raise Unauthorised("edit", self._classname,
translator=self._client.translator)
class HTMLClass(HTMLInputMixin, HTMLPermissions):
''' Accesses through a class (either through *class* or *db.*)
'''
def __init__(self, client, classname, anonymous=0):
self._client = client
self._ = client._
self._db = client.db
self._anonymous = anonymous
# we want classname to be exposed, but _classname gives a
# consistent API for extending Class/Item
self._classname = self.classname = classname
self._klass = self._db.getclass(self.classname)
self._props = self._klass.getprops()
HTMLInputMixin.__init__(self)
def is_edit_ok(self):
''' Is the user allowed to Create the current class?
'''
return self._db.security.hasPermission('Create', self._client.userid,
self._classname)
def is_view_ok(self):
''' Is the user allowed to View the current class?
'''
return self._db.security.hasPermission('View', self._client.userid,
self._classname)
def is_only_view_ok(self):
''' Is the user only allowed to View (ie. not Create) the current class?
'''
return self.is_view_ok() and not self.is_edit_ok()
def __repr__(self):
return ''%(id(self), self.classname)
def __getitem__(self, item):
''' return an HTMLProperty instance
'''
#print 'HTMLClass.getitem', (self, item)
# we don't exist
if item == 'id':
return None
# get the property
try:
prop = self._props[item]
except KeyError:
raise KeyError, 'No such property "%s" on %s'%(item, self.classname)
# look up the correct HTMLProperty class
form = self._client.form
for klass, htmlklass in propclasses:
if not isinstance(prop, klass):
continue
if form.has_key(item):
if isinstance(prop, hyperdb.Multilink):
value = lookupIds(self._db, prop,
handleListCGIValue(form[item]), fail_ok=1)
elif isinstance(prop, hyperdb.Link):
value = form[item].value.strip()
if value:
value = lookupIds(self._db, prop, [value],
fail_ok=1)[0]
else:
value = None
else:
value = form[item].value.strip() or None
else:
if isinstance(prop, hyperdb.Multilink):
value = []
else:
value = None
return htmlklass(self._client, self._classname, '', prop, item,
value, self._anonymous)
# no good
raise KeyError, item
def __getattr__(self, attr):
''' convenience access '''
try:
return self[attr]
except KeyError:
raise AttributeError, attr
def designator(self):
''' Return this class' designator (classname) '''
return self._classname
def getItem(self, itemid, num_re=re.compile('^-?\d+$')):
''' Get an item of this class by its item id.
'''
# make sure we're looking at an itemid
if not isinstance(itemid, type(1)) and not num_re.match(itemid):
itemid = self._klass.lookup(itemid)
return HTMLItem(self._client, self.classname, itemid)
def properties(self, sort=1):
''' Return HTMLProperty for all of this class' properties.
'''
l = []
for name, prop in self._props.items():
for klass, htmlklass in propclasses:
if isinstance(prop, hyperdb.Multilink):
value = []
else:
value = None
if isinstance(prop, klass):
l.append(htmlklass(self._client, self._classname, '',
prop, name, value, self._anonymous))
if sort:
l.sort(lambda a,b:cmp(a._name, b._name))
return l
def list(self, sort_on=None):
''' List all items in this class.
'''
# get the list and sort it nicely
l = self._klass.list()
sortfunc = make_sort_function(self._db, self._classname, sort_on)
l.sort(sortfunc)
# check perms
check = self._client.db.security.hasPermission
userid = self._client.userid
l = [HTMLItem(self._client, self._classname, id) for id in l
if check('View', userid, self._classname, itemid=id)]
return l
def csv(self):
''' Return the items of this class as a chunk of CSV text.
'''
props = self.propnames()
s = StringIO.StringIO()
writer = csv.writer(s)
writer.writerow(props)
for nodeid in self._klass.list():
l = []
for name in props:
value = self._klass.get(nodeid, name)
if value is None:
l.append('')
elif isinstance(value, type([])):
l.append(':'.join(map(str, value)))
else:
l.append(str(self._klass.get(nodeid, name)))
writer.writerow(l)
return s.getvalue()
def propnames(self):
''' Return the list of the names of the properties of this class.
'''
idlessprops = self._klass.getprops(protected=0).keys()
idlessprops.sort()
return ['id'] + idlessprops
def filter(self, request=None, filterspec={}, sort=(None,None),
group=(None,None)):
''' Return a list of items from this class, filtered and sorted
by the current requested filterspec/filter/sort/group args
"request" takes precedence over the other three arguments.
'''
if request is not None:
filterspec = request.filterspec
sort = request.sort
group = request.group
check = self._db.security.hasPermission
userid = self._client.userid
l = [HTMLItem(self._client, self.classname, id)
for id in self._klass.filter(None, filterspec, sort, group)
if check('View', userid, self.classname, itemid=id)]
return l
def classhelp(self, properties=None, label=''"(list)", width='500',
height='400', property='', form='itemSynopsis'):
'''Pop up a javascript window with class help
This generates a link to a popup window which displays the
properties indicated by "properties" of the class named by
"classname". The "properties" should be a comma-separated list
(eg. 'id,name,description'). Properties defaults to all the
properties of a class (excluding id, creator, created and
activity).
You may optionally override the label displayed, the width and
height. The popup window will be resizable and scrollable.
If the "property" arg is given, it's passed through to the
javascript help_window function.
If the "form" arg is given, it's passed through to the
javascript help_window function. - it's the name of the form
the "property" belongs to.
'''
if properties is None:
properties = self._klass.getprops(protected=0).keys()
properties.sort()
properties = ','.join(properties)
if property:
property = '&property=%s'%property
if form:
form = '&form=%s'%form
help_url = "%s?@startwith=0&@template=help&"\
"properties=%s%s%s" % \
(self.classname, properties, property, form)
onclick = "javascript:help_window('%s', '%s', '%s');return false;" % \
(help_url, width, height)
return '%s' % \
(help_url, onclick, self._(label))
def submit(self, label=''"Submit New Entry"):
''' Generate a submit button (and action hidden element)
Generate nothing if we're not editable.
'''
if not self.is_edit_ok():
return ''
return self.input(type="hidden", name="@action", value="new") + \
'\n' + \
self.input(type="submit", name="submit", value=self._(label))
def history(self):
if not self.is_view_ok():
return self._('[hidden]')
return self._('New node - no history')
def renderWith(self, name, **kwargs):
''' Render this class with the given template.
'''
# create a new request and override the specified args
req = HTMLRequest(self._client)
req.classname = self.classname
req.update(kwargs)
# new template, using the specified classname and request
pt = self._client.instance.templates.get(self.classname, name)
# use our fabricated request
args = {
'ok_message': self._client.ok_message,
'error_message': self._client.error_message
}
return pt.render(self._client, self.classname, req, **args)
class _HTMLItem(HTMLInputMixin, HTMLPermissions):
''' Accesses through an *item*
'''
def __init__(self, client, classname, nodeid, anonymous=0):
self._client = client
self._db = client.db
self._classname = classname
self._nodeid = nodeid
self._klass = self._db.getclass(classname)
self._props = self._klass.getprops()
# do we prefix the form items with the item's identification?
self._anonymous = anonymous
HTMLInputMixin.__init__(self)
def is_edit_ok(self):
''' Is the user allowed to Edit the current class?
'''
return self._db.security.hasPermission('Edit', self._client.userid,
self._classname, itemid=self._nodeid)
def is_view_ok(self):
''' Is the user allowed to View the current class?
'''
if self._db.security.hasPermission('View', self._client.userid,
self._classname, itemid=self._nodeid):
return 1
return self.is_edit_ok()
def is_only_view_ok(self):
''' Is the user only allowed to View (ie. not Edit) the current class?
'''
return self.is_view_ok() and not self.is_edit_ok()
def __repr__(self):
return ''%(id(self), self._classname,
self._nodeid)
def __getitem__(self, item):
''' return an HTMLProperty instance
'''
#print 'HTMLItem.getitem', (self, item)
if item == 'id':
return self._nodeid
# get the property
prop = self._props[item]
# get the value, handling missing values
value = None
if int(self._nodeid) > 0:
value = self._klass.get(self._nodeid, item, None)
if value is None:
if isinstance(self._props[item], hyperdb.Multilink):
value = []
# look up the correct HTMLProperty class
for klass, htmlklass in propclasses:
if isinstance(prop, klass):
return htmlklass(self._client, self._classname,
self._nodeid, prop, item, value, self._anonymous)
raise KeyError, item
def __getattr__(self, attr):
''' convenience access to properties '''
try:
return self[attr]
except KeyError:
raise AttributeError, attr
def designator(self):
"""Return this item's designator (classname + id)."""
return '%s%s'%(self._classname, self._nodeid)
def is_retired(self):
"""Is this item retired?"""
return self._klass.is_retired(self._nodeid)
def submit(self, label=''"Submit Changes"):
"""Generate a submit button.
Also sneak in the lastactivity and action hidden elements.
"""
return self.input(type="hidden", name="@lastactivity",
value=self.activity.local(0)) + '\n' + \
self.input(type="hidden", name="@action", value="edit") + '\n' + \
self.input(type="submit", name="submit", value=self._(label))
def journal(self, direction='descending'):
''' Return a list of HTMLJournalEntry instances.
'''
# XXX do this
return []
def history(self, direction='descending', dre=re.compile('^\d+$')):
if not self.is_view_ok():
return self._('[hidden]')
# pre-load the history with the current state
current = {}
for prop_n in self._props.keys():
prop = self[prop_n]
if not isinstance(prop, HTMLProperty):
continue
current[prop_n] = prop.plain()
# make link if hrefable
if (self._props.has_key(prop_n) and
isinstance(self._props[prop_n], hyperdb.Link)):
classname = self._props[prop_n].classname
try:
template = find_template(self._db.config.TEMPLATES,
classname, 'item')
if template[1].startswith('_generic'):
raise NoTemplate, 'not really...'
except NoTemplate:
pass
else:
id = self._klass.get(self._nodeid, prop_n, None)
current[prop_n] = '%s'%(
classname, id, current[prop_n])
# get the journal, sort and reverse
history = self._klass.history(self._nodeid)
history.sort()
history.reverse()
timezone = self._db.getUserTimezone()
l = []
comments = {}
for id, evt_date, user, action, args in history:
date_s = str(evt_date.local(timezone)).replace("."," ")
arg_s = ''
if action == 'link' and type(args) == type(()):
if len(args) == 3:
linkcl, linkid, key = args
arg_s += '%s%s %s'%(linkcl, linkid,
linkcl, linkid, key)
else:
arg_s = str(args)
elif action == 'unlink' and type(args) == type(()):
if len(args) == 3:
linkcl, linkid, key = args
arg_s += '%s%s %s'%(linkcl, linkid,
linkcl, linkid, key)
else:
arg_s = str(args)
elif type(args) == type({}):
cell = []
for k in args.keys():
# try to get the relevant property and treat it
# specially
try:
prop = self._props[k]
except KeyError:
prop = None
if prop is None:
# property no longer exists
comments['no_exist'] = self._(
"The indicated property no longer exists")
cell.append(self._('%s: %s\n')
% (self._(k), str(args[k])))
continue
if args[k] and (isinstance(prop, hyperdb.Multilink) or
isinstance(prop, hyperdb.Link)):
# figure what the link class is
classname = prop.classname
try:
linkcl = self._db.getclass(classname)
except KeyError:
labelprop = None
comments[classname] = self._(
"The linked class %(classname)s no longer exists"
) % locals()
labelprop = linkcl.labelprop(1)
try:
template = find_template(self._db.config.TEMPLATES,
classname, 'item')
if template[1].startswith('_generic'):
raise NoTemplate, 'not really...'
hrefable = 1
except NoTemplate:
hrefable = 0
if isinstance(prop, hyperdb.Multilink) and args[k]:
ml = []
for linkid in args[k]:
if isinstance(linkid, type(())):
sublabel = linkid[0] + ' '
linkids = linkid[1]
else:
sublabel = ''
linkids = [linkid]
subml = []
for linkid in linkids:
label = classname + linkid
# if we have a label property, try to use it
# TODO: test for node existence even when
# there's no labelprop!
try:
if labelprop is not None and \
labelprop != 'id':
label = linkcl.get(linkid, labelprop)
except IndexError:
comments['no_link'] = self._(
"The linked node"
" no longer exists")
subml.append('%s'%label)
else:
if hrefable:
subml.append('%s'%(
classname, linkid, label))
else:
subml.append(label)
ml.append(sublabel + ', '.join(subml))
cell.append('%s:\n %s'%(self._(k), ', '.join(ml)))
elif isinstance(prop, hyperdb.Link) and args[k]:
label = classname + args[k]
# if we have a label property, try to use it
# TODO: test for node existence even when
# there's no labelprop!
if labelprop is not None and labelprop != 'id':
try:
label = linkcl.get(args[k], labelprop)
except IndexError:
comments['no_link'] = self._(
"The linked node"
" no longer exists")
cell.append(' %s,\n'%label)
# "flag" this is done .... euwww
label = None
if label is not None:
if hrefable:
old = '%s'%(classname, args[k], label)
else:
old = label;
cell.append('%s: %s' % (self._(k), old))
if current.has_key(k):
cell[-1] += ' -> %s'%current[k]
current[k] = old
elif isinstance(prop, hyperdb.Date) and args[k]:
if args[k] is None:
d = ''
else:
d = date.Date(args[k],
translator=self._client).local(timezone)
cell.append('%s: %s'%(self._(k), str(d)))
if current.has_key(k):
cell[-1] += ' -> %s' % current[k]
current[k] = str(d)
elif isinstance(prop, hyperdb.Interval) and args[k]:
val = str(date.Interval(args[k],
translator=self._client))
cell.append('%s: %s'%(self._(k), val))
if current.has_key(k):
cell[-1] += ' -> %s'%current[k]
current[k] = val
elif isinstance(prop, hyperdb.String) and args[k]:
val = cgi.escape(args[k])
cell.append('%s: %s'%(self._(k), val))
if current.has_key(k):
cell[-1] += ' -> %s'%current[k]
current[k] = val
elif isinstance(prop, hyperdb.Boolean) and args[k] is not None:
val = args[k] and ''"Yes" or ''"No"
cell.append('%s: %s'%(self._(k), val))
if current.has_key(k):
cell[-1] += ' -> %s'%current[k]
current[k] = val
elif not args[k]:
if current.has_key(k):
cell.append('%s: %s'%(self._(k), current[k]))
current[k] = '(no value)'
else:
cell.append(self._('%s: (no value)')%self._(k))
else:
cell.append('%s: %s'%(self._(k), str(args[k])))
if current.has_key(k):
cell[-1] += ' -> %s'%current[k]
current[k] = str(args[k])
arg_s = ' '.join(cell)
else:
# unkown event!!
comments['unknown'] = self._(
"This event is not handled"
" by the history display!")
arg_s = '' + str(args) + ''
date_s = date_s.replace(' ', ' ')
# if the user's an itemid, figure the username (older journals
# have the username)
if dre.match(user):
user = self._db.user.get(user, 'username')
l.append('
%s
%s
%s
%s
'%(
date_s, user, self._(action), arg_s))
if comments:
l.append(self._(
'
Note:
'))
for entry in comments.values():
l.append('
%s
'%entry)
if direction == 'ascending':
l.reverse()
l[0:0] = ['
'
'
',
self._('History'),
'
',
self._('
Date
'),
self._('
User
'),
self._('
Action
'),
self._('
Args
'),
'
']
l.append('
')
return '\n'.join(l)
def renderQueryForm(self):
''' Render this item, which is a query, as a search form.
'''
# create a new request and override the specified args
req = HTMLRequest(self._client)
req.classname = self._klass.get(self._nodeid, 'klass')
name = self._klass.get(self._nodeid, 'name')
req.updateFromURL(self._klass.get(self._nodeid, 'url') +
'&@queryname=%s'%urllib.quote(name))
# new template, using the specified classname and request
pt = self._client.instance.templates.get(req.classname, 'search')
# use our fabricated request
return pt.render(self._client, req.classname, req)
def download_url(self):
''' Assume that this item is a FileClass and that it has a name
and content. Construct a URL for the download of the content.
'''
name = self._klass.get(self._nodeid, 'name')
url = '%s%s/%s'%(self._classname, self._nodeid, name)
return urllib.quote(url)
class _HTMLUser(_HTMLItem):
'''Add ability to check for permissions on users.
'''
_marker = []
def hasPermission(self, permission, classname=_marker,
property=None, itemid=None):
'''Determine if the user has the Permission.
The class being tested defaults to the template's class, but may
be overidden for this test by suppling an alternate classname.
'''
if classname is self._marker:
classname = self._client.classname
return self._client.db.security.hasPermission(permission,
self._nodeid, classname)
def HTMLItem(client, classname, nodeid, anonymous=0):
if classname == 'user':
return _HTMLUser(client, classname, nodeid, anonymous)
else:
return _HTMLItem(client, classname, nodeid, anonymous)
class HTMLProperty(HTMLInputMixin, HTMLPermissions):
''' String, Number, Date, Interval HTMLProperty
Has useful attributes:
_name the name of the property
_value the value of the property if any
A wrapper object which may be stringified for the plain() behaviour.
'''
def __init__(self, client, classname, nodeid, prop, name, value,
anonymous=0):
self._client = client
self._db = client.db
self._ = client._
self._classname = classname
self._nodeid = nodeid
self._prop = prop
self._value = value
self._anonymous = anonymous
self._name = name
if not anonymous:
self._formname = '%s%s@%s'%(classname, nodeid, name)
else:
self._formname = name
HTMLInputMixin.__init__(self)
def __repr__(self):
return ''%(id(self), self._formname,
self._prop, self._value)
def __str__(self):
return self.plain()
def __cmp__(self, other):
if isinstance(other, HTMLProperty):
return cmp(self._value, other._value)
return cmp(self._value, other)
def __nonzero__(self):
return not not self._value
def isset(self):
'''Is my _value not None?'''
return self._value is not None
def is_edit_ok(self):
'''Should the user be allowed to use an edit form field for this
property. Check "Create" for new items, or "Edit" for existing
ones.
'''
if self._nodeid:
return self._db.security.hasPermission('Edit', self._client.userid,
self._classname, self._name, self._nodeid)
return self._db.security.hasPermission('Create', self._client.userid,
self._classname, self._name)
def is_view_ok(self):
''' Is the user allowed to View the current class?
'''
if self._db.security.hasPermission('View', self._client.userid,
self._classname, self._name, self._nodeid):
return 1
return self.is_edit_ok()
class StringHTMLProperty(HTMLProperty):
hyper_re = re.compile(r'((?P\w{3,6}://\S+)|'
r'(?P[-+=%/\w\.]+@[\w\.\-]+)|'
r'(?P(?P[A-Za-z_]+)(\s*)(?P\d+)))')
def _hyper_repl(self, match):
if match.group('url'):
s = match.group('url')
return '%s'%(s, s)
elif match.group('email'):
s = match.group('email')
return '%s'%(s, s)
else:
s = match.group('item')
s1 = match.group('class').lower()
s2 = match.group('id')
try:
# make sure s1 is a valid tracker classname
cl = self._db.getclass(s1)
if not cl.hasnode(s2):
raise KeyError, 'oops'
return '%s'%(s1, s2, s)
except KeyError:
return s
def hyperlinked(self):
''' Render a "hyperlinked" version of the text '''
return self.plain(hyperlink=1)
def plain(self, escape=0, hyperlink=0):
'''Render a "plain" representation of the property
- "escape" turns on/off HTML quoting
- "hyperlink" turns on/off in-text hyperlinking of URLs, email
addresses and designators
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
if escape:
s = cgi.escape(str(self._value))
else:
s = str(self._value)
if hyperlink:
# no, we *must* escape this text
if not escape:
s = cgi.escape(s)
s = self.hyper_re.sub(self._hyper_repl, s)
return s
def stext(self, escape=0):
''' Render the value of the property as StructuredText.
This requires the StructureText module to be installed separately.
'''
if not self.is_view_ok():
return self._('[hidden]')
s = self.plain(escape=escape)
if not StructuredText:
return s
return StructuredText(s,level=1,header=0)
def field(self, size = 30):
''' Render the property as a field in HTML.
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
if self._value is None:
value = ''
else:
value = cgi.escape(str(self._value))
value = '"'.join(value.split('"'))
return self.input(name=self._formname,value=value,size=size)
def multiline(self, escape=0, rows=5, cols=40):
''' Render a multiline form edit field for the property.
If not editable, just display the plain() value in a
tag.
'''
if not self.is_edit_ok():
return '
%s
'%self.plain()
if self._value is None:
value = ''
else:
value = cgi.escape(str(self._value))
value = '"'.join(value.split('"'))
return ''%(
self._formname, rows, cols, value)
def email(self, escape=1):
''' Render the value of the property as an obscured email address
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
value = ''
else:
value = str(self._value)
if value.find('@') != -1:
name, domain = value.split('@')
domain = ' '.join(domain.split('.')[:-1])
name = name.replace('.', ' ')
value = '%s at %s ...'%(name, domain)
else:
value = value.replace('.', ' ')
if escape:
value = cgi.escape(value)
return value
class PasswordHTMLProperty(HTMLProperty):
def plain(self):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
return self._('*encrypted*')
def field(self, size = 30):
''' Render a form edit field for the property.
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
return self.input(type="password", name=self._formname, size=size)
def confirm(self, size = 30):
''' Render a second form edit field for the property, used for
confirmation that the user typed the password correctly. Generates
a field with name "@confirm@name".
If not editable, display nothing.
'''
if not self.is_edit_ok():
return ''
return self.input(type="password",
name="@confirm@%s"%self._formname, size=size)
class NumberHTMLProperty(HTMLProperty):
def plain(self):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
return str(self._value)
def field(self, size = 30):
''' Render a form edit field for the property.
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
if self._value is None:
value = ''
else:
value = cgi.escape(str(self._value))
value = '"'.join(value.split('"'))
return self.input(name=self._formname,value=value,size=size)
def __int__(self):
''' Return an int of me
'''
return int(self._value)
def __float__(self):
''' Return a float of me
'''
return float(self._value)
class BooleanHTMLProperty(HTMLProperty):
def plain(self):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
return self._value and "Yes" or "No"
def field(self):
''' Render a form edit field for the property
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
value = self._value
if isinstance(value, str) or isinstance(value, unicode):
value = value.strip().lower() in ('checked', 'yes', 'true',
'on', '1')
checked = value and "checked" or ""
if value:
s = self.input(type="radio", name=self._formname, value="yes",
checked="checked")
s += 'Yes'
s +=self.input(type="radio", name=self._formname, value="no")
s += 'No'
else:
s = self.input(type="radio", name=self._formname, value="yes")
s += 'Yes'
s +=self.input(type="radio", name=self._formname, value="no",
checked="checked")
s += 'No'
return s
class DateHTMLProperty(HTMLProperty):
_marker = []
def __init__(self, client, classname, nodeid, prop, name, value,
anonymous=0, offset=None):
HTMLProperty.__init__(self, client, classname, nodeid, prop, name,
value, anonymous=anonymous)
if self._value and not (isinstance(self._value, str) or
isinstance(self._value, unicode)):
self._value.setTranslator(self._client.translator)
self._offset = offset
def plain(self):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
if self._offset is None:
offset = self._db.getUserTimezone()
else:
offset = self._offset
return str(self._value.local(offset))
def now(self, str_interval=None):
''' Return the current time.
This is useful for defaulting a new value. Returns a
DateHTMLProperty.
'''
if not self.is_view_ok():
return self._('[hidden]')
ret = date.Date('.', translator=self._client)
if isinstance(str_interval, basestring):
sign = 1
if str_interval[0] == '-':
sign = -1
str_interval = str_interval[1:]
interval = date.Interval(str_interval, translator=self._client)
if sign > 0:
ret = ret + interval
else:
ret = ret - interval
return DateHTMLProperty(self._client, self._classname, self._nodeid,
self._prop, self._formname, ret)
def field(self, size=30, default=None, format=_marker):
'''Render a form edit field for the property
If not editable, just display the value via plain().
The format string is a standard python strftime format string.
'''
if not self.is_edit_ok():
if format is self._marker:
return self.plain()
else:
return self.pretty(format)
value = self._value
if value is None:
if default is None:
raw_value = None
else:
if isinstance(default, basestring):
raw_value = Date(default, translator=self._client)
elif isinstance(default, date.Date):
raw_value = default
elif isinstance(default, DateHTMLProperty):
raw_value = default._value
else:
raise ValueError, self._('default value for '
'DateHTMLProperty must be either DateHTMLProperty '
'or string date representation.')
elif isinstance(value, str) or isinstance(value, unicode):
# most likely erroneous input to be passed back to user
value = cgi.escape(str(value), 1)
return self.input(name=self._formname, value=value, size=size)
else:
raw_value = value
if raw_value is None:
value = ''
elif isinstance(raw_value, str) or isinstance(raw_value, unicode):
if format is self._marker:
value = raw_value
else:
value = date.Date(raw_value).pretty(format)
else:
tz = self._db.getUserTimezone()
value = raw_value.local(tz)
if format is not self._marker:
value = value.pretty(format)
value = cgi.escape(str(value), 1)
return self.input(name=self._formname, value=value, size=size)
def reldate(self, pretty=1):
''' Render the interval between the date and now.
If the "pretty" flag is true, then make the display pretty.
'''
if not self.is_view_ok():
return self._('[hidden]')
if not self._value:
return ''
# figure the interval
interval = self._value - date.Date('.', translator=self._client)
if pretty:
return interval.pretty()
return str(interval)
def pretty(self, format=_marker):
''' Render the date in a pretty format (eg. month names, spaces).
The format string is a standard python strftime format string.
Note that if the day is zero, and appears at the start of the
string, then it'll be stripped from the output. This is handy
for the situatin when a date only specifies a month and a year.
'''
if not self.is_view_ok():
return self._('[hidden]')
if not self._value:
return ''
elif format is not self._marker:
return self._value.pretty(format)
else:
return self._value.pretty()
def local(self, offset):
''' Return the date/time as a local (timezone offset) date/time.
'''
if not self.is_view_ok():
return self._('[hidden]')
return DateHTMLProperty(self._client, self._classname, self._nodeid,
self._prop, self._formname, self._value, offset=offset)
class IntervalHTMLProperty(HTMLProperty):
def __init__(self, client, classname, nodeid, prop, name, value,
anonymous=0):
HTMLProperty.__init__(self, client, classname, nodeid, prop,
name, value, anonymous)
if self._value:
self._value.setTranslator(self._client.translator)
def plain(self):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
return str(self._value)
def pretty(self):
''' Render the interval in a pretty format (eg. "yesterday")
'''
if not self.is_view_ok():
return self._('[hidden]')
return self._value.pretty()
def field(self, size = 30):
''' Render a form edit field for the property
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
if self._value is None:
value = ''
else:
value = cgi.escape(str(self._value))
value = '"'.join(value.split('"'))
return self.input(name=self._formname,value=value,size=size)
class LinkHTMLProperty(HTMLProperty):
''' Link HTMLProperty
Include the above as well as being able to access the class
information. Stringifying the object itself results in the value
from the item being displayed. Accessing attributes of this object
result in the appropriate entry from the class being queried for the
property accessed (so item/assignedto/name would look up the user
entry identified by the assignedto property on item, and then the
name property of that user)
'''
def __init__(self, *args, **kw):
HTMLProperty.__init__(self, *args, **kw)
# if we're representing a form value, then the -1 from the form really
# should be a None
if str(self._value) == '-1':
self._value = None
def __getattr__(self, attr):
''' return a new HTMLItem '''
#print 'Link.getattr', (self, attr, self._value)
if not self._value:
msg = self._('Attempt to look up %(attr)s on a missing value')
return MissingValue(msg%locals())
i = HTMLItem(self._client, self._prop.classname, self._value)
return getattr(i, attr)
def plain(self, escape=0):
''' Render a "plain" representation of the property
'''
if not self.is_view_ok():
return self._('[hidden]')
if self._value is None:
return ''
linkcl = self._db.classes[self._prop.classname]
k = linkcl.labelprop(1)
value = str(linkcl.get(self._value, k))
if escape:
value = cgi.escape(value)
return value
def field(self, showid=0, size=None):
''' Render a form edit field for the property
If not editable, just display the value via plain().
'''
if not self.is_edit_ok():
return self.plain()
# edit field
linkcl = self._db.getclass(self._prop.classname)
if self._value is None:
value = ''
else:
k = linkcl.getkey()
if k:
value = linkcl.get(self._value, k)
else:
value = self._value
value = cgi.escape(str(value))
value = '"'.join(value.split('"'))
return ''%(self._formname,
value, size)
def menu(self, size=None, height=None, showid=0, additional=[],
sort_on=None, **conditions):
''' Render a form select list for this property
"size" is used to limit the length of the list labels
"height" is used to set the