def save(self, user): contacts_service = ContactsService(self.cleaned_data['email'], self.cleaned_data['password']) contacts_service.ProgrammaticLogin() #based on django-friends importer module entries = [] feed = contacts_service.GetContactsFeed() entries.extend(feed.entry) next_link = feed.GetNextLink() while next_link: feed = contacts_service.GetContactsFeed(uri=next_link.href) entries.extend(feed.entry) next_link = feed.GetNextLink() total = 0 imported = 0 for entry in entries: name = entry.title.text for e in entry.email: email = e.address total += 1 try: Contact.objects.get(user=user, email=email) except Contact.DoesNotExist: Contact(user=user, name=name, email=email).save() imported += 1 return imported, total
def clean(self): if 'email' in self.cleaned_data and 'password' in self.cleaned_data: contacts_service = ContactsService(self.cleaned_data['email'], self.cleaned_data['password']) try: contacts_service.ProgrammaticLogin() except gdata.service.BadAuthentication, msg: raise forms.ValidationError( _(u'Incorrect Google account credentials'))
def sync(info, entries, test=True): # setup client cli = ContactsService() cli.email = info['email'] cli.password = info['password'] cli.source = 'sas-contactsImport' cli.ProgrammaticLogin() # go through and find new groups groups = set("(Palm) " + c.category for c in entries) existing = [e.title.text for e in cli.GetGroupsFeed().entry] new_groups = groups.difference(existing) # create new groups for grp in new_groups: cli.CreateGroup(contacts.GroupEntry(title=atom.Title(text=grp))) # build group lookup dictionary feed = cli.GetGroupsFeed() groups_href = {} for entry in feed.entry: groups_href[str(entry.title.text)] = str(entry.id.text) # loop through contacts for contact in entries: if (test): contact.pprint() print "" continue # create basic info info = {} if contact.name: info['title'] = atom.Title(text=contact.name) # company info if contact.company or contact.title: org = contacts.Organization() org.org_name = contacts.OrgName(text=contact.company) org.org_title = contacts.OrgTitle(text=contact.title) info['organization'] = org # emails if contact.emails: info['email'] = [contacts.Email(address=s) for s in contact.emails] # phones if contact.phones: info['phone_number'] = [ contacts.PhoneNumber(text=num.text, rel=num.rel) for num in contact.phones ] # addresses if contact.addresses: info['postal_address'] = [ contacts.PostalAddress(text=addr.text, rel=addr.rel) for addr in contact.addresses ] # notes if contact.notes: info['content'] = atom.Content(text=contact.notes) # groups if contact.category: info['group_membership_info'] = [ contacts.GroupMembershipInfo( href=groups_href["(Palm) " + contact.category]) ] # save contact try: entry = contacts.ContactEntry(**info) cli.CreateContact(entry) except Exception as ex: print "\nTrouble with contact:" contact.pprint() print "----------------------------" traceback.print_exc() sys.exit()