from django.conf import settings
from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.shortcuts import redirect
from django.utils.http import is_safe_url
from django.utils.translation import gettext_lazy as _
from django.views.generic import View
from registration.backends.default import views as default_views
from registration.backends.simple import views as simple_views
from shuup.core.models import get_company_contact, get_person_contact
from shuup.front.apps.registration.forms import CompanyRegistrationForm
from shuup.front.template_helpers import urls
from shuup.front.utils.companies import allow_company_registration
[docs]
def activation_complete(request):
messages.success(request, _("Activation successful"))
if urls.has_url("shuup:customer_edit"):
return redirect("shuup:customer_edit")
else:
return redirect(settings.LOGIN_REDIRECT_URL)
[docs]
def registration_complete(request):
if settings.SHUUP_REGISTRATION_REQUIRES_ACTIVATION:
messages.success(
request,
_("Registration complete. Please follow the instructions sent to your email address."),
)
return redirect(settings.LOGIN_REDIRECT_URL)
[docs]
class RegistrationViewMixin:
template_name = "shuup/registration/register.jinja"
[docs]
def get_success_url(self, *args, **kwargs):
url = self.request.GET.get(REDIRECT_FIELD_NAME) or self.request.POST.get(REDIRECT_FIELD_NAME)
if url and is_safe_url(url, self.request.get_host()):
return url
return ("shuup:registration_complete", (), {})
[docs]
def register(self, form):
user = super().register(form)
get_person_contact(user).add_to_shop(self.request.shop)
return user
[docs]
class RegistrationNoActivationView(RegistrationViewMixin, simple_views.RegistrationView):
pass
[docs]
class RegistrationWithActivationView(RegistrationViewMixin, default_views.RegistrationView):
SEND_ACTIVATION_EMAIL = False
[docs]
class RegistrationView(View):
[docs]
def dispatch(self, request, *args, **kwargs):
if settings.SHUUP_REGISTRATION_REQUIRES_ACTIVATION:
view_class = RegistrationWithActivationView
else:
view_class = RegistrationNoActivationView
return view_class.as_view()(request, *args, **kwargs)
[docs]
class CompanyRegistrationView(RegistrationViewMixin, default_views.RegistrationView):
template_name = "shuup/registration/company_register.jinja"
form_class = CompanyRegistrationForm
SEND_ACTIVATION_EMAIL = False
[docs]
def dispatch(self, request, *args, **kwargs):
if not allow_company_registration(request.shop):
return redirect("shuup:registration_register")
return super().dispatch(request, *args, **kwargs)
[docs]
def register(self, form):
user = super().register(form)
if settings.SHUUP_ENABLE_MULTIPLE_SHOPS and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP:
company = get_company_contact(user)
company.add_to_shop(self.request.shop)
[docs]
class ActivationView(default_views.ActivationView):
template_name = "shuup/registration/activation_failed.jinja"
[docs]
def get_success_url(self, *args, **kwargs):
return ("shuup:registration_activation_complete", (), {})