diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 81f6c0f..9eb9e5d 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -16,10 +16,12 @@ class AutoCompleteWidget(widgets.Input): def get_context(self, name, value, attrs): context = super().get_context(name, value, attrs) - try: - instance = self.foreign_model.objects.get(id=uuid.UUID(value)) - except Exception as ex: - print(ex) + if value is not None: + try: + instance = self.foreign_model.objects.get(id=uuid.UUID(value)) + except Exception as ex: + instance = None + else: instance = None image = None diff --git a/shimatta_kenkyusho/parts/migrations/0008_auto_20220101_1250.py b/shimatta_kenkyusho/parts/migrations/0008_auto_20220101_1250.py new file mode 100644 index 0000000..f457f79 --- /dev/null +++ b/shimatta_kenkyusho/parts/migrations/0008_auto_20220101_1250.py @@ -0,0 +1,34 @@ +# Generated by Django 3.2.5 on 2022-01-01 12:50 + +from django.conf import settings +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('parts', '0007_stock_lot'), + ] + + operations = [ + migrations.AlterField( + model_name='stock', + name='lot', + field=models.CharField(blank=True, default='', max_length=255), + preserve_default=False, + ), + migrations.CreateModel( + name='QrPrintJob', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)), + ('qrdata', models.CharField(blank=True, max_length=256)), + ('text', models.TextField(blank=True, max_length=512)), + ('print_count', models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(32)])), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index 8c14ffe..2195da3 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -5,6 +5,7 @@ from django.contrib.auth.models import User as AuthUser from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.dispatch import receiver +from django.core.validators import MinValueValidator, MaxValueValidator import os import uuid @@ -227,7 +228,7 @@ class Stock(models.Model): storage = models.ForeignKey(Storage, on_delete=models.PROTECT, blank=True, null=True) amount = models.PositiveIntegerField() watermark = models.IntegerField() # negative is no watermark - lot = models.CharField(max_length=255, null=True, blank=True) + lot = models.CharField(max_length=255, null=False, blank=True) def atomic_increment(self, increment): if self.amount + increment < 0: @@ -266,6 +267,18 @@ class DistributorNum(models.Model): def __str__(self): return self.component.name + '@' + self.distributor.name + ': ' + self.distributor_part_number +class QrPrintJob(models.Model): + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) + user = models.ForeignKey(AuthUser, on_delete=models.CASCADE, null=False, blank=False) + qrdata = models.CharField(max_length=256, blank=True, null=False) + text = models.TextField(max_length=512, blank=True, null=False) + print_count = models.PositiveIntegerField(default=1, validators=[MinValueValidator(0), MaxValueValidator(32)]) + + def clean(self): + super().clean() + if self.qrdata == '' and self.text == '': + raise ValidationError('Either QR Data or text must be set') + # These functions ensure that the uploaded images are deleted if the model is deleted or the image file is changed. @receiver(models.signals.post_delete, sender=Component) @receiver(models.signals.post_delete, sender=Distributor) diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index e31073a..a669055 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -119,7 +119,7 @@ -
+ {% csrf_token %} {{comp_form|crispy}} diff --git a/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html b/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html index f1afd1e..7898fa0 100644 --- a/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html +++ b/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html @@ -1,5 +1,5 @@