def send(request, email): context = {} if request.method == 'POST': u = User.objects.get(email=email) c = Contact(sender=request.user, receiver=u, accepted=False) c.save() context['message'] = 'Sent!' return render(request, 'friends/message.html', context) return render(request, 'friends/profile.html', context)
def accept(request, email): context = {} if request.method == 'POST': u = User.objects.get(email=email) c = Contact.objects.get(sender=u, receiver=request.user, accepted=False) c.accepted = True c_compliment = Contact(sender=request.user, receiver=u, accepted=True) c.save() c_compliment.save() context['message'] = 'Request accepted.' return render(request, 'friends/message.html', context) return render(request, 'friends/profile.html', context)
def import_google(authsub_token, user): """ Uses the given AuthSub token to retrieve Google Contacts and import the entries with an email address into the contacts of the given user. Returns a tuple of (number imported, total number of entries). """ contacts_service = gdata.contacts.service.ContactsService() contacts_service.auth_token = authsub_token contacts_service.UpgradeToSessionToken() 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 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 create_contact_from_values(owner=None, type=None, **values): created = False email = values.get('email') if not email: return None, False try: contact = Contact.objects.get(owner=owner, email=email) except Contact.DoesNotExist: created = True contact = Contact(owner=owner, **values) try: contact.user = User.objects.get(email__iexact=email) except User.DoesNotExist: pass if type: contact.type = type contact.save() return contact, created
def loadGoogleAB(user) : import gdata.auth import gdata.contacts.service from friends.models import Contact try : oauth = OAuthStore.objects.get(user = user, source = 'google') token = oauth.token secret = oauth.secret except OAuthStore.DoesNotExist : token = None client = gdata.contacts.service.ContactsService() iparam = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, settings.GOOGLE_OAUTH_KEY, settings.GOOGLE_OAUTH_SECRET) #extra_parameters = { 'token' : token }) client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, settings.GOOGLE_OAUTH_KEY, settings.GOOGLE_OAUTH_SECRET) ot = gdata.auth.OAuthToken(token, secret, scopes=[client.GetFeedUri()], oauth_input_params=iparam) client.SetOAuthToken(ot) otoken = client.FetchOAuthRequestToken() feed = client.GetContactsFeed() data = [] for entry in feed.entry : for em in entry.email : try : c = Contact(user=user, name=entry.title.text, email=em.address, source='google') c.save() except : # Hopefully duplicate key pass
def import_yahoo(bbauth_token, user): """ Uses the given BBAuth token to retrieve a Yahoo Address Book and import the entries with an email address into the contacts of the given user. Returns a tuple of (number imported, total number of entries). """ ybbauth = ybrowserauth.YBrowserAuth(settings.BBAUTH_APP_ID, settings.BBAUTH_SHARED_SECRET) ybbauth.token = bbauth_token address_book_json = ybbauth.makeAuthWSgetCall( "http://address.yahooapis.com/v1/searchContacts?format=json&email.present=1&fields=name,email" ) address_book = json.loads(address_book_json) total = 0 imported = 0 for contact in address_book["contacts"]: total += 1 email = contact['fields'][0]['data'] try: first_name = contact['fields'][1]['first'] except (KeyError, IndexError): first_name = None try: last_name = contact['fields'][1]['last'] except (KeyError, IndexError): last_name = None if first_name and last_name: name = first_name + " " + last_name elif first_name: name = first_name elif last_name: name = last_name else: name = None 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 save(self, user): total, imported = 0, 0 for row in csv.reader(self.cleaned_data['csv_file']): if row: try: name, email = row except ValueError: #default behaviour continue 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 import_vcards(stream, user): """ Imports the given vcard stream into the contacts of the given user. Returns a tuple of (number imported, total number of cards). """ total = 0 imported = 0 for card in vobject.readComponents(stream): total += 1 try: name = card.fn.value email = card.email.value try: Contact.objects.get(user=user, email=email) except Contact.DoesNotExist: Contact(user=user, name=name, email=email).save() imported += 1 except AttributeError: pass # missing value so don't add anything return imported, total