web/views/cwproperties.py
author sylvain.thenault@logilab.fr
Wed, 29 Apr 2009 10:29:03 +0200
branchtls-sprint
changeset 1533 bcd4bfff658b
parent 1507 525965b1c23a
child 1537 788b5ca792bd
permissions -rw-r--r--
update rtags api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
     1
"""Specific views for CWProperty
0
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
984
536e421b082b import updates
sylvain.thenault@logilab.fr
parents: 940
diff changeset
     4
:copyright: 2007-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
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
     9
from logilab.mtconverter import html_escape
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    10
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    11
from logilab.common.decorators import cached
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    12
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    13
from cubicweb import UnknownProperty
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    14
from cubicweb.selectors import (one_line_rset, none_rset, implements,
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    15
                                match_user_groups, entity_implements)
940
15dcdc863965 fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 879
diff changeset
    16
from cubicweb.utils import UStringIO
984
536e421b082b import updates
sylvain.thenault@logilab.fr
parents: 940
diff changeset
    17
from cubicweb.view import StartupView
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1314
diff changeset
    18
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param, uicfg
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.web.views import baseviews
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    20
from cubicweb.web import stdmsgs
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1314
diff changeset
    21
from cubicweb.web.form import CompositeForm, EntityFieldsForm, FormViewMixIn
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    22
from cubicweb.web.formfields import FIELDS, StringField
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    23
from cubicweb.web.formwidgets import Select, Button, SubmitButton
1151
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    24
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    25
_ = unicode
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    26
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1507
diff changeset
    27
uicfg.rfields.tag_relation(PropertyKeyField, ('CWProperty', 'pkey', '*'), 'subject')
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1507
diff changeset
    28
uicfg.rfields.tag_relation(PropertyValueField, ('CWProperty', 'value', '*'), 'subject')
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1507
diff changeset
    29
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    30
# some string we want to be internationalizable for nicer display of eproperty
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    31
# groups
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    32
_('navigation')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    33
_('ui')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    34
_('actions')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    35
_('boxes')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    36
_('components')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    37
_('contentnavigation')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    39
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    40
def make_togglable_link(nodeid, label, cookiename):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    41
    """builds a HTML link that switches the visibility & remembers it"""
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    42
    action = u"javascript: toggle_and_remember_visibility('%s', '%s')" % \
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    43
        (nodeid, cookiename)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    44
    return u'<a href="%s">%s</a>' % (action, label)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    45
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    46
def css_class(someclass):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    47
    return someclass and 'class="%s"' % someclass or ''
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    48
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    49
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
    50
class CWPropertyPrimaryView(baseviews.PrimaryView):
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
    51
    __select__ = implements('CWProperty')
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    52
    skip_none = False
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    53
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    54
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1314
diff changeset
    55
class SystemEPropertiesForm(FormViewMixIn, StartupView):
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    56
    id = 'systemepropertiesform'
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    57
    __select__ = none_rset() & match_user_groups('managers')
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    58
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    59
    title = _('site configuration')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    60
    category = 'startupview'
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    61
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    62
    def linkable(self):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    63
        return True
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    64
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    65
    def url(self):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    66
        """return the url associated with this view. We can omit rql here"""
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    67
        return self.build_url('view', vid=self.id)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    68
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    69
    def _cookie_name(self, somestr):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    70
        return str('%s_property_%s' % (self.config.appid, somestr))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    71
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    72
    def _group_status(self, group, default=u'hidden'):
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
    73
        """return css class name 'hidden' (collapsed), or '' (open)"""
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    74
        cookies = self.req.get_cookie()
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    75
        cookiename = self._cookie_name(group)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    76
        cookie = cookies.get(cookiename)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    77
        if cookie is None:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    78
            cookies[cookiename] = default
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    79
            self.req.set_cookie(cookies, cookiename, maxage=None)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    80
            status = default
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    81
        else:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    82
            status = cookie.value
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    83
        return status
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    84
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    85
    def call(self, **kwargs):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    86
        """The default view representing the application's index"""
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1314
diff changeset
    87
        self.req.add_js('cubicweb.preferences.js')
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    88
        self.req.add_css('cubicweb.preferences.css')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    89
        vreg = self.vreg
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    90
        values = self.defined_keys
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    91
        groupedopts = {}
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    92
        mainopts = {}
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    93
        # "self.id=='systemepropertiesform'" to skip site wide properties on
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    94
        # user's preference but not site's configuration
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    95
        for key in vreg.user_property_keys(self.id=='systemepropertiesform'):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    96
            parts = key.split('.')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    97
            if parts[0] in vreg:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    98
                # appobject configuration
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
    99
                reg, oid, propid = parts
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   100
                groupedopts.setdefault(reg, {}).setdefault(oid, []).append(key)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   101
            else:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   102
                mainopts.setdefault(parts[0], []).append(key)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   103
        # precompute form to consume error message
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   104
        for group, keys in mainopts.items():
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   105
            mainopts[group] = self.form(keys, True)
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   106
        for group, objects in groupedopts.items():
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   107
            for oid, keys in objects.items():
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   108
                groupedopts[group][oid] = self.form(keys, True)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   109
        w = self.w
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   110
        req = self.req
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   111
        _ = req._
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   112
        w(u'<h1>%s</h1>\n' % _(self.title))
