Implement change amount / watermark form

This commit is contained in:
Mario Hüttel 2021-11-07 20:36:12 +01:00
parent c0eff5822b
commit 88bebfa2c8
4 changed files with 111 additions and 34 deletions

View File

@ -1,4 +1,5 @@
from django import forms from django import forms
from django.core.exceptions import ValidationError
from parts import models as parts_models from parts import models as parts_models
class MyTestForm(forms.Form): class MyTestForm(forms.Form):
@ -7,27 +8,67 @@ class MyTestForm(forms.Form):
class AddSubStorageForm(forms.Form): class AddSubStorageForm(forms.Form):
storage_name = forms.CharField(label="storage_name", initial='') storage_name = forms.CharField(label="storage_name", initial='')
responsible = forms.CharField(label='responsible_user') 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): class DeleteStockForm(forms.Form):
stock_uuid = forms.UUIDField() 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)

View File

@ -13,7 +13,7 @@ from .models import Storage, Stock
from .qr_parser import QrCodeValidator from .qr_parser import QrCodeValidator
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.core.exceptions import ValidationError 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 import Q
from django.db.models.functions import Lower from django.db.models.functions import Lower
import uuid import uuid
@ -274,6 +274,24 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
return self.render_to_response(context) 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): def post(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
@ -284,5 +302,10 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
return self.handle_del_storage_post(request, **kwargs) return self.handle_del_storage_post(request, **kwargs)
elif 'submit-delete-stock' in request.POST: elif 'submit-delete-stock' in request.POST:
return self.handle_del_stock_post(request, **kwargs) 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) return super().post(request, *args, **kwargs)

View File

@ -1,7 +1,6 @@
{% comment "" %} {% comment "" %}
needs following context: needs following context:
- stock: Creates a modal with id = change_stock-modal-{{stock.id}} - stock: Creates a modal with id = change_stock-modal-{{stock.id}}
- form: EditStockForm
{% endcomment %} {% endcomment %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% load static %} {% load static %}
@ -27,14 +26,28 @@ needs following context:
</div> </div>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<form action="" method="post">
{% csrf_token %}
<div class="modal-body"> <div class="modal-body">
<form method="post">
{% csrf_token %}
<input type="hidden" name="stock_uuid" value="{{stock.id}}">
<div class="input-group mb-3">
<input type="submit" class="btn btn-primary" name="submit-amount-reduce" value="-">
<input type="number" name="amount" id="ch-stk-amount-{{stock.id}}" class="form-control" value="1" min="0" required>
<input type="submit" class="btn btn-primary" name="submit-amount-increase" value="+">
</div>
</form>
<form method="post">
{% csrf_token %}
<input type="hidden" name="stock_uuid" value="{{stock.id}}">
<div class="input-group mb-3">
<div class="input-group-text">
<input type="checkbox" class="form-check-input mt-0" name="watermark_active" id="ch-stk-watermark-act-{{stock.id}}" {% if stock.watermark >= 0 %}checked{%endif%}>
</div>
<input type="number" name="watermark" id="ch-stk-watermark-{{stock.id}}" class="form-control" value="{% if stock.watermark < 0 %}0{% else %}{{stock.watermark}}{% endif %}" required>
<input type="submit" class="btn btn-primary" name="submit-edit-watermark" value="Update Watermark">
</div>
</form>
</div> </div>
<div class="modal-footer">
</div>
</form>
</div> </div>
</div> </div>
</div> </div>

View File

@ -17,10 +17,10 @@
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-3">
<div class="row-md"> <div class="row">
{% qr_from_text object.get_qr_code size="m" image_format="svg" %} {% qr_from_text object.get_qr_code size="m" image_format="svg" %}
</div> </div>
<div class="row-md"> <div class="row">
{% if object.parent_storage %} {% if object.parent_storage %}
<h1>Sub-Storages <a class="btn btn-secondary" href="{% url 'parts-stocks-detail' uuid=object.parent_storage.id %}">Parent Storage</a> {% else %} <h1>Sub-Storages <a class="btn btn-secondary" href="{% url 'parts-stocks-detail' uuid=object.parent_storage.id %}">Parent Storage</a> {% else %}
<h1>Sub-Storages <a class="btn btn-secondary" href="{% url 'parts-stocks'%}">Stock Overview</a> <h1>Sub-Storages <a class="btn btn-secondary" href="{% url 'parts-stocks'%}">Stock Overview</a>
@ -44,7 +44,7 @@
{% include 'paginator.html' with paginator=storages get_param='storage_page' aria_label='Storage Page Navigation' %} {% include 'paginator.html' with paginator=storages get_param='storage_page' aria_label='Storage Page Navigation' %}
</div> </div>
</div> </div>
<div class="col-md"> <div class="col">
<h1>Stocked Components</h1> <h1>Stocked Components</h1>
<form method="get"> <form method="get">
<div class="input-group"> <div class="input-group">