Fiddling around with forms and stuff. No clue where it will end

This commit is contained in:
Mario Hüttel 2021-11-01 22:17:20 +01:00
parent 81a644cd95
commit c0eff5822b
5 changed files with 120 additions and 5 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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 %}
<div class="modal fade" id="change-stock-modal-{{stock.id}}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="flex-shrink-0">
{% if stock.component.get_resolved_image %}
<img src="{{stock.component.get_resolved_image}}" style="max-width:64px;max-height:64px;" alt="{{ low.component.name }}" class="mr-3">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" style="width:64px;max-height:64px;" alt="{{ low.component.name }}" class="mr-3">
{% endif %}
</div>
<div class="flex-grow-1 ms-3">
<h6 class="mt-0 text-primary">{{ stock.component.name }}</h6>
{% if stock.component.package %}
Package: {{stock.component.package}}<br>
{% endif %}
{% if stock.component.manufacturer %}
Manufacturer: {{stock.component.manufacturer}}
{% endif %}
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="post">
{% csrf_token %}
<div class="modal-body">
</div>
<div class="modal-footer">
</div>
</form>
</div>
</div>
</div>

View File

@ -1,6 +1,8 @@
{% extends 'base.html' %}
{% load qr_code %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<nav aria-label="breadcrumb" class="fs-4">
@ -72,12 +74,33 @@
Manufacturer: {{stock.component.manufacturer}}
{% endif %}
</div>
<div class="ms-3">
Amount: {{stock.amount}}
{% if stock.watermark >= 0 %}
<br>Watermark: {{stock.watermark}}
{% else %}
<span class="text-secondary"><br>No Watermark</span>
{% endif %}
</div>
<div class="ms-3">
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#change-stock-modal-{{stock.id}}">Change</button>
</div>
<div>
<form method="post">
{% csrf_token %}
<input type="hidden" name="stock_uuid" value="{{stock.id}}">
<input type="submit" class="btn btn-danger" name="submit-delete-stock" value="X">
</form>
</div>
</li>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=stocks get_param='stock_page' aria_label='Stock Page Navigation' %}
</div>
</div>
{% for stock in stocks %}
{% include 'parts/modals/update-stock-modal.html' with stock=stock form=change_stock_form %}
{% endfor %}
<!-- Modal for adding a substorage-->
{% with add_storage_form as form %}
{% include 'parts/modals/new-substorage-modal.html' %}