2021-12-23 22:44:31 +01:00
|
|
|
from django_tables2 import RequestConfig
|
2020-01-07 21:59:12 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2021-12-22 19:14:18 +01:00
|
|
|
from django.http.response import HttpResponse
|
2020-01-07 20:11:36 +01:00
|
|
|
from django.shortcuts import render
|
2022-03-23 12:18:40 +01:00
|
|
|
from django.template.context_processors import csrf
|
2021-12-23 15:24:50 +01:00
|
|
|
from django.template.response import TemplateResponse
|
2020-05-23 14:18:16 +02:00
|
|
|
from django.urls import reverse
|
2020-08-03 16:35:36 +02:00
|
|
|
from django.views.generic import DeleteView
|
2020-01-07 21:59:12 +01:00
|
|
|
|
2022-03-23 12:18:40 +01:00
|
|
|
from crispy_forms.utils import render_crispy_form
|
|
|
|
from crispy_forms.templatetags.crispy_forms_filters import as_crispy_field
|
|
|
|
|
2020-02-29 15:48:29 +01:00
|
|
|
from core import utils
|
2021-12-23 15:24:50 +01:00
|
|
|
from .forms import CustomerForm
|
2022-03-23 13:49:58 +01:00
|
|
|
from .forms import DummyLocationForm
|
2022-03-23 12:18:40 +01:00
|
|
|
from .forms import LocationForm
|
2022-03-23 13:49:58 +01:00
|
|
|
from .models import Customer, DummyLocation
|
2020-01-07 21:59:12 +01:00
|
|
|
from .tables import CustomersTable
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def customers_table_view(request):
|
2021-12-30 14:57:47 +01:00
|
|
|
customers = utils.objects_for_allowed_customers(
|
2021-12-24 12:48:56 +01:00
|
|
|
Customer, request.user)
|
2021-12-23 22:12:14 +01:00
|
|
|
table = CustomersTable(customers)
|
2020-01-07 21:59:12 +01:00
|
|
|
RequestConfig(request).configure(table)
|
2020-02-29 21:07:52 +01:00
|
|
|
return render(request,
|
|
|
|
'customers/customer_list.html',
|
|
|
|
{'customers': table})
|
2020-01-07 21:59:12 +01:00
|
|
|
|
|
|
|
|
2021-12-22 19:14:18 +01:00
|
|
|
@login_required
|
2022-02-10 21:40:49 +01:00
|
|
|
def create_customer(request):
|
2021-12-22 19:14:18 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
form = CustomerForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save(commit=True)
|
2021-12-23 15:10:29 +01:00
|
|
|
return HttpResponse("<script>location.reload()</script>")
|
2021-12-22 19:14:18 +01:00
|
|
|
else:
|
2021-12-23 15:24:50 +01:00
|
|
|
return TemplateResponse(request,
|
|
|
|
"customers/partials/customer_create.html",
|
|
|
|
context={"form": form})
|
2021-12-22 19:14:18 +01:00
|
|
|
form = CustomerForm()
|
2021-12-23 15:24:50 +01:00
|
|
|
context = {"form": form}
|
2021-12-23 18:38:00 +01:00
|
|
|
return TemplateResponse(request,
|
|
|
|
"customers/partials/customer_create.html",
|
|
|
|
context)
|
2021-12-21 17:51:57 +01:00
|
|
|
|
|
|
|
|
2021-12-23 18:50:03 +01:00
|
|
|
@login_required
|
|
|
|
def customer_detail_view(request, pk):
|
2021-12-23 19:28:47 +01:00
|
|
|
customer = utils.get_object_with_view_permission(
|
|
|
|
Customer, user=request.user, pk=pk)
|
|
|
|
context = {'customer': customer}
|
|
|
|
return TemplateResponse(request,
|
|
|
|
"customers/customer_details.html",
|
|
|
|
context)
|
2020-05-23 14:18:16 +02:00
|
|
|
|
|
|
|
|
2020-08-03 16:35:36 +02:00
|
|
|
class CustomerDeleteView(LoginRequiredMixin, DeleteView):
|
|
|
|
model = Customer
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2021-12-23 15:11:41 +01:00
|
|
|
return reverse('customers')
|
2022-03-23 12:18:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def htmx_create_location(request):
|
|
|
|
context = {}
|
|
|
|
user = request.user
|
|
|
|
if request.method == "POST" and 'save_location' in request.POST:
|
|
|
|
form = LocationForm(request.POST, user=user)
|
|
|
|
if form.is_valid():
|
2022-03-23 13:49:58 +01:00
|
|
|
location = form.save(commit=True)
|
|
|
|
dummy_model = DummyLocation()
|
|
|
|
dummy_model.location = location
|
|
|
|
dummy_form = DummyLocationForm(instance=dummy_model)
|
|
|
|
form_html = as_crispy_field(dummy_form["location"])
|
2022-03-23 12:18:40 +01:00
|
|
|
else:
|
|
|
|
context.update(csrf(request))
|
|
|
|
form.helper.attrs['hx-swap-oob'] = 'true'
|
|
|
|
form_html = render_crispy_form(form)
|
|
|
|
context["valid"] = form.is_valid()
|
|
|
|
context['form'] = form_html
|
|
|
|
template_path = "customers/partials/location_response.html"
|
|
|
|
return TemplateResponse(request, template_path, context)
|
|
|
|
|
|
|
|
form = LocationForm(user=user)
|
|
|
|
context["form"] = form
|
|
|
|
template_path = "customers/partials/location_create.html"
|
|
|
|
return TemplateResponse(request, template_path, context)
|