Add a lot of stuff.. Can't remember

This commit is contained in:
Mario Hüttel 2021-11-10 19:38:39 +01:00
parent 7eb2652433
commit e2aba765d4
6 changed files with 213 additions and 18 deletions

View File

@ -104,4 +104,99 @@ class AddStockForm(forms.Form):
new_stock = parts_models.Stock.objects.create(storage=storage, component=component, watermark=watermark, amount=amount)
new_stock.save()
class EditComponentForm(forms.Form):
name = forms.CharField(required=True)
datasheet_link = forms.CharField(max_length=300, required=False)
description = forms.CharField(required=False, widget=forms.Textarea)
# Look up these fields later. Will be autocompleted in UI
manufacturer = forms.CharField(required=False)
component_type = forms.CharField(required=False, label='Component Type')
pref_distri = forms.CharField(required=False, label='Preferred Distributor')
package = forms.CharField(required=False)
image = forms.ImageField(required=False)
def __init__(self, *args, instance=None, **kwargs):
super().__init__(*args, **kwargs)
self.instance = instance
if instance:
self.fields['name'].initial = instance.name
self.fields['datasheet_link'].initial = instance.datasheet_link
if instance.component_type:
self.fields['component_type'].initial = instance.component_type.class_name
self.fields['description'].initial = instance.description
if instance.manufacturer:
self.fields['manufacturer'].initial = instance.manufacturer.name
if instance.package:
self.fields['package'].initial = instance.package.name
if instance.pref_distri:
self.fields['pref_distri'].initial = instance.pref_distri.name
self.fields['image'].initial = instance.image
def clean_component_type(self):
data = self.cleaned_data['component_type'].strip()
if len(data) == 0:
self.cleaned_data['component_type_object'] = None
return
try:
self.cleaned_data['component_type_object'] = parts_models.ComponentType.objects.get(class_name=data)
except:
raise ValidationError('Invalid Component Type')
def clean_manufacturer(self):
data = self.cleaned_data['manufacturer'].strip()
if len(data) == 0:
self.cleaned_data['manufacturer_object'] = None
return
try:
self.cleaned_data['manufacturer_object'] = parts_models.Manufacturer.objects.get(name=data)
except:
raise ValidationError('Invalid Manufacturer')
def clean_pref_distri(self):
data = self.cleaned_data['pref_distri'].strip()
if len(data) == 0:
self.cleaned_data['pref_distri_object'] = None
return
try:
self.cleaned_data['pref_distri_object'] = parts_models.Distributor.objects.get(name=data)
except:
raise ValidationError('Invalid Distributor')
def clean_package(self):
data = self.cleaned_data['package'].strip()
if len(data) == 0:
self.cleaned_data['package_object'] = None
return
try:
self.cleaned_data['package_object'] = parts_models.Package.objects.get(name=data)
except:
raise ValidationError('Invalid Package')
def save(self):
if self.instance is None:
self.instance = parts_models.Component.objects.create()
self.instance.name = self.cleaned_data['name']
self.instance.datasheet_link = self.cleaned_data['datasheet_link']
self.instance.description = self.cleaned_data['description']
if bool(self.cleaned_data['image']) is False:
self.instance.image = None
else:
self.instance.image = self.cleaned_data['image']
self.instance.manufacturer = self.cleaned_data['manufacturer_object']
self.instance.pref_distri = self.cleaned_data['pref_distri_object']
self.instance.package = self.cleaned_data['package_object']
self.instance.component_type = self.cleaned_data['component_type_object']
self.instance.save()

View File

