web/views/startup.py
branchtls-sprint
changeset 1498 2c6eec0b46b9
parent 1398 5fe84a5f7035
child 1522 47b2ffbee760
equal deleted inserted replaced
1493:8270580b65a0 1498:2c6eec0b46b9
    17 _ = unicode
    17 _ = unicode
    18 
    18 
    19 
    19 
    20 class ManageView(StartupView):
    20 class ManageView(StartupView):
    21     id = 'manage'
    21     id = 'manage'
    22     title = _('manage')    
    22     title = _('manage')
    23     http_cache_manager = EtagHTTPCacheManager
    23     http_cache_manager = EtagHTTPCacheManager
    24 
    24 
    25     def display_folders(self):
    25     def display_folders(self):
    26         return False
    26         return False
    27     
    27 
    28     def call(self, **kwargs):
    28     def call(self, **kwargs):
    29         """The default view representing the application's management"""
    29         """The default view representing the application's management"""
    30         self.req.add_css('cubicweb.manageview.css')
    30         self.req.add_css('cubicweb.manageview.css')
    31         self.w(u'<div>\n')
    31         self.w(u'<div>\n')
    32         if not self.display_folders():
    32         if not self.display_folders():
    33             self._main_index()
    33             self._main_index()
    34         else:
    34         else:
    35             self.w(u'<table><tr>\n')
    35             self.w(u'<table><tr>\n')
    36             self.w(u'<td style="width:40%">')
    36             self.w(u'<td style="width:40%">')
    37             self._main_index()
    37             self._main_index()
    38             self.w(u'</td><td style="width:60%">')            
    38             self.w(u'</td><td style="width:60%">')
    39             self.folders()
    39             self.folders()
    40             self.w(u'</td>')
    40             self.w(u'</td>')
    41             self.w(u'</tr></table>\n')
    41             self.w(u'</tr></table>\n')
    42         self.w(u'</div>\n')
    42         self.w(u'</div>\n')
    43 
    43 
    61                 label = self.req._('edit the index page')
    61                 label = self.req._('edit the index page')
    62             else:
    62             else:
    63                 href = req.build_url('view', vid='creation', etype='Card', wikiid='index')
    63                 href = req.build_url('view', vid='creation', etype='Card', wikiid='index')
    64                 label = self.req._('create an index page')
    64                 label = self.req._('create an index page')
    65             self.w(u'<br/><a href="%s">%s</a>\n' % (html_escape(href), label))
    65             self.w(u'<br/><a href="%s">%s</a>\n' % (html_escape(href), label))
    66         
    66 
    67     def folders(self):
    67     def folders(self):
    68         self.w(u'<h4>%s</h4>\n' % self.req._('Browse by category'))
    68         self.w(u'<h4>%s</h4>\n' % self.req._('Browse by category'))
    69         self.vreg.select_view('tree', self.req, None).dispatch(w=self.w)
    69         self.vreg.select_view('tree', self.req, None).dispatch(w=self.w)
    70         
    70 
    71     def startup_views(self):
    71     def startup_views(self):
    72         self.w(u'<h4>%s</h4>\n' % self.req._('Startup views'))
    72         self.w(u'<h4>%s</h4>\n' % self.req._('Startup views'))
    73         self.startupviews_table()
    73         self.startupviews_table()
    74         
    74 
    75     def startupviews_table(self):
    75     def startupviews_table(self):
    76         for v in self.vreg.possible_views(self.req, None):
    76         for v in self.vreg.possible_views(self.req, None):
    77             if v.category != 'startupview' or v.id in ('index', 'tree', 'manage'):
    77             if v.category != 'startupview' or v.id in ('index', 'tree', 'manage'):
    78                 continue
    78                 continue
    79             self.w('<p><a href="%s">%s</a></p>' % (
    79             self.w('<p><a href="%s">%s</a></p>' % (
    80                 html_escape(v.url()), html_escape(self.req._(v.title).capitalize())))
    80                 html_escape(v.url()), html_escape(self.req._(v.title).capitalize())))
    81         
    81 
    82     def entities(self):
    82     def entities(self):
    83         schema = self.schema
    83         schema = self.schema
    84         self.w(u'<h4>%s</h4>\n' % self.req._('The repository holds the following entities'))
    84         self.w(u'<h4>%s</h4>\n' % self.req._('The repository holds the following entities'))
    85         manager = self.req.user.matching_groups('managers')
    85         manager = self.req.user.matching_groups('managers')
    86         self.w(u'<table class="startup">')
    86         self.w(u'<table class="startup">')
    87         if manager:
    87         if manager:
    88             self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('application entities'))
    88             self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('application entities'))
    89         self.entity_types_table(eschema for eschema in schema.entities()
    89         self.entity_types_table(eschema for eschema in schema.entities()
    90                                 if not eschema.meta and not eschema.is_subobject(strict=True))
    90                                 if not eschema.meta and not eschema.is_subobject(strict=True))
    91         if manager: 
    91         if manager:
    92             self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('system entities'))
    92             self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('system entities'))
    93             self.entity_types_table(eschema for eschema in schema.entities()
    93             self.entity_types_table(eschema for eschema in schema.entities()
    94                                     if eschema.meta and not eschema.schema_entity())
    94                                     if eschema.meta and not eschema.schema_entity())
    95             if 'CWAttribute' in schema: # check schema support
    95             if 'CWAttribute' in schema: # check schema support
    96                 self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('schema entities'))
    96                 self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('schema entities'))
    97                 self.entity_types_table(schema.eschema(etype)
    97                 self.entity_types_table(schema.eschema(etype)
    98                                         for etype in schema.schema_entity_types())
    98                                         for etype in schema.schema_entity_types())
    99         self.w(u'</table>')
    99         self.w(u'</table>')
   100         
   100 
   101     def entity_types_table(self, eschemas):
   101     def entity_types_table(self, eschemas):
   102         newline = 0
   102         newline = 0
   103         infos = sorted(self.entity_types(eschemas),
   103         infos = sorted(self.entity_types(eschemas),
   104                        key=lambda (l,a,e):unormalize(l))
   104                        key=lambda (l,a,e):unormalize(l))
   105         q, r = divmod(len(infos), 2)
   105         q, r = divmod(len(infos), 2)
   109         for (_, etypelink, addlink), (_, etypelink2, addlink2) in infos:
   109         for (_, etypelink, addlink), (_, etypelink2, addlink2) in infos:
   110             self.w(u'<tr>\n')
   110             self.w(u'<tr>\n')
   111             self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink,  etypelink))
   111             self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink,  etypelink))
   112             self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink2, etypelink2))
   112             self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink2, etypelink2))
   113             self.w(u'</tr>\n')
   113             self.w(u'</tr>\n')
   114         
   114 
   115         
   115 
   116     def entity_types(self, eschemas):
   116     def entity_types(self, eschemas):
   117         """return a list of formatted links to get a list of entities of
   117         """return a list of formatted links to get a list of entities of
   118         a each entity's types
   118         a each entity's types
   119         """
   119         """
   120         req = self.req
   120         req = self.req
   131             else:
   131             else:
   132                 url = self.build_url('view', rql='%s X' % etype)
   132                 url = self.build_url('view', rql='%s X' % etype)
   133             etypelink = u'&nbsp;<a href="%s">%s</a> (%d)' % (
   133             etypelink = u'&nbsp;<a href="%s">%s</a> (%d)' % (
   134                 html_escape(url), label, nb)
   134                 html_escape(url), label, nb)
   135             yield (label, etypelink, self.add_entity_link(eschema, req))
   135             yield (label, etypelink, self.add_entity_link(eschema, req))
   136     
   136 
   137     def add_entity_link(self, eschema, req):
   137     def add_entity_link(self, eschema, req):
   138         """creates a [+] link for adding an entity if user has permission to do so"""
   138         """creates a [+] link for adding an entity if user has permission to do so"""
   139         if not eschema.has_perm(req, 'add'):
   139         if not eschema.has_perm(req, 'add'):
   140             return u''
   140             return u''
   141         return u'[<a href="%s" title="%s">+</a>]' % (
   141         return u'[<a href="%s" title="%s">+</a>]' % (
   142             html_escape(self.create_url(eschema.type)),
   142             html_escape(self.create_url(eschema.type)),
   143             self.req.__('add a %s' % eschema))
   143             self.req.__('add a %s' % eschema))
   144 
   144 
   145     
   145 
   146 class IndexView(ManageView):
   146 class IndexView(ManageView):
   147     id = 'index'
   147     id = 'index'
   148     title = _('index')
   148     title = _('index')
   149     
   149 
   150     def display_folders(self):
   150     def display_folders(self):
   151         return 'Folder' in self.schema and self.req.execute('Any COUNT(X) WHERE X is Folder')[0][0]
   151         return 'Folder' in self.schema and self.req.execute('Any COUNT(X) WHERE X is Folder')[0][0]
   152     
   152 
   153 
   153 
   154 
   154 
   155 class SchemaView(StartupView):
   155 class SchemaView(StartupView):
   156     id = 'schema'
   156     id = 'schema'
   157     title = _('application schema')
   157     title = _('application schema')