mhu/4-port-to-django-5.1 Port to recent Django version #18
@ -1,24 +1,14 @@
|
||||
from django.shortcuts import render, redirect
|
||||
import uuid
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
from django.contrib.auth import logout, login
|
||||
from django.contrib.auth.forms import AuthenticationForm as AuthForm
|
||||
from django.contrib.auth.forms import PasswordChangeForm
|
||||
from django.contrib.auth import update_session_auth_hash
|
||||
import django.forms as forms
|
||||
from django.views.generic import TemplateView, DetailView
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from ..models import Storage, Stock, Component, Distributor, Manufacturer, Package, ComponentParameter, DistributorNum
|
||||
from django.core.paginator import Paginator
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import ProtectedError
|
||||
from ..forms import *
|
||||
from .component_import import import_components_from_csv
|
||||
from django.db.models import Q
|
||||
from django.db.models.functions import Lower
|
||||
from django.forms import formset_factory
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
from ..models import Storage, Stock
|
||||
from ..forms import *
|
||||
from .generic_views import BaseTemplateMixin
|
||||
|
||||
class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
||||
@ -85,7 +75,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
|
||||
def search_stock_queryset(self, search):
|
||||
stocks_in_storage = Stock.objects.filter(storage=self.object).order_by(Lower('component__name'))
|
||||
|
||||
|
||||
if search is None or search == '':
|
||||
return stocks_in_storage
|
||||
|
||||
@ -106,7 +96,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
stocks_in_storage = stocks_in_storage.filter(Q(component__name__icontains = search) |
|
||||
Q(component__package__name__icontains = search) |
|
||||
Q(component__manufacturer__name__icontains = search))
|
||||
|
||||
|
||||
return stocks_in_storage
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
@ -119,7 +109,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
stock_search_input = self.request.GET.get('search')
|
||||
|
||||
componente_stock_page = self.request.GET.get('stock_page', default=1)
|
||||
|
||||
|
||||
stock_paginator = Paginator(self.search_stock_queryset(stock_search_input), self.default_pagination_size)
|
||||
|
||||
context['storages'] = storage_paginator.get_page(storage_page)
|
||||
@ -160,7 +150,6 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
def handle_change_storage_post(self, request, **kwargs):
|
||||
f = ChangeStorageForm(data=request.POST)
|
||||
if f.is_valid():
|
||||
sub_name = f.cleaned_data['storage_name']
|
||||
try:
|
||||
self.object.name = f.cleaned_data['storage_name']
|
||||
self.object.verbose_name = f.cleaned_data.get('verbose_name')
|
||||
@ -175,20 +164,20 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
|
||||
def handle_del_storage_post(self, request, **kwargs):
|
||||
parent = self.object.parent_storage
|
||||
try:
|
||||
try:
|
||||
self.object.delete()
|
||||
except:
|
||||
context = self.get_context_data(**kwargs)
|
||||
context['delete_storage_errors'] = ['Error deleting Storage '+str(self.object)]
|
||||
return self.render_to_response(context)
|
||||
|
||||
|
||||
if parent is None:
|
||||
return redirect('parts-stocks')
|
||||
else:
|
||||
return redirect(reverse('parts-stocks-detail', kwargs={'uuid':parent.id}))
|
||||
|
||||
def handle_del_stock_post(self, request, **kwargs):
|
||||
del_error = None
|
||||
del_error = None # TODO: Check error handling. This is clearly not working as intended :P
|
||||
if 'stock_uuid' in request.POST:
|
||||
f = DeleteStockForm(data=request.POST)
|
||||
if f.is_valid():
|
||||
@ -199,16 +188,15 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
if s.storage == self.object:
|
||||
s.delete()
|
||||
else:
|
||||
del_error = 'Cannot delete stock from another storage.'
|
||||
del_error = 'Cannot delete stock from another storage.'
|
||||
except:
|
||||
del_error = 'Could not find requested stock in this storage.'
|
||||
|
||||
|
||||
context = self.get_context_data(**kwargs)
|
||||
return self.render_to_response(context)
|
||||
|
||||
|
||||
def handle_update_watermark(self, request, **kwargs):
|
||||
edit_form = EditWatermarkForm(data=request.POST)
|
||||
update_watermark_error = None
|
||||
if edit_form.is_valid():
|
||||
edit_form.save()
|
||||
else:
|
||||
@ -216,7 +204,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
|
||||
context = self.get_context_data(**kwargs)
|
||||
return self.render_to_response(context)
|
||||
|
||||
|
||||
def handle_amount_change_post(self, request, increase, **kwargs):
|
||||
edit_form = EditStockAmountForm(data=request.POST)
|
||||
if edit_form.is_valid():
|
||||
@ -259,7 +247,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
||||
elif 'submit-amount-reduce' in request.POST:
|
||||
return self.handle_amount_change_post(request, False, **kwargs)
|
||||
elif 'submit-amount-increase' in request.POST:
|
||||
return self.handle_amount_change_post(request, True, **kwargs)
|
||||
return self.handle_amount_change_post(request, True, **kwargs)
|
||||
elif 'submit-add-stock' in request.POST:
|
||||
return self.handle_add_stock_post(request, **kwargs)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user