@ -15,7 +15,7 @@ from .models import Storage, Stock, Component, Distributor, Manufacturer, Packag
from .qr_parser import QrCodeValidator
from django.core.paginator import Paginator
from django.core.exceptions import ValidationError
from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm, AddStockForm
from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm, AddStockForm, EditComponentForm
from django.db.models import Q
from django.db.models.functions import Lower
import uuid
@ -233,6 +233,10 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
if search is None or search == '':
return stocks_in_storage
if search.startswith('[comp_uuid]'):
search = search.replace('[comp_uuid]', '')
# Check if the searhc equals a UUID
test_uuid = None
try:
@ -391,11 +395,31 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
self.base_title = 'Component / '+self.object.name
context = super().get_context_data(**kwargs)
context['component'] = self.object
context['edit_form'] = EditComponentForm(instance=self.object)
return context
def handle_submit_edit_post(self, request, **kwargs):
form_error = False
form = EditComponentForm(instance=self.object, data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
else:
print("Error")
form_error = True
context = self.get_context_data(**kwargs)
if form_error:
context['edit_form'] = form
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
if 'submit-edit-comp' in request.POST:
return self.handle_submit_edit_post(request, **kwargs)
super().post(request, *args, **kwargs)
class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
template_name = 'parts/packages-detail.html'
model = Package

View File

@ -26,7 +26,7 @@ SECRET_KEY = 'django-insecure-vq_@ue3ul@&4bz7wkcpf3pjrwf8o$7g!z-rw$ftr-$)7l3*m=^
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['localhost']
# Application definition

View File

@ -4,16 +4,55 @@
{% block content %}
<div class="container">
<div class="col-md-3">
<div class="row justify-content-center">
{% if component.get_resolved_image %}
<img src="{{component.get_resolved_image}}" alt="{{component.name}}" class="component-img-big btn" data-bs-toggle="modal" data-bs-target="#comp-img-modal">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" alt="{{component.name}}" class="component-img-def-big">
<div class="row">
<div class="col-md-3">
<div class="row justify-content-center">
{% if component.get_resolved_image %}
<img src="{{component.get_resolved_image}}" alt="{{component.name}}" class="component-img-big btn" data-bs-toggle="modal" data-bs-target="#comp-img-modal">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" alt="{{component.name}}" class="component-img-def-big">
{% endif %}
</div>
<div class="row">
{% qr_from_text component.get_qr_code size="m" image_format="svg" %}
</div>
<div class="row">
{% if component.datasheet_link %}
<a class="btn btn-secondary mb-2" href="{{component.datasheet_link}}"><i class="bi bi-file-pdf-fill"></i> Datasheet</a>
{% endif %}
<button class="btn btn-primary mb-2" data-bs-toggle="modal" data-bs-target="#comp-edit-modal"><i class="bi bi-pencil-square"></i> Edit Component</button>
</div>
</div>
<div class="row">
{% qr_from_text component.get_qr_code size="m" image_format="svg" %}
<div class="col">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Package</th>
<th scope="col">Manufacturer</th>
<th scope="col">Type</th>
<th scope="col">Total #</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{{component.name}}</th>
<th>{% if component.package %}<a href="{% url 'parts-packages-detail' uuid=component.package.id %}" class="link-primary text-decoration-none">{{component.package.name}}</a>{% endif %}</th>
<th>{% if component.manufacturer %}{{component.manufacturer.name}}{% endif %}</th>
<th>{% if component.component_type %}{{component.component_type.class_name}}{% endif %}
<th>{{component.get_total_amount}}</th>
</tr>
</tbody>
</table>
<h2>Description</h2>
{% if component.description %}
{{component.description}}
{% else %}
<div class="alert alert-secondary" role="alert">
No description available
</div>
{% endif %}
</div>
</div>
</div>
@ -41,5 +80,15 @@
</div>
</div>
{% endif %}
{% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name edit_form=edit_form %}
{% endblock content %}
{% block custom_scripts %}
<script type="text/javascript">
{% if edit_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')).show()
{% endif %}
</script>
{% endblock custom_scripts %}
{% endblock content %}

View File

@ -0,0 +1,27 @@
{% comment "" %}
Needs following context:
- heading
- edit_form EditComponentForm
{% endcomment %}
{% load crispy_forms_tags %}
<div class="modal fade" id="comp-edit-modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2>{{heading}}</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal-body">
{{edit_form|crispy}}
</div>
<div class="modal-footer">
<input type="submit" name="submit-edit-comp" class="btn btn-primary" value="Save">
</div>
</form>
</div>
</div>
</div>

View File

@ -47,15 +47,15 @@
<div class="col">
<h1>Stocked Components</h1>
<form method="get">
<div class="input-group">
<div class="input-group mb-3">
<input class="form-control" name="search" type="search" placeholder="Search..." {% if stock_search %}value="{{stock_search}}"{% endif %}>
<button type="submit" class="btn btn-primary">
<i class="bi bi-search"></i>
</button>
</div>
</form>
<button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#add-stock-modal">
<i class="bi bi-plus-circle"></i>
<button class="btn btn-success mb-3" data-bs-toggle="modal" data-bs-target="#add-stock-modal">
<i class="bi bi-plus-circle"></i> Add Stock
</button>
<div class="list-group">
{% for stock in stocks %}