web/views/eproperties.py
branchtls-sprint
changeset 1398 5fe84a5f7035
parent 1365 39a3bc7899d5
child 1498 2c6eec0b46b9
equal deleted inserted replaced
1397:6cbc7bc8ea6d 1398:5fe84a5f7035
     1 """Specific views for EProperty
     1 """Specific views for CWProperty
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
    43 
    43 
    44 def css_class(someclass):
    44 def css_class(someclass):
    45     return someclass and 'class="%s"' % someclass or ''
    45     return someclass and 'class="%s"' % someclass or ''
    46 
    46 
    47 
    47 
    48 class EPropertyPrimaryView(baseviews.PrimaryView):
    48 class CWPropertyPrimaryView(baseviews.PrimaryView):
    49     __select__ = implements('EProperty')
    49     __select__ = implements('CWProperty')
    50     skip_none = False
    50     skip_none = False
    51 
    51 
    52 
    52 
    53 class SystemEPropertiesForm(FormViewMixIn, StartupView):
    53 class SystemEPropertiesForm(FormViewMixIn, StartupView):
    54     id = 'systemepropertiesform'
    54     id = 'systemepropertiesform'
   139             w(u'</div>')
   139             w(u'</div>')
   140 
   140 
   141     @property
   141     @property
   142     @cached
   142     @cached
   143     def eprops_rset(self):
   143     def eprops_rset(self):
   144         return self.req.execute('Any P,K,V WHERE P is EProperty, P pkey K, '
   144         return self.req.execute('Any P,K,V WHERE P is CWProperty, P pkey K, '
   145                                 'P value V, NOT P for_user U')
   145                                 'P value V, NOT P for_user U')
   146 
   146 
   147     @property
   147     @property
   148     def defined_keys(self):
   148     def defined_keys(self):
   149         values = {}
   149         values = {}
   154     def entity_for_key(self, key):
   154     def entity_for_key(self, key):
   155         values = self.defined_keys
   155         values = self.defined_keys
   156         if key in values:
   156         if key in values:
   157             entity = self.eprops_rset.get_entity(values[key], 0)
   157             entity = self.eprops_rset.get_entity(values[key], 0)
   158         else:
   158         else:
   159             entity = self.vreg.etype_class('EProperty')(self.req, None, None)
   159             entity = self.vreg.etype_class('CWProperty')(self.req, None, None)
   160             entity.eid = self.req.varmaker.next()
   160             entity.eid = self.req.varmaker.next()
   161             entity['pkey'] = key
   161             entity['pkey'] = key
   162             entity['value'] = self.vreg.property_value(key)
   162             entity['value'] = self.vreg.property_value(key)
   163         return entity
   163         return entity
   164 
   164 
   214         return self.rset.get_entity(self.row or 0, self.col or 0)
   214         return self.rset.get_entity(self.row or 0, self.col or 0)
   215 
   215 
   216     @property
   216     @property
   217     @cached
   217     @cached
   218     def eprops_rset(self):
   218     def eprops_rset(self):
   219         return self.req.execute('Any P,K,V WHERE P is EProperty, P pkey K, P value V,'
   219         return self.req.execute('Any P,K,V WHERE P is CWProperty, P pkey K, P value V,'
   220                                 'P for_user U, U eid %(x)s', {'x': self.user.eid})
   220                                 'P for_user U, U eid %(x)s', {'x': self.user.eid})
   221 
   221 
   222     def form_row(self, form, key, splitlabel):
   222     def form_row(self, form, key, splitlabel):
   223         subform = super(EPropertiesForm, self).form_row(form, key, splitlabel)
   223         subform = super(EPropertiesForm, self).form_row(form, key, splitlabel)
   224         # if user is in the managers group and the property is being created,
   224         # if user is in the managers group and the property is being created,
   251             value + '<div class="helper">%s</div>' % self.msg
   251             value + '<div class="helper">%s</div>' % self.msg
   252         return value
   252         return value
   253 
   253 
   254 
   254 
   255 class PropertyKeyField(StringField):
   255 class PropertyKeyField(StringField):
   256     """specific field for EProperty.pkey to set the value widget according to
   256     """specific field for CWProperty.pkey to set the value widget according to
   257     the selected key
   257     the selected key
   258     """
   258     """
   259     widget = Select
   259     widget = Select
   260     
   260     
   261     def render(self, form, renderer):
   261     def render(self, form, renderer):
   274         choices = entity.vreg.user_property_keys()
   274         choices = entity.vreg.user_property_keys()
   275         return [(u'', u'')] + sorted(zip((_(v) for v in choices), choices))
   275         return [(u'', u'')] + sorted(zip((_(v) for v in choices), choices))
   276 
   276 
   277 
   277 
   278 class PropertyValueField(StringField):
   278 class PropertyValueField(StringField):
   279     """specific field for EProperty.value  which will be different according to
   279     """specific field for CWProperty.value  which will be different according to
   280     the selected key type and vocabulary information
   280     the selected key type and vocabulary information
   281     """
   281     """
   282     widget = PlaceHolderWidget
   282     widget = PlaceHolderWidget
   283     
   283     
   284     def render(self, form, renderer=None, tabindex=None):
   284     def render(self, form, renderer=None, tabindex=None):
   303                                             '%s (%s)' % (msg, ex))
   303                                             '%s (%s)' % (msg, ex))
   304         if entity.pkey.startswith('system.'):
   304         if entity.pkey.startswith('system.'):
   305             msg = form.req._('value associated to this key is not editable '
   305             msg = form.req._('value associated to this key is not editable '
   306                              'manually')
   306                              'manually')
   307             self.widget = NotEditableWidget(entity.printable_value('value'), msg)
   307             self.widget = NotEditableWidget(entity.printable_value('value'), msg)
   308         # XXX race condition when used from EPropertyForm, should not rely on
   308         # XXX race condition when used from CWPropertyForm, should not rely on
   309         # instance attributes
   309         # instance attributes
   310         self.initial = pdef['default']
   310         self.initial = pdef['default']
   311         self.help = pdef['help']
   311         self.help = pdef['help']
   312         vocab = pdef['vocabulary']
   312         vocab = pdef['vocabulary']
   313         if vocab is not None:
   313         if vocab is not None:
   323                 self.choices = [(form.req._('yes'), '1'), (form.req._('no'), '')]
   323                 self.choices = [(form.req._('yes'), '1'), (form.req._('no'), '')]
   324             elif pdef['type'] in ('Float', 'Int'):
   324             elif pdef['type'] in ('Float', 'Int'):
   325                 wdg.attrs.setdefault('size', 3)
   325                 wdg.attrs.setdefault('size', 3)
   326         self.widget = wdg
   326         self.widget = wdg
   327 
   327 
   328 uicfg.rfields.set_rtag(PropertyKeyField, 'pkey', 'subject', 'EProperty')
   328 uicfg.rfields.set_rtag(PropertyKeyField, 'pkey', 'subject', 'CWProperty')
   329 uicfg.rfields.set_rtag(PropertyValueField, 'value', 'subject', 'EProperty')
   329 uicfg.rfields.set_rtag(PropertyValueField, 'value', 'subject', 'CWProperty')
   330     
   330