author | sylvain.thenault@logilab.fr |
Mon, 16 Feb 2009 20:55:00 +0100 | |
branch | tls-sprint |
changeset 646 | 8a9551089912 |
parent 631 | 99f5852f8604 |
child 680 | 99d0b471cc0c |
permissions | -rw-r--r-- |
0 | 1 |
"""Specific views for users |
2 |
||
3 |
:organization: Logilab |
|
523 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from logilab.common.decorators import cached |
|
523 | 10 |
from logilab.mtconverter import html_escape |
0 | 11 |
|
12 |
from cubicweb.schema import display_name |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
13 |
from cubicweb.common.selectors import one_line_rset, implements, match_user_groups |
0 | 14 |
from cubicweb.web import INTERNAL_FIELD_VALUE |
15 |
from cubicweb.web.form import EntityForm |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
16 |
from cubicweb.web.action import Action |
523 | 17 |
from cubicweb.web.views.baseviews import PrimaryView, EntityView |
557 | 18 |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
19 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
20 |
class UserPreferencesEntityAction(Action): |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
21 |
id = 'prefs' |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
22 |
__selectors__ = (one_line_rset, |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
23 |
implements('EUser'), |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
24 |
match_user_groups('owners', 'managers')) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
25 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
26 |
title = _('preferences') |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
27 |
category = 'mainactions' |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
28 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
29 |
def url(self): |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
30 |
login = self.rset.get_entity(self.row or 0, self.col or 0).login |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
31 |
return self.build_url('euser/%s'%login, vid='epropertiesform') |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
32 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
33 |
|
0 | 34 |
class EUserPrimaryView(PrimaryView): |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
35 |
__selectors__ = (implements('EUser'),) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
36 |
|
0 | 37 |
skip_attrs = ('firstname', 'surname') |
38 |
||
39 |
def iter_relations(self, entity): |
|
40 |
# don't want to display user's entities |
|
41 |
for rschema, targetschemas, x in super(EUserPrimaryView, self).iter_relations(entity): |
|
42 |
if x == 'object' and rschema.type in ('owned_by', 'for_user'): |
|
43 |
continue |
|
44 |
yield rschema, targetschemas, x |
|
45 |
||
46 |
def content_title(self, entity): |
|
47 |
return entity.name() |
|
48 |
||
49 |
def is_side_related(self, rschema, eschema): |
|
50 |
return rschema.type in ['interested_in', 'tags', |
|
51 |
'todo_by', 'bookmarked_by', |
|
556 | 52 |
] |
523 | 53 |
class FoafView(EntityView): |
54 |
id = 'foaf' |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
55 |
__selectors__ = (implements('EUser'),) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
56 |
|
523 | 57 |
title = _('foaf') |
58 |
templatable = False |
|
59 |
content_type = 'text/xml' |
|
60 |
||
61 |
def call(self): |
|
619
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
62 |
self.w('''<?xml version="1.0" encoding="%s"?> |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
63 |
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
64 |
xmlns:rdfs="http://www.w3org/2000/01/rdf-schema#" |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
65 |
xmlns:foaf="http://xmlns.com/foaf/0.1/"> '''% self.req.encoding) |
523 | 66 |
for i in xrange(self.rset.rowcount): |
67 |
self.cell_call(i, 0) |
|
68 |
self.w(u'</rdf:RDF>\n') |
|
69 |
||
70 |
def cell_call(self, row, col): |
|
71 |
entity = self.complete_entity(row, col) |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
72 |
self.w(u'''<foaf:PersonalProfileDocument rdf:about=""> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
73 |
<foaf:maker rdf:resource="%s"/> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
74 |
<foaf:primaryTopic rdf:resource="%s"/> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
75 |
</foaf:PersonalProfileDocument>''' % (entity.absolute_url(), entity.absolute_url())) |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
76 |
self.w(u'<foaf:Person rdf:ID="%s">\n' % entity.eid) |
523 | 77 |
self.w(u'<foaf:name>%s</foaf:name>\n' % html_escape(entity.dc_long_title())) |
78 |
if entity.surname: |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
79 |
self.w(u'<foaf:family_name>%s</foaf:family_name>\n' |
523 | 80 |
% html_escape(entity.surname)) |
81 |
if entity.firstname: |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
82 |
self.w(u'<foaf:givenname>%s</foaf:givenname>\n' |
523 | 83 |
% html_escape(entity.firstname)) |
84 |
emailaddr = entity.get_email() |
|
85 |
if emailaddr: |
|
586 | 86 |
self.w(u'<foaf:mbox>%s</foaf:mbox>\n' % html_escape(emailaddr)) |
87 |
self.w(u'</foaf:Person>\n') |
|
523 | 88 |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
89 |
|
619
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
90 |
class FoafUsableView(FoafView): |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
91 |
id = 'foaf_usable' |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
92 |
|
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
93 |
def call(self): |
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
94 |
self.cell_call(0, 0) |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
619
diff
changeset
|
95 |
|
619
9efa2cc9c02c
adding foaf_usable views to supports reusing foaf fragment in other views (for instance sioc view)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
586
diff
changeset
|
96 |
|
0 | 97 |
class EditGroups(EntityForm): |
98 |
"""displays a simple euser / egroups editable table""" |
|
99 |
||
100 |
id = 'editgroups' |
|
101 |
accepts = ('EUser',) |
|
102 |
||
103 |
def call(self): |
|
104 |
self.req.add_css('cubicweb.acl.css') |
|
105 |
_ = self.req._ |
|
106 |
self.w(u'<form id="editgroup" method="post" action="edit">') |
|
107 |
self.w(u'<table id="groupedit">\n') |
|
108 |
self.w(u'<tr>') |
|
109 |
self.w(u'<th>%s</th>' % display_name(self.req, 'EUser')) |
|
110 |
self.w(u''.join(u'<th>%s</th>' % _(gname) for geid, gname in self.egroups)) |
|
111 |
self.w(u'</tr>') |
|
112 |
for row in xrange(len(self.rset)): |
|
113 |
self.build_table_line(row) |
|
114 |
self.w(u'</table>') |
|
115 |
self.w(u'<fieldset>') |
|
116 |
self.w(self.button_cancel()) |
|
117 |
self.w(self.button_ok()) |
|
118 |
self.w(u'</fieldset>') |
|
119 |
self.w(u'</form>') |
|
120 |
||
121 |
||
122 |
def build_table_line(self, row): |
|
123 |
euser = self.entity(row) |
|
124 |
euser_groups = [group.name for group in euser.in_group] |
|
125 |
if euser_groups: |
|
126 |
self.w(u'<tr>') |
|
127 |
else: |
|
128 |
self.w(u'<tr class="nogroup">') |
|
129 |
self.w(u'<th><fieldset>') |
|
130 |
self.w(u'<input type="hidden" name="eid" value="%s" />' % euser.eid) |
|
131 |
self.w(u'<input type="hidden" name="__type:%s" value="EUser" />' % euser.eid) |
|
132 |
# this should not occur (for now) since in_group relation is mandatory |
|
133 |
if not euser_groups: |
|
134 |
self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s">' % |
|
135 |
(euser.eid, INTERNAL_FIELD_VALUE)) |
|
136 |
self.w(euser.dc_title()) |
|
137 |
self.w(u'</fieldset></th>') |
|
138 |
for geid, gname in self.egroups: |
|
139 |
self.w(u'<td><fieldset>') |
|
140 |
if gname in euser_groups: |
|
141 |
self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s" />' % |
|
142 |
(euser.eid, geid)) |
|
143 |
self.w(u'<input type="checkbox" name="in_group:%s" value="%s" checked="checked" />' % |
|
144 |
(euser.eid, geid)) |
|
145 |
else: |
|
146 |
self.w(u'<input type="checkbox" name="in_group:%s" value="%s" />' % |
|
147 |
(euser.eid, geid)) |
|
148 |
self.w(u'</fieldset></td>') |
|
149 |
self.w(u'</tr>\n') |
|
150 |
||
151 |
||
152 |
@property |
|
153 |
@cached |
|
154 |
def egroups(self): |
|
155 |
groups = self.req.execute('Any G, N ORDERBY N WHERE G is EGroup, G name N') |
|
156 |
return [(geid, gname) for geid, gname in groups.rows if gname != 'owners'] |
|
157 |
||
158 |