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

backend modifications to support individual confirmation pages for each social app

parent d689e354
......@@ -4,10 +4,13 @@ from __future__ import absolute_import, print_function
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.views.generic import FormView
from django.forms import Form
# third party imports
from allauth.account.adapter import get_adapter as get_account_adapter
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 braces.views import LoginRequiredMixin
......@@ -74,29 +77,78 @@ class AccountAdapter(DefaultSocialAccountAdapter):
raise ImmediateHttpResponse(update_redirect)
class RemoveSocialConfirmationView(LoginRequiredMixin, ConnectionsView):
class RemoveSocialConfirmationView(LoginRequiredMixin, FormView):
"""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
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"
login_url = 'login'
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!
messages.add_message(self.request,
messages.INFO,
"Select a social media icon to connect an account.")
return HttpResponseRedirect(reverse('update_student',
kwargs={'slug': self.request.user.username}))
else:
return super(RemoveSocialConfirmationView, self).get(request, *args, **kwargs)
def form_valid(self, form):
# copied directly, except using SUCCESS rather than INFO
get_account_adapter().add_message(self.request,
messages.SUCCESS,
'socialaccount/messages/'
'account_disconnected.txt')
form.save()
def get_context_data(self, **kwargs):
context = super(RemoveSocialConfirmationView, self).get_context_data(**kwargs)
current_url = self.request.get_full_path()
social_application = current_url.split('/')[6]
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())
def get_success_url(self):
......
......@@ -33,7 +33,7 @@ urlpatterns = [
DeleteStudent.as_view(), name='delete_student'),
# 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(),
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