| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.contrib.auth.decorators import login_required |
| 7 |
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect |
| 8 |
from django.urls import reverse, reverse_lazy |
| 9 |
from django.utils.decorators import method_decorator |
| 10 |
from django.views.generic.base import TemplateView |
| 11 |
from django.views.generic.edit import FormView |
| 12 |
|
| 13 |
from allauth.account.internal.decorators import login_not_required |
| 14 |
from allauth.account.internal.templatekit import get_entrance_context_data |
| 15 |
from allauth.core.internal.httpkit import authenticated_user |
| 16 |
from allauth.socialaccount.forms import DisconnectForm, SignupForm |
| 17 |
from allauth.socialaccount.internal import flows |
| 18 |
from allauth.socialaccount.models import SocialAccount, SocialLogin |
| 19 |
|
| 20 |
from ..account import app_settings as account_settings |
| 21 |
from ..account.views import ( |
| 22 |
AjaxCapableProcessFormViewMixin, |
| 23 |
CloseableSignupMixin, |
| 24 |
RedirectAuthenticatedUserMixin, |
| 25 |
) |
| 26 |
from ..utils import get_form_class |
| 27 |
from . import app_settings |
| 28 |
from .adapter import get_adapter |
| 29 |
|
| 30 |
|
| 31 |
class SignupView( |
| 32 |
RedirectAuthenticatedUserMixin, |
| 33 |
CloseableSignupMixin, |
| 34 |
AjaxCapableProcessFormViewMixin, |
| 35 |
FormView, |
| 36 |
): |
| 37 |
form_class = SignupForm |
| 38 |
template_name = f"socialaccount/signup.{account_settings.TEMPLATE_EXTENSION}" |
| 39 |
sociallogin: SocialLogin |
| 40 |
|
| 41 |
def get_form_class(self): |
| 42 |
return get_form_class(app_settings.FORMS, "signup", self.form_class) |
| 43 |
|
| 44 |
@method_decorator(login_not_required) |
| 45 |
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 46 |
sociallogin = flows.signup.get_pending_signup(request) |
| 47 |
if not sociallogin: |
| 48 |
return HttpResponseRedirect(reverse("account_login")) |
| 49 |
self.sociallogin = sociallogin |
| 50 |
return super().dispatch(request, *args, **kwargs) |
| 51 |
|
| 52 |
def is_open(self) -> bool: |
| 53 |
return get_adapter(self.request).is_open_for_signup( |
| 54 |
self.request, self.sociallogin |
| 55 |
) |
| 56 |
|
| 57 |
def get_form_kwargs(self) -> dict: |
| 58 |
ret = super().get_form_kwargs() |
| 59 |
ret["sociallogin"] = self.sociallogin |
| 60 |
return ret |
| 61 |
|
| 62 |
def form_valid(self, form) -> HttpResponse: |
| 63 |
return flows.signup.signup_by_form(self.request, self.sociallogin, form) |
| 64 |
|
| 65 |
def get_context_data(self, **kwargs) -> dict: |
| 66 |
ret = super().get_context_data(**kwargs) |
| 67 |
ret.update(get_entrance_context_data(self.request)) |
| 68 |
ret.update( |
| 69 |
dict( |
| 70 |
account=self.sociallogin.account, |
| 71 |
) |
| 72 |
) |
| 73 |
return ret |
| 74 |
|
| 75 |
def get_authenticated_redirect_url(self) -> str: |
| 76 |
return reverse("socialaccount_connections") |
| 77 |
|
| 78 |
|
| 79 |
signup = SignupView.as_view() |
| 80 |
|
| 81 |
|
| 82 |
@method_decorator(login_not_required, name="dispatch") |
| 83 |
class LoginCancelledView(TemplateView): |
| 84 |
template_name = ( |
| 85 |
f"socialaccount/login_cancelled.{account_settings.TEMPLATE_EXTENSION}" |
| 86 |
) |
| 87 |
|
| 88 |
def get_context_data(self, **kwargs) -> dict: |
| 89 |
ret = super().get_context_data(**kwargs) |
| 90 |
ret.update(get_entrance_context_data(self.request)) |
| 91 |
return ret |
| 92 |
|
| 93 |
|
| 94 |
login_cancelled = LoginCancelledView.as_view() |
| 95 |
|
| 96 |
|
| 97 |
class LoginErrorView(TemplateView): |
| 98 |
template_name = ( |
| 99 |
f"socialaccount/authentication_error.{account_settings.TEMPLATE_EXTENSION}" |
| 100 |
) |
| 101 |
|
| 102 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 103 |
return self.render_to_response( |
| 104 |
self.get_context_data(**kwargs), |
| 105 |
status=HTTPStatus.UNAUTHORIZED, |
| 106 |
) |
| 107 |
|
| 108 |
def get_context_data(self, **kwargs) -> dict: |
| 109 |
ret = super().get_context_data(**kwargs) |
| 110 |
ret.update(get_entrance_context_data(self.request)) |
| 111 |
return ret |
| 112 |
|
| 113 |
|
| 114 |
login_error = LoginErrorView.as_view() |
| 115 |
|
| 116 |
|
| 117 |
@method_decorator(login_required, name="dispatch") |
| 118 |
class ConnectionsView(AjaxCapableProcessFormViewMixin, FormView): |
| 119 |
template_name = f"socialaccount/connections.{account_settings.TEMPLATE_EXTENSION}" |
| 120 |
form_class = DisconnectForm |
| 121 |
success_url = reverse_lazy("socialaccount_connections") |
| 122 |
|
| 123 |
def get_form_class(self): |
| 124 |
return get_form_class(app_settings.FORMS, "disconnect", self.form_class) |
| 125 |
|
| 126 |
def get_form_kwargs(self) -> dict: |
| 127 |
kwargs = super().get_form_kwargs() |
| 128 |
kwargs["request"] = self.request |
| 129 |
return kwargs |
| 130 |
|
| 131 |
def form_valid(self, form) -> HttpResponse: |
| 132 |
form.save() |
| 133 |
return super().form_valid(form) |
| 134 |
|
| 135 |
def get_ajax_data(self) -> dict: |
| 136 |
account_data = [] |
| 137 |
for account in SocialAccount.objects.filter( |
| 138 |
user_id=authenticated_user(self.request).pk |
| 139 |
): |
| 140 |
provider_account = account.get_provider_account() |
| 141 |
account_data.append( |
| 142 |
{ |
| 143 |
"id": account.pk, |
| 144 |
"provider": account.provider, |
| 145 |
"name": provider_account.to_str(), |
| 146 |
} |
| 147 |
) |
| 148 |
return {"socialaccounts": account_data} |
| 149 |
|
| 150 |
|
| 151 |
connections = ConnectionsView.as_view() |