web/views/startup.py
changeset 2181 94ca417b9b07
parent 2126 a25859917ccc
parent 2158 a2f2430dcd50
child 2234 1fbcf202882d
equal deleted inserted replaced
2144:51c84d585456 2181:94ca417b9b07
    12 from logilab.common.textutils import unormalize
    12 from logilab.common.textutils import unormalize
    13 from logilab.mtconverter import html_escape
    13 from logilab.mtconverter import html_escape
    14 
    14 
    15 from cubicweb.view import StartupView
    15 from cubicweb.view import StartupView
    16 from cubicweb.selectors import match_user_groups, implements
    16 from cubicweb.selectors import match_user_groups, implements
       
    17 from cubicweb.schema import META_RELATIONS_TYPES
    17 from cubicweb.common.uilib import ureport_as_html
    18 from cubicweb.common.uilib import ureport_as_html
    18 from cubicweb.web import ajax_replace_url, uicfg, httpcache
    19 from cubicweb.web import ajax_replace_url, uicfg, httpcache
    19 from cubicweb.web.views import tabs, management, schema
    20 from cubicweb.web.views import tabs, management, schema as schemamod
    20 
    21 
    21 class ManageView(StartupView):
    22 class ManageView(StartupView):
    22     id = 'manage'
    23     id = 'manage'
    23     title = _('manage')
    24     title = _('manage')
    24     http_cache_manager = httpcache.EtagHTTPCacheManager
    25     http_cache_manager = httpcache.EtagHTTPCacheManager
   173         self.req.add_js('cubicweb.ajax.js')
   174         self.req.add_js('cubicweb.ajax.js')
   174         self.req.add_css(('cubicweb.schema.css','cubicweb.acl.css'))
   175         self.req.add_css(('cubicweb.schema.css','cubicweb.acl.css'))
   175         self.w(u'<h1>%s</h1>' % _('Schema of the data model'))
   176         self.w(u'<h1>%s</h1>' % _('Schema of the data model'))
   176         self.render_tabs(self.tabs, self.default_tab)
   177         self.render_tabs(self.tabs, self.default_tab)
   177 
   178 
       
   179 
   178 class SchemaTabImageView(StartupView):
   180 class SchemaTabImageView(StartupView):
   179     id = 'schema-image'
   181     id = 'schema-image'
   180 
   182 
   181     def call(self):
   183     def call(self):
   182         self.w(_(u'<div>This schema of the data model <em>excludes</em> the '
   184         self.w(_(u'<div>This schema of the data model <em>excludes</em> the '
   183                  u'meta-data, but you can also display a <a href="%s">complete '
   185                  u'meta-data, but you can also display a <a href="%s">complete '
   184                  u'schema with meta-data</a>.</div>')
   186                  u'schema with meta-data</a>.</div>')
   185                % html_escape(self.build_url('view', vid='schemagraph', withmeta=1)))
   187                % html_escape(self.build_url('view', vid='schemagraph', skipmeta=0)))
   186         self.w(u'<img src="%s" alt="%s"/>\n' % (
   188         self.w(u'<img src="%s" alt="%s"/>\n' % (
   187             html_escape(self.req.build_url('view', vid='schemagraph', withmeta=0)),
   189             html_escape(self.req.build_url('view', vid='schemagraph', skipmeta=1)),
   188             self.req._("graphical representation of the application'schema")))
   190             self.req._("graphical representation of the application'schema")))
       
   191 
   189 
   192 
   190 class SchemaTabTextView(StartupView):
   193 class SchemaTabTextView(StartupView):
   191     id = 'schema-text'
   194     id = 'schema-text'
   192 
   195 
   193     def call(self):
   196     def call(self):
   201                                 'X meta M, X final F')
   204                                 'X meta M, X final F')
   202         self.wview('editable-table', rset, displayfilter=True)
   205         self.wview('editable-table', rset, displayfilter=True)
   203 
   206 
   204 
   207 
   205 class ManagerSchemaPermissionsView(StartupView, management.SecurityViewMixIn):
   208 class ManagerSchemaPermissionsView(StartupView, management.SecurityViewMixIn):
   206     id = 'schema_security'
   209     id = 'schema-security'
   207     __select__ = StartupView.__select__ & match_user_groups('managers')
   210     __select__ = StartupView.__select__ & match_user_groups('managers')
   208 
   211 
   209     def call(self, display_relations=True,
   212     def call(self, display_relations=True):
   210              skiprels=('is', 'is_instance_of', 'identity', 'owned_by', 'created_by')):
   213         self.req.add_css('cubicweb.acl.css')
   211         _ = self.req._
   214         skiptypes = schemamod.skip_types(self.req)
   212         formparams = {}
   215         formparams = {}
   213         formparams['sec'] = self.id
   216         formparams['sec'] = self.id
   214         formparams['withmeta'] = int(self.req.form.get('withmeta', True))
   217         if not skiptypes:
       
   218             formparams['skipmeta'] = u'0'
   215         schema = self.schema
   219         schema = self.schema
   216         # compute entities
   220         # compute entities
   217         entities = [eschema for eschema in schema.entities()
   221         entities = sorted(eschema for eschema in schema.entities()
   218                    if not eschema.is_final()]
   222                           if not (eschema.is_final() or eschema in skiptypes))
   219         if not formparams['withmeta']:
       
   220             entities = [eschema for eschema in entities
       
   221                         if not eschema.schema_entity()]
       
   222         # compute relations
   223         # compute relations
   223         relations = []
       
   224         if display_relations:
   224         if display_relations:
   225             relations = [rschema for rschema in schema.relations()
   225             relations = sorted(rschema for rschema in schema.relations()
   226                          if not (rschema.is_final() or rschema.type in skiprels)]
   226                                if not (rschema.is_final()
   227             if not formparams['withmeta']:
   227                                        or rschema in skiptypes
   228                 relations = [rschema for rschema in relations
   228                                        or rschema in META_RELATIONS_TYPES))
   229                              if not rschema.schema_relation()]
   229         else:
       
   230             relations = []
   230         # index
   231         # index
       
   232         _ = self.req._
   231         self.w(u'<div id="schema_security"><a id="index" href="index"/>')
   233         self.w(u'<div id="schema_security"><a id="index" href="index"/>')
   232         self.w(u'<h2 class="schema">%s</h2>' % _('index').capitalize())
   234         self.w(u'<h2 class="schema">%s</h2>' % _('index').capitalize())
   233         self.w(u'<h4>%s</h4>' %   _('Entities').capitalize())
   235         self.w(u'<h4>%s</h4>' %   _('Entities').capitalize())
   234         ents = []
   236         ents = []
   235         for eschema in sorted(entities):
   237         for eschema in entities:
   236             url = html_escape(self.build_url('schema', **formparams) + '#' + eschema.type)
   238             url = html_escape(self.build_url('schema', **formparams))
   237             ents.append(u'<a class="grey" href="%s">%s</a> (%s)' % (url,  eschema.type, _(eschema.type)))
   239             ents.append(u'<a class="grey" href="%s#%s">%s</a> (%s)' % (
   238         self.w('%s' %  ', '.join(ents))
   240                 url,  eschema.type, eschema.type, _(eschema.type)))
       
   241         self.w(u', '.join(ents))
   239         self.w(u'<h4>%s</h4>' % (_('relations').capitalize()))
   242         self.w(u'<h4>%s</h4>' % (_('relations').capitalize()))
   240         rels = []
   243         rels = []
   241         for eschema in sorted(relations):
   244         for rschema in relations:
   242             url = html_escape(self.build_url('schema', **formparams) + '#' + eschema.type)
   245             url = html_escape(self.build_url('schema', **formparams))
   243             rels.append(u'<a class="grey" href="%s">%s</a> (%s), ' %  (url , eschema.type, _(eschema.type)))
   246             rels.append(u'<a class="grey" href="%s#%s">%s</a> (%s), ' %  (
   244         self.w('%s' %  ', '.join(ents))
   247                 url , rschema.type, rschema.type, _(rschema.type)))
       
   248         self.w(u', '.join(ents))
   245         # entities
   249         # entities
   246         self.display_entities(entities, formparams)
   250         self.display_entities(entities, formparams)
   247         # relations
   251         # relations
   248         if relations:
   252         if relations:
   249             self.display_relations(relations, formparams)
   253             self.display_relations(relations, formparams)
   251 
   255 
   252     def display_entities(self, entities, formparams):
   256     def display_entities(self, entities, formparams):
   253         _ = self.req._
   257         _ = self.req._
   254         self.w(u'<a id="entities" href="entities"/>')
   258         self.w(u'<a id="entities" href="entities"/>')
   255         self.w(u'<h2 class="schema">%s</h2>' % _('permissions for entities').capitalize())
   259         self.w(u'<h2 class="schema">%s</h2>' % _('permissions for entities').capitalize())
   256         for eschema in sorted(entities):
   260         for eschema in entities:
   257             self.w(u'<a id="%s" href="%s"/>' %  (eschema.type, eschema.type))
   261             self.w(u'<a id="%s" href="%s"/>' %  (eschema.type, eschema.type))
   258             self.w(u'<h3 class="schema">%s (%s) ' % (eschema.type, _(eschema.type)))
   262             self.w(u'<h3 class="schema">%s (%s) ' % (eschema.type, _(eschema.type)))
   259             url = html_escape(self.build_url('schema', **formparams) + '#index')
   263             url = html_escape(self.build_url('schema', **formparams) + '#index')
   260             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
   264             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
   261                 url,  self.req.external_resource('UP_ICON'), _('up')))
   265                 url,  self.req.external_resource('UP_ICON'), _('up')))
   278 
   282 
   279     def display_relations(self, relations, formparams):
   283     def display_relations(self, relations, formparams):
   280         _ = self.req._
   284         _ = self.req._
   281         self.w(u'<a id="relations" href="relations"/>')
   285         self.w(u'<a id="relations" href="relations"/>')
   282         self.w(u'<h2 class="schema">%s </h2>' % _('permissions for relations').capitalize())
   286         self.w(u'<h2 class="schema">%s </h2>' % _('permissions for relations').capitalize())
   283         for rschema in sorted(relations):
   287         for rschema in relations:
   284             self.w(u'<a id="%s" href="%s"/>' %  (rschema.type, rschema.type))
   288             self.w(u'<a id="%s" href="%s"/>' %  (rschema.type, rschema.type))
   285             self.w(u'<h3 class="schema">%s (%s) ' % (rschema.type, _(rschema.type)))
   289             self.w(u'<h3 class="schema">%s (%s) ' % (rschema.type, _(rschema.type)))
   286             url = html_escape(self.build_url('schema', **formparams) + '#index')
   290             url = html_escape(self.build_url('schema', **formparams) + '#index')
   287             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
   291             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
   288                 url,  self.req.external_resource('UP_ICON'), _('up')))
   292                 url,  self.req.external_resource('UP_ICON'), _('up')))