mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 05:26:45 +00:00
Add validator to part name
- Check for 'illegal' characters - Mostly just as a test for validators!
This commit is contained in:
parent
68b273aae1
commit
8ac180a9e3
15
InvenTree/InvenTree/validators.py
Normal file
15
InvenTree/InvenTree/validators.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
"""
|
||||||
|
Custom field validators for InvenTree
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
def validate_part_name(value):
|
||||||
|
# Prevent some illegal characters in part names
|
||||||
|
for c in ['/', '\\', '|', '#', '$']:
|
||||||
|
if c in str(value):
|
||||||
|
raise ValidationError(
|
||||||
|
_('Invalid character in part name')
|
||||||
|
)
|
19
InvenTree/part/migrations/0021_auto_20190510_2220.py
Normal file
19
InvenTree/part/migrations/0021_auto_20190510_2220.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 2.2 on 2019-05-10 12:20
|
||||||
|
|
||||||
|
import InvenTree.validators
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('part', '0020_auto_20190510_2022'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='part',
|
||||||
|
name='name',
|
||||||
|
field=models.CharField(help_text='Part name', max_length=100, validators=[InvenTree.validators.validate_part_name]),
|
||||||
|
),
|
||||||
|
]
|
@ -25,6 +25,7 @@ from django.db.models.signals import pre_delete
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from InvenTree import helpers
|
from InvenTree import helpers
|
||||||
|
from InvenTree import validators
|
||||||
from InvenTree.models import InvenTreeTree
|
from InvenTree.models import InvenTreeTree
|
||||||
from company.models import Company
|
from company.models import Company
|
||||||
|
|
||||||
@ -172,7 +173,9 @@ class Part(models.Model):
|
|||||||
else:
|
else:
|
||||||
return static('/img/blank_image.png')
|
return static('/img/blank_image.png')
|
||||||
|
|
||||||
name = models.CharField(max_length=100, blank=False, help_text='Part name')
|
name = models.CharField(max_length=100, blank=False, help_text='Part name',
|
||||||
|
validators=[validators.validate_part_name]
|
||||||
|
)
|
||||||
|
|
||||||
variant = models.CharField(max_length=32, blank=True, help_text='Part variant or revision code')
|
variant = models.CharField(max_length=32, blank=True, help_text='Part variant or revision code')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user