Commit 88565977 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

backend modifications to support individual confirmation pages for each social app

parent d689e354
...@@ -4,10 +4,13 @@ from __future__ import absolute_import, print_function ...@@ -4,10 +4,13 @@ from __future__ import absolute_import, print_function
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib import messages from django.contrib import messages
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.views.generic import FormView
from django.forms import Form
# third party imports # third party imports
from allauth.account.adapter import get_adapter as get_account_adapter from allauth.account.adapter import get_adapter as get_account_adapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.socialaccount.views import ConnectionsView from allauth.socialaccount.models import SocialAccount
from allauth.socialaccount.signals import social_account_removed
from allauth.exceptions import ImmediateHttpResponse from allauth.exceptions import ImmediateHttpResponse
from braces.views import LoginRequiredMixin from braces.views import LoginRequiredMixin
...@@ -74,29 +77,78 @@ class AccountAdapter(DefaultSocialAccountAdapter): ...@@ -74,29 +77,78 @@ class AccountAdapter(DefaultSocialAccountAdapter):
raise ImmediateHttpResponse(update_redirect) raise ImmediateHttpResponse(update_redirect)
class RemoveSocialConfirmationView(LoginRequiredMixin, ConnectionsView): class RemoveSocialConfirmationView(LoginRequiredMixin, FormView):
"""To customize where users are sent when removing their social media connections. """To customize where users are sent when removing their social media connections.
We have written our own template to handle this feature that is much prettier than We have written our own template to handle this feature that is much prettier than
the one provided by allauth.""" the one provided by allauth."""
# we're not using this, but we're not allowed to have None
form_class = Form
template_name = "social/remove_social.html" template_name = "social/remove_social.html"
login_url = 'login' login_url = 'login'
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
if not request.user.socialaccount_set.all(): current_url = self.request.get_full_path()
# [u'', u'accounts', u'student', u'dbond2', u'settings', u'social', u'github', u'remove', u'']
social_application = current_url.split('/')[6]
connected_accounts = [account.provider
for account in request.user.socialaccount_set.all()]
# first, check that the user has an account of the type specified in the url
if not(social_application in connected_accounts):
# no social media accounts? back to the settings page with you! # no social media accounts? back to the settings page with you!
messages.add_message(self.request,
messages.INFO,
"Select a social media icon to connect an account.")
return HttpResponseRedirect(reverse('update_student', return HttpResponseRedirect(reverse('update_student',
kwargs={'slug': self.request.user.username})) kwargs={'slug': self.request.user.username}))
else: else:
return super(RemoveSocialConfirmationView, self).get(request, *args, **kwargs) return super(RemoveSocialConfirmationView, self).get(request, *args, **kwargs)
def form_valid(self, form): def get_context_data(self, **kwargs):
# copied directly, except using SUCCESS rather than INFO context = super(RemoveSocialConfirmationView, self).get_context_data(**kwargs)
get_account_adapter().add_message(self.request,
messages.SUCCESS, current_url = self.request.get_full_path()
'socialaccount/messages/' social_application = current_url.split('/')[6]
'account_disconnected.txt')
form.save() for account in self.request.user.socialaccount_set.all():
if account.provider == social_application:
branding = account.get_provider().name
context['branding'] = branding
context['application'] = social_application
return context
def post(self, request, *args, **kwargs):
current_url = self.request.get_full_path()
social_application = current_url.split('/')[6]
for account in request.user.socialaccount_set.all():
if account.provider == social_application:
social_account = account
branding = account.get_provider().name
# we do not need to use validate_disconnect, because accounts are not
# associated with being able to log in
try:
social_account.delete()
social_account_removed.send(sender=SocialAccount,
request=request,
socialaccount=social_account)
message = "%s has been successfully disconnected." % branding
messages.add_message(self.request,
messages.SUCCESS,
message)
# if multiple posts went in, there won't be any 'social_account' or 'branding'
# basically, that means it's already gone and it already works
except UnboundLocalError:
get_account_adapter().add_message(self.request,
messages.SUCCESS,
'socialaccount/messages/'
'account_disconnected.txt')
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
def get_success_url(self): def get_success_url(self):
......
...@@ -33,7 +33,7 @@ urlpatterns = [ ...@@ -33,7 +33,7 @@ urlpatterns = [
DeleteStudent.as_view(), name='delete_student'), DeleteStudent.as_view(), name='delete_student'),
# custom allauth page to disconnect a social media account # custom allauth page to disconnect a social media account
url(r'^student/(?P<slug>[\w-]+)/settings/social/remove/$', url(r'^student/(?P<slug>[\w-]+)/settings/social/(?P<application>[\w-]+)/remove/$',
RemoveSocialConfirmationView.as_view(), RemoveSocialConfirmationView.as_view(),
name='remove_social'), name='remove_social'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment