mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
FIX: Prevent creating duplicate cable types
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from django import forms
|
||||
|
||||
from assets import models
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
class AssetForm(forms.ModelForm):
|
||||
@@ -40,3 +41,12 @@ class CableTypeForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.CableType
|
||||
fields = '__all__'
|
||||
|
||||
def clean(self):
|
||||
form_data = self.cleaned_data
|
||||
queryset = models.CableType.objects.filter(Q(plug=form_data['plug']) & Q(socket=form_data['socket']) & Q(circuits=form_data['circuits']) & Q(cores=form_data['cores']))
|
||||
# Being identical to itself doesn't count...
|
||||
print(self.instance.id)
|
||||
if queryset.exists() and self.instance.pk != queryset[0].pk:
|
||||
raise forms.ValidationError("A cable type that exactly matches this one already exists, please use that instead.", code="notunique")
|
||||
return form_data
|
||||
|
||||
@@ -3,7 +3,6 @@ from django.core.exceptions import ValidationError
|
||||
from django.db import models, connection
|
||||
from django.urls import reverse
|
||||
|
||||
from django.db.models import F
|
||||
from django.db.models.signals import pre_save
|
||||
from django.dispatch.dispatcher import receiver
|
||||
|
||||
|
||||
@@ -8,7 +8,11 @@
|
||||
{% if create %}Create{% elif edit %}Edit{% endif %} Cable Type
|
||||
</h1>
|
||||
</div>
|
||||
<form method="post" id="asset_update_form" action="{% url 'asset_create'%}">
|
||||
{% if create %}
|
||||
<form method="POST" action="{% url 'cable_type_create'%}">
|
||||
{% elif edit %}
|
||||
<form method="POST" action="{% url 'cable_type_update' object.id %}">
|
||||
{% endif %}
|
||||
{% include 'form_errors.html' %}
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="id" value="{{ object.id|default:0 }}" hidden=true>
|
||||
|
||||
@@ -277,6 +277,9 @@ class CableTypeCreate(generic.CreateView):
|
||||
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("cable_type_detail", kwargs={"pk": self.object.pk })
|
||||
|
||||
class CableTypeUpdate(generic.UpdateView):
|
||||
model = models.CableType
|
||||
template_name = "cable_type_form.html"
|
||||
@@ -287,3 +290,6 @@ class CableTypeUpdate(generic.UpdateView):
|
||||
context["edit"] = True
|
||||
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("cable_type_detail", kwargs={"pk": self.object.pk })
|
||||
|
||||
Reference in New Issue
Block a user