def parse_contacts(self, contacts): "Break down CSV file into fields" for row in contacts: # Tidy up keys (iterkeys strip()) try: type = row['type'] except Exception: pass # Set type to default type try: name = row['name'] except Exception: try: firstname = row['firstname'] surname = row['surname'] name = firstname + " " + surname except Exception: continue contact_type = ContactType.objects.filter(name=type) if contact_type: contact_type = contact_type[0] # Create a new contact if it doesn't exist contact_exists = Contact.objects.filter(name=name, contact_type__name=type, trash=False) # TODO: If one does exist then append the data on that contact if not contact_exists: contact = Contact() contact.name = name contact.contact_type = contact_type contact.auto_notify = False contact.save() fields = contact_type.fields.filter(trash=False) for field in fields: if field.name in row: x = row[field.name] if field.field_type == 'email': x = self.verify_email(x) if field.field_type == 'url': x = self.verify_url(x) if x: contact_value = ContactValue() contact_value.field = field contact_value.contact = contact contact_value.value = x contact_value.save()
def parse_contacts(self, contacts): "Break down CSV file into fields" for row in contacts: # Tidy up keys (iterkeys strip()) try: type = row['type'] except Exception: pass # Set type to default type try: name = row['name'] except Exception: try: firstname = row['firstname'] surname = row['surname'] name = firstname + " " + surname except Exception: continue contact_type = ContactType.objects.filter(name=type) if contact_type: contact_type = contact_type[0] # Create a new contact if it doesn't exist contact_exists = Contact.objects.filter( name=name, contact_type__name=type, trash=False) # TODO: If one does exist then append the data on that contact if not contact_exists: contact = Contact() contact.name = name contact.contact_type = contact_type contact.auto_notify = False contact.save() fields = contact_type.fields.filter(trash=False) for field in fields: if field.name in row: x = row[field.name] if field.field_type == 'email': x = self.verify_email(x) if field.field_type == 'url': x = self.verify_url(x) if x: contact_value = ContactValue() contact_value.field = field contact_value.contact = contact contact_value.value = x contact_value.save()