web/views/startup.py
changeset 2451 b44611f734ba
parent 2436 44b2eea35efa
child 2476 1294a6bdf3bf
equal deleted inserted replaced
2450:d1529b46b638 2451:b44611f734ba
    12 from logilab.common.textutils import unormalize
    12 from logilab.common.textutils import unormalize
    13 from logilab.mtconverter import xml_escape
    13 from logilab.mtconverter import xml_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, display_name
    17 from cubicweb.schema import display_name
    18 from cubicweb.common.uilib import ureport_as_html
       
    19 from cubicweb.web import ajax_replace_url, uicfg, httpcache
    18 from cubicweb.web import ajax_replace_url, uicfg, httpcache
    20 from cubicweb.web.views import tabs, management, schema as schemamod
       
    21 
    19 
    22 class ManageView(StartupView):
    20 class ManageView(StartupView):
    23     id = 'manage'
    21     id = 'manage'
    24     title = _('manage')
    22     title = _('manage')
    25     http_cache_manager = httpcache.EtagHTTPCacheManager
    23     http_cache_manager = httpcache.EtagHTTPCacheManager
   161     title = _('view_index')
   159     title = _('view_index')
   162 
   160 
   163     def display_folders(self):
   161     def display_folders(self):
   164         return 'Folder' in self.schema and self.req.execute('Any COUNT(X) WHERE X is Folder')[0][0]
   162         return 'Folder' in self.schema and self.req.execute('Any COUNT(X) WHERE X is Folder')[0][0]
   165 
   163 
   166 class SchemaView(tabs.TabsMixin, StartupView):
       
   167     id = 'schema'
       
   168     title = _('application schema')
       
   169     tabs = [_('schema-text'), _('schema-image')]
       
   170     default_tab = 'schema-text'
       
   171 
       
   172     def call(self):
       
   173         """display schema information"""
       
   174         self.req.add_js('cubicweb.ajax.js')
       
   175         self.req.add_css(('cubicweb.schema.css','cubicweb.acl.css'))
       
   176         self.w(u'<h1>%s</h1>' % _('Schema of the data model'))
       
   177         self.render_tabs(self.tabs, self.default_tab)
       
   178 
       
   179 
       
   180 class SchemaTabImageView(StartupView):
       
   181     id = 'schema-image'
       
   182 
       
   183     def call(self):
       
   184         self.w(_(u'<div>This schema of the data model <em>excludes</em> the '
       
   185                  u'meta-data, but you can also display a <a href="%s">complete '
       
   186                  u'schema with meta-data</a>.</div>')
       
   187                % xml_escape(self.build_url('view', vid='schemagraph', withmeta=1)))
       
   188         self.w(u'<img src="%s" alt="%s"/>\n' % (
       
   189             xml_escape(self.req.build_url('view', vid='schemagraph', skipmeta=1)),
       
   190             self.req._("graphical representation of the application'schema")))
       
   191 
       
   192 
       
   193 class SchemaTabTextView(StartupView):
       
   194     id = 'schema-text'
       
   195 
       
   196     def call(self):
       
   197         rset = self.req.execute('Any X ORDERBY N WHERE X is CWEType, X name N, '
       
   198                                 'X final FALSE')
       
   199         self.wview('table', rset, displayfilter=True)
       
   200 
       
   201 
       
   202 class ManagerSchemaPermissionsView(StartupView, management.SecurityViewMixIn):
       
   203     id = 'schema-security'
       
   204     __select__ = StartupView.__select__ & match_user_groups('managers')
       
   205 
       
   206     def call(self, display_relations=True):
       
   207         self.req.add_css('cubicweb.acl.css')
       
   208         skiptypes = schemamod.skip_types(self.req)
       
   209         formparams = {}
       
   210         formparams['sec'] = self.id
       
   211         if not skiptypes:
       
   212             formparams['skipmeta'] = u'0'
       
   213         schema = self.schema
       
   214         # compute entities
       
   215         entities = sorted(eschema for eschema in schema.entities()
       
   216                           if not (eschema.is_final() or eschema in skiptypes))
       
   217         # compute relations
       
   218         if display_relations:
       
   219             relations = sorted(rschema for rschema in schema.relations()
       
   220                                if not (rschema.is_final()
       
   221                                        or rschema in skiptypes
       
   222                                        or rschema in META_RELATIONS_TYPES))
       
   223         else:
       
   224             relations = []
       
   225         # index
       
   226         _ = self.req._
       
   227         self.w(u'<div id="schema_security"><a id="index" href="index"/>')
       
   228         self.w(u'<h2 class="schema">%s</h2>' % _('index').capitalize())
       
   229         self.w(u'<h4>%s</h4>' %   _('Entities').capitalize())
       
   230         ents = []
       
   231         for eschema in sorted(entities):
       
   232             url = xml_escape(self.build_url('schema', **formparams))
       
   233             ents.append(u'<a class="grey" href="%s#%s">%s</a> (%s)' % (
       
   234                 url,  eschema.type, eschema.type, _(eschema.type)))
       
   235         self.w(u', '.join(ents))
       
   236         self.w(u'<h4>%s</h4>' % (_('relations').capitalize()))
       
   237         rels = []
       
   238         for rschema in sorted(relations):
       
   239             url = xml_escape(self.build_url('schema', **formparams))
       
   240             rels.append(u'<a class="grey" href="%s#%s">%s</a> (%s), ' %  (
       
   241                 url , rschema.type, rschema.type, _(rschema.type)))
       
   242         self.w(u', '.join(ents))
       
   243         # entities
       
   244         self.display_entities(entities, formparams)
       
   245         # relations
       
   246         if relations:
       
   247             self.display_relations(relations, formparams)
       
   248         self.w(u'</div>')
       
   249 
       
   250     def display_entities(self, entities, formparams):
       
   251         _ = self.req._
       
   252         self.w(u'<a id="entities" href="entities"/>')
       
   253         self.w(u'<h2 class="schema">%s</h2>' % _('permissions for entities').capitalize())
       
   254         for eschema in entities:
       
   255             self.w(u'<a id="%s" href="%s"/>' %  (eschema.type, eschema.type))
       
   256             self.w(u'<h3 class="schema">%s (%s) ' % (eschema.type, _(eschema.type)))
       
   257             url = xml_escape(self.build_url('schema', **formparams) + '#index')
       
   258             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
       
   259                 url,  self.req.external_resource('UP_ICON'), _('up')))
       
   260             self.w(u'</h3>')
       
   261             self.w(u'<div style="margin: 0px 1.5em">')
       
   262             self.schema_definition(eschema, link=False)
       
   263             # display entity attributes only if they have some permissions modified
       
   264             modified_attrs = []
       
   265             for attr, etype in  eschema.attribute_definitions():
       
   266                 if self.has_schema_modified_permissions(attr, attr.ACTIONS):
       
   267                     modified_attrs.append(attr)
       
   268             if  modified_attrs:
       
   269                 self.w(u'<h4>%s</h4>' % _('attributes with modified permissions:').capitalize())
       
   270                 self.w(u'</div>')
       
   271                 self.w(u'<div style="margin: 0px 6em">')
       
   272                 for attr in  modified_attrs:
       
   273                     self.w(u'<h4 class="schema">%s (%s)</h4> ' % (attr.type, _(attr.type)))
       
   274                     self.schema_definition(attr, link=False)
       
   275             self.w(u'</div>')
       
   276 
       
   277     def display_relations(self, relations, formparams):
       
   278         _ = self.req._
       
   279         self.w(u'<a id="relations" href="relations"/>')
       
   280         self.w(u'<h2 class="schema">%s </h2>' % _('permissions for relations').capitalize())
       
   281         for rschema in relations:
       
   282             self.w(u'<a id="%s" href="%s"/>' %  (rschema.type, rschema.type))
       
   283             self.w(u'<h3 class="schema">%s (%s) ' % (rschema.type, _(rschema.type)))
       
   284             url = xml_escape(self.build_url('schema', **formparams) + '#index')
       
   285             self.w(u'<a href="%s"><img src="%s" alt="%s"/></a>' % (
       
   286                 url,  self.req.external_resource('UP_ICON'), _('up')))
       
   287             self.w(u'</h3>')
       
   288             self.w(u'<div style="margin: 0px 1.5em">')
       
   289             subjects = [str(subj) for subj in rschema.subjects()]
       
   290             self.w(u'<div><strong>%s</strong> %s (%s)</div>' % (
       
   291                 _('subject_plural:'),
       
   292                 ', '.join(str(subj) for subj in rschema.subjects()),
       
   293                 ', '.join(_(str(subj)) for subj in rschema.subjects())))
       
   294             self.w(u'<div><strong>%s</strong> %s (%s)</div>' % (
       
   295                 _('object_plural:'),
       
   296                 ', '.join(str(obj) for obj in rschema.objects()),
       
   297                 ', '.join(_(str(obj)) for obj in rschema.objects())))
       
   298             self.schema_definition(rschema, link=False)
       
   299             self.w(u'</div>')
       
   300 
       
   301 
       
   302 class SchemaUreportsView(StartupView):
       
   303     id = 'schematext'
       
   304 
       
   305     def call(self):
       
   306         from cubicweb.schemaviewer import SchemaViewer
       
   307         if int(self.req.form.get('skipmeta', True)):
       
   308             skip = schema.SKIP_TYPES
       
   309         else:
       
   310             skip = ()
       
   311         viewer = SchemaViewer(self.req)
       
   312         layout = viewer.visit_schema(self.schema, display_relations=True,
       
   313                                      skiptypes=skip)
       
   314         self.w(ureport_as_html(layout))
       
   315 
       
   316