web/form.py
author sylvain.thenault@logilab.fr
Wed, 11 Mar 2009 19:53:39 +0100
branchtls-sprint
changeset 1072 e36f2889e1bd
parent 1071 abe11abb37a0
child 1073 e4342c74ed2d
permissions -rw-r--r--
unnecessary
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""abstract form classes for CubicWeb web client
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
751
ec16f43107d3 update import
sylvain.thenault@logilab.fr
parents: 692
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
     9
from warnings import warn
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
    10
from datetime import date, datetime
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
    11
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from simplejson import dumps
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
    14
from logilab.common.compat import any
1058
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
    15
from logilab.common.decorators import iclassmethod
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
from logilab.mtconverter import html_escape
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
    18
from yams.constraints import SizeConstraint, StaticVocabularyConstraint
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
    19
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
from cubicweb import typed_eid
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
    21
from cubicweb.appobject import AppObject
867
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
    22
from cubicweb.utils import ustrftime
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
    23
from cubicweb.selectors import yes, match_form_params, non_final_entity
751
ec16f43107d3 update import
sylvain.thenault@logilab.fr
parents: 692
diff changeset
    24
from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView
1048
2a93712a6f8b fix format field vocabulary
sylvain.thenault@logilab.fr
parents: 1047
diff changeset
    25
from cubicweb.schema import FormatConstraint
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
from cubicweb.common.registerers import accepts_registerer
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
    27
from cubicweb.common.uilib import toggle_action
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
from cubicweb.web import stdmsgs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from cubicweb.web.httpcache import NoHTTPCacheManager
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
    30
from cubicweb.web.controller import NAV_FORM_PARAMETERS, redirect_params
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
    31
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
    32
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
def relation_id(eid, rtype, target, reid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    if target == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        return u'%s:%s:%s' % (eid, rtype, reid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    return u'%s:%s:%s' % (reid, rtype, eid)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
    38
        
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
    39
def toggable_relation_link(eid, nodeid, label='x'):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
    40
    js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid)))
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
    41
    return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
