From 5215af349a2f6f35b6f1ef63441d7c92d141a6eb Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Tue, 15 Feb 2022 00:16:33 +0000 Subject: [PATCH] Tweak asset ID autogeneration Closes #443, admittedly with a different approach. --- assets/models.py | 19 ------------------- assets/views.py | 13 ++++++++++--- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/assets/models.py b/assets/models.py index 335d2b3d..bb51eae2 100644 --- a/assets/models.py +++ b/assets/models.py @@ -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() diff --git a/assets/views.py b/assets/views.py index d940efdf..56528d7c 100644 --- a/assets/views.py +++ b/assets/views.py @@ -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: