1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | """
Functions for generating individual and group vCards from Django
The functions assume a "Person" object with the following fields:
firstname
lastname
email
phone
id (automatically created by Django)
This code uses VObject: http://vobject.skyhouseconsulting.com/
"""
import vobject
def _vcard_string(person):
"""
Helper function for vcard views. Accepts a 'person' object
with certain attributes (firstname, lastname, email, phone, id)
and returns a string containing serialized vCard data.
"""
# vobject API is a bit verbose...
v = vobject.vCard()
v.add('n')
v.n.value = vobject.vcard.Name(family=person.lastname, given=person.firstname)
v.add('fn')
v.fn.value = "%s %s" % (person.firstname, person.lastname)
v.add('email')
v.email.value = person.email
v.add('tel')
v.tel.value = person.phone
v.tel.type_param = 'WORK'
v.add('url')
v.url.value = "http://example.org/people/%s/" % person.id
output = v.serialize()
return output
def vcard(request, person_id):
"""
View function for returning single vcard
"""
person = Person.objects.get(pk=person_id)
output = _vcard_string(person)
filename = "%s%s.vcf" % (person.firstname, person.lastname)
response = HttpResponse(output, mimetype="text/x-vCard")
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
def group_vcard(request):
"""
View function for returning group vcard
"""
all = Person.objects.order_by('lastname', 'firstname')
output = '\n'.join(_vcard_string(one) for one in all)
response = HttpResponse(output, mimetype="text/x-vCard")
response['Content-Disposition'] = 'attachment; filename=example_org_people.vcf'
return response
|
Comments