'Using a given email tries to find an existing Contact or create new one if not found.
If name is not specified the given email address is used for name instead.'

def get_or_create_by_email(email, name=None, contact_type=None):

    created = False
    if (not contact_type):
        try:
            contact_type = ContactType.objects.get(slug='person')
        except ContactType.DoesNotExist:
            try:
                contact_type = ContactType.objects.all()[0]
            except KeyError:
                return (None, created)
    if (not name):
        name = email
    contact = Contact.objects.filter(contactvalue__value=email, contactvalue__field__field_type='email')[:1]
    if contact:
        return (contact[0], created)
    else:
        contact = Contact(contact_type=contact_type, name=name)
        contact.save()
        created = True
        try:
            emailfield = contact_type.fields.filter(field_type='email')[:1][0]
            ContactValue(field=emailfield, contact=contact, value=email).save()
        except IndexError:
            pass
    return (contact, created)
