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