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

View file

@ -355,23 +355,22 @@ class Pod(Field):
if not queryRelated: if not queryRelated:
# This is a POD for a single object: personalize the file name with # This is a POD for a single object: personalize the file name with
# the object title. # 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 return fileName + '.' + format
def getVisibleTemplates(self, obj): def getVisibleTemplates(self, obj):
'''Returns, among self.template, the template(s) that can be shown.''' '''Returns, among self.template, the template(s) that can be shown.'''
res = [] res = []
if not self.showTemplate: 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: for template in self.template:
res.append(Object(template=template, formats=self.formats, res.append(Object(template=template, formats=self.formats,
freezeFormats=self.getFreezeFormats(obj, template))) freezeFormats=self.getFreezeFormats(obj, template)))
else: else:
isManager = obj.user.has_role('Manager')
for template in self.template: for template in self.template:
formats = self.showTemplate(obj, template) formats = self.showTemplate(obj, template)
if not formats: continue if not formats: continue
if isManager: formats = self.getAllFormats(template)
elif isinstance(formats, bool): formats = self.formats elif isinstance(formats, bool): formats = self.formats
elif isinstance(formats, basestring): formats = (formats,) elif isinstance(formats, basestring): formats = (formats,)
res.append(Object(template=template, 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. # Get a FileInfo instance to manipulate the file on the filesystem.
return FileInfo(result, inDb=False, uploadName=fileName) 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='.'): def getFreezeName(self, template=None, format='pdf', sep='.'):
'''Gets the name on disk on the frozen document corresponding to this '''Gets the name on disk on the frozen document corresponding to this
pod field, p_template and p_format.''' pod field, p_template and p_format.'''
template = template or self.template[0] return '%s_%s%s%s' % (self.name,self.getBaseName(template),sep,format)
template = os.path.basename(template)
templateName = os.path.splitext(template)[0].replace(os.sep, '_')
return '%s_%s%s%s' % (self.name, templateName, sep, format)
def isFrozen(self, obj, template=None, format='pdf'): def isFrozen(self, obj, template=None, format='pdf'):
'''Is there a frozen document for thid pod field, on p_obj, for '''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.''' whose values are the previous field values.'''
self.o.addDataChange(data) self.o.addDataChange(data)
def getLastEvent(self, transition, notBefore=None): def getLastEvent(self, transition, notBefore=None, history=None):
'''Gets, from the object history, the last occurrence of transition '''Gets, from the object history (or from p_history if not None), the
named p_transition. p_transition can be a list of names: in this last occurrence of transition named p_transition. p_transition can be
case, it returns the most recent occurrence of those transitions. If a list of names: in this case, it returns the most recent occurrence
p_notBefore is given, it corresponds to a kind of start transition of those transitions. If p_notBefore is given, it corresponds to a
for the search: we will not search in the history preceding the last kind of start transition for the search: we will not search in the
occurrence of this transition.''' history preceding the last occurrence of this transition.'''
history = self.history history = history or self.history
i = len(history)-1 i = len(history) - 1
while i >= 0: while i >= 0:
event = history[i] event = history[i]
if notBefore and (event['action'] == notBefore): return if notBefore and (event['action'] == notBefore): return