From c0eff5822ba16e4186ef6d63de5af8afe292b3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 1 Nov 2021 22:17:20 +0100 Subject: [PATCH] Fiddling around with forms and stuff. No clue where it will end --- shimatta_kenkyusho/api/views.py | 2 + shimatta_kenkyusho/parts/forms.py | 25 ++++++++++++ shimatta_kenkyusho/parts/views.py | 35 +++++++++++++--- .../parts/modals/update-stock-modal.html | 40 +++++++++++++++++++ .../templates/parts/stocks-detail.html | 23 +++++++++++ 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html diff --git a/shimatta_kenkyusho/api/views.py b/shimatta_kenkyusho/api/views.py index 50c2ecd..b0c54a5 100644 --- a/shimatta_kenkyusho/api/views.py +++ b/shimatta_kenkyusho/api/views.py @@ -46,6 +46,8 @@ class PartsComponentViewSet(viewsets.ModelViewSet): queryset = parts_models.Component.objects.all() serializer_class = ComponentSerializer permission_classes = [permissions.DjangoModelPermissions] + filter_backends = [filters.SearchFilter] + search_fields = ['name', 'package__name', 'manufacturer__name'] class PartsStockViewSet(viewsets.ModelViewSet): queryset = parts_models.Stock.objects.all() diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 1733841..81fda80 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -1,4 +1,5 @@ from django import forms +from parts import models as parts_models class MyTestForm(forms.Form): pass @@ -6,3 +7,27 @@ 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 diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index bec709b..0e3ec5d 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -13,8 +13,9 @@ 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 +from .forms import MyTestForm, AddSubStorageForm, EditStockForm, DeleteStockForm from django.db.models import Q +from django.db.models.functions import Lower import uuid class QrSearchForm(forms.Form): @@ -179,7 +180,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): return crumbs def search_stock_queryset(self, search): - stocks_in_storage = Stock.objects.filter(storage=self.object) + stocks_in_storage = Stock.objects.filter(storage=self.object).order_by(Lower('component__name')) if search is None or search == '': return stocks_in_storage @@ -192,10 +193,11 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): pass if test_uuid is not None: - stocks_in_storage = stocks_in_storage.filter(component__id = test_uuid) + stocks_in_storage = stocks_in_storage.filter(Q(component__id = test_uuid) | Q(id= test_uuid)) else: stocks_in_storage = stocks_in_storage.filter(Q(component__name__contains = search) | - Q(component__package__name__contains = search)) + Q(component__package__name__contains = search) | + Q(component__manufacturer__name__contains = search)) return stocks_in_storage @@ -214,12 +216,14 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): stock_paginator = Paginator(self.search_stock_queryset(stock_search_input), self.default_pagination_size) context['storages'] = storage_paginator.get_page(storage_page) - context['stocks'] = stock_paginator.get_page(componente_stock_page) + stocks = stock_paginator.get_page(componente_stock_page) + context['stocks'] = stocks context['stock_search'] = stock_search_input add_storage_form = AddSubStorageForm() add_storage_form.fields['responsible'].initial = self.request.user.username context['add_storage_form'] = add_storage_form context['delete_storage_error'] = None + return context def handle_add_storage_post(self, request, **kwargs): @@ -251,6 +255,25 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): 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 + if 'stock_uuid' in request.POST: + f = DeleteStockForm(data=request.POST) + if f.is_valid(): + try: + s = Stock.objects.get(id=f.cleaned_data['stock_uuid']) + print(s.storage) + print(self.object) + if s.storage == self.object: + s.delete() + else: + 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 post(self, request, *args, **kwargs): self.object = self.get_object() @@ -259,5 +282,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): return self.handle_add_storage_post(request, **kwargs) elif 'submit-delete-storage' in request.POST: return self.handle_del_storage_post(request, **kwargs) + elif 'submit-delete-stock' in request.POST: + return self.handle_del_stock_post(request, **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 new file mode 100644 index 0000000..06748dc --- /dev/null +++ b/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html @@ -0,0 +1,40 @@ +{% comment "" %} +needs following context: +- stock: Creates a modal with id = change_stock-modal-{{stock.id}} +- form: EditStockForm +{% endcomment %} +{% load crispy_forms_tags %} +{% load static %} + \ 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 481d8a5..8b3de56 100644 --- a/shimatta_kenkyusho/templates/parts/stocks-detail.html +++ b/shimatta_kenkyusho/templates/parts/stocks-detail.html @@ -1,6 +1,8 @@ {% extends 'base.html' %} {% load qr_code %} {% load static %} +{% load crispy_forms_tags %} + {% block content %}
+
+ Amount: {{stock.amount}} + {% if stock.watermark >= 0 %} +
Watermark: {{stock.watermark}} + {% else %} +
No Watermark
+ {% endif %} +
+
+ +
+
+
+ {% csrf_token %} + + +
+
{% endfor %} {% include 'paginator.html' with paginator=stocks get_param='stock_page' aria_label='Stock Page Navigation' %} + {% for stock in stocks %} + {% include 'parts/modals/update-stock-modal.html' with stock=stock form=change_stock_form %} + {% endfor %} {% with add_storage_form as form %} {% include 'parts/modals/new-substorage-modal.html' %}