move validators to central place since multiple models will start using it

pull/3/head
lalanza808 5 years ago
parent e68125ff3b
commit d00202554c

@ -0,0 +1,14 @@
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from monero.address import address
def address_is_valid_monero(value):
try:
address(value)
return True
except ValueError:
raise ValidationError(
_('%(value)s is an invalid Monero address'),
params={'value': value},
)

@ -4,6 +4,7 @@ from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import items.models import items.models
import core.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -24,7 +25,7 @@ class Migration(migrations.Migration):
('description', models.TextField(max_length=500)), ('description', models.TextField(max_length=500)),
('ask_price_xmr', models.FloatField()), ('ask_price_xmr', models.FloatField()),
('available', models.BooleanField(default=True)), ('available', models.BooleanField(default=True)),
('payout_address', models.CharField(max_length=100, validators=[items.models.address_is_valid_monero])), ('payout_address', models.CharField(max_length=100, validators=[core.validators.address_is_valid_monero])),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='owner', to=settings.AUTH_USER_MODEL)), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='owner', to=settings.AUTH_USER_MODEL)),
], ],
), ),

@ -1,26 +1,15 @@
from os import path as os_path from os import path as os_path
from secrets import token_urlsafe from secrets import token_urlsafe
from django.db import models from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import InMemoryUploadedFile from django.core.files.uploadedfile import InMemoryUploadedFile
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User from django.contrib.auth.models import User
from monero.address import address from monero.address import address
from PIL import Image, ExifTags from PIL import Image, ExifTags
from io import BytesIO from io import BytesIO
from core.monero import AuctionDaemon from core.monero import AuctionDaemon
from core.validators import address_is_valid_monero
def address_is_valid_monero(value):
try:
address(value)
return True
except ValueError:
raise ValidationError(
_('%(value)s is an invalid Monero address'),
params={'value': value},
)
class Item(models.Model): class Item(models.Model):
owner = models.ForeignKey(User, related_name='owner', on_delete=models.CASCADE) owner = models.ForeignKey(User, related_name='owner', on_delete=models.CASCADE)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)