[refactoring] introduce add_etype_button function to properly create button to add a new entity stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 16 Sep 2011 16:42:42 +0200
branchstable
changeset 7806 aa30c665bd06
parent 7805 7111bb64b58f
child 7807 f2a5805615f8
child 7811 c468ff909e98
[refactoring] introduce add_etype_button function to properly create button to add a new entity also properly use cw_create_url to generate the URL to add the entity
web/action.py
web/views/__init__.py
web/views/actions.py
web/views/cwsources.py
web/views/cwuser.py
--- a/web/action.py	Fri Sep 16 17:10:33 2011 +0200
+++ b/web/action.py	Fri Sep 16 16:42:42 2011 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -141,7 +141,7 @@
             ttype = self.target_etype
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         linkto = '%s:%s:%s' % (self.rtype, entity.eid, target(self))
-        return self._cw.build_url('add/%s' % ttype, __linkto=linkto,
-                                  __redirectpath=entity.rest_path(),
+        return self._cw.vreg["etypes"].etype_class(ttype).cw_create_url(self._cw,
+                                  __redirectpath=entity.rest_path(), __linkto=linkto,
                                   __redirectvid=self._cw.form.get('__redirectvid', ''))
 
--- a/web/views/__init__.py	Fri Sep 16 17:10:33 2011 +0200
+++ b/web/views/__init__.py	Fri Sep 16 16:42:42 2011 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -22,7 +22,9 @@
 import os
 import sys
 import tempfile
+
 from rql import nodes
+from logilab.mtconverter import xml_escape
 
 
 def need_table_view(rset, schema):
@@ -113,6 +115,16 @@
     return "javascript: selectForAssociation('%s', '%s');" % (triplets, eid)
 
 
+def add_etype_button(req, etype, csscls='addButton right', **urlkwargs):
+    vreg = req.vreg
+    eschema = vreg.schema.eschema(etype)
+    if eschema.has_perm(req, 'add'):
+        url = vreg['etypes'].etype_class(etype).cw_create_url(req, **urlkwargs)
+        return u'<a href="%s" class="%s">%s</a>' % (
+            xml_escape(url), csscls, req.__('New %s' % etype))
+    return u''
+
+
 class TmpFileViewMixin(object):
     binary = True
     content_type = 'application/octet-stream'
--- a/web/views/actions.py	Fri Sep 16 17:10:33 2011 +0200
+++ b/web/views/actions.py	Fri Sep 16 16:42:42 2011 +0200
@@ -248,7 +248,7 @@
         return self._cw.__('add a %s' % self.rsettype) # generated msgid
 
     def url(self):
-        return self._cw.build_url('add/%s' % self.rsettype)
+        return self._cw.vreg["etypes"].etype_class(self.rsettype).cw_create_url(self._cw)
 
 
 class AddRelatedActions(action.Action):
@@ -319,8 +319,9 @@
                         yield rschema, teschema, role
 
     def linkto_url(self, entity, rtype, etype, target, **kwargs):
-        return self._cw.build_url('add/%s' % etype,
-                                  __linkto='%s:%s:%s' % (rtype, entity.eid, target), **kwargs)
+        return self._cw.vreg["etypes"].etype_class(etype).cw_create_url(
+                self._cw, __linkto='%s:%s:%s' % (rtype, entity.eid, target),
+                **kwargs)
 
 
 class ViewSameCWEType(action.Action):
--- a/web/views/cwsources.py	Fri Sep 16 17:10:33 2011 +0200
+++ b/web/views/cwsources.py	Fri Sep 16 16:42:42 2011 +0200
@@ -28,7 +28,7 @@
 from cubicweb.view import EntityView, StartupView
 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, display_name
 from cubicweb.web import uicfg, formwidgets as wdgs
-from cubicweb.web.views import tabs, actions
+from cubicweb.web.views import tabs, actions, add_etype_button
 
 
 _abaa = uicfg.actionbox_appearsin_addmenu
@@ -69,13 +69,9 @@
     def entity_call(self, entity):
         _ = self._cw._
         self.w('<h3>%s</h3>' % _('Entity and relation supported by this source'))
-        eschema = self._cw.vreg.schema.eschema('CWSourceSchemaConfig')
-        if eschema.has_perm(self._cw, 'add'):
-            self.w(u'<a href="%s" class="addButton right">%s</a>' % (
-                self._cw.build_url('add/%s' % eschema,
-                                   __linkto='cw_for_source:%s:subject' % entity.eid),
-                self._cw._('add a CWSourceSchemaConfig')))
-            self.w(u'<div class="clear"></div>')
+        self.w(add_etype_button(self._cw, 'CWSourceSchemaConfig',
+                                __linkto='cw_for_source:%s:subject' % entity.eid))
+        self.w(u'<div class="clear"></div>')
         rset = self._cw.execute(
             'Any X, SCH, XO ORDERBY ET WHERE X options XO, X cw_for_source S, S eid %(s)s, '
             'X cw_schema SCH, SCH is ET', {'s': entity.eid})
@@ -97,11 +93,11 @@
         checker = MAPPING_CHECKERS.get(entity.type, MappingChecker)(entity)
         checker.check()
         if (checker.errors or checker.warnings or checker.infos):
-                self.w('<h2>%s</h2>' % _('Detected problems'))
-                errors = zip(repeat(_('error')), checker.errors)
-                warnings = zip(repeat(_('warning')), checker.warnings)
-                infos = zip(repeat(_('warning')), checker.infos)
-                self.wview('pyvaltable', pyvalue=chain(errors, warnings, infos))
+            self.w('<h2>%s</h2>' % _('Detected problems'))
+            errors = zip(repeat(_('error')), checker.errors)
+            warnings = zip(repeat(_('warning')), checker.warnings)
+            infos = zip(repeat(_('warning')), checker.infos)
+            self.wview('pyvaltable', pyvalue=chain(errors, warnings, infos))
 
 
 class MappingChecker(object):
--- a/web/views/cwuser.py	Fri Sep 16 17:10:33 2011 +0200
+++ b/web/views/cwuser.py	Fri Sep 16 16:42:42 2011 +0200
@@ -29,7 +29,7 @@
 from cubicweb.selectors import one_line_rset, is_instance, match_user_groups
 from cubicweb.view import EntityView, StartupView
 from cubicweb.web import action, uicfg, formwidgets
-from cubicweb.web.views import tabs, tableview, actions
+from cubicweb.web.views import tabs, tableview, actions, add_etype_button
 
 _pvs = uicfg.primaryview_section
 _pvs.tag_attribute(('CWUser', 'login'), 'hidden')
@@ -182,12 +182,8 @@
 
     def call(self, **kwargs):
         self.w('<h1>%s</h1>' % self._cw._(self.title))
-        for etype in ('CWUser', 'CWGroup'):
-            eschema = self._cw.vreg.schema.eschema(etype)
-            if eschema.has_perm(self._cw, 'add'):
-                self.w(u'<a href="%s" class="addButton right">%s</a>' % (
-                    self._cw.build_url('add/%s' % eschema),
-                    self._cw.__('New %s' % etype).capitalize()))
+        self.w(add_etype_button(self._cw, 'CWUser'))
+        self.w(add_etype_button(self._cw, 'CWGroup'))
         self.w(u'<div class="clear"></div>')
         self.wview('cw.user-table', self._cw.execute(self.rql))