Tweak asset ID autogeneration

Closes #443, admittedly with a different approach.
This commit is contained in:
2022-02-15 00:16:33 +00:00
parent a5e888fef5
commit 5215af349a
2 changed files with 10 additions and 22 deletions

View File

@@ -91,25 +91,6 @@ class CableType(models.Model):
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):
def search(self, query=None):
qs = self.get_queryset()

View File

@@ -25,7 +25,6 @@ from z3c.rml import rml2pdf
from PyRIGS.views import GenericListView, GenericDetailView, GenericUpdateView, GenericCreateView, ModalURLMixin, \
is_ajax, OEmbedView
from assets import forms, models
from assets.models import get_available_asset_id
class AssetList(LoginRequiredMixin, generic.ListView):
@@ -153,7 +152,8 @@ class AssetCreate(LoginRequiredMixin, generic.CreateView):
def get_initial(self, *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
def get_success_url(self):
@@ -168,6 +168,13 @@ class DuplicateMixin:
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):
context = super().get_context_data(**kwargs)
context["create"] = None
@@ -276,7 +283,7 @@ class SupplierUpdate(GenericUpdateView, ModalURLMixin):
form_class = forms.SupplierForm
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):
context['override'] = "base_ajax.html"
else: