# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2021, Shuup Commerce Inc. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.views import PasswordChangeView
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, TemplateView
from shuup.core.models import CompanyContact, SavedAddress, get_company_contact
from shuup.front.utils.companies import allow_company_registration
from shuup.front.views.dashboard import DashboardViewMixin
from shuup.utils.django_compat import reverse_lazy
from shuup.utils.importing import cached_load
[docs]class CustomPasswordChangeView(PasswordChangeView):
template_name = "shuup/customer_information/change_password.jinja"
success_url = reverse_lazy("shuup:customer_edit")
form_class = PasswordChangeForm
[docs] def post(self, *args, **kwargs):
response = super().post(*args, **kwargs)
if response.status_code == 302:
messages.success(self.request, _("Password changed."))
return response
[docs]class CustomerEditView(DashboardViewMixin, FormView):
template_name = "shuup/customer_information/edit_customer.jinja"
[docs]class CompanyEditView(DashboardViewMixin, FormView):
template_name = "shuup/customer_information/edit_company.jinja"
[docs] def dispatch(self, request, *args, **kwargs):
if not (bool(get_company_contact(self.request.user)) or allow_company_registration(self.request.shop)):
return redirect("shuup:customer_edit")
return super(CompanyEditView, self).dispatch(request, *args, **kwargs)
[docs] def get_context_data(self, **kwargs):
context = super(CompanyEditView, self).get_context_data(**kwargs)
context["pending_company_approval"] = CompanyContact.objects.filter(
members__in=[self.request.customer], is_active=False
).exists()
return context
[docs]class AddressBookView(DashboardViewMixin, TemplateView):
template_name = "shuup/customer_information/addressbook/index.jinja"
[docs] def get_context_data(self, **kwargs):
context = super(AddressBookView, self).get_context_data(**kwargs)
context["addresses"] = SavedAddress.objects.filter(owner=self.request.customer)
context["customer"] = self.request.customer
return context
[docs]class AddressBookEditView(DashboardViewMixin, FormView):
template_name = "shuup/customer_information/addressbook/edit.jinja"
instance = None
[docs] def dispatch(self, request, *args, **kwargs):
try:
self.instance = SavedAddress.objects.get(pk=kwargs.get("pk", 0), owner=self.request.customer)
except Exception:
self.instance = None
return super(AddressBookEditView, self).dispatch(request, *args, **kwargs)
[docs]def delete_address(request, pk):
try:
SavedAddress.objects.get(pk=pk, owner=request.customer).delete()
except SavedAddress.DoesNotExist:
messages.error(request, _("Cannot remove address because it doesn't exist."))
return redirect("shuup:address_book")