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
|
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
|
|
|
|
from django.views.generic import CreateView
|
2020-08-03 16:35:36 +02:00
|
|
|
from django.views.generic import DeleteView
|
2020-01-07 21:59:12 +01:00
|
|
|
from django_tables2 import RequestConfig
|
|
|
|
|
2020-02-29 15:48:29 +01:00
|
|
|
from core import utils
|
2021-12-23 15:24:50 +01:00
|
|
|
from .forms import CustomerForm
|
2020-01-07 21:59:12 +01:00
|
|
|
from .models import Customer
|
|
|
|
from .tables import CustomersTable
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def customers_table_view(request):
|
2020-02-29 15:48:29 +01:00
|
|
|
table = CustomersTable(utils.get_customers(request.user))
|
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
|
|
|
|
def htmx_create_customer(request):
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class CustomerCreateView(LoginRequiredMixin, CreateView):
|
|
|
|
"""
|
|
|
|
A view to create a customer.
|
|
|
|
"""
|
|
|
|
model = Customer
|
|
|
|
template_name = 'customers/customer_create.html'
|
2020-06-09 21:43:34 +02:00
|
|
|
fields = ['name', 'description']
|
2020-05-23 14:18:16 +02:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('customer', args=(self.object.pk,))
|
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')
|