views.py 4.39 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
"""Specific views for person entities

:organization: Logilab
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
4
:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
5
6
7
8
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"

9
from logilab.mtconverter import xml_escape
Nicolas Chauvat's avatar
Nicolas Chauvat committed
10

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
11
12
13
from cubicweb.selectors import implements, rql_condition
from cubicweb.web import uicfg, action
from cubicweb.web.views import baseviews, primary, vcard
Nicolas Chauvat's avatar
Nicolas Chauvat committed
14
from cubicweb.web.facet import AttributeFacet
Nicolas Chauvat's avatar
Nicolas Chauvat committed
15

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
16
for attr in ('civility', 'description', 'firstname', 'surname'):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
17
    uicfg.primaryview_section.tag_attribute(('Person', attr), 'hidden')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
18
for rtype in ('phone', 'use_email', 'primary_email'):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
19
    uicfg.primaryview_section.tag_subject_of(('Person', rtype, '*'), 'hidden')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
20

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
21
uicfg.autoform_is_inlined.tag_subject_of(('Person', 'phone', '*'), True)
22
uicfg.autoform_is_inlined.tag_subject_of(('Person', 'postal_address', '*'), True)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
23

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
24
25
26
class PersonPrimaryView(primary.PrimaryView):
    __select__ = implements('Person')

27
    def render_entity_title(self, entity):
Sylvain Thénault's avatar
Sylvain Thénault committed
28
        self.w(u'<h1>%s</h1>' % xml_escape(entity.name(civility=True)))
29

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
30
    def render_entity_attributes(self, entity):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
31
        if getattr(entity, 'phone', None):
32
            self.field('phone', ', '.join(xml_escape(e.view('text'))
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
33
                                          for e in entity.phone))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
34
35
36
        if entity.use_email:
            self.field('use_email',
                       ', '.join(e.view('oneline') for e in entity.use_email))
37
38
39
40
41
42
43
44

        if getattr(entity, 'postal_address', None):
            self.w(u"<table>")
            for postaladdress in entity.postal_address:
                self.w(u'<tr><th class="labelCol">%s</th><td>%s</td></tr>' % 
                       (display_name(self.req, 'PostalAddress'),
                        postaladdress.view('incontext')) )
            self.w(u"</table>")
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
45
        super(PersonPrimaryView, self).render_entity_attributes(entity)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
46
47

    def summary(self, entity):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
48
49
        return entity.view('reledit', rtype='description')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
50
51

class PersonTextView(baseviews.TextView):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
52
53
    __select__ = implements('Person')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
54
55
    def cell_call(self, row, col):
        entity = self.entity(row, col)
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
56
        self.w(entity.name())
Nicolas Chauvat's avatar
Nicolas Chauvat committed
57

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
58
59

class PersonEmailView(baseviews.EntityView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
60
61
    """display emails sent or received by one of this person's addresses"""
    id = 'emails'
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
62
    __select__ = implements('Person')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
63
    title = _('emails')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
64

Nicolas Chauvat's avatar
Nicolas Chauvat committed
65
66
67
    def cell_call(self, row, col=0):
        entity = self.entity(row, col)
        self.w(u'<h3>')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
68
        self.w(self.req._('emails sent or received by %s')
69
               % xml_escape(entity.dc_title()))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
70
71
72
73
74
        self.w(u'</h3>')
        done = set()
        if getattr(entity, 'primary_email', None):
            email = entity.primary_email[0]
            email.view('shortprimary', w=self.w, skipeids=done)
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
75
            pemaileid = email.eid
Nicolas Chauvat's avatar
Nicolas Chauvat committed
76
77
78
79
80
81
82
83
        else:
            pemaileid = None
        for email in getattr(entity, 'use_email', ()):
            if email.eid == pemaileid:
                continue
            email.view('shortprimary', w=self.w, skipeids=done)


sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
84
class VCardPersonView(vcard.VCardCWUserView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
85
    """export a person information as a vcard"""
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
86
87
    __select__ = implements('Person')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
88
89
90
91
92
93
94
95
96
97
98
    def vcard_content(self, entity):
        w = self.w
        who = u'%s %s' % (entity.surname or '',
                          entity.firstname or '')
        w(u'FN:%s\n' % who)
        w(u'N:%s;;;;\n' % who)
        w(u'TITLE:%s\n' % who)
        # XXX
        #if entity.address:
        #    w(u'ADR;TYPE=WORK,POSTAL,PARCEL:;;%s\n' % ';'.join(entity.address.splitlines()))
        for phone in getattr(entity, 'phone', ()):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
99
            ptype = vcard.VCARD_PHONE_TYPES.get(phone.type, phone.type.upper())
Nicolas Chauvat's avatar
Nicolas Chauvat committed
100
101
102
103
104
105
106
            w(u'TEL;TYPE=%s;VOICE:%s\n' % (ptype, phone.number))
        for email in getattr(entity, 'use_email', ()):
            w(u'EMAIL;TYPE=INTERNET:%s\n' % email.address)


## actions ####################################################################

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
107
class AddPersonFromEmailAction(action.LinkToEntityAction):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
108
    id = 'addperson-fromemail'
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
109
110
    __select__ = implements('EmailAddress') & rql_condition('NOT P use_email X')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
111
112
113
114
115
    title = _('add Person use_email EmailAddress object')
    etype = 'Person'
    rtype = 'use_email'
    target = 'subject'

Nicolas Chauvat's avatar
Nicolas Chauvat committed
116
## facets #####################################################################
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
117

Nicolas Chauvat's avatar
Nicolas Chauvat committed
118
119
120
121
class CivilityFacet(AttributeFacet):
    id = 'civility-facet'
    __select__ = implements('Person')
    rtype = 'civility'