# HG changeset patch # User Sylvain Thénault # Date 1316184162 -7200 # Node ID aa30c665bd06afbe59ff7936a8c3fa99e78f0ef0 # Parent 7111bb64b58f2167c3da42bc00c781d451c6a1db [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 diff -r 7111bb64b58f -r aa30c665bd06 web/action.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', '')) diff -r 7111bb64b58f -r aa30c665bd06 web/views/__init__.py --- 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'%s' % ( + xml_escape(url), csscls, req.__('New %s' % etype)) + return u'' + + class TmpFileViewMixin(object): binary = True content_type = 'application/octet-stream' diff -r 7111bb64b58f -r aa30c665bd06 web/views/actions.py --- 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): diff -r 7111bb64b58f -r aa30c665bd06 web/views/cwsources.py --- 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('

%s

' % _('Entity and relation supported by this source')) - eschema = self._cw.vreg.schema.eschema('CWSourceSchemaConfig') - if eschema.has_perm(self._cw, 'add'): - self.w(u'%s' % ( - self._cw.build_url('add/%s' % eschema, - __linkto='cw_for_source:%s:subject' % entity.eid), - self._cw._('add a CWSourceSchemaConfig'))) - self.w(u'
') + self.w(add_etype_button(self._cw, 'CWSourceSchemaConfig', + __linkto='cw_for_source:%s:subject' % entity.eid)) + self.w(u'
') 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('

%s

' % _('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('

%s

' % _('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): diff -r 7111bb64b58f -r aa30c665bd06 web/views/cwuser.py --- 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('

%s

' % 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'%s' % ( - 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'
') self.wview('cw.user-table', self._cw.execute(self.rql))