mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-25 17:32:16 +00:00
Tweak asset ID autogeneration
Closes #443, admittedly with a different approach.
This commit is contained in:
@@ -91,25 +91,6 @@ class CableType(models.Model):
|
|||||||
return reverse('cable_type_detail', kwargs={'pk': self.pk})
|
return reverse('cable_type_detail', kwargs={'pk': self.pk})
|
||||||
|
|
||||||
|
|
||||||
def get_available_asset_id(wanted_prefix=""):
|
|
||||||
sql = """
|
|
||||||
SELECT a.asset_id_number+1
|
|
||||||
FROM assets_asset a
|
|
||||||
LEFT OUTER JOIN assets_asset b ON
|
|
||||||
(a.asset_id_number + 1 = b.asset_id_number AND
|
|
||||||
a.asset_id_prefix = b.asset_id_prefix)
|
|
||||||
WHERE b.asset_id IS NULL AND a.asset_id_number >= %s AND a.asset_id_prefix = %s;
|
|
||||||
"""
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute(sql, [9000, wanted_prefix])
|
|
||||||
row = cursor.fetchone()
|
|
||||||
if row is None or row[0] is None:
|
|
||||||
return 9000
|
|
||||||
else:
|
|
||||||
return row[0]
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
|
|
||||||
class AssetManager(models.Manager):
|
class AssetManager(models.Manager):
|
||||||
def search(self, query=None):
|
def search(self, query=None):
|
||||||
qs = self.get_queryset()
|
qs = self.get_queryset()
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ from z3c.rml import rml2pdf
|
|||||||
from PyRIGS.views import GenericListView, GenericDetailView, GenericUpdateView, GenericCreateView, ModalURLMixin, \
|
from PyRIGS.views import GenericListView, GenericDetailView, GenericUpdateView, GenericCreateView, ModalURLMixin, \
|
||||||
is_ajax, OEmbedView
|
is_ajax, OEmbedView
|
||||||
from assets import forms, models
|
from assets import forms, models
|
||||||
from assets.models import get_available_asset_id
|
|
||||||
|
|
||||||
|
|
||||||
class AssetList(LoginRequiredMixin, generic.ListView):
|
class AssetList(LoginRequiredMixin, generic.ListView):
|
||||||
@@ -153,7 +152,8 @@ class AssetCreate(LoginRequiredMixin, generic.CreateView):
|
|||||||
|
|
||||||
def get_initial(self, *args, **kwargs):
|
def get_initial(self, *args, **kwargs):
|
||||||
initial = super().get_initial(*args, **kwargs)
|
initial = super().get_initial(*args, **kwargs)
|
||||||
initial["asset_id"] = get_available_asset_id()
|
last_asset = self.model.objects.filter(asset_id_prefix="").last()
|
||||||
|
initial["asset_id"] = str(last_asset.asset_id_number + 1)
|
||||||
return initial
|
return initial
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
@@ -168,6 +168,13 @@ class DuplicateMixin:
|
|||||||
|
|
||||||
|
|
||||||
class AssetDuplicate(DuplicateMixin, AssetIDUrlMixin, AssetCreate):
|
class AssetDuplicate(DuplicateMixin, AssetIDUrlMixin, AssetCreate):
|
||||||
|
def get_initial(self, *args, **kwargs):
|
||||||
|
initial = super().get_initial(*args, **kwargs)
|
||||||
|
prefix = self.get_object().asset_id_prefix
|
||||||
|
last_asset = self.model.objects.filter(asset_id_prefix=prefix).last()
|
||||||
|
initial["asset_id"] = prefix + str(last_asset.asset_id_number + 1)
|
||||||
|
return initial
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context["create"] = None
|
context["create"] = None
|
||||||
@@ -276,7 +283,7 @@ class SupplierUpdate(GenericUpdateView, ModalURLMixin):
|
|||||||
form_class = forms.SupplierForm
|
form_class = forms.SupplierForm
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(SupplierUpdate, self).get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
if is_ajax(self.request):
|
if is_ajax(self.request):
|
||||||
context['override'] = "base_ajax.html"
|
context['override'] = "base_ajax.html"
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user