mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
Fix asset sample data command
This commit is contained in:
@@ -2,6 +2,7 @@ import random
|
|||||||
|
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from reversion import revisions as reversion
|
from reversion import revisions as reversion
|
||||||
|
|
||||||
@@ -125,5 +126,9 @@ class Command(BaseCommand):
|
|||||||
if i % 3 == 0:
|
if i % 3 == 0:
|
||||||
asset.purchased_from = random.choice(self.suppliers)
|
asset.purchased_from = random.choice(self.suppliers)
|
||||||
|
|
||||||
asset.clean()
|
with transaction.atomic():
|
||||||
asset.save()
|
try:
|
||||||
|
asset.clean()
|
||||||
|
asset.save()
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -100,6 +100,11 @@ class AssetManager(models.Manager):
|
|||||||
return qs
|
return qs
|
||||||
|
|
||||||
|
|
||||||
|
def get_available_asset_id(wanted_prefix=""):
|
||||||
|
last_asset = Asset.objects.filter(asset_id_prefix=wanted_prefix).last()
|
||||||
|
return 9000 if last_asset is None else wanted_prefix + str(last_asset.asset_id_number + 1)
|
||||||
|
|
||||||
|
|
||||||
@reversion.register
|
@reversion.register
|
||||||
class Asset(models.Model, RevisionMixin):
|
class Asset(models.Model, RevisionMixin):
|
||||||
parent = models.ForeignKey(to='self', related_name='asset_parent',
|
parent = models.ForeignKey(to='self', related_name='asset_parent',
|
||||||
|
|||||||
@@ -151,8 +151,7 @@ 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)
|
||||||
last_asset = self.model.objects.filter(asset_id_prefix="").last()
|
initial["asset_id"] = models.get_available_asset_id()
|
||||||
initial["asset_id"] = 9000 if last_asset is None else str(last_asset.asset_id_number + 1)
|
|
||||||
return initial
|
return initial
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
@@ -169,9 +168,7 @@ class DuplicateMixin:
|
|||||||
class AssetDuplicate(DuplicateMixin, AssetIDUrlMixin, AssetCreate):
|
class AssetDuplicate(DuplicateMixin, AssetIDUrlMixin, AssetCreate):
|
||||||
def get_initial(self, *args, **kwargs):
|
def get_initial(self, *args, **kwargs):
|
||||||
initial = super().get_initial(*args, **kwargs)
|
initial = super().get_initial(*args, **kwargs)
|
||||||
prefix = self.get_object().asset_id_prefix
|
initial["asset_id"] = models.get_available_asset_id(wanted_prefix=self.get_object().asset_id_prefix)
|
||||||
last_asset = self.model.objects.filter(asset_id_prefix=prefix).last()
|
|
||||||
initial["asset_id"] = 9000 if last_asset is None else prefix + str(last_asset.asset_id_number + 1)
|
|
||||||
return initial
|
return initial
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user