2020-04-16 13:22:03 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
from core.models import AdminProfile
|
2020-04-18 13:02:41 +00:00
|
|
|
from core.forms import AdminEditForm
|
2020-05-21 12:54:59 +00:00
|
|
|
from core.views.generic import BackendFormView as FormView, BackendListView as ListView, BackendDeleteView as DeleteView
|
2020-04-16 13:22:03 +00:00
|
|
|
|
|
|
|
class AdminListView(ListView):
|
|
|
|
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(FormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/update.html"
|
2020-04-18 13:02:41 +00:00
|
|
|
form_class = AdminEditForm
|
|
|
|
success_url = reverse_lazy("admins")
|
2020-04-16 13:22:03 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-04-18 13:02:41 +00:00
|
|
|
context["title"] = "Edit Administrator"
|
2020-04-16 13:22:03 +00:00
|
|
|
return context
|
|
|
|
|
2020-04-18 13:02:41 +00:00
|
|
|
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)
|
|
|
|
|
2020-04-16 13:22:03 +00:00
|
|
|
class AdminDeleteView(DeleteView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/delete.html"
|
|
|
|
model = get_user_model()
|
2020-04-18 13:02:41 +00:00
|
|
|
success_url = reverse_lazy("admins")
|
2020-04-16 13:22:03 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["title"] = "Delete Administrator"
|
|
|
|
return context
|
|
|
|
|
2020-04-18 13:02:41 +00:00
|
|
|
def get_object(self, queryset=None):
|
|
|
|
admin = super().get_object(queryset=queryset)
|
|
|
|
assert type(admin.profile) == AdminProfile
|
|
|
|
return admin
|
|
|
|
|
2020-04-16 13:22:03 +00:00
|
|
|
class AdminCreateView(FormView):
|
|
|
|
template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/create.html"
|
|
|
|
model = get_user_model()
|
2020-04-18 13:02:41 +00:00
|
|
|
success_url = reverse_lazy("admins")
|
2020-04-16 13:22:03 +00:00
|
|
|
fields = ["key", "value"]
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-04-18 13:02:41 +00:00
|
|
|
context["title"] = "Create Administrator"
|
2020-04-16 13:22:03 +00:00
|
|
|
return context
|