Bonjour,

Voici le code d'un widget qui calcul par script son contenu.
Ce widget prend comme paramétre le nom d'un script python qui calcule le contenu. Le script reçoit comme parametre la datastructure. Par exemple, un script qui calcule un titre à partir de champ du doc.:
##parameters=ds

du = ds['Du_date']
au = ds['Au_date']
type = ds['Type']
return "%s, du %s au %s" % (type, du, au)

Cordialement,

   Pierre

## Class (Python) "CPSSimulateWidget.py"

"""
=================================================================
Description:
  Widget permettant    de remplacer le    titre d'un CPSDocument par :
  -    soit le    résultat retourné par un script    python
  -    soit une concatenation d'autre champ du document

=================================================================
"""


from zLOG import LOG, INFO,    DEBUG, PROBLEM
from Globals import    InitializeClass
from AccessControl import ClassSecurityInfo

from Products.CMFCore.CMFCorePermissions import    ManageProperties
from Products.CMFCore.utils    import getToolByName

from Products.CPSSchemas.Widget    import CPSWidget
from Products.CPSSchemas.BasicWidgets import    CPSStringWidget
from Products.CPSSchemas.Widget    import CPSWidgetType
from Products.CPSSchemas.WidgetTypesTool import    WidgetTypeRegistry



class CPSSimulateWidget(CPSStringWidget):
    """CPS Simulate widget."""
    meta_type =    "CPS Simulate Widget"

    field_types    = ('CPS    String Field',)

    display_width =    20
    size_max = 0

    simulated_value    = ""
    simulated_script = ""
   
    _properties    = CPSWidget._properties    + (
    {'id': 'simulated_value', 'type': 'string',    'mode':    'w','label': 'Titre    calcule a partir des champs'},
    {'id': 'simulated_script', 'type': 'string', 'mode': 'w','label': 'Script Python'},
    )

    def    validate(self, datastructure, **kw):
        """Validate    datastructure and update datamodel."""
        LOG('Simulate',DEBUG,'validate %s' % datastructure)
        widget_id =    self.getWidgetId()
        err, v = self._extractValue(datastructure[widget_id])
        if err:
            datastructure.setError(widget_id, err)
            datastructure[widget_id] = v
        else:
            datamodel =    datastructure.getDataModel()
            external_fields = self.simulated_value.split(' ')
            title = ''
            if external_fields:
                for w_id in external_fields:
                    try:
                        v = str(datastructure[w_id])
                        title = title+' '+v
                    except:
                        pass
            if self.simulated_script:
                script = self.simulated_script
                meth = getattr(self, script, None)
                if meth:
                    title = meth(datastructure)

            datamodel[self.fields[0]] =    title

        return not err
       
    def render(self, mode, datastructure, **kw):
        """Render this widget from the datastructure or datamodel."""
        return ""
   

InitializeClass(CPSSimulateTitleWidget)


class CPSSimulateWidgetType(CPSWidgetType):
    """String widget type."""
    meta_type =    "CPS Simulate Widget Type"
    cls    = CPSSimulateWidget

InitializeClass(CPSSimulateWidgetType)

WidgetTypeRegistry.register(CPSSimulateWidgetType,
                            CPSSimulateWidget)





Baudot, Valentin wrote:

Bonjour,

 

J’ai besoin de générer le titre d’un document lorsque l’utilisateur crée un nouveau document. Quelqu’un sait comment je peux faire çà ? est-ce possible sans modifier de classes python ?

 

J’utilise CPS 3.2.4. Lorsque l’utilisateur crée un nouveau doc il faudrait que le titre soit déjà rempli avec des acronymes dépendant du nom du workspace dans lequel il est créé : "XXX-yy_zz" par exemple dans le workspace XXX.

 

Toute aide est la bienvenue.

Merci.

Valentin Baudot


_______________________________________________ cps-users-fr Adresse de la liste : [email protected] Gestion de l'abonnement : <http://lists.nuxeo.com/mailman/listinfo/cps-users-fr>


This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.
_______________________________________________
cps-users-fr 
Adresse de la liste : [email protected]
Gestion de l'abonnement : <http://lists.nuxeo.com/mailman/listinfo/cps-users-fr>

Répondre à