replace image field with svg_image field, added validator, and change serializer
This commit is contained in:
parent
f0a84a9847
commit
5edcd0afe9
24
apps/location/migrations/0007_auto_20190826_1342.py
Normal file
24
apps/location/migrations/0007_auto_20190826_1342.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-08-26 13:42
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import utils.methods
|
||||||
|
import utils.validators
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('location', '0006_country_image'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='country',
|
||||||
|
name='image',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='country',
|
||||||
|
name='svg_image',
|
||||||
|
field=models.FileField(blank=True, default=None, null=True, upload_to=utils.methods.svg_image_path, validators=[utils.validators.svg_image_validator], verbose_name='SVG image'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -3,14 +3,14 @@ from django.contrib.gis.db import models
|
||||||
from django.contrib.postgres.fields import JSONField
|
from django.contrib.postgres.fields import JSONField
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from utils.models import ProjectBaseMixin, LocaleManagerMixin, ImageMixin
|
from utils.models import ProjectBaseMixin, LocaleManagerMixin, SVGImageMixin
|
||||||
|
|
||||||
|
|
||||||
class CountryManager(LocaleManagerMixin):
|
class CountryManager(LocaleManagerMixin):
|
||||||
"""Extended manager for Country model."""
|
"""Extended manager for Country model."""
|
||||||
|
|
||||||
|
|
||||||
class Country(ImageMixin, ProjectBaseMixin):
|
class Country(SVGImageMixin, ProjectBaseMixin):
|
||||||
"""Country model."""
|
"""Country model."""
|
||||||
|
|
||||||
name = JSONField(null=True, blank=True, default=None,
|
name = JSONField(null=True, blank=True, default=None,
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class CountrySerializer(serializers.ModelSerializer):
|
||||||
fields = [
|
fields = [
|
||||||
'id',
|
'id',
|
||||||
'code',
|
'code',
|
||||||
'image',
|
'svg_image',
|
||||||
'name_trans',
|
'name_trans',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Utils app method."""
|
"""Utils app method."""
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http.request import HttpRequest
|
from django.http.request import HttpRequest
|
||||||
from django.utils.timezone import datetime
|
from django.utils.timezone import datetime
|
||||||
|
|
@ -40,3 +41,12 @@ def image_path(instance, filename):
|
||||||
instance._meta.model_name,
|
instance._meta.model_name,
|
||||||
datetime.now().strftime(settings.REST_DATE_FORMAT),
|
datetime.now().strftime(settings.REST_DATE_FORMAT),
|
||||||
filename)
|
filename)
|
||||||
|
|
||||||
|
|
||||||
|
def svg_image_path(instance, filename):
|
||||||
|
"""Determine SVG path method."""
|
||||||
|
filename = '%s.svg' % generate_code()
|
||||||
|
return 'image/svg/%s/%s/%s' % (
|
||||||
|
instance._meta.model_name,
|
||||||
|
datetime.now().strftime(settings.REST_DATE_FORMAT),
|
||||||
|
filename)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ from django.utils.html import mark_safe
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from easy_thumbnails.fields import ThumbnailerImageField
|
from easy_thumbnails.fields import ThumbnailerImageField
|
||||||
|
|
||||||
from utils.methods import image_path
|
from utils.methods import image_path, svg_image_path
|
||||||
|
from utils.validators import svg_image_validator
|
||||||
|
|
||||||
|
|
||||||
class ProjectBaseMixin(models.Model):
|
class ProjectBaseMixin(models.Model):
|
||||||
|
|
@ -86,6 +87,18 @@ class ImageMixin(models.Model):
|
||||||
image_tag.allow_tags = True
|
image_tag.allow_tags = True
|
||||||
|
|
||||||
|
|
||||||
|
class SVGImageMixin(models.Model):
|
||||||
|
"""SVG image model."""
|
||||||
|
|
||||||
|
svg_image = models.FileField(upload_to=svg_image_path,
|
||||||
|
blank=True, null=True, default=None,
|
||||||
|
validators=[svg_image_validator, ],
|
||||||
|
verbose_name=_('SVG image'))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
class PlatformMixin(models.Model):
|
class PlatformMixin(models.Model):
|
||||||
"""Platforms"""
|
"""Platforms"""
|
||||||
|
|
||||||
|
|
|
||||||
22
apps/utils/validators.py
Normal file
22
apps/utils/validators.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
"""DB field validators"""
|
||||||
|
import xml.etree.cElementTree as et
|
||||||
|
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
def svg_image_validator(file: object) -> object:
|
||||||
|
"""Validate SVG file"""
|
||||||
|
tag = None
|
||||||
|
try:
|
||||||
|
for event, el in et.iterparse(file, ('start',)):
|
||||||
|
tag = el.tag
|
||||||
|
break
|
||||||
|
assert tag == '{http://www.w3.org/2000/svg}svg'
|
||||||
|
except:
|
||||||
|
raise ValidationError(
|
||||||
|
message='Invalid SVG image file',
|
||||||
|
code='invalid_svg_image',
|
||||||
|
params={'value': file},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return file
|
||||||
|
|
@ -74,4 +74,5 @@ urlpatterns = urlpatterns + \
|
||||||
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|
||||||
urlpatterns.extend(urlpatterns_doc)
|
urlpatterns.extend(urlpatterns_doc)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user