class FormMixIn(object):
944
e9da555f19c5 [forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 913
diff changeset
    45
    """abstract form mix-in
e9da555f19c5 [forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 913
diff changeset
    46
    XXX: you should inherit from this FIRST (obscure pb with super call)"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    category = 'form'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    controller = 'edit'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    domid = 'entityForm'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    http_cache_manager = NoHTTPCacheManager
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    add_to_breadcrumbs = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    skip_relations = set()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
    def __init__(self, req, rset):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        super(FormMixIn, self).__init__(req, rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        self.maxrelitems = self.req.property_value('navigation.related-limit')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        self.maxcomboitems = self.req.property_value('navigation.combobox-limit')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        self.force_display = not not req.form.get('__force_display')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        # get validation session data which may have been previously set.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        # deleting validation errors here breaks form reloading (errors are
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        # no more available), they have to be deleted by application's publish
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        # method on successful commit
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        formurl = req.url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        forminfo = req.get_session_data(formurl)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        if forminfo:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
            req.data['formvalues'] = forminfo['values']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
            req.data['formerrors'] = errex = forminfo['errors']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
            req.data['displayederrors'] = set()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            # if some validation error occured on entity creation, we have to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
            # get the original variable name from its attributed eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
            foreid = errex.entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            for var, eid in forminfo['eidmap'].items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
                if foreid == eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
                    errex.eid = var
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
                    break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                errex.eid = foreid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    def html_headers(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        """return a list of html headers (eg something to be inserted between
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        <head> and </head> of the returned page
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        by default forms are neither indexed nor followed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        return [NOINDEX, NOFOLLOW]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    def linkable(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
        """override since forms are usually linked by an action,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        so we don't want them to be listed by appli.possible_views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        return False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    def limit(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        if self.force_display:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        return self.maxrelitems + 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    def need_multipart(self, entity, categories=('primary', 'secondary')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        """return a boolean indicating if form's enctype should be multipart
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        for rschema, _, x in entity.relations_by_category(categories):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
            if entity.get_widget(rschema, x).need_multipart:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
                return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        # let's find if any of our inlined entities needs multipart
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        for rschema, targettypes, x in entity.relations_by_category('inlineview'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
            assert len(targettypes) == 1, \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
                   "I'm not able to deal with several targets and inlineview"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
            ttype = targettypes[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
            inlined_entity = self.vreg.etype_class(ttype)(self.req, None, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
            for irschema, _, x in inlined_entity.relations_by_category(categories):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
                if inlined_entity.get_widget(irschema, x).need_multipart:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
                    return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        return False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    def error_message(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        """return formatted error message
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        This method should be called once inlined field errors has been consumed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        errex = self.req.data.get('formerrors')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        # get extra errors
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        if errex is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
            errormsg = self.req._('please correct the following errors:')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            displayed = self.req.data['displayederrors']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
            errors = sorted((field, err) for field, err in errex.errors.items()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                            if not field in displayed)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
            if errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
                if len(errors) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                    templstr = '<li>%s</li>\n' 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
                    templstr = '&nbsp;%s\n'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
                for field, err in errors:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
                    if field is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
                        errormsg += templstr % err
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
                    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
                        errormsg += templstr % '%s: %s' % (self.req._(field), err)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
                if len(errors) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
                    errormsg = '<ul>%s</ul>' % errormsg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
            return u'<div class="errorMessage">%s</div>' % errormsg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
    def restore_pending_inserts(self, entity, cell=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
        """used to restore edition page as it was before clicking on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        'search for <some entity type>'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        eid = entity.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        cell = cell and "div_insert_" or "tr"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        pending_inserts = set(self.req.get_pending_inserts(eid))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        for pendingid in pending_inserts:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
            eidfrom, rtype, eidto = pendingid.split(':')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
            if typed_eid(eidfrom) == entity.eid: # subject
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
                label = display_name(self.req, rtype, 'subject')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
                reid = eidto
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
                label = display_name(self.req, rtype, 'object')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
                reid = eidfrom
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
            jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
                     % (pendingid, cell, eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
            rset = self.req.eid_rset(reid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
            eview = self.view('text', rset, row=0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
            # XXX find a clean way to handle baskets
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
            if rset.description[0][0] == 'Basket':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
                eview = '%s (%s)' % (eview, display_name(self.req, 'Basket'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
            yield rtype, pendingid, jscall, label, reid, eview
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    def force_display_link(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        return (u'<span class="invisible">' 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
                u'[<a href="javascript: window.location.href+=\'&amp;__force_display=1\'">%s</a>]'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
                u'</span>' % self.req._('view all'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    def relations_table(self, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        """yiels 3-tuples (rtype, target, related_list)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
        where <related_list> itself a list of :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
          - node_id (will be the entity element's DOM id)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
          - appropriate javascript's togglePendingDelete() function call
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
          - status 'pendingdelete' or ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
          - oneline view of related entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        eid = entity.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        pending_deletes = self.req.get_pending_deletes(eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        # XXX (adim) : quick fix to get Folder relations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
        for label, rschema, target in entity.srelations_by_category(('generic', 'metadata'), 'add'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
            if rschema in self.skip_relations:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
            relatedrset = entity.related(rschema, target, limit=self.limit)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
            toggable_rel_link = self.toggable_relation_link_func(rschema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
            related = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
            for row in xrange(relatedrset.rowcount):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
                nodeid = relation_id(eid, rschema, target, relatedrset[row][0])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
                if nodeid in pending_deletes:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
                    status = u'pendingDelete'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
                    label = '+'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
                    status = u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
                    label = 'x'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
                dellink = toggable_rel_link(eid, nodeid, label)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
                eview = self.view('oneline', relatedrset, row=row)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
                related.append((nodeid, dellink, status, eview))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
            yield (rschema, target, related)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
    def toggable_relation_link_func(self, rschema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
        if not rschema.has_perm(self.req, 'delete'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
            return lambda x, y, z: u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
        return toggable_relation_link
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    def redirect_url(self, entity=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        """return a url to use as next direction if there are some information
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        specified in current form params, else return the result the reset_url
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
        method which should be defined in concrete classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        rparams = redirect_params(self.req.form)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
        if rparams:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
            return self.build_url('view', **rparams)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
        return self.reset_url(entity)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    def reset_url(self, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
        raise NotImplementedError('implement me in concrete classes')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    BUTTON_STR = u'<input class="validateButton" type="submit" name="%s" value="%s" tabindex="%s"/>'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    ACTION_SUBMIT_STR = u'<input class="validateButton" type="button" onclick="postForm(\'%s\', \'%s\', \'%s\')" value="%s" tabindex="%s"/>'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    def button_ok(self, label=None, tabindex=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        label = self.req._(label or stdmsgs.BUTTON_OK).capitalize()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
        return self.BUTTON_STR % ('defaultsubmit', label, tabindex or 2)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    def button_apply(self, label=None, tabindex=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
        label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize()
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   233
        return self.ACTION_SUBMIT_STR % ('__action_apply', label, self.domid,
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   234
                                         label, tabindex or 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
    def button_delete(self, label=None, tabindex=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize()
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   238
        return self.ACTION_SUBMIT_STR % ('__action_delete', label, self.domid,
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   239
                                         label, tabindex or 3)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
    def button_cancel(self, label=None, tabindex=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize()
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   243
        return self.ACTION_SUBMIT_STR % ('__action_cancel', label, self.domid,
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   244
                                         label, tabindex or 4)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    def button_reset(self, label=None, tabindex=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
        label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
        return u'<input class="validateButton" type="reset" value="%s" tabindex="%s"/>' % (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
            label, tabindex or 4)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   250
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   251
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   252
###############################################################################
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   253
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   254
from cubicweb.common import tags
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   255
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   256
# widgets ############
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   257
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   258
class FieldWidget(object):
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   259
    needs_js = ()
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   260
    needs_css = ()
1032
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   261
    setdomid = True
1055
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   262
    settabindex = True
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   263
    
1055
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   264
    def __init__(self, attrs=None, setdomid=None, settabindex=None):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   265
        self.attrs = attrs or {}
1032
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   266
        if setdomid is not None:
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   267
            # override class's default value
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   268
            self.setdomid = setdomid
1055
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   269
        if settabindex is not None:
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   270
            # override class's default value
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   271
            self.settabindex = settabindex
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   272
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   273
    def add_media(self, form):
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   274
        """adds media (CSS & JS) required by this widget"""
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   275
        req = form.req
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   276
        if self.needs_js:
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   277
            req.add_js(self.needs_js)
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   278
        if self.needs_css:
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   279
            req.add_css(self.needs_css)
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   280
        
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   281
    def render(self, form, field):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   282
        raise NotImplementedError
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   283
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   284
    def _render_attrs(self, form, field):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   285
        name = form.context[field]['name']
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   286
        values = form.context[field]['value']
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   287
        if not isinstance(values, (tuple, list)):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   288
            values = (values,)
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   289
        attrs = dict(self.attrs)
1032
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   290
        if self.setdomid:
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   291
            attrs['id'] = form.context[field]['id']
1055
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   292
        if self.settabindex:
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   293
            attrs['tabindex'] = form.req.next_tabindex()
910
a86ab461b8fd fix DateTimePicker implementation, it should now work
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 907
diff changeset
   294
        return name, values, attrs
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   295
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   296
class Input(FieldWidget):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   297
    type = None
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   298
    
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   299
    def render(self, form, field):
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   300
        self.add_media(form)
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   301
        name, values, attrs = self._render_attrs(form, field)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   302
        inputs = [tags.input(name=name, value=value, type=self.type, **attrs)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   303
                  for value in values]
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   304
        return u'\n'.join(inputs)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   305
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   306
class TextInput(Input):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   307
    type = 'text'
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   308
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   309
class PasswordInput(Input):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   310
    type = 'password'
908
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   311
    
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   312
    def render(self, form, field):
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   313
        self.add_media(form)
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   314
        name, values, attrs = self._render_attrs(form, field)
911
sylvain.thenault@logilab.fr
parents: 908
diff changeset
   315
        assert len(values) == 1
sylvain.thenault@logilab.fr
parents: 908
diff changeset
   316
        inputs = [tags.input(name=name, value=values[0], type=self.type, **attrs),
908
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   317
                  '<br/>',
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   318
                  tags.input(name=name+'-confirm', type=self.type, **attrs),
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   319
                  '&nbsp;', tags.span(form.req._('confirm password'),
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   320
                                      **{'class': 'emphasis'})]
136d91725ecf confirmation in password widget
sylvain.thenault@logilab.fr
parents: 907
diff changeset
   321
        return u'\n'.join(inputs)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   322
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   323
class FileInput(Input):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   324
    type = 'file'
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   325
    
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   326
    def _render_attrs(self, form, field):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   327
        # ignore value which makes no sense here (XXX even on form validation error?)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   328
        name, values, attrs = super(FileInput, self)._render_attrs(form, field)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   329
        return name, ('',), attrs
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   330
        
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   331
class HiddenInput(Input):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   332
    type = 'hidden'
1032
25ec009daa95 by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents: 1031
diff changeset
   333
    setdomid = False # by default, don't set id attribute on hidden input
1055
b87222b63950 set tabindex
sylvain.thenault@logilab.fr
parents: 1053
diff changeset
   334
    settabindex = False
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   335
    
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   336
class ButtonInput(Input):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   337
    type = 'button'
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   338
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   339
class TextArea(FieldWidget):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   340
    def render(self, form, field):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   341
        name, values, attrs = self._render_attrs(form, field)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   342
        attrs.setdefault('onkeypress', 'autogrow(this)')
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   343
        if not values:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   344
            value = u''
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   345
        elif len(values) == 1:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   346
            value = values[0]
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   347
        else:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   348
            raise ValueError('a textarea is not supposed to be multivalued')
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   349
        return tags.textarea(value, name=name, **attrs)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   350
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   351
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   352
class FCKEditor(TextArea):
1059
87dc8b1903a2 test fixes
sylvain.thenault@logilab.fr
parents: 1058
diff changeset
   353
    def __init__(self, *args, **kwargs):
87dc8b1903a2 test fixes
sylvain.thenault@logilab.fr
parents: 1058
diff changeset
   354
        super(FCKEditor, self).__init__(*args, **kwargs)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   355
        self.attrs['cubicweb:type'] = 'wysiwyg'
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   356
    
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   357
    def render(self, form, field):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   358
        form.req.fckeditor_config()
1059
87dc8b1903a2 test fixes
sylvain.thenault@logilab.fr
parents: 1058
diff changeset
   359
        return super(FCKEditor, self).render(form, field)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   360
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   361
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   362
class Select(FieldWidget):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   363
    def __init__(self, attrs=None, multiple=False):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   364
        super(Select, self).__init__(attrs)
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   365
        self.multiple = multiple
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   366
        
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   367
    def render(self, form, field):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   368
        name, curvalues, attrs = self._render_attrs(form, field)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   369
        options = []
1056
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   370
        for label, value in field.vocabulary(form):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   371
            if value in curvalues:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   372
                options.append(tags.option(label, value=value, selected='selected'))
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   373
            else:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   374
                options.append(tags.option(label, value=value))
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   375
        return tags.select(name=name, multiple=self.multiple,
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   376
                           options=options, **attrs)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   377
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   378
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   379
class CheckBox(Input):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   380
    type = 'checkbox'
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   381
    
1067
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   382
    def render(self, form, field):
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   383
        name, curvalues, attrs = self._render_attrs(form, field)
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   384
        options = []
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   385
        for label, value in field.vocabulary(form):
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   386
            if value in curvalues:
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   387
                tag = tags.input(name=name, value=value, type=self.type,
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   388
                                 checked='checked', **attrs)
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   389
            else:
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   390
                tag = tags.input(name=name, value=value, type=self.type,
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   391
                                 **attrs)
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   392
            options.append(tag + label)
f37ee6855e42 checkbox widget should use vocabulary
sylvain.thenault@logilab.fr
parents: 1064
diff changeset
   393
        return '<br/>\n'.join(options)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   394
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   395
        
1056
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   396
class Radio(Input):
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   397
    type = 'radio'
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   398
    setdomid = False
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   399
    
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   400
    def render(self, form, field):
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   401
        name, curvalues, attrs = self._render_attrs(form, field)
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   402
        options = []
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   403
        for label, value in field.vocabulary(form):
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   404
            if value in curvalues:
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   405
                options.append(tags.input(name=name, type=self.type, value=value, checked='checked', **attrs))
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   406
            else:
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   407
                options.append(tags.option(name=name, type=self.type, value=value, **attrs))
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   408
            options[-1] += label + '<br/>'
be43d95ebb52 radio widget
sylvain.thenault@logilab.fr
parents: 1055
diff changeset
   409
        return '\n'.join(options)
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   410
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   411
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   412
class DateTimePicker(TextInput):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   413
    monthnames = ('january', 'february', 'march', 'april',
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   414
                  'may', 'june', 'july', 'august',
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   415
                  'september', 'october', 'november', 'december')
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   416
    daynames = ('monday', 'tuesday', 'wednesday', 'thursday',
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   417
                'friday', 'saturday', 'sunday')
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   418
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   419
    needs_js = ('cubicweb.ajax.js', 'cubicweb.calendar.js')
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   420
    needs_css = ('cubicweb.calendar_popup.css',)
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   421
    
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   422
    @classmethod
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   423
    def add_localized_infos(cls, req):
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   424
        """inserts JS variables defining localized months and days"""
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   425
        # import here to avoid dependancy from cubicweb-common to simplejson
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   426
        _ = req._
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   427
        monthnames = [_(mname) for mname in cls.monthnames]
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   428
        daynames = [_(dname) for dname in cls.daynames]
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   429
        req.html_headers.define_var('MONTHNAMES', monthnames)
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   430
        req.html_headers.define_var('DAYNAMES', daynames)
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   431
    
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   432
    def render(self, form, field):
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   433
        txtwidget = super(DateTimePicker, self).render(form, field)
910
a86ab461b8fd fix DateTimePicker implementation, it should now work
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 907
diff changeset
   434
        self.add_localized_infos(form.req)
867
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   435
        cal_button = self._render_calendar_popup(form, field)
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   436
        return txtwidget + cal_button
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   437
    
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   438
    def _render_calendar_popup(self, form, field):
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   439
        req = form.req
867
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   440
        value = form.context[field]['rawvalue']
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   441
        inputid = form.context[field]['id']
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   442
        helperid = '%shelper' % inputid
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   443
        if not value:
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   444
            value = date.today()
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   445
        year, month = value.year, value.month
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   446
        onclick = "toggleCalendar('%s', '%s', %s, %s);" % (
867
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   447
            helperid, inputid, year, month)
865
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   448
        return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   449
<img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
90a1aea85637 DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 864
diff changeset
   450
                % (helperid, inputid, year, month,
867
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   451
                   req.external_resource('CALENDAR_ICON'),
14a79faa3a56 fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 865
diff changeset
   452
                   req._('calendar'), helperid) )
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   453
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   454
1069
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   455
class AjaxWidget(FieldWidget):
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   456
    def __init__(self, wdgtype, inputid=None, **kwargs):
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   457
        super(AjaxWidget, self).__init__(**kwargs)
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   458
        self.attrs.setdefault('class', 'widget')
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   459
        self.attrs.setdefault('cubicweb:loadtype', 'auto')
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   460
        self.attrs['cubicweb:wdgtype'] = wdgtype
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   461
        if inputid is not None:
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   462
            self.attrs['cubicweb:inputid'] = inputid
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   463
            
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   464
    def render(self, form, field):
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   465
        self.add_media(form)
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   466
        name, values, attrs = self._render_attrs(form, field)
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   467
        return tags.div(**attrs)
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   468
84bc7dce1876 new AjaxWidget
sylvain.thenault@logilab.fr
parents: 1068
diff changeset
   469
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   470
# fields ############
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   471
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   472
class Field(object):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   473
    """field class is introduced to control what's displayed in edition form
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   474
    """
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   475
    widget = TextInput
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   476
    needs_multipart = False
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   477
    creation_rank = 0
904
4f1ce95aa686 fix label generation in forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 903
diff changeset
   478
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   479
    def __init__(self, name=None, id=None, label=None,
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   480
                 widget=None, required=False, initial=None,
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   481
                 choices=None, help=None, eidparam=False):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   482
        self.required = required
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   483
        if widget is not None:
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   484
            self.widget = widget
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   485
        if isinstance(self.widget, type):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   486
            self.widget = self.widget()
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   487
        self.name = name
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   488
        self.label = label or name
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   489
        self.id = id or name
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   490
        self.initial = initial
1070
3bc7d4ebb543 if choices is None, call form_field_vocabulary
sylvain.thenault@logilab.fr
parents: 1069
diff changeset
   491
        self.choices = choices
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   492
        self.help = help
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   493
        self.eidparam = eidparam
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   494
        self.role = 'subject'
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   495
        # global fields ordering in forms
1071
abe11abb37a0 must record creation rank as an instance attribute
sylvain.thenault@logilab.fr
parents: 1070
diff changeset
   496
        self.creation_rank = Field.creation_rank
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   497
        Field.creation_rank += 1
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   498
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   499
    def set_name(self, name):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   500
        assert name
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   501
        self.name = name
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   502
        if not self.id:
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   503
            self.id = name
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   504
        if not self.label:
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   505
            self.label = name
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   506
            
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   507
    def is_visible(self):
904
4f1ce95aa686 fix label generation in forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 903
diff changeset
   508
        return not isinstance(self.widget, HiddenInput)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   509
    
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   510
    def actual_fields(self, form):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   511
        yield self
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   512
900
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   513
    def __unicode__(self):
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   514
        return u'<%s name=%r label=%r id=%r initial=%r>' % (
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   515
            self.__class__.__name__, self.name, self.label,
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   516
            self.id, self.initial)
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   517
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   518
    def __repr__(self):
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   519
        return self.__unicode__().encode('utf-8')
8f95ebbe2fd8 field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 899
diff changeset
   520
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   521
    def format_value(self, req, value):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   522
        if isinstance(value, (list, tuple)):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   523
            return [self.format_single_value(req, val) for val in value]
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   524
        return self.format_single_value(req, value)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   525
    
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   526
    def format_single_value(self, req, value):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   527
        if value is None:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   528
            return u''
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   529
        return unicode(value)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   530
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   531
    def get_widget(self, form):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   532
        return self.widget
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   533
    
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   534
    def example_format(self, req):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   535
        return u''
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   536
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   537
    def render(self, form, renderer):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   538
        return self.get_widget(form).render(form, self)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   539
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   540
    def vocabulary(self, form):
1070
3bc7d4ebb543 if choices is None, call form_field_vocabulary
sylvain.thenault@logilab.fr
parents: 1069
diff changeset
   541
        if self.choices is not None:
3bc7d4ebb543 if choices is None, call form_field_vocabulary
sylvain.thenault@logilab.fr
parents: 1069
diff changeset
   542
            return self.choices
3bc7d4ebb543 if choices is None, call form_field_vocabulary
sylvain.thenault@logilab.fr
parents: 1069
diff changeset
   543
        return form.form_field_vocabulary(self)
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   544
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   545
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   546
class StringField(Field):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   547
    def __init__(self, max_length=None, **kwargs):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   548
        super(StringField, self).__init__(**kwargs)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   549
        self.max_length = max_length
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   550
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   551
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   552
class TextField(Field):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   553
    widget = TextArea
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   554
    def __init__(self, rows=10, cols=80, **kwargs):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   555
        super(TextField, self).__init__(**kwargs)
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   556
        self.rows = rows
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   557
        self.cols = cols
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   558
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   559
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   560
class RichTextField(TextField):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   561
    widget = None
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   562
    def __init__(self, format_field=None, **kwargs):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   563
        super(RichTextField, self).__init__(**kwargs)
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   564
        self.format_field = format_field
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   565
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   566
    def get_widget(self, form):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   567
        if self.widget is None:
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   568
            if self.use_fckeditor(form):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   569
                return FCKEditor()
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   570
            return TextArea()
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   571
        return self.widget
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   572
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   573
    def get_format_field(self, form):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   574
        if self.format_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   575
            return self.format_field
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   576
        # we have to cache generated field since it's use as key in the
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   577
        # context dictionnary
1064
e17d57ee8c8c cleanup
sylvain.thenault@logilab.fr
parents: 1063
diff changeset
   578
        req = form.req
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   579
        try:
1064
e17d57ee8c8c cleanup
sylvain.thenault@logilab.fr
parents: 1063
diff changeset
   580
            return req.data[self]
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   581
        except KeyError:
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   582
            if self.use_fckeditor(form):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   583
                # if fckeditor is used and format field isn't explicitly
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   584
                # deactivated, we want an hidden field for the format
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   585
                widget = HiddenInput()
1048
2a93712a6f8b fix format field vocabulary
sylvain.thenault@logilab.fr
parents: 1047
diff changeset
   586
                choices = None
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   587
            else:
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   588
                # else we want a format selector
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   589
                # XXX compute vocabulary
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   590
                widget = Select
1048
2a93712a6f8b fix format field vocabulary
sylvain.thenault@logilab.fr
parents: 1047
diff changeset
   591
                choices = [(req._(format), format) for format in FormatConstraint().vocabulary(req=req)]
2a93712a6f8b fix format field vocabulary
sylvain.thenault@logilab.fr
parents: 1047
diff changeset
   592
            field = StringField(name=self.name + '_format', widget=widget,
2a93712a6f8b fix format field vocabulary
sylvain.thenault@logilab.fr
parents: 1047
diff changeset
   593
                                choices=choices)
1064
e17d57ee8c8c cleanup
sylvain.thenault@logilab.fr
parents: 1063
diff changeset
   594
            req.data[self] = field
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   595
            return field
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   596
    
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   597
    def actual_fields(self, form):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   598
        yield self
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 892
diff changeset
   599
        format_field = self.get_format_field(form)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   600
        if format_field:
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   601
            yield format_field
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   602
            
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   603
    def use_fckeditor(self, form):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   604
        """return True if fckeditor should be used to edit entity's attribute named
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   605
        `attr`, according to user preferences
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   606
        """
1013
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 1009
diff changeset
   607
        if form.req.use_fckeditor():
1060
d795477ad16b rename method to form_field_format
sylvain.thenault@logilab.fr
parents: 1059
diff changeset
   608
            return form.form_field_format(self) == 'text/html'
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   609
        return False
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   610
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   611
    def render(self, form, renderer):
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 892
diff changeset
   612
        format_field = self.get_format_field(form)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   613
        if format_field:
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   614
            result = format_field.render(form, renderer)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   615
        else:
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   616
            result = u''
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   617
        return result + self.get_widget(form).render(form, self)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   618
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   619
    
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   620
class FileField(StringField):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   621
    widget = FileInput
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   622
    needs_multipart = True
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   623
    
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   624
    def __init__(self, format_field=None, encoding_field=None, **kwargs):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   625
        super(FileField, self).__init__(**kwargs)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   626
        self.format_field = format_field
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   627
        self.encoding_field = encoding_field
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   628
        
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   629
    def actual_fields(self, form):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   630
        yield self
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   631
        if self.format_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   632
            yield self.format_field
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   633
        if self.encoding_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   634
            yield self.encoding_field
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   635
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   636
    def render(self, form, renderer):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   637
        wdgs = [self.get_widget(form).render(form, self)]
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   638
        if self.format_field or self.encoding_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   639
            divid = '%s-advanced' % form.context[self]['name']
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   640
            wdgs.append(u'<a href="%s" title="%s"><img src="%s" alt="%s"/></a>' %
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   641
                        (html_escape(toggle_action(divid)),
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   642
                         form.req._('show advanced fields'),
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   643
                         html_escape(form.req.build_url('data/puce_down.png')),
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   644
                         form.req._('show advanced fields')))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   645
            wdgs.append(u'<div id="%s" class="hidden">' % divid)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   646
            if self.format_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   647
                wdgs.append(self.render_subfield(form, self.format_field, renderer))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   648
            if self.encoding_field:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   649
                wdgs.append(self.render_subfield(form, self.encoding_field, renderer))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   650
            wdgs.append(u'</div>')            
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   651
        if not self.required and form.context[self]['value']:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   652
            # trick to be able to delete an uploaded file
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   653
            wdgs.append(u'<br/>')
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   654
            wdgs.append(tags.input(name=u'%s__detach' % form.context[self]['name'],
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   655
                                   type=u'checkbox'))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   656
            wdgs.append(form.req._('detach attached file'))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   657
        return u'\n'.join(wdgs)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   658
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   659
    def render_subfield(self, form, field, renderer):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   660
        return (renderer.render_label(form, field)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   661
                + field.render(form, renderer)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   662
                + renderer.render_help(form, field)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   663
                + u'<br/>')
1064
e17d57ee8c8c cleanup
sylvain.thenault@logilab.fr
parents: 1063
diff changeset
   664
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   665
        
1062
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   666
class EditableFileField(FileField):
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   667
    editable_formats = ('text/plain', 'text/html', 'text/rest')
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   668
    
1062
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   669
    def render(self, form, renderer):
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   670
        wdgs = [super(EditableFileField, self).render(form, renderer)]
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   671
        if form.form_field_format(self) in self.editable_formats:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   672
            data = form.form_field_value(self, {}, load_bytes=True)
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   673
            if data:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   674
                encoding = form.form_field_encoding(self)
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   675
                try:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   676
                    form.context[self]['value'] = unicode(data.getvalue(), encoding)
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   677
                except UnicodeError:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   678
                    pass
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   679
                else:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   680
                    if not self.required:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   681
                        msg = form.req._(
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   682
                            'You can either submit a new file using the browse button above'
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   683
                            ', or choose to remove already uploaded file by checking the '
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   684
                            '"detach attached file" check-box, or edit file content online '
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   685
                            'with the widget below.')
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   686
                    else:
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   687
                        msg = form.req._(
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   688
                            'You can either submit a new file using the browse button above'
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   689
                            ', or edit file content online with the widget below.')
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   690
                    wdgs.append(u'<p><b>%s</b></p>' % msg)
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   691
                    wdgs.append(TextArea(setdomid=False).render(form, self))
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   692
                    # XXX restore form context?
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   693
        return '\n'.join(wdgs)
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   694
185d630d1aef EditableFileField
sylvain.thenault@logilab.fr
parents: 1061
diff changeset
   695
        
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   696
class IntField(Field):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   697
    def __init__(self, min=None, max=None, **kwargs):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   698
        super(IntField, self).__init__(**kwargs)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   699
        self.min = min
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   700
        self.max = max
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   701
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   702
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   703
class BooleanField(Field):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   704
    widget = Radio
1063
6e55fd09afc4 implement vocabulary for Boolean field
sylvain.thenault@logilab.fr
parents: 1062
diff changeset
   705
        
6e55fd09afc4 implement vocabulary for Boolean field
sylvain.thenault@logilab.fr
parents: 1062
diff changeset
   706
    def vocabulary(self, form):
6e55fd09afc4 implement vocabulary for Boolean field
sylvain.thenault@logilab.fr
parents: 1062
diff changeset
   707
        if self.choices:
6e55fd09afc4 implement vocabulary for Boolean field
sylvain.thenault@logilab.fr
parents: 1062
diff changeset
   708
            return self.choices
6e55fd09afc4 implement vocabulary for Boolean field
sylvain.thenault@logilab.fr
parents: 1062
diff changeset
   709
        return [(form.req._('yes'), '1'), (form.req._('no'), '')]
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   710
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   711
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   712
class FloatField(IntField):    
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   713
    def format_single_value(self, req, value):
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   714
        formatstr = entity.req.property_value('ui.float-format')
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   715
        if value is None:
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   716
            return u''
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   717
        return formatstr % float(value)
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   718
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   719
    def render_example(self, req):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   720
        return self.format_value(req, 1.234)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   721
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   722
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   723
class DateField(StringField):
899
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   724
    format_prop = 'ui.date-format'
905
64fd6eaaa9a4 add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 904
diff changeset
   725
    widget = DateTimePicker
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   726
    
899
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   727
    def format_single_value(self, req, value):
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   728
        return value and ustrftime(value, req.property_value(self.format_prop)) or u''
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   729
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   730
    def render_example(self, req):
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   731
        return self.format_value(req, datetime.now())
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   732
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   733
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   734
class DateTimeField(DateField):
899
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   735
    format_prop = 'ui.datetime-format'
864
e0947007542a basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 863
diff changeset
   736
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   737
899
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   738
class TimeField(DateField):
48d465b5171a rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 898
diff changeset
   739
    format_prop = 'ui.datetime-format'
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
   740
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   741
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   742
class HiddenInitialValueField(Field):
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   743
    def __init__(self, visible_field, name):
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   744
        super(HiddenInitialValueField, self).__init__(name=name,
898
583f64567256 fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 897
diff changeset
   745
                                                      widget=HiddenInput,
583f64567256 fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 897
diff changeset
   746
                                                      eidparam=True)
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   747
        self.visible_field = visible_field
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   748
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   749
                 
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   750
class RelationField(Field):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   751
    def __init__(self, **kwargs):
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   752
        super(RelationField, self).__init__(**kwargs)
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   753
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   754
    @staticmethod
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   755
    def fromcardinality(card, role, **kwargs):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   756
        return RelationField(widget=Select(multiple=card in '*+'),
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   757
                             **kwargs)
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   758
        
901
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   759
    def vocabulary(self, form):
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   760
        entity = form.entity
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   761
        req = entity.req
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   762
        # first see if its specified by __linkto form parameters
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   763
        linkedto = entity.linked_to(self.name, self.role)
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   764
        if linkedto:
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   765
            entities = (req.eid_rset(eid).get_entity(0, 0) for eid in linkedto)
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   766
            return [(entity.view('combobox'), entity.eid) for entity in entities]
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   767
        # it isn't, check if the entity provides a method to get correct values
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   768
        res = []
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   769
        if not self.required:
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   770
            res.append(('', INTERNAL_FIELD_VALUE))
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   771
        # vocabulary doesn't include current values, add them
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   772
        if entity.has_eid():
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   773
            rset = entity.related(self.name, self.role)
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   774
            relatedvocab = [(e.view('combobox'), e.eid) for e in rset.entities()]
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   775
        else:
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   776
            relatedvocab = []
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   777
        return res + form.form_field_vocabulary(self) + relatedvocab
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   778
    
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   779
    def format_single_value(self, req, value):
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   780
        return value
0dcf01eb97a0 implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 900
diff changeset
   781
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   782
# forms ############
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   783
class metafieldsform(type):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   784
    def __new__(mcs, name, bases, classdict):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   785
        allfields = []
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   786
        for base in bases:
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   787
            if hasattr(base, '_fields_'):
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   788
                allfields += base._fields_
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   789
        clsfields = (item for item in classdict.items()
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   790
                     if isinstance(item[1], Field))
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   791
        for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   792
            if not field.name:
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   793
                field.set_name(fieldname)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   794
            allfields.append(field)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   795
        classdict['_fields_'] = allfields
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   796
        return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict)
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   797
    
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   798
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   799
class FieldsForm(FormMixIn, AppObject):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   800
    __metaclass__ = metafieldsform
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   801
    __registry__ = 'forms'
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   802
    __select__ = yes()
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   803
    
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   804
    def __init__(self, req, rset=None, domid=None, title=None, action='edit',
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   805
                 onsubmit="return freezeFormButtons('%(domid)s');",
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   806
                 cssclass=None, cssstyle=None, cwtarget=None, buttons=None,
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   807
                 redirect_path=None, set_error_url=True, copy_nav_params=False):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   808
        self.req = req
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   809
        self.rset = rset
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   810
        self.config = req.vreg.config
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
   811
        self.domid = domid or 'form'
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   812
        self.title = title
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   813
        self.action = action
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   814
        self.onsubmit = onsubmit
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   815
        self.cssclass = cssclass
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   816
        self.cssstyle = cssstyle
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   817
        self.cwtarget = cwtarget
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
   818
        self.redirect_path = redirect_path
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   819
        self.fields = list(self.__class__._fields_)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   820
        if set_error_url:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   821
            self.form_add_hidden('__errorurl', req.url())
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   822
        if copy_nav_params:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   823
            for param in NAV_FORM_PARAMETERS:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   824
                value = req.form.get(param)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   825
                if value:
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   826
                    self.form_add_hidden(param, initial=value)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   827
        self.buttons = buttons or []
1053
e4d965b5ca37 cleanup
sylvain.thenault@logilab.fr
parents: 1050
diff changeset
   828
        self.context = None
1058
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   829
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   830
    @iclassmethod
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   831
    def field_by_name(cls_or_self, name):
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   832
        if isinstance(cls_or_self, type):
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   833
            fields = cls_or_self._fields_
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   834
        else:
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   835
            fields = cls_or_self.fields
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   836
        for field in fields:
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   837
            if field.name == name:
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   838
                return field
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   839
        raise Exception('field %s not found' % name)
c27be37daef8 field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents: 1056
diff changeset
   840
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   841
    @property
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   842
    def form_needs_multipart(self):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   843
        return any(field.needs_multipart for field in self.fields) 
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   844
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   845
    def form_add_hidden(self, name, value=None, **kwargs):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   846
        self.fields.append(StringField(name=name, widget=HiddenInput,
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
   847
                                       initial=value, **kwargs))
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   848
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   849
    def form_render(self, **values):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   850
        renderer = values.pop('renderer', FormRenderer())
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   851
        return renderer.render(self, values)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   852
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   853
    def form_build_context(self, values):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   854
        self.context = context = {}
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   855
        # on validation error, we get a dictionnary of previously submitted values
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   856
        previous_values = self.req.data.get('formvalues')
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   857
        if previous_values:
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   858
            values.update(previous_values)
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   859
        for field in self.fields:
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 892
diff changeset
   860
            for field in field.actual_fields(self):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   861
                value = self.form_field_value(field, values)
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   862
                context[field] = {'value': field.format_value(self.req, value),
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   863
                                  'rawvalue': value,
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   864
                                  'name': self.form_field_name(field),
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   865
                                  'id': self.form_field_id(field),
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   866
                                  }
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   867
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   868
    def form_field_value(self, field, values, load_bytes=False):
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   869
        """looks for field's value in
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   870
        1. kw args given to render_form (including previously submitted form
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   871
           values if any)
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   872
        2. req.form
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   873
        3. field's initial value
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   874
        """
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   875
        if field.name in values:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   876
            value = values[field.name]
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   877
        elif field.name in self.req.form:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   878
            value = self.req.form[field.name]
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   879
        else:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   880
            value = field.initial
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   881
        return value
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   882
1060
d795477ad16b rename method to form_field_format
sylvain.thenault@logilab.fr
parents: 1059
diff changeset
   883
    def form_field_format(self, field):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   884
        return self.req.property_value('ui.default-text-format')
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   885
    
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   886
    def form_field_encoding(self, field):
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   887
        return self.req.encoding
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   888
    
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   889
    def form_field_name(self, field):
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   890
        return field.name
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   891
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   892
    def form_field_id(self, field):
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   893
        return field.id
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   894
   
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
   895
    def form_field_vocabulary(self, field):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   896
        raise NotImplementedError
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   897
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   898
    def form_buttons(self):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   899
        return self.buttons
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   900
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   901
   
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   902
class EntityFieldsForm(FieldsForm):
1047
21d4d5e6aa45 make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents: 1032
diff changeset
   903
    __select__ = non_final_entity()
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   904
    
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   905
    def __init__(self, *args, **kwargs):
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
   906
        kwargs.setdefault('domid', 'entityForm')
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   907
        self.entity = kwargs.pop('entity', None)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   908
        super(EntityFieldsForm, self).__init__(*args, **kwargs)
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
   909
        self.form_add_hidden('__type', eidparam=True)
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
   910
        self.form_add_hidden('eid')
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   911
        
874
90d98d80af82 EntityFieldsForm.form_render() doesn't need entity anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 869
diff changeset
   912
    def form_render(self, **values):
90d98d80af82 EntityFieldsForm.form_render() doesn't need entity anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 869
diff changeset
   913
        self.form_add_entity_hiddens(self.entity.e_schema)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   914
        return super(EntityFieldsForm, self).form_render(**values)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   915
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   916
    def form_add_entity_hiddens(self, eschema):
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   917
        for field in self.fields[:]:
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 892
diff changeset
   918
            for field in field.actual_fields(self):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   919
                fieldname = field.name
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   920
                if fieldname != 'eid' and (
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   921
                    (eschema.has_subject_relation(fieldname) or
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   922
                     eschema.has_object_relation(fieldname))):
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   923
                    field.eidparam = True
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   924
                    self.fields.append(self.form_entity_hidden_field(field))
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   925
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   926
    def form_entity_hidden_field(self, field):
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   927
        """returns the hidden field which will indicate the value
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   928
        before the modification
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   929
        """
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   930
        # Only RelationField has a `role` attribute, others are used
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   931
        # to describe attribute fields => role is 'subject'
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   932
        if getattr(field, 'role', 'subject') == 'subject':
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   933
            name = 'edits-%s' % field.name
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   934
        else:
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   935
            name = 'edito-%s' % field.name
898
583f64567256 fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 897
diff changeset
   936
        return HiddenInitialValueField(field, name=name)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
   937
        
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   938
    def form_field_value(self, field, values, load_bytes=False):
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   939
        """look for field's value with the following rules:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   940
        1. handle special __type and eid fields
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   941
        2. looks in kw args given to render_form (including previously submitted
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
   942
           form values if any)
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   943
        3. looks in req.form
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   944
        4. if entity has an eid:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   945
             1. looks for an associated attribute / method
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   946
             2. use field's initial value
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   947
           else:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   948
             1. looks for a default_<fieldname> attribute / method on the form
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   949
             2. use field's initial value
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   950
             
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   951
        values found in step 4 may be a callable which'll then be called.
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   952
        """
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   953
        fieldname = field.name
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   954
        if fieldname.startswith('edits-') or fieldname.startswith('edito-'):
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
   955
            # edit[s|o]- fieds must have the actual value stored on the entity
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
   956
            if self.entity.has_eid():
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   957
                value = self._form_field_entity_value(field.visible_field,
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   958
                                                      default_initial=False)
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
   959
            else:
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   960
                value = INTERNAL_FIELD_VALUE
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   961
        elif fieldname == '__type':
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   962
            value = self.entity.id
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   963
        elif fieldname == 'eid':
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   964
            value = self.entity.eid
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   965
        elif fieldname in values:
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   966
            value = values[fieldname]
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   967
        elif fieldname in self.req.form:
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   968
            value = self.req.form[fieldname]
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   969
        else:
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
   970
            if self.entity.has_eid() and field.eidparam:
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   971
                # use value found on the entity or field's initial value if it's
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   972
                # not an attribute of the entity (XXX may conflicts and get
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   973
                # undesired value)
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   974
                value = self._form_field_entity_value(field, default_initial=True,
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
   975
                                                      load_bytes=load_bytes)
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   976
            else:
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
   977
                defaultattr = 'default_%s' % fieldname
848
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   978
                if hasattr(self.entity, defaultattr):
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   979
                    # XXX bw compat, default_<field name> on the entity
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   980
                    warn('found %s on %s, should be set on a specific form'
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   981
                         % (defaultattr, self.entity.id), DeprecationWarning)
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   982
                    value = getattr(self.entity, defaultattr)
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   983
                elif hasattr(self, defaultattr):
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   984
                    # search for default_<field name> on the form instance
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   985
                    value = getattr(self, defaultattr)
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   986
                else:
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   987
                    # use field's initial value
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   988
                    value = field.initial
f5aca5c5f6ca set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents: 847
diff changeset
   989
            if callable(value):
1059
87dc8b1903a2 test fixes
sylvain.thenault@logilab.fr
parents: 1058
diff changeset
   990
                value = value()
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
   991
        return value
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   992
    
1060
d795477ad16b rename method to form_field_format
sylvain.thenault@logilab.fr
parents: 1059
diff changeset
   993
    def form_field_format(self, field):
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   994
        entity = self.entity
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   995
        if field.eidparam and entity.has_format(field.name) and (
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   996
            entity.has_eid() or '%s_format' % field.name in entity):
1059
87dc8b1903a2 test fixes
sylvain.thenault@logilab.fr
parents: 1058
diff changeset
   997
            return self.entity.format(field.name)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
   998
        return self.req.property_value('ui.default-text-format')
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
   999
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1000
    def form_field_encoding(self, field):
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1001
        entity = self.entity
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1002
        if field.eidparam and entity.has_encoding(field.name) and (
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1003
            entity.has_eid() or '%s_encoding' % field.name in entity):
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1004
            return self.entity.text_encoding()
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1005
        return super(EntityFieldsForm, self).form_field_encoding(field)
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1006
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1007
    def _form_field_entity_value(self, field, default_initial=True, load_bytes=False):
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
  1008
        attr = field.name 
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
  1009
        if field.role == 'object':
1049
f8e92c2f3bef fix attribute name for object relation
sylvain.thenault@logilab.fr
parents: 1048
diff changeset
  1010
            attr = 'reverse_' + attr
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1011
        else:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1012
            attrtype = self.entity.e_schema.destination(attr)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1013
            if attrtype == 'Password':
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1014
                return self.entity.has_eid() and INTERNAL_FIELD_VALUE or ''
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1015
            if attrtype == 'Bytes':
1061
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1016
                if self.entity.has_eid():
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1017
                    if load_bytes:
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1018
                        return getattr(self.entity, attr)
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1019
                    # XXX value should reflect if some file is already attached
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1020
                    return True
7d2e6b5fae3c * new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents: 1060
diff changeset
  1021
                return False
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
  1022
        if default_initial:
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1023
            value = getattr(self.entity, attr, field.initial)
888
603327e9aef4 fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents: 887
diff changeset
  1024
        else:
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1025
            value = getattr(self.entity, attr)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1026
        if isinstance(field, RelationField):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1027
            # in this case, value is the list of related entities
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1028
            value = [ent.eid for ent in value]
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1029
        return value
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1030
    
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
  1031
    def form_field_name(self, field):
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1032
        if field.eidparam:
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1033
            return eid_param(field.name, self.entity.eid)
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1034
        return field.name
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1035
849
8591d896db7e update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents: 848
diff changeset
  1036
    def form_field_id(self, field):
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1037
        if field.eidparam:
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1038
            return eid_param(field.id, self.entity.eid)
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1039
        return field.id
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1040
        
1031
1a89683cb687 restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1041
    def form_field_vocabulary(self, field, limit=None):
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1042
        role, rtype = field.role, field.name
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1043
        try:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1044
            vocabfunc = getattr(self.entity, '%s_%s_vocabulary' % (role, rtype))
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1045
        except AttributeError:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1046
            vocabfunc = getattr(self, '%s_relation_vocabulary' % role)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1047
        else:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1048
            # XXX bw compat, default_<field name> on the entity
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1049
            warn('found %s_%s_vocabulary on %s, should be set on a specific form'
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1050
                 % (role, rtype, self.entity.id), DeprecationWarning)
1009
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1051
        # NOTE: it is the responsibility of `vocabfunc` to sort the result
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1052
        #       (direclty through RQL or via a python sort). This is also
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1053
        #       important because `vocabfunc` might return a list with
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1054
        #       couples (label, None) which act as separators. In these
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1055
        #       cases, it doesn't make sense to sort results afterwards.
1031
1a89683cb687 restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1056
        return vocabfunc(rtype, limit)
903
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1057
## XXX BACKPORT ME
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1058
##         if self.sort:
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1059
##             choices = sorted(choices)
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1060
##         if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'):
63a8ab7eeb9c The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 902
diff changeset
  1061
##             return zip((entity.req._(v) for v in choices), choices)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1062
902
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1063
    def subject_relation_vocabulary(self, rtype, limit=None):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1064
        """defaut vocabulary method for the given relation, looking for
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1065
        relation's object entities (i.e. self is the subject)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1066
        """
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1067
        entity = self.entity
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1068
        if isinstance(rtype, basestring):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1069
            rtype = entity.schema.rschema(rtype)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1070
        done = None
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1071
        assert not rtype.is_final(), rtype
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1072
        if entity.has_eid():
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1073
            done = set(e.eid for e in getattr(entity, str(rtype)))
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1074
        result = []
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1075
        rsetsize = None
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1076
        for objtype in rtype.objects(entity.e_schema):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1077
            if limit is not None:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1078
                rsetsize = limit - len(result)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1079
            result += self.relation_vocabulary(rtype, objtype, 'subject',
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1080
                                               rsetsize, done)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1081
            if limit is not None and len(result) >= limit:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1082
                break
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1083
        return result
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1084
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1085
    def object_relation_vocabulary(self, rtype, limit=None):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1086
        """defaut vocabulary method for the given relation, looking for
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1087
        relation's subject entities (i.e. self is the object)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1088
        """
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1089
        entity = self.entity
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1090
        if isinstance(rtype, basestring):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1091
            rtype = entity.schema.rschema(rtype)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1092
        done = None
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1093
        if entity.has_eid():
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1094
            done = set(e.eid for e in getattr(entity, 'reverse_%s' % rtype))
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1095
        result = []
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1096
        rsetsize = None
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1097
        for subjtype in rtype.subjects(entity.e_schema):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1098
            if limit is not None:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1099
                rsetsize = limit - len(result)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1100
            result += self.relation_vocabulary(rtype, subjtype, 'object',
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1101
                                               rsetsize, done)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1102
            if limit is not None and len(result) >= limit:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1103
                break
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1104
        return result
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1105
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1106
    def relation_vocabulary(self, rtype, targettype, role,
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1107
                            limit=None, done=None):
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1108
        if done is None:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1109
            done = set()
1009
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1110
        rset = self.entity.unrelated(rtype, targettype, role, limit)
902
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1111
        res = []
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1112
        for entity in rset.entities():
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1113
            if entity.eid in done:
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1114
                continue
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1115
            done.add(entity.eid)
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1116
            res.append((entity.view('combobox'), entity.eid))
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1117
        return res
e4de959c76af vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 901
diff changeset
  1118
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1119
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1120
class MultipleFieldsForm(FieldsForm):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1121
    def __init__(self, *args, **kwargs):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1122
        super(MultipleFieldsForm, self).__init__(*args, **kwargs)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1123
        self.forms = []
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1124
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1125
    def form_add_subform(self, subform):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1126
        self.forms.append(subform)
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1127
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1128
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1129
# form renderers ############
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1130
class FormRenderer(object):
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1131
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1132
    # renderer interface ######################################################
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1133
    
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1134
    def render(self, form, values, display_help=True):
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1135
        data = []
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1136
        w = data.append
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1137
        w(self.open_form(form))
896
6e0379fc1988 put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 894
diff changeset
  1138
        w(u'<div id="progress">%s</div>' % form.req._('validating...'))
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1139
        w(u'<fieldset>')
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
  1140
        w(tags.input(type='hidden', name='__form_id', value=form.domid))
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1141
        if form.redirect_path:
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
  1142
            w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path))
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1143
        self.render_fields(w, form, values, display_help)
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1144
        self.render_buttons(w, form)
844
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1145
        w(u'</fieldset>')
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1146
        w(u'</form>')
8ab6f64c3750 start django like forms
sylvain.thenault@logilab.fr
parents: 765
diff changeset
  1147
        return '\n'.join(data)
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1148
        
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1149
    def render_label(self, form, field):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1150
        label = form.req._(field.label)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1151
        attrs = {'for': form.context[field]['id']}
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1152
        if field.required:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1153
            attrs['class'] = 'required'
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1154
        return tags.label(label, **attrs)
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1155
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1156
    def render_help(self, form, field):
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1157
        help = [ u'<br/>' ]
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1158
        descr = field.help
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1159
        if descr:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1160
            help.append('<span class="helper">%s</span>' % req._(descr))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1161
        example = field.example_format(form.req)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1162
        if example:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1163
            help.append('<span class="helper">(%s: %s)</span>'
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1164
                        % (req._('sample format'), example))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1165
        return u'&nbsp;'.join(help)
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1166
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1167
    # specific methods (mostly to ease overriding) #############################
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1168
    
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1169
    def open_form(self, form):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1170
        if form.form_needs_multipart:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1171
            enctype = 'multipart/form-data'
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1172
        else:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1173
            enctype = 'application/x-www-form-urlencoded'
1009
bcd0c3ae1935 typos, backport comment
sylvain.thenault@logilab.fr
parents: 944
diff changeset
  1174
        tag = ('<form action="%s" method="post" id="%s" enctype="%s"' % (
887
51e371245bc5 various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents: 874
diff changeset
  1175
            html_escape(form.action or '#'), form.domid, enctype))
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1176
        if form.onsubmit:
1050
c9815bef16d7 expand onsubmit using form's __dict__
sylvain.thenault@logilab.fr
parents: 1049
diff changeset
  1177
            tag += ' onsubmit="%s"' % html_escape(form.onsubmit % form.__dict__)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1178
        if form.cssstyle:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1179
            tag += ' style="%s"' % html_escape(form.cssstyle)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1180
        if form.cssclass:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1181
            tag += ' class="%s"' % html_escape(form.cssclass)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1182
        if form.cwtarget:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1183
            tag += ' cubicweb:target="%s"' % html_escape(form.cwtarget)
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1184
        return tag + '>'
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1185
        
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1186
    def render_fields(self, w, form, values, display_help=True):
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1187
        form.form_build_context(values)
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
  1188
        fields = form.fields[:]
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
  1189
        for field in form.fields:
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
  1190
            if not field.is_visible():
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1191
                w(field.render(form, self))
863
4fde01fc39ca restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 849
diff changeset
  1192
                fields.remove(field)
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1193
        if fields:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1194
            w(u'<table>')
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1195
            for field in fields:
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1196
                w(u'<tr>')
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1197
                w('<th>%s</th>' % self.render_label(form, field))
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1198
                w(u'<td style="width:100%;">')
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1199
                w(field.render(form, self))
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1200
                if display_help == True:
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 905
diff changeset
  1201
                    w(self.render_help(form, field))
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1202
                w(u'</td></tr>')
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1203
            w(u'</table>')
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1204
        for childform in getattr(form, 'forms', []):
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1205
            self.render_fields(w, childform, values)
892
1558772340a7 rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents: 888
diff changeset
  1206
        
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1207
    def render_buttons(self, w, form):
896
6e0379fc1988 put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 894
diff changeset
  1208
        w(u'<table class="formButtonBar">\n<tr>\n')
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 867
diff changeset
  1209
        for button in form.form_buttons():
896
6e0379fc1988 put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 894
diff changeset
  1210
            w(u'<td>%s</td>\n' % button)
6e0379fc1988 put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 894
diff changeset
  1211
        w(u'</tr></table>')
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1212
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1213
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1214
def stringfield_from_constraints(constraints, **kwargs):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1215
    field = None
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1216
    for cstr in constraints:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1217
        if isinstance(cstr, StaticVocabularyConstraint):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1218
            return StringField(widget=Select(vocabulary=cstr.vocabulary),
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1219
                               **kwargs)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1220
        if isinstance(cstr, SizeConstraint) and cstr.max is not None:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1221
            if cstr.max > 257:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1222
                field = textfield_from_constraint(cstr, **kwargs)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1223
            else:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1224
                field = StringField(max_length=cstr.max, **kwargs)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1225
    return field or TextField(**kwargs)
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 844
diff changeset
  1226
        
897
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1227
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1228
def textfield_from_constraint(constraint, **kwargs):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1229
    if 256 < constraint.max < 513:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1230
        rows, cols = 5, 60
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1231
    else:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1232
        rows, cols = 10, 80
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1233
    return TextField(rows, cols, **kwargs)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1234
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1235
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1236
def find_field(eclass, subjschema, rschema, role='subject'):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1237
    """return the most adapated widget to edit the relation
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1238
    'subjschema rschema objschema' according to information found in the schema
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1239
    """
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1240
    fieldclass = None
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1241
    if role == 'subject':
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1242
        objschema = rschema.objects(subjschema)[0]
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1243
        cardidx = 0
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1244
    else:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1245
        objschema = rschema.subjects(subjschema)[0]
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1246
        cardidx = 1
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1247
    card = rschema.rproperty(subjschema, objschema, 'cardinality')[cardidx]
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1248
    required = card in '1+'
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1249
    if rschema in eclass.widgets:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1250
        fieldclass = eclass.widgets[rschema]
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1251
        if isinstance(fieldclass, basestring):
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1252
            return StringField(name=rschema.type)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1253
    elif not rschema.is_final():
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1254
        return RelationField.fromcardinality(card, role,name=rschema.type,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1255
                                             required=required)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1256
    else:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1257
        fieldclass = FIELDS[objschema]
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1258
    if fieldclass is StringField:
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1259
        constraints = rschema.rproperty(subjschema, objschema, 'constraints')
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1260
        return stringfield_from_constraints(constraints, name=rschema.type,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1261
                                            required=required)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1262
    return fieldclass(name=rschema.type, required=required)
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1263
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1264
FIELDS = {
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1265
    'Boolean':  BooleanField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1266
    'Bytes':    FileField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1267
    'Date':     DateField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1268
    'Datetime': DateTimeField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1269
    'Int':      IntField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1270
    'Float':    FloatField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1271
    'Decimal':  StringField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1272
    'Password': StringField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1273
    'String' :  StringField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1274
    'Time':     TimeField,
f5b91f11d8b6 provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 896
diff changeset
  1275
    }