← Back to team overview

wikipbx-dev team mailing list archive

Re: address book application

 

Il 05/11/2010 14:13, Stas Shtin ha scritto:
8. Export templates should be stored in django templates (i.e.
contacts/templates/vcard_2_1.vcf). Unset entries should be skipped when
generating exported data to keep file size down.

Do you mean to use it as standard django template or just store the skeleton vcf file there? I've tried the first method but does not work because of missing \r. See attached patch.

riccardo
=== added file 'wikipbx/contacts/templates/vcard_2_1.vcf'
--- wikipbx/contacts/templates/vcard_2_1.vcf	1970-01-01 00:00:00 +0000
+++ wikipbx/contacts/templates/vcard_2_1.vcf	2010-11-05 17:10:01 +0000
@@ -0,0 +1,9 @@
+{% for vcard in contacts %}
+BEGIN:VCARD
+VERSION:2.1
+FN: {{ vcard.name }}
+N: {{ vcard.name}}
+TEL;HOME:{{ vcard.numbers }}
+END:VCARD
+
+{% endfor %}

=== modified file 'wikipbx/contacts/views.py'
--- wikipbx/contacts/views.py	2010-11-05 16:15:01 +0000
+++ wikipbx/contacts/views.py	2010-11-05 17:09:03 +0000
@@ -1,4 +1,6 @@
 from django.views.decorators.http import require_POST
+from django.template.loader import get_template
+from django.template import Context
 from django.utils.translation import gettext as _
 from django.shortcuts import get_object_or_404
 from django.core.urlresolvers import reverse
@@ -52,21 +54,9 @@
     return http.HttpResponseRedirect(
         reverse('contact-list') + "?infomsg=%s" % msg)
 
-VCF_SKELETON = \
-"""BEGIN:VCARD\r
-VERSION:2.1\r
-FN: %s\r
-N: %s\r
-TEL;HOME:%s\r
-END:VCARD\r
-
-"""
-
-def gigaset_vcard_entry(contact):
-    return VCF_SKELETON % (contact.name, contact.name, contact.number)
-
 def gigaset_export(contacts):
-    vcf = string.join(map(gigaset_vcard_entry, contacts), "")
+    t = get_template("vcard_2_1.vcf")
+    vcf = t.render(Context({'contacts': contacts}))
     response = http.HttpResponse(vcf, mimetype="text/x-vcard")
     response['Content-Disposition'] = 'filename=teledir.vcf'
     return response


Follow ups

References