|
1 """Specific views for users |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2008 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 |
|
11 from cubicweb.schema import display_name |
|
12 from cubicweb.web import INTERNAL_FIELD_VALUE |
|
13 from cubicweb.web.form import EntityForm |
|
14 from cubicweb.web.views.baseviews import PrimaryView |
|
15 |
|
16 class EUserPrimaryView(PrimaryView): |
|
17 accepts = ('EUser',) |
|
18 skip_attrs = ('firstname', 'surname') |
|
19 |
|
20 def iter_relations(self, entity): |
|
21 # don't want to display user's entities |
|
22 for rschema, targetschemas, x in super(EUserPrimaryView, self).iter_relations(entity): |
|
23 if x == 'object' and rschema.type in ('owned_by', 'for_user'): |
|
24 continue |
|
25 yield rschema, targetschemas, x |
|
26 |
|
27 def content_title(self, entity): |
|
28 return entity.name() |
|
29 |
|
30 def is_side_related(self, rschema, eschema): |
|
31 return rschema.type in ['interested_in', 'tags', |
|
32 'todo_by', 'bookmarked_by', |
|
33 ] |
|
34 |
|
35 |
|
36 class EditGroups(EntityForm): |
|
37 """displays a simple euser / egroups editable table""" |
|
38 |
|
39 id = 'editgroups' |
|
40 accepts = ('EUser',) |
|
41 |
|
42 def call(self): |
|
43 self.req.add_css('cubicweb.acl.css') |
|
44 _ = self.req._ |
|
45 self.w(u'<form id="editgroup" method="post" action="edit">') |
|
46 self.w(u'<table id="groupedit">\n') |
|
47 self.w(u'<tr>') |
|
48 self.w(u'<th>%s</th>' % display_name(self.req, 'EUser')) |
|
49 self.w(u''.join(u'<th>%s</th>' % _(gname) for geid, gname in self.egroups)) |
|
50 self.w(u'</tr>') |
|
51 for row in xrange(len(self.rset)): |
|
52 self.build_table_line(row) |
|
53 self.w(u'</table>') |
|
54 self.w(u'<fieldset>') |
|
55 self.w(self.button_cancel()) |
|
56 self.w(self.button_ok()) |
|
57 self.w(u'</fieldset>') |
|
58 self.w(u'</form>') |
|
59 |
|
60 |
|
61 def build_table_line(self, row): |
|
62 euser = self.entity(row) |
|
63 euser_groups = [group.name for group in euser.in_group] |
|
64 if euser_groups: |
|
65 self.w(u'<tr>') |
|
66 else: |
|
67 self.w(u'<tr class="nogroup">') |
|
68 self.w(u'<th><fieldset>') |
|
69 self.w(u'<input type="hidden" name="eid" value="%s" />' % euser.eid) |
|
70 self.w(u'<input type="hidden" name="__type:%s" value="EUser" />' % euser.eid) |
|
71 # this should not occur (for now) since in_group relation is mandatory |
|
72 if not euser_groups: |
|
73 self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s">' % |
|
74 (euser.eid, INTERNAL_FIELD_VALUE)) |
|
75 self.w(euser.dc_title()) |
|
76 self.w(u'</fieldset></th>') |
|
77 for geid, gname in self.egroups: |
|
78 self.w(u'<td><fieldset>') |
|
79 if gname in euser_groups: |
|
80 self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s" />' % |
|
81 (euser.eid, geid)) |
|
82 self.w(u'<input type="checkbox" name="in_group:%s" value="%s" checked="checked" />' % |
|
83 (euser.eid, geid)) |
|
84 else: |
|
85 self.w(u'<input type="checkbox" name="in_group:%s" value="%s" />' % |
|
86 (euser.eid, geid)) |
|
87 self.w(u'</fieldset></td>') |
|
88 self.w(u'</tr>\n') |
|
89 |
|
90 |
|
91 @property |
|
92 @cached |
|
93 def egroups(self): |
|
94 groups = self.req.execute('Any G, N ORDERBY N WHERE G is EGroup, G name N') |
|
95 return [(geid, gname) for geid, gname in groups.rows if gname != 'owners'] |
|
96 |
|
97 |