[gen] appy.Object: allow to get object attributes like accessing dict keys, ie object[attr]; Pod field: bugfix (download name of pod template being links to other pod templates + UI bug); Pod field: Manager does not have all output formats anymore, because in some situations, generating a doc in some formats does make sens (ie for frozen docs); Wrapper.getLastEvent can now work on some history given as parameter instead of object.history.

This commit is contained in:
Gaetan Delannay 2014-11-20 09:47:37 +01:00
parent e6adfe39d4
commit 477a533728
3 changed files with 29 additions and 19 deletions

View file

@ -36,9 +36,9 @@ class Object:
res += u'%s=<encoding problem> ' % attrName
res = res.strip() + '>'
return res.encode('utf-8')
def __nonzero__(self):
return bool(self.__dict__)
def __nonzero__(self): return bool(self.__dict__)
def get(self, name, default=None): return getattr(self, name, default)
def __getitem__(self, k): return getattr(self, k)
def update(self, other):
'''Includes information from p_other into p_self.'''
for k, v in other.__dict__.iteritems():

View file

@ -355,23 +355,22 @@ class Pod(Field):
if not queryRelated:
# This is a POD for a single object: personalize the file name with
# the object title.
fileName = '%s-%s' % (norm(obj.title)[:140], fileName)
title = obj.o.getShownValue('title')
fileName = '%s-%s' % (norm(title)[:140], fileName)
return fileName + '.' + format
def getVisibleTemplates(self, obj):
'''Returns, among self.template, the template(s) that can be shown.'''
res = []
if not self.showTemplate:
# Show them all in the formats spoecified in self.formats.
# Show them all in the formats specified in self.formats.
for template in self.template:
res.append(Object(template=template, formats=self.formats,
freezeFormats=self.getFreezeFormats(obj, template)))
else:
isManager = obj.user.has_role('Manager')
for template in self.template:
formats = self.showTemplate(obj, template)
if not formats: continue
if isManager: formats = self.getAllFormats(template)
elif isinstance(formats, bool): formats = self.formats
elif isinstance(formats, basestring): formats = (formats,)
res.append(Object(template=template, formats=formats,
@ -563,13 +562,24 @@ class Pod(Field):
# Get a FileInfo instance to manipulate the file on the filesystem.
return FileInfo(result, inDb=False, uploadName=fileName)
def getBaseName(self, template=None):
'''Gets the "base name" of p_template (or self.template[0] if not
given). The base name is the name of the template, without path
and extension. Moreover, if the template is a pointer to another one
(ie Item.odt.something), the base name integrates the specific
extension. In the example, the base name will be "ItemSomething".'''
template = template or self.template[0]
elems = os.path.splitext(os.path.basename(template))
if elems[1] in ('.odt', '.ods'):
res = elems[0]
else:
res = os.path.splitext(elems[0])[0] + elems[1][1:].capitalize()
return res
def getFreezeName(self, template=None, format='pdf', sep='.'):
'''Gets the name on disk on the frozen document corresponding to this
pod field, p_template and p_format.'''
template = template or self.template[0]
template = os.path.basename(template)
templateName = os.path.splitext(template)[0].replace(os.sep, '_')
return '%s_%s%s%s' % (self.name, templateName, sep, format)
return '%s_%s%s%s' % (self.name,self.getBaseName(template),sep,format)
def isFrozen(self, obj, template=None, format='pdf'):
'''Is there a frozen document for thid pod field, on p_obj, for

View file

@ -1122,15 +1122,15 @@ class AbstractWrapper(object):
whose values are the previous field values.'''
self.o.addDataChange(data)
def getLastEvent(self, transition, notBefore=None):
'''Gets, from the object history, the last occurrence of transition
named p_transition. p_transition can be a list of names: in this
case, it returns the most recent occurrence of those transitions. If
p_notBefore is given, it corresponds to a kind of start transition
for the search: we will not search in the history preceding the last
occurrence of this transition.'''
history = self.history
i = len(history)-1
def getLastEvent(self, transition, notBefore=None, history=None):
'''Gets, from the object history (or from p_history if not None), the
last occurrence of transition named p_transition. p_transition can be
a list of names: in this case, it returns the most recent occurrence
of those transitions. If p_notBefore is given, it corresponds to a
kind of start transition for the search: we will not search in the
history preceding the last occurrence of this transition.'''
history = history or self.history
i = len(history) - 1
while i >= 0:
event = history[i]
if notBefore and (event['action'] == notBefore): return