A little refactoring for crud template and project structure
This commit is contained in:
@@ -3,25 +3,53 @@ from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.forms import forms
|
||||
from django.forms.widgets import HiddenInput
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils import timezone
|
||||
from django.views import View
|
||||
from django.views.generic import TemplateView, FormView, CreateView, ListView
|
||||
|
||||
from DepoT.mixins.crudListViewMixin import CRUDListViewMixin
|
||||
from common.models import ContainerTypeModel, LinesModel
|
||||
from common.utils.utils import filter_queryset_by_user, get_preinfo_by_number
|
||||
from preinfo.forms import PreinfoBaseForm, PreinfoCreateForm
|
||||
from preinfo.forms import PreinfoBaseForm, PreinfoCreateForm, PreinfoEditForm
|
||||
from preinfo.models import Preinfo
|
||||
|
||||
|
||||
|
||||
class ClientPreinfoView(LoginRequiredMixin, CreateView):
|
||||
template_name = 'client-preinfo-content.html'
|
||||
form_class = PreinfoCreateForm
|
||||
success_url = reverse_lazy('client_preinfo')
|
||||
class ClientPreinfoView(LoginRequiredMixin, CRUDListViewMixin, ListView):
|
||||
# template_name = 'client-preinfo-content.html'
|
||||
model = Preinfo
|
||||
template_name = 'client/preinfo-list.html'
|
||||
create_form_class = PreinfoCreateForm
|
||||
form_class = PreinfoEditForm
|
||||
context_object_name = 'objects'
|
||||
success_url = reverse_lazy('client_preinfo')
|
||||
base_template = 'client-base.html'
|
||||
|
||||
def get_object_data(self, obj):
|
||||
return {
|
||||
'container_number': obj.container_number,
|
||||
'container_type': obj.container_type.id,
|
||||
'line': obj.line.id
|
||||
}
|
||||
|
||||
def handle_form_submission(self, request, *args, **kwargs):
|
||||
object_id = request.POST.get('object_id')
|
||||
if object_id:
|
||||
obj = get_object_or_404(self.model, id=object_id)
|
||||
form = self.form_class(request.POST, instance=obj)
|
||||
else:
|
||||
form = self.create_form_class(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect(self.success_url)
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['form'] = self.form_class() # Add empty form to context
|
||||
context['create_form'] = self.create_form_class() # Add empty form to context
|
||||
context['base_template'] = self.base_template # Add empty form to context
|
||||
queryset = self.model.objects.all().order_by('-created_on')
|
||||
user = self.request.user
|
||||
# !!! important
|
||||
@@ -82,8 +110,33 @@ class PreinfoSearchView(View):
|
||||
return render(request, self.template_name, {'error': 'Not found'})
|
||||
|
||||
|
||||
class PreinfoListView(ListView):
|
||||
class PreinfoListView(CRUDListViewMixin, ListView):
|
||||
model = Preinfo
|
||||
template_name = 'employee/preinfo-list.html'
|
||||
context_object_name = 'preinfos'
|
||||
paginate_by = 30 # Number of items per page
|
||||
context_object_name = 'objects'
|
||||
paginate_by = 30
|
||||
form_class = PreinfoEditForm
|
||||
base_template = 'employee-base.html'
|
||||
|
||||
def get_object_data(self, obj):
|
||||
return {
|
||||
'container_number': obj.container_number,
|
||||
'container_type': obj.container_type.id,
|
||||
'line': obj.line.id
|
||||
}
|
||||
|
||||
def handle_form_submission(self, request, *args, **kwargs):
|
||||
object_id = request.POST.get('object_id')
|
||||
if object_id:
|
||||
obj = get_object_or_404(self.model, id=object_id)
|
||||
form = self.form_class(request.POST, instance=obj)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect('employee_preinfo')
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['form'] = self.form_class() # Add empty form to context
|
||||
context['base_template'] = self.base_template # Add empty form to context
|
||||
return context
|
||||
Reference in New Issue
Block a user