web/views/euser.py
changeset 1808 aa09e20dd8c0
parent 1693 49075f57cf2c
parent 1807 6d541c610165
child 1810 e95e876be17c
equal deleted inserted replaced
1693:49075f57cf2c 1808:aa09e20dd8c0
     1 """Specific views for users
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 from logilab.common.decorators import cached
       
    10 from logilab.mtconverter import html_escape
       
    11 
       
    12 from cubicweb.schema import display_name
       
    13 from cubicweb.web import INTERNAL_FIELD_VALUE
       
    14 from cubicweb.web.form import EntityForm
       
    15 from cubicweb.web.views.baseviews import PrimaryView, EntityView
       
    16 
       
    17 class EUserPrimaryView(PrimaryView):
       
    18     accepts = ('EUser',)
       
    19     skip_attrs = ('firstname', 'surname')
       
    20     
       
    21     def iter_relations(self, entity):
       
    22         # don't want to display user's entities
       
    23         for rschema, targetschemas, x in super(EUserPrimaryView, self).iter_relations(entity):
       
    24             if x == 'object' and rschema.type in ('owned_by', 'for_user'):
       
    25                 continue
       
    26             yield rschema, targetschemas, x
       
    27 
       
    28     def content_title(self, entity):
       
    29         return entity.name()
       
    30 
       
    31     def is_side_related(self, rschema, eschema):
       
    32         return  rschema.type in ['interested_in', 'tags', 
       
    33                                  'todo_by', 'bookmarked_by',
       
    34                                  ]
       
    35 class FoafView(EntityView):
       
    36     id = 'foaf'
       
    37     accepts = ('EUser',)
       
    38     title = _('foaf')
       
    39     templatable = False
       
    40     content_type = 'text/xml'
       
    41 
       
    42     def call(self):
       
    43         self.w(u'''<?xml version="1.0" encoding="%s"?>
       
    44 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       
    45          xmlns:rdfs="http://www.w3org/2000/01/rdf-schema#"
       
    46          xmlns:foaf="http://xmlns.com/foaf/0.1/"> '''% self.req.encoding)
       
    47         for i in xrange(self.rset.rowcount):
       
    48             self.cell_call(i, 0)
       
    49         self.w(u'</rdf:RDF>\n')
       
    50 
       
    51     def cell_call(self, row, col):
       
    52         entity = self.complete_entity(row, col)
       
    53         self.w(u'''<foaf:PersonalProfileDocument rdf:about="">
       
    54                       <foaf:maker rdf:resource="%s"/>
       
    55                       <foaf:primaryTopic rdf:resource="%s"/>
       
    56                    </foaf:PersonalProfileDocument>''' % (entity.absolute_url(), entity.absolute_url()))
       
    57                       
       
    58         self.w(u'<foaf:Person rdf:ID="%s">\n' % entity.eid)
       
    59         self.w(u'<foaf:name>%s</foaf:name>\n' % html_escape(entity.dc_long_title()))
       
    60         if entity.surname:
       
    61             self.w(u'<foaf:family_name>%s</foaf:family_name>\n'
       
    62                    % html_escape(entity.surname))
       
    63         if entity.firstname:
       
    64             self.w(u'<foaf:givenname>%s</foaf:givenname>\n'
       
    65                    % html_escape(entity.firstname))
       
    66         emailaddr = entity.get_email()
       
    67         if emailaddr:
       
    68             self.w(u'<foaf:mbox>%s</foaf:mbox>\n' % html_escape(emailaddr))
       
    69         self.w(u'</foaf:Person>\n')
       
    70                    
       
    71 class EditGroups(EntityForm):
       
    72     """displays a simple euser / egroups editable table"""
       
    73     
       
    74     id = 'editgroups'
       
    75     accepts = ('EUser',)
       
    76     
       
    77     def call(self):
       
    78         self.req.add_css('cubicweb.acl.css')            
       
    79         _ = self.req._
       
    80         self.w(u'<form id="editgroup" method="post" action="edit">')
       
    81         self.w(u'<table id="groupedit">\n')
       
    82         self.w(u'<tr>')
       
    83         self.w(u'<th>%s</th>' % display_name(self.req, 'EUser'))
       
    84         self.w(u''.join(u'<th>%s</th>' % _(gname) for geid, gname in self.egroups))
       
    85         self.w(u'</tr>')
       
    86         for row in xrange(len(self.rset)):
       
    87             self.build_table_line(row)
       
    88         self.w(u'</table>')
       
    89         self.w(u'<fieldset>')
       
    90         self.w(self.button_cancel())
       
    91         self.w(self.button_ok())
       
    92         self.w(u'</fieldset>')
       
    93         self.w(u'</form>')
       
    94 
       
    95 
       
    96     def build_table_line(self, row):
       
    97         euser = self.entity(row)
       
    98         euser_groups = [group.name for group in euser.in_group]
       
    99         if euser_groups:
       
   100             self.w(u'<tr>')
       
   101         else:
       
   102             self.w(u'<tr class="nogroup">')
       
   103         self.w(u'<th><fieldset>')
       
   104         self.w(u'<input type="hidden" name="eid" value="%s" />' % euser.eid)
       
   105         self.w(u'<input type="hidden" name="__type:%s" value="EUser" />' % euser.eid)
       
   106         # this should not occur (for now) since in_group relation is mandatory
       
   107         if not euser_groups:
       
   108             self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s">' %
       
   109                    (euser.eid, INTERNAL_FIELD_VALUE))
       
   110         self.w(euser.dc_title())
       
   111         self.w(u'</fieldset></th>')
       
   112         for geid, gname in self.egroups:
       
   113             self.w(u'<td><fieldset>')
       
   114             if gname in euser_groups:
       
   115                 self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s" />' %
       
   116                        (euser.eid, geid))
       
   117                 self.w(u'<input type="checkbox" name="in_group:%s" value="%s" checked="checked" />' %
       
   118                        (euser.eid, geid))
       
   119             else:
       
   120                 self.w(u'<input type="checkbox" name="in_group:%s" value="%s" />' %
       
   121                        (euser.eid, geid))
       
   122             self.w(u'</fieldset></td>')
       
   123         self.w(u'</tr>\n')
       
   124 
       
   125         
       
   126     @property
       
   127     @cached
       
   128     def egroups(self):
       
   129         groups = self.req.execute('Any G, N ORDERBY N WHERE G is EGroup, G name N')
       
   130         return [(geid, gname) for geid, gname in groups.rows if gname != 'owners']
       
   131                 
       
   132