"""
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