def get(self, request): # Get the state param from the request. state = str(request.GET.get('state')) # Replace %xx characters with single quotes and UTF8 decode the string. state = urllib.unquote(state).decode('utf8') # Deserialize the JSON string. state = anyjson.deserialize(b64decode(state)) if not validate_token(settings.SECRET_KEY, state.get('token'), request.user.pk): return HttpResponseBadRequest() error = request.GET.get('error') if error: messages.error( self.request, _('Sorry, Lily needs authorization from Google to synchronize your email account.' )) return HttpResponseRedirect('/#/preferences/emailaccounts') credentials = FLOW.step2_exchange(code=request.GET.get('code')) # Setup service to retrieve email address. service = build_gmail_service(credentials) profile = service.users().getProfile(userId='me').execute() # Create account based on email address. account, created = EmailAccount.objects.get_or_create( owner=request.user, email_address=profile.get('emailAddress')) # Store credentials based on new email account. storage = Storage(GmailCredentialsModel, 'id', account, 'credentials') storage.put(credentials) # Set account as authorized. account.is_authorized = True account.is_deleted = False account.label = account.label or account.email_address account.from_name = account.from_name or ' '.join( account.email_address.split('@')[0].split('.')).title() set_to_public = bool(int(state.get('public'))) if account.public is not set_to_public: account.public = set_to_public only_sync_new_mails = bool(int(state.get('only_new'))) if only_sync_new_mails and created: # Setting it before the first sync means it will only fetch changes starting now. account.history_id = profile.get('historyId') account.full_sync_finished = True account.save() post_intercom_event(event_name='email-account-added', user_id=request.user.id) return HttpResponseRedirect('/#/preferences/emailaccounts/edit/%s' % account.pk)
def get(self, request): if not validate_token(settings.SECRET_KEY, str(request.GET.get('state')), request.user.pk): return HttpResponseBadRequest() credentials = FLOW.step2_exchange(code=request.GET.get('code')) account = create_account(credentials, request.user) return HttpResponseRedirect('/#/preferences/emailaccounts/edit/%s' % account.pk)
def get(self, request): # Get the state param from the request. state = str(request.GET.get('state')) # Replace %xx characters with single quotes and UTF8 decode the string. state = urllib.unquote(state).decode('utf8') # Deserialize the JSON string. state = anyjson.deserialize(b64decode(state)) if not validate_token(settings.SECRET_KEY, state.get('token'), request.user.pk): return HttpResponseBadRequest() error = request.GET.get('error') if error: messages.error( self.request, _('Sorry, Lily needs authorization from Google to synchronize your email account.') ) return HttpResponseRedirect('/#/preferences/emailaccounts') credentials = FLOW.step2_exchange(code=request.GET.get('code')) # Setup service to retrieve email address. service = build_gmail_service(credentials) profile = service.users().getProfile(userId='me').execute() # Create account based on email address. account, created = EmailAccount.objects.get_or_create( owner=request.user, email_address=profile.get('emailAddress') ) # Store credentials based on new email account. storage = Storage(GmailCredentialsModel, 'id', account, 'credentials') storage.put(credentials) # Set account as authorized. account.is_authorized = True account.is_deleted = False account.label = account.label or account.email_address account.from_name = account.from_name or ' '.join(account.email_address.split('@')[0].split('.')).title() set_to_public = bool(int(state.get('public'))) if account.public is not set_to_public: account.public = set_to_public only_sync_new_mails = bool(int(state.get('only_new'))) if only_sync_new_mails and created: # Setting it before the first sync means it will only fetch changes starting now. account.history_id = profile.get('historyId') account.first_sync_finished = True account.save() post_intercom_event(event_name='email-account-added', user_id=request.user.id) return HttpResponseRedirect('/#/preferences/emailaccounts/edit/%s' % account.pk)
def get(self, request): if not validate_token(settings.SECRET_KEY, str( request.GET.get('state')), request.user.pk): return HttpResponseBadRequest() credentials = FLOW.step2_exchange(code=request.GET.get('code')) account = create_account(credentials, request.user) return HttpResponseRedirect('/#/preferences/emailaccounts/edit/%s' % account.pk)
def get(self, request): error = request.GET.get('error') if error: messages.error( self.request, _('Sorry, Lily needs authorization from Google to synchronize your email account.') ) return HttpResponseRedirect('/#/preferences/emailaccounts') # Get the state param from the request. state = str(request.GET.get('state')) # Replace %xx characters with single quotes and UTF8 decode the string. state = urllib.unquote(state).decode('utf8') # Deserialize the JSON string. state = anyjson.deserialize(b64decode(state)) if not validate_token(settings.SECRET_KEY, state.get('token'), request.user.pk): return HttpResponseBadRequest() credentials = FLOW.step2_exchange(code=request.GET.get('code')) # Setup service to retrieve email address from Google. gmail_service = GmailService(credentials) try: profile = gmail_service.execute_service(gmail_service.service.users().getProfile(userId='me')) except HttpError as error: error = anyjson.loads(error.content) error = error.get('error', error) if error.get('code') == 400 and error.get('message') == 'Mail service not enabled': messages.error(self.request, _('Mail is not enabled for this email account.')) else: messages.error(self.request, error.get('message')) if not request.user.info.registration_finished: # User is still busy with registration, so redirect to email account setup step again. return HttpResponseRedirect(reverse('register_email_account_setup')) else: return HttpResponseRedirect('/#/preferences/emailaccounts') # Create account based on email address. try: account, created = EmailAccount.objects.get_or_create( owner=request.user, tenant_id=request.user.tenant_id, email_address=profile.get('emailAddress') ) except EmailAccount.MultipleObjectsReturned: account, created = EmailAccount.objects.get_or_create( owner=request.user, tenant_id=request.user.tenant_id, email_address=profile.get('emailAddress'), is_deleted=False ) # Store credentials based on new email account. storage = Storage(GmailCredentialsModel, 'id', account, 'credentials') storage.put(credentials) account.is_deleted = False if request.user.tenant.billing.is_free_plan: account.privacy = EmailAccount.PRIVATE if created: account.only_new = None account.save() post_intercom_event(event_name='email-account-added', user_id=request.user.id) if not request.user.info.registration_finished: # User is still busy with registration, so redirect to the next step in the flow. return HttpResponseRedirect(reverse('register_email_account_details')) else: return HttpResponseRedirect('/#/preferences/emailaccounts/edit/%s' % account.pk)
def get(self, request): error = request.GET.get('error') if error: messages.error( self.request, _('Sorry, Lily needs authorization from Google to synchronize your email account.' )) return HttpResponseRedirect('/#/preferences/emailaccounts') # Get the state param from the request. state = str(request.GET.get('state')) # Replace %xx characters with single quotes and UTF8 decode the string. state = urllib.unquote(state).decode('utf8') # Deserialize the JSON string. state = anyjson.deserialize(b64decode(state)) if not validate_token(settings.SECRET_KEY, state.get('token'), request.user.pk): return HttpResponseBadRequest() credentials = FLOW.step2_exchange(code=request.GET.get('code')) # Setup service to retrieve email address from Google. gmail_service = GmailService(credentials) try: profile = gmail_service.execute_service( gmail_service.service.users().getProfile(userId='me')) except HttpError as error: error = anyjson.loads(error.content) error = error.get('error', error) if error.get('code') == 400 and error.get( 'message') == 'Mail service not enabled': messages.error( self.request, _('Mail is not enabled for this email account.')) else: messages.error(self.request, error.get('message')) # Adding email account failed, administer it as skipping the email setup or otherwise the user will be # stuck in redirect loop. user = self.request.user user.info.email_account_status = UserInfo.SKIPPED user.info.save() return HttpResponseRedirect('/#/preferences/emailaccounts') # Create account based on email address. try: account, created = EmailAccount.objects.get_or_create( owner=request.user, tenant_id=request.user.tenant_id, email_address=profile.get('emailAddress')) except EmailAccount.MultipleObjectsReturned: account, created = EmailAccount.objects.get_or_create( owner=request.user, tenant_id=request.user.tenant_id, email_address=profile.get('emailAddress'), is_deleted=False) # Store credentials based on new email account. storage = Storage(GmailCredentialsModel, 'id', account, 'credentials') storage.put(credentials) account.is_deleted = False if request.user.tenant.billing.is_free_plan: account.privacy = EmailAccount.PRIVATE if created: account.only_new = None account.save() post_intercom_event(event_name='email-account-added', user_id=request.user.id) if request.user.info and not request.user.info.email_account_status: # First time setup, so we want a different view. return HttpResponseRedirect( '/#/preferences/emailaccounts/setup/%s' % account.pk) else: return HttpResponseRedirect( '/#/preferences/emailaccounts/edit/%s' % account.pk)