From 88bebfa2c86a40ffcaecb19e4de25e21832ea2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 7 Nov 2021 20:36:12 +0100 Subject: [PATCH] Implement change amount / watermark form --- shimatta_kenkyusho/parts/forms.py | 85 ++++++++++++++----- shimatta_kenkyusho/parts/views.py | 27 +++++- .../parts/modals/update-stock-modal.html | 27 ++++-- .../templates/parts/stocks-detail.html | 6 +- 4 files changed, 111 insertions(+), 34 deletions(-) diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 81fda80..e84bbd3 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -1,4 +1,5 @@ from django import forms +from django.core.exceptions import ValidationError from parts import models as parts_models class MyTestForm(forms.Form): @@ -7,27 +8,67 @@ class MyTestForm(forms.Form): class AddSubStorageForm(forms.Form): storage_name = forms.CharField(label="storage_name", initial='') responsible = forms.CharField(label='responsible_user') -class EditStockForm(forms.Form): - stock_uuid = forms.UUIDField() - increment = forms.IntegerField(label='Change Amount') - watermark_active = forms.BooleanField() - watermark = forms.IntegerField(min_value=0) - -class EditStockForm(forms.Form): - def __init__(self, *args, **kwargs): - # only change attributes if an instance is passed - instance = kwargs.get('instance') - if instance: - self.fields['amount_change'] = forms.IntegerField(min_value=0) - wm = instance.watermark - wm_active = True - if wm < 0: - wm = 0 - wm_active = False - self.fields['watermark'] = forms.IntegerField(min_value=0, initial=wm) - self.fields['watermark_active'] = forms.BooleanField(initial=wm_active) - print('Hohew') - super().__init__(*args, **kwargs) class DeleteStockForm(forms.Form): - stock_uuid = forms.UUIDField() \ No newline at end of file + stock_uuid = forms.UUIDField() + +class EditWatermarkForm(forms.Form): + stock_uuid = forms.UUIDField() + watermark_active = forms.BooleanField(required=False) #If it is false, the webbrowser won't send it at all. Therefore we have to set it to required=False + watermark = forms.IntegerField(min_value=0) + + def clean(self): + cleaned_data = super().clean() + id = cleaned_data.get("stock_uuid") + + if not id: + raise ValidationError("No stock UUID given") + + stock = None + try: + stock = parts_models.Stock.objects.get(id=id) + except: + raise ValidationError("Stock with uuid %s does not exist" % (id)) + cleaned_data['stock'] = stock + + return cleaned_data + + def save(self): + stock = self.cleaned_data['stock'] + active = self.cleaned_data['watermark_active'] + watermark = self.cleaned_data['watermark'] + + if not active: + watermark = -1 + + stock.watermark = watermark + stock.save() + +class EditStockAmountForm(forms.Form): + stock_uuid = forms.UUIDField() + amount = forms.IntegerField(min_value=0) + + def clean(self): + cleaned_data = super().clean() + id = cleaned_data.get("stock_uuid") + + if not id: + raise ValidationError("No stock UUID given") + + stock = None + try: + stock = parts_models.Stock.objects.get(id=id) + except: + raise ValidationError("Stock with uuid %s does not exist" % (id)) + cleaned_data['stock'] = stock + + return cleaned_data + + def save(self, increase: bool): + stock = self.cleaned_data['stock'] + amount = self.cleaned_data['amount'] + + if not increase: + amount = -amount + + return stock.atomic_increment(amount) \ No newline at end of file diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 0e3ec5d..db752a8 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -13,7 +13,7 @@ from .models import Storage, Stock from .qr_parser import QrCodeValidator from django.core.paginator import Paginator from django.core.exceptions import ValidationError -from .forms import MyTestForm, AddSubStorageForm, EditStockForm, DeleteStockForm +from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm from django.db.models import Q from django.db.models.functions import Lower import uuid @@ -274,6 +274,24 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): 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: + pass # Todo: Handle error + + 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(): + edit_form.save(increase) + + context = self.get_context_data(**kwargs) + return self.render_to_response(context) def post(self, request, *args, **kwargs): self.object = self.get_object() @@ -284,5 +302,10 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): return self.handle_del_storage_post(request, **kwargs) elif 'submit-delete-stock' in request.POST: return self.handle_del_stock_post(request, **kwargs) - + elif 'submit-edit-watermark' in request.POST: + return self.handle_update_watermark(request, **kwargs) + 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 super().post(request, *args, **kwargs) \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html b/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html index 06748dc..6729c98 100644 --- a/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html +++ b/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html @@ -1,7 +1,6 @@ {% comment "" %} needs following context: - stock: Creates a modal with id = change_stock-modal-{{stock.id}} -- form: EditStockForm {% endcomment %} {% load crispy_forms_tags %} {% load static %} @@ -27,14 +26,28 @@ needs following context: -
- {% csrf_token %}
- - \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/stocks-detail.html b/shimatta_kenkyusho/templates/parts/stocks-detail.html index 8b3de56..8296512 100644 --- a/shimatta_kenkyusho/templates/parts/stocks-detail.html +++ b/shimatta_kenkyusho/templates/parts/stocks-detail.html @@ -17,10 +17,10 @@
-
+
{% qr_from_text object.get_qr_code size="m" image_format="svg" %}
-
+
{% if object.parent_storage %}

Sub-Storages Parent Storage {% else %}

Sub-Storages Stock Overview @@ -44,7 +44,7 @@ {% include 'paginator.html' with paginator=storages get_param='storage_page' aria_label='Storage Page Navigation' %}

-
+

Stocked Components