Add functionality to 'Add substorage modal' on stock pages

This commit is contained in:
Mario Hüttel 2021-10-24 21:14:11 +02:00
parent e4a83f3e58
commit bd2cb79e04
3 changed files with 74 additions and 5 deletions

View File

@ -1,6 +1,7 @@
from django.shortcuts import render, redirect
from django.urls import resolve, reverse
from django.contrib.auth import logout, login
from django.contrib.auth.models import User
from django.http import HttpResponse
from .navbar import NavBar
from django.contrib.auth.forms import AuthenticationForm as AuthForm
@ -11,6 +12,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMix
from .models import Storage, Stock
from .qr_parser import QrCodeValidator
from django.core.paginator import Paginator
from django.core.exceptions import ValidationError
class QrSearchForm(forms.Form):
my_qr_validator = QrCodeValidator()
@ -123,6 +125,10 @@ class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return context
class AddSubStorageForm(forms.Form):
storage_name = forms.CharField(label="storage_name", initial='')
responsible = forms.CharField(label='responsible_user')
class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
template_name = 'parts/stocks-detail.html'
model = Storage
@ -148,7 +154,31 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
storage_paginator = Paginator(Storage.objects.filter(parent_storage=self.object), self.default_pagination_size)
context['storages'] = storage_paginator.get_page(storage_page)
add_storage_form = AddSubStorageForm()
add_storage_form.fields['responsible'].initial = self.request.user.username
context['add_storage_form'] = add_storage_form
return context
def post(self):
pass
def handle_add_storage_post(self, request, **kwargs):
f = AddSubStorageForm(data=request.POST)
if f.is_valid():
sub_name = f.cleaned_data['storage_name']
try:
user = User.objects.get(username=f.cleaned_data['responsible'])
try:
Storage.objects.create(name=sub_name, parent_storage=self.object, responsible=user)
except ValidationError as v_err:
f.add_error('storage_name', '. '.join(v_err.messages))
except:
f.add_error('responsible', 'Invalid user')
context = self.get_context_data(**kwargs)
context['add_storage_form'] = f
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
if 'submit-add-storage' in request.POST:
return self.handle_add_storage_post(request)
return super().post(request, *args, **kwargs)

View File

@ -53,5 +53,9 @@
{% endblock content %}
<script type="text/javascript" src="{% static 'js/bootstrap.bundle.min.js' %}"></script>
{% block custom_scripts %}
{% endblock custom_scripts %}
</body>
</html>

View File

@ -40,6 +40,7 @@
</div>
</div>
<!-- Modal for adding a substorage-->
{% with add_storage_form as form %}
<div class="modal fade" id="add-sub-modal">
<div class="modal-dialog">
<div class="modal-content">
@ -50,15 +51,49 @@
<form action="" method="post">
{% csrf_token %}
<div class="modal-body">
<label for="add-storage-name" class="form-label">Storage Name</label>
<input class="form-control" id="add-storage-name" name="storage-name" type="text">
<label for="add-storage-name" class="form-label">Storage Name</label>
<div class="input-group has-validation">
<input value="{{form.storage_name.value}}" class="form-control{% if form.storage_name.errors or form.non_field_errors %} is-invalid{% endif %}" id="add-storage-name" name="{{form.storage_name.name}}" type="text" aria-describedby="validationStorageName" required>
<div id="validationStorageName" class="invalid-feedback">
{% for msg in form.storage_name.errors %}
{{msg}}
{% endfor %}
</div>
</div>
<label for="{{form.responsible.id_for_label}}">Responsible</label>
<div class="input-group has-validation">
<span class="input-group-text" id="add_storage_username_prepend">@</span><input type="text" value="{{form.responsible.value}}" class="form-control{% if form.responsible.errors or form.non_field_errors %} is-invalid{% endif %}" id="{{form.responsible.id_for_label}}" name="{{form.responsible.name}}" aria-describedby="add_storage_username_prepend validationServerUsernameFeedback" required>
<div id="validationServerUsernameFeedback" class="invalid-feedback">
{% for msg in form.responsible.errors %}
{{msg}}
{% endfor %}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Add Storage" name="submit-add-storage">
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<p class="text-danger text-center">{{ error }}</p>
{% endfor %}
{% endif %}
</div>
</form>
</div>
{% endwith %}
</div>
</div>
{% endblock content %}
{% endblock content %}
{% block custom_scripts %}
{% if add_storage_form.errors %}
<script type="text/javascript">
var addSubStorageModal = document.querySelector('#add-sub-modal');
var modal = bootstrap.Modal.getOrCreateInstance(addSubStorageModal);
modal.show();
</script>
{% endif %}
{% endblock custom_scripts %}