adapter.py 3.61 KB
Newer Older
1
# standard library imports
2
from __future__ import absolute_import, print_function
3
from datetime import datetime, timedelta
Daniel W Bond's avatar
Daniel W Bond committed
4
# core django imports
5 6
from django.core.urlresolvers import reverse
from django.views.generic import FormView
7 8
from django.contrib import messages
from django.http import HttpResponseRedirect
9
# third party imports
10 11 12
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.socialaccount.views import ConnectionsView
from allauth.socialaccount.forms import DisconnectForm
13
from allauth.exceptions import ImmediateHttpResponse
14
from braces.views import LoginRequiredMixin
15

16
class AccountAdapter(DefaultSocialAccountAdapter):
17

18
    # the request processed by the adapter is one from the successful oauth callback
19

20 21
    #def pre_social_login(self, request, sociallogin):
        #print(request.get_full_path(), 'pre_login')
22

23 24 25 26 27 28 29 30
    def populate_user(self, request, sociallogin, data):
        # we don't actually want to overwrite anything from the
        # social media account user
        user = sociallogin.user
        return user

    def get_connect_redirect_url(self, request, socialaccount):
        # where the user is sent if the social account is indeed authenticated
31
        assert request.user.is_authenticated()
32 33 34 35 36 37 38 39 40
        #print(request.get_full_path())
        #if 'welcome' in request.get_full_path():

        # ergo, we go with more of an approximation (at least for now)

        if not request.user.student.completedSocial:
            return reverse('welcomeSocial', kwargs={
                'slug': request.user.username,
            })
41
        else:
42
            return reverse('update_student', kwargs={
43
                'slug': request.user.username,
44 45 46 47 48 49 50 51 52
            })

    def authentication_error(self, request, provider_id, error=None, exception=None,
                             extra_context=None):

        error_message = """Looks like something went awry with your social
                           authentication. Wait a moment and try your username and
                           password again. If things are still broken, let us know by
                           sending an email to roomlist@lists.srct.gmu.edu."""
53

54 55 56 57 58 59 60 61
        if not request.user.student.completedSocial:
            messages.add_message(request, messages.ERROR, error_message)
            social_redirect = HttpResponseRedirect(reverse('welcomeSocial', kwargs={
                                  'slug': request.user.username,
                              }))
            raise ImmediateHttpResponse(social_redirect)
        else:
            messages.add_message(request, messages.ERROR, error_message)
62
            update_redirect =  HttpResponseRedirect(reverse('update_student', kwargs={
63 64 65
                                  'slug': request.user.username,
                              }))
            raise ImmediateHttpResponse(update_redirect)
66 67 68 69 70 71 72 73

class RemoveSocialConfirmationView(LoginRequiredMixin, ConnectionsView):
    template_name = "remove_social.html"
    login_url = 'login'

    def get(self, request, *args, **kwargs):
        if not request.user.socialaccount_set.all():
            # no social media accounts? back to the settings page with you!
74
            return HttpResponseRedirect(reverse('update_student',
75
                                        kwargs={'slug':self.request.user.username}))
76 77 78 79 80 81 82
        else:
            return super(RemoveSocialConfirmationView, self).get(request, *args, **kwargs)

    def form_valid(self, form):
        return super(RemoveSocialConfirmationView, self).form_valid(form)

    def get_success_url(self):
83
        return reverse('update_student',
84
                        kwargs={'slug':self.request.user.username})