1365
39a3bc7899d5 avoid duplicated progress div
sylvain.thenault@logilab.fr
parents: 1320
diff changeset
   113
        # we don't want this in each sub-forms
39a3bc7899d5 avoid duplicated progress div
sylvain.thenault@logilab.fr
parents: 1320
diff changeset
   114
        w(u'<div id="progress">%s</div>' % self.req._('validating...'))
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   115
        for label, group, form in sorted((_(g), g, f)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   116
                                         for g, f in mainopts.iteritems()):
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   117
            status = css_class(self._group_status(group))
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   118
            w(u'<h2 class="propertiesform">%s</h2>\n' %
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   119
              (make_togglable_link('fieldset_' + group, label,
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   120
                                   self._cookie_name(group))))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   121
            w(u'<div id="fieldset_%s" %s>' % (group, status))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   122
            w(form)
1320
c85f4d8eff8b cleanup
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   123
            w(u'</div>')
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   124
        for label, group, objects in sorted((_(g), g, o)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   125
                                            for g, o in groupedopts.iteritems()):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   126
            status = css_class(self._group_status(group))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   127
            w(u'<h2 class="propertiesform">%s</h2>\n' %
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   128
              (make_togglable_link('fieldset_' + group, label,
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   129
                                   self._cookie_name(group))))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   130
            w(u'<div id="fieldset_%s" %s>' % (group, status))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   131
            for label, oid, form in sorted((self.req.__('%s_%s' % (group, o)), o, f)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   132
                                           for o, f in objects.iteritems()):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   133
                w(u'<fieldset class="subentity">')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   134
                w(u'<legend class="componentTitle">%s</legend>\n' % label)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   135
                docmsgid = '%s_%s_description' % (group, oid)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   136
                doc = _(docmsgid)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   137
                if doc != docmsgid:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   138
                    w(u'<p class="description">%s</p>' % html_escape(doc))
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   139
                w(form)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   140
                w(u'</fieldset>')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   141
            w(u'</div>')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   142
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   143
    @property
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   144
    @cached
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   145
    def eprops_rset(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   146
        return self.req.execute('Any P,K,V WHERE P is CWProperty, P pkey K, '
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   147
                                'P value V, NOT P for_user U')
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   148
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   149
    @property
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   150
    def defined_keys(self):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   151
        values = {}
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   152
        for i, entity in enumerate(self.eprops_rset.entities()):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   153
            values[entity.pkey] = i
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   154
        return values
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   155
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   156
    def entity_for_key(self, key):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   157
        values = self.defined_keys
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   158
        if key in values:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   159
            entity = self.eprops_rset.get_entity(values[key], 0)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   160
        else:
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   161
            entity = self.vreg.etype_class('CWProperty')(self.req, None, None)
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   162
            entity.eid = self.req.varmaker.next()
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   163
            entity['pkey'] = key
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   164
            entity['value'] = self.vreg.property_value(key)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   165
        return entity
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   166
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   167
    def form(self, keys, splitlabel=False):
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   168
        buttons = [SubmitButton(),
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   169
                   Button(stdmsgs.BUTTON_CANCEL, cwaction='cancel')]
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   170
        form = CompositeForm(self.req, domid=None, action=self.build_url(),
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   171
                             form_buttons=buttons,
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   172
                             submitmsg=self.req._('changes applied'))
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   173
        path = self.req.relative_path()
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   174
        if '?' in path:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   175
            path, params = path.split('?', 1)
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   176
            form.form_add_hidden('__redirectparams', params)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   177
        form.form_add_hidden('__redirectpath', path)
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   178
        for key in keys:
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   179
            self.form_row(form, key, splitlabel)
1365
39a3bc7899d5 avoid duplicated progress div
sylvain.thenault@logilab.fr
parents: 1320
diff changeset
   180
        return form.form_render(display_progress_div=False)
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   181
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   182
    def form_row(self, form, key, splitlabel):
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   183
        entity = self.entity_for_key(key)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   184
        if splitlabel:
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   185
            label = key.split('.')[-1]
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   186
        else:
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   187
            label = key
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   188
        subform = EntityFieldsForm(self.req, entity=entity, set_error_url=False)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   189
        subform.append_field(PropertyValueField(name='value', label=label,
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   190
                                                eidparam=True))
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   191
        subform.vreg = self.vreg
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   192
        subform.form_add_hidden('pkey', key, eidparam=True)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   193
        form.form_add_subform(subform)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   194
        return subform
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   195
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   196
879
329fd038c5a3 fix custom selector
sylvain.thenault@logilab.fr
parents: 792
diff changeset
   197
def is_user_prefs(cls, req, rset, row=None, col=0, **kwargs):
329fd038c5a3 fix custom selector
sylvain.thenault@logilab.fr
parents: 792
diff changeset
   198
    return req.user.eid == rset[row or 0][col]
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   199
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   200
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   201
class EPropertiesForm(SystemEPropertiesForm):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   202
    id = 'epropertiesform'
728
a95b284150d1 first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 691
diff changeset
   203
    __select__ = (
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   204
        # we don't want guests to be able to come here
792
685f020f855f fix selector
sylvain.thenault@logilab.fr
parents: 731
diff changeset
   205
        match_user_groups('users', 'managers') &
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   206
        (none_rset() | ((one_line_rset() & is_user_prefs) &
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   207
                        (one_line_rset() & match_user_groups('managers'))))
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   208
        )
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   209
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   210
    title = _('preferences')
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   211
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   212
    @property
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   213
    def user(self):
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   214
        if self.rset is None:
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   215
            return self.req.user
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   216
        return self.rset.get_entity(self.row or 0, self.col or 0)
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   217
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   218
    @property
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   219
    @cached
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   220
    def eprops_rset(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   221
        return self.req.execute('Any P,K,V WHERE P is CWProperty, P pkey K, P value V,'
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   222
                                'P for_user U, U eid %(x)s', {'x': self.user.eid})
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   223
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   224
    def form_row(self, form, key, splitlabel):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   225
        subform = super(EPropertiesForm, self).form_row(form, key, splitlabel)
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   226
        # if user is in the managers group and the property is being created,
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   227
        # we have to set for_user explicitly
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   228
        if not subform.edited_entity.has_eid() and self.user.matching_groups('managers'):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   229
            subform.form_add_hidden('for_user', self.user.eid, eidparam=True)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   230
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   231
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   232
# eproperty form objects ######################################################
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   233
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   234
class PlaceHolderWidget(object):
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   235
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   236
    def render(self, form, field):
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   237
        domid = form.context[field]['id']
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   238
        # empty span as well else html validation fail (label is refering to
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   239
        # this id)
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   240
        return '<div id="div:%s"><span id="%s">%s</span></div>' % (
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   241
            domid, domid, form.req._('select a key first'))
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   242
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   243
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   244
class NotEditableWidget(object):
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   245
    def __init__(self, value, msg=None):
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   246
        self.value = value
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   247
        self.msg = msg
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   248
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   249
    def render(self, form, field):
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   250
        domid = form.context[field]['id']
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   251
        value = '<span class="value" id="%s">%s</span>' % (domid, self.value)
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   252
        if self.msg:
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   253
            value + '<div class="helper">%s</div>' % self.msg
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   254
        return value
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   255
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   256
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   257
class PropertyKeyField(StringField):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   258
    """specific field for CWProperty.pkey to set the value widget according to
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   259
    the selected key
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   260
    """
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   261
    widget = Select
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   262
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   263
    def render(self, form, renderer):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   264
        wdg = self.get_widget(form)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   265
        wdg.attrs['tabindex'] = form.req.next_tabindex()
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   266
        wdg.attrs['onchange'] = "javascript:setPropValueWidget('%s', %s)" % (
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   267
            form.edited_entity.eid, form.req.next_tabindex())
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   268
        return wdg.render(form, self)
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   269
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   270
    def vocabulary(self, form):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   271
        entity = form.edited_entity
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   272
        _ = form.req._
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   273
        if entity.has_eid():
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   274
            return [(_(entity.pkey), entity.pkey)]
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   275
        # key beginning with 'system.' should usually not be edited by hand
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   276
        choices = entity.vreg.user_property_keys()
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   277
        return [(u'', u'')] + sorted(zip((_(v) for v in choices), choices))
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   278
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   279
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   280
class PropertyValueField(StringField):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   281
    """specific field for CWProperty.value  which will be different according to
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   282
    the selected key type and vocabulary information
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   283
    """
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   284
    widget = PlaceHolderWidget
1507
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   285
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   286
    def render(self, form, renderer=None, tabindex=None):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   287
        wdg = self.get_widget(form)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   288
        if tabindex is not None:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   289
            wdg.attrs['tabindex'] = tabindex
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   290
        return wdg.render(form, self)
691
35133d86970b move EProperty management views into eproperties.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   291
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   292
    def form_init(self, form):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   293
        entity = form.edited_entity
1314
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   294
        if not (entity.has_eid() or 'pkey' in entity):
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   295
            # no key set yet, just include an empty div which will be filled
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   296
            # on key selection
dc5499bff1a9 fix primary edition of eproperty (weird js pb remaining though)
sylvain.thenault@logilab.fr
parents: 1309
diff changeset
   297
            return
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   298
        try:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   299
            pdef = form.vreg.property_info(entity.pkey)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   300
        except UnknownProperty, ex:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   301
            self.warning('%s (you should probably delete that property '
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   302
                         'from the database)', ex)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   303
            msg = form.req._('you should probably delete that property')
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   304
            self.widget = NotEditableWidget(entity.printable_value('value'),
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   305
                                            '%s (%s)' % (msg, ex))
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   306
        if entity.pkey.startswith('system.'):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   307
            msg = form.req._('value associated to this key is not editable '
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   308
                             'manually')
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   309
            self.widget = NotEditableWidget(entity.printable_value('value'), msg)
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1365
diff changeset
   310
        # XXX race condition when used from CWPropertyForm, should not rely on
1309
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   311
        # instance attributes
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   312
        self.initial = pdef['default']
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   313
        self.help = pdef['help']
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   314
        vocab = pdef['vocabulary']
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   315
        if vocab is not None:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   316
            if callable(vocab):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   317
                # list() just in case its a generator function
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   318
                self.choices = list(vocab(form.req))
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   319
            else:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   320
                self.choices = vocab
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   321
            wdg = Select()
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   322
        else:
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   323
            wdg = FIELDS[pdef['type']].widget()
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   324
            if pdef['type'] == 'Boolean':
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   325
                self.choices = [(form.req._('yes'), '1'), (form.req._('no'), '')]
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   326
            elif pdef['type'] in ('Float', 'Int'):
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   327
                wdg.attrs.setdefault('size', 3)
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   328
        self.widget = wdg
a4eb20f86cb0 eproperty forms handling
sylvain.thenault@logilab.fr
parents: 1153
diff changeset
   329