author | Laure Bourgois <Laure.Bourgois@logilab.fr> |
Fri, 06 Feb 2009 11:05:41 +0100 | |
changeset 557 | 8bb60606e5ad |
parent 556 | 64d26324d3eb |
child 558 | a8887dd924f4 |
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 |
|
13 |
from cubicweb.web import INTERNAL_FIELD_VALUE |
|
14 |
from cubicweb.web.form import EntityForm |
|
523 | 15 |
from cubicweb.web.views.baseviews import PrimaryView, EntityView |
557 | 16 |
|
17 |
import hashlib |
|
18 |
||
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
19 |
try: |
557 | 20 |
from hashlib import sha1 |
21 |
||
22 |
except ImportError: |
|
23 |
from sha import sha as sha1 |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
24 |
|
0 | 25 |
|
26 |
class EUserPrimaryView(PrimaryView): |
|
27 |
accepts = ('EUser',) |
|
28 |
skip_attrs = ('firstname', 'surname') |
|
29 |
||
30 |
def iter_relations(self, entity): |
|
31 |
# don't want to display user's entities |
|
32 |
for rschema, targetschemas, x in super(EUserPrimaryView, self).iter_relations(entity): |
|
33 |
if x == 'object' and rschema.type in ('owned_by', 'for_user'): |
|
34 |
continue |
|
35 |
yield rschema, targetschemas, x |
|
36 |
||
37 |
def content_title(self, entity): |
|
38 |
return entity.name() |
|
39 |
||
40 |
def is_side_related(self, rschema, eschema): |
|
41 |
return rschema.type in ['interested_in', 'tags', |
|
42 |
'todo_by', 'bookmarked_by', |
|
556 | 43 |
] |
44 |
||
523 | 45 |
class FoafView(EntityView): |
46 |
id = 'foaf' |
|
47 |
accepts = ('EUser',) |
|
48 |
title = _('foaf') |
|
49 |
templatable = False |
|
50 |
content_type = 'text/xml' |
|
51 |
||
52 |
def call(self): |
|
53 |
self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding) |
|
54 |
self.w(u'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n') |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
55 |
self.w(u'xmlns:rdfs="http://www.w3org/2000/01/rdf-schema#"\n') |
523 | 56 |
self.w(u'xmlns:foaf="http://xmlns.com/foaf/0.1/">\n') |
57 |
for i in xrange(self.rset.rowcount): |
|
58 |
self.cell_call(i, 0) |
|
59 |
self.w(u'</rdf:RDF>\n') |
|
60 |
||
61 |
def cell_call(self, row, col): |
|
62 |
entity = self.complete_entity(row, col) |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
63 |
self.w(u'''<foaf:PersonalProfileDocument rdf:about=""> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
64 |
<foaf:maker rdf:resource="%s"/> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
65 |
<foaf:primaryTopic rdf:resource="%s"/> |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
66 |
</foaf:PersonalProfileDocument>''' % (entity.absolute_url(), entity.absolute_url())) |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
67 |
|
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
68 |
self.w(u'<foaf:Person rdf:ID="%s">\n' % entity.eid) |
523 | 69 |
self.w(u'<foaf:name>%s</foaf:name>\n' % html_escape(entity.dc_long_title())) |
70 |
if entity.surname: |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
71 |
self.w(u'<foaf:family_name>%s</foaf:family_name>\n' |
523 | 72 |
% html_escape(entity.surname)) |
73 |
if entity.firstname: |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
74 |
self.w(u'<foaf:givenname>%s</foaf:givenname>\n' |
523 | 75 |
% html_escape(entity.firstname)) |
76 |
emailaddr = entity.get_email() |
|
77 |
if emailaddr: |
|
555
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
78 |
m = hashlib.sha1() |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
79 |
m.update(html_escape(emailaddr)) |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
80 |
crypt_sha1 = m.hexdigest() |
b40d885ba7a4
new foaf version
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
523
diff
changeset
|
81 |
self.w(u'<foaf:mbox_sha1sum>%s</foaf:mbox_sha1sum>\n' % crypt_sha1) |
556 | 82 |
self.w(u'</foaf:Person>\n') |
523 | 83 |
|
0 | 84 |
|
85 |
class EditGroups(EntityForm): |
|
86 |
"""displays a simple euser / egroups editable table""" |
|
87 |
||
88 |
id = 'editgroups' |
|
89 |
accepts = ('EUser',) |
|
90 |
||
91 |
def call(self): |
|
92 |
self.req.add_css('cubicweb.acl.css') |
|
93 |
_ = self.req._ |
|
94 |
self.w(u'<form id="editgroup" method="post" action="edit">') |
|
95 |
self.w(u'<table id="groupedit">\n') |
|
96 |
self.w(u'<tr>') |
|
97 |
self.w(u'<th>%s</th>' % display_name(self.req, 'EUser')) |
|
98 |
self.w(u''.join(u'<th>%s</th>' % _(gname) for geid, gname in self.egroups)) |
|
99 |
self.w(u'</tr>') |
|
100 |
for row in xrange(len(self.rset)): |
|
101 |
self.build_table_line(row) |
|
102 |
self.w(u'</table>') |
|
103 |
self.w(u'<fieldset>') |
|
104 |
self.w(self.button_cancel()) |
|
105 |
self.w(self.button_ok()) |
|
106 |
self.w(u'</fieldset>') |
|
107 |
self.w(u'</form>') |
|
108 |
||
109 |
||
110 |
def build_table_line(self, row): |
|
111 |
euser = self.entity(row) |
|
112 |
euser_groups = [group.name for group in euser.in_group] |
|
113 |
if euser_groups: |
|
114 |
self.w(u'<tr>') |
|
115 |
else: |
|
116 |
self.w(u'<tr class="nogroup">') |
|
117 |
self.w(u'<th><fieldset>') |
|
118 |
self.w(u'<input type="hidden" name="eid" value="%s" />' % euser.eid) |
|
119 |
self.w(u'<input type="hidden" name="__type:%s" value="EUser" />' % euser.eid) |
|
120 |
# this should not occur (for now) since in_group relation is mandatory |
|
121 |
if not euser_groups: |
|
122 |
self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s">' % |
|
123 |
(euser.eid, INTERNAL_FIELD_VALUE)) |
|
124 |
self.w(euser.dc_title()) |
|
125 |
self.w(u'</fieldset></th>') |
|
126 |
for geid, gname in self.egroups: |
|
127 |
self.w(u'<td><fieldset>') |
|
128 |
if gname in euser_groups: |
|
129 |
self.w(u'<input type="hidden" name="edits-in_group:%s" value="%s" />' % |
|
130 |
(euser.eid, geid)) |
|
131 |
self.w(u'<input type="checkbox" name="in_group:%s" value="%s" checked="checked" />' % |
|
132 |
(euser.eid, geid)) |
|
133 |
else: |
|
134 |
self.w(u'<input type="checkbox" name="in_group:%s" value="%s" />' % |
|
135 |
(euser.eid, geid)) |
|
136 |
self.w(u'</fieldset></td>') |
|
137 |
self.w(u'</tr>\n') |
|
138 |
||
139 |
||
140 |
@property |
|
141 |
@cached |
|
142 |
def egroups(self): |
|
143 |
groups = self.req.execute('Any G, N ORDERBY N WHERE G is EGroup, G name N') |
|
144 |
return [(geid, gname) for geid, gname in groups.rows if gname != 'owners'] |
|
145 |
||
146 |