Implement distributor numbers #1

Merged
mhu merged 1 commits from distributor-num into master 2022-01-02 16:25:07 +01:00
4 changed files with 102 additions and 16 deletions
Showing only changes of commit 494aded95b - Show all commits

View File

@ -198,3 +198,21 @@ class ManufacturerForm(forms.ModelForm):
class Meta: class Meta:
model = parts_models.Manufacturer model = parts_models.Manufacturer
fields = '__all__' fields = '__all__'
class DistributorNumberCreateForm(forms.ModelForm):
distributor = AutocompleteForeingKeyField(api_search_url='distributor-list', foreign_model=parts_models.Distributor, required=True)
class Meta:
model = parts_models.DistributorNum
fields = ['distributor', 'distributor_part_number']
class DistributorNumberDeleteForm(forms.Form):
distributor_num = forms.UUIDField(required=True)
def clean_distributor_num(self):
my_uuid = self.cleaned_data['distributor_num']
try:
distributor_number = parts_models.DistributorNum.objects.get(id=my_uuid)
except:
raise ValidationError('distributor number invalid')
return distributor_number

View File

@ -526,24 +526,14 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
base_title = '' base_title = ''
navbar_selected = 'Components' navbar_selected = 'Components'
def prepare_initial_param_formset_data(self):
parameters = ComponentParameter.objects.filter(component=self.object)
initdata = []
for param in parameters:
param_type = param.parameter_type.parameter_type
if param_type == 'F':
value = param.text_value
else:
value = param.value
initdata.append({'parameter_type': param.parameter_type, 'value': value})
return initdata
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
self.base_title = 'Component / '+self.object.name self.base_title = 'Component / '+self.object.name
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['component'] = self.object context['component'] = self.object
context['stocks'] = Stock.objects.filter(component=self.object) context['stocks'] = Stock.objects.filter(component=self.object)
context['comp_form'] = ComponentForm(instance=self.object) context['comp_form'] = ComponentForm(instance=self.object)
context['new_distri_num_form'] = DistributorNumberCreateForm()
context['distri_nums'] = DistributorNum.objects.filter(component=self.object)
return context return context
@ -578,12 +568,39 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context['protected_stuff'] = protected_stuff context['protected_stuff'] = protected_stuff
return self.render_to_response(context) return self.render_to_response(context)
def handle_submit_new_distri_num_post(self, request, **kwargs):
form = DistributorNumberCreateForm(data=request.POST)
if form.is_valid():
new_number = form.save(commit=False)
new_number.component = self.object
try:
new_number.save()
except IntegrityError as ie:
form.add_error('__all__', 'Number for given distributor already exists')
context = self.get_context_data(**kwargs)
if not form.is_valid():
context['new_distri_num_form'] = form
return self.render_to_response(context)
def handle_submit_delete_distri_num_post(self, request, **kwargs):
form = DistributorNumberDeleteForm(data=request.POST)
if form.is_valid():
form.cleaned_data['distributor_num'].delete()
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()
if 'submit-edit-component' in request.POST: if 'submit-edit-component' in request.POST:
return self.handle_submit_edit_component_post(request, **kwargs) return self.handle_submit_edit_component_post(request, **kwargs)
elif 'submit-component-delete' in request.POST: elif 'submit-component-delete' in request.POST:
return self.handle_submit_delete_post(request, **kwargs) return self.handle_submit_delete_post(request, **kwargs)
elif 'submit-create-new-distri-num' in request.POST:
return self.handle_submit_new_distri_num_post(request, **kwargs)
elif 'submit-delete-distributor-num' in request.POST:
return self.handle_submit_delete_distri_num_post(request, **kwargs)
else: else:
return super().post(request, *args, **kwargs) return super().post(request, *args, **kwargs)

View File

@ -98,8 +98,29 @@
<h3>Parameters</h3> <h3>Parameters</h3>
</div> </div>
<div class="col"> <div class="col">
<h3>Distributor Part Numbers</h3> <h3>Distributor Part Numbers <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#new-distributor-number-modal"><i class="bi bi-plus-circle"></i></button></h3>
<table class="table">
<thead>
<th scope="col">Distributor</th>
<th scope="col">Part Number</th>
<th scope="col"></th>
</thead>
<tbody>
{% for num in distri_nums %}
<tr>
<td><a class="link-primary text-decoration-none" href="{% url 'parts-distributors-detail' uuid=num.distributor.id %}">{{num.distributor.name}}</a></td>
<td>{{num.distributor_part_number}}</td>
<td>
<form method="post">
{% csrf_token %}
<input type="hidden" value="{{num.id}}" name="distributor_num">
<button class="btn btn-danger" name="submit-delete-distributor-num">X</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div> </div>
</div> </div>
<div class="row mt-2"> <div class="row mt-2">
@ -181,6 +202,7 @@
</div> </div>
</div> </div>
{% include 'parts/modals/new-distributor-number-modal.html' with component_name=component.name form=new_distri_num_form %}
{% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name form=comp_form %} {% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name form=comp_form %}
{% endblock content %} {% endblock content %}
@ -193,6 +215,9 @@ bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')).
{% if delete_error %} {% if delete_error %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('component-delete-modal')).show(); bootstrap.Modal.getOrCreateInstance(document.getElementById('component-delete-modal')).show();
{% endif %} {% endif %}
{% if new_distri_num_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('new-distributor-number-modal')).show();
{% endif %}
</script> </script>
{% endblock custom_scripts %} {% endblock custom_scripts %}

View File

@ -0,0 +1,26 @@
{% comment "" %}
Needs:
- form : DistributorNuberCreateForm
- component_name: Component's name
{% endcomment %}
{% load crispy_forms_tags %}
<div class="modal fade" id="new-distributor-number-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add Part Number for {{component_name}}</h5>
<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">
{{form|crispy}}
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Add Number" name="submit-create-new-distri-num">
</div>
</form>
</div>
</div>
</div>