web/views/schema.py
changeset 2126 a25859917ccc
parent 1998 12040c090aa4
child 2181 94ca417b9b07
--- a/web/views/schema.py	Thu Jun 18 21:01:55 2009 +0200
+++ b/web/views/schema.py	Fri Jun 19 14:42:04 2009 +0200
@@ -7,12 +7,11 @@
 """
 __docformat__ = "restructuredtext en"
 
-from itertools import cycle
-
 from logilab.mtconverter import html_escape
 from yams import schema2dot as s2d
 
 from cubicweb.selectors import implements, yes
+from cubicweb.schema import META_RELATIONS_TYPES, SCHEMA_TYPES
 from cubicweb.schemaviewer import SchemaViewer
 from cubicweb.view import EntityView, StartupView
 from cubicweb.common import tags, uilib
@@ -20,6 +19,9 @@
 from cubicweb.web.views import TmpFileViewMixin, primary, baseviews, tabs
 from cubicweb.web.facet import AttributeFacet
 
+SKIP_TYPES = set()
+SKIP_TYPES.update(META_RELATIONS_TYPES)
+SKIP_TYPES.update(SCHEMA_TYPES)
 
 class ViewSchemaAction(action.Action):
     id = 'schema'
@@ -56,14 +58,11 @@
         if final:
             self.w(u'</em>')
 
-SKIPPED_RELS = ('is', 'is_instance_of', 'identity', 'created_by', 'owned_by',
-                'has_text',)
 
 class CWETypePrimaryView(tabs.TabsMixin, primary.PrimaryView):
     __select__ = implements('CWEType')
     title = _('in memory entity schema')
     main_related_section = False
-    skip_rels = SKIPPED_RELS
     tabs = [_('cwetype-schema-text'), _('cwetype-schema-image'), 
             _('cwetype-schema-permissions'), _('cwetype-workflow')]
     default_tab = 'cwetype-schema-text'
@@ -186,94 +185,56 @@
 
 # schema images ###############################################################
 
-class RestrictedSchemaDotPropsHandler(s2d.SchemaDotPropsHandler):
-    def __init__(self, req):
-        # FIXME: colors are arbitrary
-        self.nextcolor = cycle( ('#aa0000', '#00aa00', '#0000aa',
-                                 '#000000', '#888888') ).next
+class RestrictedSchemaVisitorMixIn(object):
+    def __init__(self, req, *args, **kwargs):
+        super(RestrictedSchemaVisitorMixIn, self).__init__(*args, **kwargs)
         self.req = req
 
-    def display_attr(self, rschema):
-        return not rschema.meta and (rschema.has_local_role('read')
-                                     or rschema.has_perm(self.req, 'read'))
+    def should_display_schema(self, schema):
+        return (super(RestrictedSchemaVisitorMixIn, self).should_display_schema(schema)
+                and rschema.has_local_role('read') or rschema.has_perm(self.req, 'read'))
 
-    # XXX remove this method once yams > 0.20 is out
-    def node_properties(self, eschema):
-        """return default DOT drawing options for an entity schema"""
-        label = ['{', eschema.type, '|']
-        label.append(r'\l'.join(rel.type for rel in eschema.subject_relations()
-                                if rel.final and self.display_attr(rel)))
-        label.append(r'\l}') # trailing \l ensure alignement of the last one
-        return {'label' : ''.join(label), 'shape' : "record",
-                'fontname' : "Courier", 'style' : "filled"}
-
-    def edge_properties(self, rschema, subjnode, objnode):
-        kwargs = super(RestrictedSchemaDotPropsHandler, self).edge_properties(rschema, subjnode, objnode)
-        # symetric rels are handled differently, let yams decide what's best
-        if not rschema.symetric:
-            kwargs['color'] = self.nextcolor()
-        kwargs['fontcolor'] = kwargs['color']
-        # dot label decoration is just awful (1 line underlining the label
-        # + 1 line going to the closest edge spline point)
-        kwargs['decorate'] = 'false'
-        return kwargs
+    def should_display_attr(self, schema):
+        return (super(RestrictedSchemaVisitorMixIn, self).should_display_attr(schema)
+                and rschema.has_local_role('read') or rschema.has_perm(self.req, 'read'))
 
 
-class RestrictedSchemaVisitorMiIn:
-    def __init__(self, req, *args, **kwargs):
-        # hack hack hack
-        assert len(self.__class__.__bases__) == 2
-        self.__parent = self.__class__.__bases__[1]
-        self.__parent.__init__(self, *args, **kwargs)
-        self.req = req
-
-    def nodes(self):
-        for etype, eschema in self.__parent.nodes(self):
-            if eschema.has_local_role('read') or eschema.has_perm(self.req, 'read'):
-                yield eschema.type, eschema
-
-    def edges(self):
-        for setype, oetype, rschema in self.__parent.edges(self):
-            if rschema.has_local_role('read') or rschema.has_perm(self.req, 'read'):
-                yield setype, oetype, rschema
-
-
-class FullSchemaVisitor(RestrictedSchemaVisitorMiIn, s2d.FullSchemaVisitor):
+class FullSchemaVisitor(RestrictedSchemaVisitorMixIn, s2d.FullSchemaVisitor):
     pass
 
-class OneHopESchemaVisitor(RestrictedSchemaVisitorMiIn, s2d.OneHopESchemaVisitor):
+class OneHopESchemaVisitor(RestrictedSchemaVisitorMixIn,
+                           s2d.OneHopESchemaVisitor):
     pass
 
-class OneHopRSchemaVisitor(RestrictedSchemaVisitorMiIn, s2d.OneHopRSchemaVisitor):
+class OneHopRSchemaVisitor(RestrictedSchemaVisitorMixIn,
+                           s2d.OneHopRSchemaVisitor):
     pass
 
 
 class SchemaImageView(TmpFileViewMixin, StartupView):
     id = 'schemagraph'
+    content_type = 'image/png'
 
-    content_type = 'image/png'
-    skip_rels = SKIPPED_RELS
     def _generate(self, tmpfile):
         """display global schema information"""
         skipmeta = not int(self.req.form.get('withmeta', 0))
-        visitor = FullSchemaVisitor(self.req, self.schema, skiprels=self.skip_rels, skipmeta=skipmeta)
-        s2d.schema2dot(outputfile=tmpfile, visitor=visitor,
-                       prophdlr=RestrictedSchemaDotPropsHandler(self.req))
+        visitor = FullSchemaVisitor(self.req, self.schema,
+                                    skiptypes=skipmeta and SKIP_TYPES or ())
+        s2d.schema2dot(outputfile=tmpfile, visitor=visitor)
 
 class CWETypeSchemaImageView(TmpFileViewMixin, EntityView):
     id = 'schemagraph'
     __select__ = implements('CWEType')
-
     content_type = 'image/png'
-    skip_rels = SKIPPED_RELS
 
     def _generate(self, tmpfile):
         """display schema information for an entity"""
         entity = self.entity(self.row, self.col)
         eschema = self.vreg.schema.eschema(entity.name)
-        visitor = OneHopESchemaVisitor(self.req, eschema, skiprels=self.skip_rels)
-        s2d.schema2dot(outputfile=tmpfile, visitor=visitor,
-                       prophdlr=RestrictedSchemaDotPropsHandler(self.req))
+        skipmeta = not int(self.req.form.get('withmeta', 0))
+        visitor = OneHopESchemaVisitor(self.req, eschema,
+                                       skiptypes=skipmeta and SKIP_TYPES or ())
+        s2d.schema2dot(outputfile=tmpfile, visitor=visitor)
 
 class CWRTypeSchemaImageView(CWETypeSchemaImageView):
     __select__ = implements('CWRType')
@@ -283,8 +244,7 @@
         entity = self.entity(self.row, self.col)
         rschema = self.vreg.schema.rschema(entity.name)
         visitor = OneHopRSchemaVisitor(self.req, rschema)
-        s2d.schema2dot(outputfile=tmpfile, visitor=visitor,
-                       prophdlr=RestrictedSchemaDotPropsHandler(self.req))
+        s2d.schema2dot(outputfile=tmpfile, visitor=visitor)
 
 ### facets