2020-06-02 15:58:20 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib import messages
|
2020-06-03 17:24:26 +00:00
|
|
|
from django.utils import timezone
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
from core.models import AdminProfile, ClientProfile, ClientGroup
|
2020-06-03 17:24:26 +00:00
|
|
|
from core.forms.profiles import AdminEditForm, AdminCreateForm, ClientEditForm, ClientCreateForm
|
2020-06-03 15:05:18 +00:00
|
|
|
from core.views.backend.generic import BackendFormView, BackendListView, BackendDeleteView, BackendUpdateView, BackendCreateView
|
2020-06-02 15:58:20 +00:00
|
|
|
from core.helpers.auth import request_password
|
|
|
|
|
|
|
|
### AdminProfiles
|
|
|
|
|
|
|
|
class AdminListView(BackendListView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/index.html"
|
|
|
|
model = AdminProfile
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Administrator Users"
|
|
|
|
return context
|
|
|
|
|
|
|
|
class AdminEditView(BackendFormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/update.html"
|
|
|
|
form_class = AdminEditForm
|
|
|
|
success_url = reverse_lazy("admins")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Edit Administrator"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
|
|
|
admin = get_user_model().objects.get(id=self.kwargs["pk"])
|
|
|
|
assert type(admin.profile) == AdminProfile
|
|
|
|
initial["first_name"] = admin.first_name
|
|
|
|
initial["last_name"] = admin.last_name
|
|
|
|
initial["email"] = admin.username
|
|
|
|
initial["mobile"] = admin.profile.mobile
|
|
|
|
initial["role"] = admin.profile.role
|
|
|
|
initial["display_name"] = admin.profile.display_name
|
|
|
|
return initial
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
admin = get_user_model().objects.get(id=self.kwargs["pk"])
|
|
|
|
admin.first_name = form.cleaned_data["first_name"]
|
|
|
|
admin.last_name = form.cleaned_data["last_name"]
|
|
|
|
admin.username = form.cleaned_data["email"]
|
|
|
|
admin.email = form.cleaned_data["email"]
|
|
|
|
admin.profile.mobile = form.cleaned_data["mobile"]
|
|
|
|
admin.profile.role = form.cleaned_data["role"]
|
|
|
|
admin.profile.display_name = form.cleaned_data["display_name"]
|
|
|
|
|
|
|
|
if form.cleaned_data["image"] or form.cleaned_data["remove_image"]:
|
|
|
|
admin.profile.image = form.cleaned_data["image"]
|
|
|
|
|
|
|
|
admin.profile.save()
|
|
|
|
admin.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
class AdminDeleteView(BackendDeleteView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/delete.html"
|
|
|
|
model = get_user_model()
|
|
|
|
success_url = reverse_lazy("admins")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Delete Administrator"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
admin = super().get_object(queryset=queryset)
|
|
|
|
assert type(admin.profile) == AdminProfile
|
|
|
|
return admin
|
|
|
|
|
|
|
|
class AdminCreateView(BackendFormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/create.html"
|
|
|
|
form_class = AdminCreateForm
|
|
|
|
success_url = reverse_lazy("admins")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Create Administrator"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
admin = get_user_model()()
|
|
|
|
admin.first_name = form.cleaned_data["first_name"]
|
|
|
|
admin.last_name = form.cleaned_data["last_name"]
|
|
|
|
admin.username = form.cleaned_data["email"]
|
|
|
|
admin.email = form.cleaned_data["email"]
|
|
|
|
|
|
|
|
profile = AdminProfile()
|
|
|
|
profile.user = admin
|
|
|
|
profile.mobile = form.cleaned_data["mobile"]
|
|
|
|
profile.role = form.cleaned_data["role"]
|
|
|
|
profile.display_name = form.cleaned_data["display_name"]
|
|
|
|
profile.image = form.cleaned_data["image"]
|
|
|
|
|
|
|
|
admin.save()
|
|
|
|
profile.save()
|
|
|
|
|
|
|
|
request_password(admin)
|
|
|
|
|
|
|
|
messages.success(self.request, f"User {admin.get_full_name} was successfully created. They should receive an email to set their password shortly.")
|
|
|
|
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
### ClientProfiles
|
|
|
|
|
|
|
|
class ClientListView(BackendListView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/index.html"
|
|
|
|
model = ClientProfile
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Clients"
|
|
|
|
return context
|
|
|
|
|
|
|
|
class ClientEditView(BackendFormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/update.html"
|
2020-06-03 17:24:26 +00:00
|
|
|
form_class = ClientEditForm
|
2020-06-02 15:58:20 +00:00
|
|
|
success_url = reverse_lazy("clients")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Edit Client"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
2020-06-03 17:24:26 +00:00
|
|
|
client = ClientProfile.objects.get(id=self.kwargs["pk"]).user
|
2020-06-02 15:58:20 +00:00
|
|
|
initial["first_name"] = client.first_name
|
|
|
|
initial["last_name"] = client.last_name
|
|
|
|
initial["email"] = client.username
|
|
|
|
initial["mobile"] = client.profile.mobile
|
2020-06-03 17:24:26 +00:00
|
|
|
initial["address1"] = client.profile.address1
|
|
|
|
initial["address2"] = client.profile.address2
|
|
|
|
initial["zip"] = client.profile.zip
|
|
|
|
initial["city"] = client.profile.city
|
|
|
|
initial["state"] = client.profile.state
|
|
|
|
initial["country"] = client.profile.country
|
|
|
|
initial["vat_id"] = client.profile.vat_id
|
|
|
|
initial["company_id"] = client.profile.company_id
|
|
|
|
initial["default_currency"] = client.profile.default_currency
|
|
|
|
initial["brands"] = client.profile.brands.all()
|
2020-06-04 04:21:13 +00:00
|
|
|
initial["client_groups"] = client.profile.client_groups.all()
|
2020-06-03 17:24:26 +00:00
|
|
|
initial["marketing_opt_in"] = bool(client.profile.marketing_opt_in)
|
|
|
|
initial["pgp_key"] = client.profile.pgp_key
|
2020-06-02 15:58:20 +00:00
|
|
|
return initial
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-06-04 04:21:13 +00:00
|
|
|
client = ClientProfile.objects.get(id=self.kwargs["pk"]).user
|
2020-06-02 15:58:20 +00:00
|
|
|
client.first_name = form.cleaned_data["first_name"]
|
|
|
|
client.last_name = form.cleaned_data["last_name"]
|
|
|
|
client.username = form.cleaned_data["email"]
|
|
|
|
client.email = form.cleaned_data["email"]
|
|
|
|
client.profile.mobile = form.cleaned_data["mobile"]
|
2020-06-03 17:24:26 +00:00
|
|
|
client.profile.address1 = form.cleaned_data["address1"]
|
|
|
|
client.profile.address2 = form.cleaned_data["address2"]
|
|
|
|
client.profile.zip = form.cleaned_data["zip"]
|
|
|
|
client.profile.city = form.cleaned_data["city"]
|
|
|
|
client.profile.state = form.cleaned_data["state"]
|
|
|
|
client.profile.country = form.cleaned_data["country"]
|
|
|
|
client.profile.vat_id = form.cleaned_data["vat_id"]
|
|
|
|
client.profile.company_id = form.cleaned_data["company_id"]
|
|
|
|
client.profile.default_currency = form.cleaned_data["default_currency"]
|
|
|
|
client.profile.brands.set(form.cleaned_data["brands"])
|
2020-06-04 04:21:13 +00:00
|
|
|
client.profile.client_groups.set(form.cleaned_data["client_groups"])
|
2020-06-03 17:24:26 +00:00
|
|
|
client.profile.marketing_opt_in = timezone.now() if form.cleaned_data["marketing_opt_in"] and not client.profile.marketing_opt_in else None
|
|
|
|
client.profile.pgp_key = form.cleaned_data["pgp_key"]
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
client.profile.save()
|
|
|
|
client.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
class ClientDeleteView(BackendDeleteView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/delete.html"
|
|
|
|
model = get_user_model()
|
|
|
|
success_url = reverse_lazy("clients")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Delete Client"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
admin = super().get_object(queryset=queryset)
|
|
|
|
assert type(admin.profile) == ClientProfile
|
|
|
|
return admin
|
|
|
|
|
|
|
|
class ClientCreateView(BackendFormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/create.html"
|
2020-06-03 17:24:26 +00:00
|
|
|
form_class = ClientCreateForm
|
2020-06-02 15:58:20 +00:00
|
|
|
success_url = reverse_lazy("admins")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Create Client"
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
client = get_user_model()()
|
|
|
|
client.first_name = form.cleaned_data["first_name"]
|
|
|
|
client.last_name = form.cleaned_data["last_name"]
|
|
|
|
client.username = form.cleaned_data["email"]
|
|
|
|
client.email = form.cleaned_data["email"]
|
|
|
|
|
|
|
|
profile = ClientProfile()
|
|
|
|
profile.user = client
|
|
|
|
profile.mobile = form.cleaned_data["mobile"]
|
2020-06-03 17:24:26 +00:00
|
|
|
profile.address1 = form.cleaned_data["address1"]
|
|
|
|
profile.address2 = form.cleaned_data["address2"]
|
|
|
|
profile.zip = form.cleaned_data["zip"]
|
|
|
|
profile.city = form.cleaned_data["city"]
|
|
|
|
profile.state = form.cleaned_data["state"]
|
|
|
|
profile.country = form.cleaned_data["country"]
|
|
|
|
profile.vat_id = form.cleaned_data["vat_id"]
|
|
|
|
profile.company_id = form.cleaned_data["company_id"]
|
|
|
|
profile.default_currency = form.cleaned_data["default_currency"]
|
|
|
|
profile.marketing_opt_in = timezone.now() if form.cleaned_data["marketing_opt_in"] else None
|
|
|
|
profile.pgp_key = form.cleaned_data["pgp_key"]
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
client.save()
|
|
|
|
profile.save()
|
|
|
|
|
2020-06-03 17:24:26 +00:00
|
|
|
profile.brands.set(form.cleaned_data["brands"])
|
2020-06-04 04:21:13 +00:00
|
|
|
profile.client_groups.set(form.cleaned_data["client_groups"])
|
2020-06-03 17:24:26 +00:00
|
|
|
profile.save()
|
|
|
|
|
|
|
|
if form.cleaned_data["send_password"]:
|
|
|
|
request_password(client)
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
messages.success(self.request, f"Client {client.get_full_name} was successfully created. They should receive an email to set their password shortly.")
|
|
|
|
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
### ClientGroups
|
|
|
|
|
|
|
|
class ClientGroupListView(BackendListView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clientgroups/index.html"
|
|
|
|
model = ClientGroup
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Client Group Settings"
|
|
|
|
return context
|
|
|
|
|
|
|
|
class ClientGroupEditView(BackendUpdateView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clientgroups/update.html"
|
|
|
|
model = ClientGroup
|
|
|
|
success_url = reverse_lazy("clientgroups")
|
|
|
|
fields = ["name", "color"]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Edit Client Group"
|
|
|
|
return context
|
|
|
|
|
|
|
|
class ClientGroupDeleteView(BackendDeleteView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clientgroups/delete.html"
|
|
|
|
model = ClientGroup
|
|
|
|
success_url = reverse_lazy("clientgroups")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Delete Client Group"
|
|
|
|
return context
|
|
|
|
|
|
|
|
class ClientGroupCreateView(BackendCreateView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/clientgroups/create.html"
|
|
|
|
model = ClientGroup
|
|
|
|
success_url = reverse_lazy("clientgroups")
|
|
|
|
fields = ["name", "color"]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Create Client Group"
|
|
|
|
return context
|