Commit 5ad9dab2 authored by Luko van der Maas's avatar Luko van der Maas
Browse files

Merge branch 'tc/file-ext-validator' into 'master'

Replace FileValidator and remove old validator from previous migrations

Closes #833

See merge request !1199
parents 6f8fcb8b 8e7c4258
...@@ -59,12 +59,4 @@ utils.translation module ...@@ -59,12 +59,4 @@ utils.translation module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
utils.validators module
-----------------------
.. automodule:: utils.validators
:members:
:undoc-members:
:show-inheritance:
...@@ -4,7 +4,6 @@ from __future__ import unicode_literals ...@@ -4,7 +4,6 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -20,7 +19,7 @@ class Migration(migrations.Migration): ...@@ -20,7 +19,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('year', models.IntegerField()), ('year', models.IntegerField()),
('file', models.FileField(upload_to='documents/association/', validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to='documents/association/', validators=[])),
('filetype', models.CharField(choices=[('policy-document', 'Policy document'), ('annual-report', 'Annual report'), ('financial-report', 'Financial report')], max_length=16)), ('filetype', models.CharField(choices=[('policy-document', 'Policy document'), ('annual-report', 'Annual report'), ('financial-report', 'Financial report')], max_length=16)),
], ],
), ),
...@@ -28,7 +27,7 @@ class Migration(migrations.Migration): ...@@ -28,7 +27,7 @@ class Migration(migrations.Migration):
name='GeneralMeeting', name='GeneralMeeting',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('minutes', models.FileField(upload_to='documents/meetings/minutes/', validators=[utils.validators.validate_file_extension])), ('minutes', models.FileField(upload_to='documents/meetings/minutes/', validators=[])),
('datetime', models.DateTimeField()), ('datetime', models.DateTimeField()),
('location', models.CharField(max_length=200)), ('location', models.CharField(max_length=200)),
], ],
...@@ -37,7 +36,7 @@ class Migration(migrations.Migration): ...@@ -37,7 +36,7 @@ class Migration(migrations.Migration):
name='GeneralMeetingDocument', name='GeneralMeetingDocument',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to='documents/meetings/files/', validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to='documents/meetings/files/', validators=[])),
('meeting', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='documents.GeneralMeeting')), ('meeting', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='documents.GeneralMeeting')),
], ],
), ),
...@@ -46,7 +45,7 @@ class Migration(migrations.Migration): ...@@ -46,7 +45,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('file', models.FileField(upload_to='documents/generic/', validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to='documents/generic/', validators=[])),
('members_only', models.BooleanField(default=False)), ('members_only', models.BooleanField(default=False)),
], ],
), ),
......
...@@ -4,7 +4,6 @@ from __future__ import unicode_literals ...@@ -4,7 +4,6 @@ from __future__ import unicode_literals
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -19,9 +18,9 @@ class Migration(migrations.Migration): ...@@ -19,9 +18,9 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('year', models.IntegerField(unique=True, validators=[django.core.validators.MinValueValidator(1990)])), ('year', models.IntegerField(unique=True, validators=[django.core.validators.MinValueValidator(1990)])),
('policy_document', models.FileField(blank=True, upload_to='documents/association/', validators=[utils.validators.validate_file_extension])), ('policy_document', models.FileField(blank=True, upload_to='documents/association/', validators=[])),
('annual_report', models.FileField(blank=True, upload_to='documents/association/', validators=[utils.validators.validate_file_extension])), ('annual_report', models.FileField(blank=True, upload_to='documents/association/', validators=[])),
('financial_report', models.FileField(blank=True, upload_to='documents/association/', validators=[utils.validators.validate_file_extension])), ('financial_report', models.FileField(blank=True, upload_to='documents/association/', validators=[])),
], ],
), ),
migrations.DeleteModel( migrations.DeleteModel(
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -18,7 +17,7 @@ class Migration(migrations.Migration): ...@@ -18,7 +17,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('file', models.FileField(upload_to='documents/miscellaneous/', validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to='documents/miscellaneous/', validators=[])),
('members_only', models.BooleanField(default=False)), ('members_only', models.BooleanField(default=False)),
], ],
), ),
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import utils.validators
def meetingdocument_upload_to(instance, filename): def meetingdocument_upload_to(instance, filename):
return 'documents/meetings/{}/files/{}'.format(instance.meeting.pk, return 'documents/meetings/{}/files/{}'.format(instance.meeting.pk,
...@@ -20,6 +19,6 @@ class Migration(migrations.Migration): ...@@ -20,6 +19,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='generalmeetingdocument', model_name='generalmeetingdocument',
name='file', name='file',
field=models.FileField(upload_to=meetingdocument_upload_to, validators=[utils.validators.validate_file_extension]), field=models.FileField(upload_to=meetingdocument_upload_to, validators=[]),
), ),
] ]
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -16,6 +15,6 @@ class Migration(migrations.Migration): ...@@ -16,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='generalmeeting', model_name='generalmeeting',
name='minutes', name='minutes',
field=models.FileField(blank=True, upload_to='documents/meetings/minutes/', validators=[utils.validators.validate_file_extension]), field=models.FileField(blank=True, upload_to='documents/meetings/minutes/', validators=[]),
), ),
] ]
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -16,6 +15,6 @@ class Migration(migrations.Migration): ...@@ -16,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='generalmeeting', model_name='generalmeeting',
name='minutes', name='minutes',
field=models.FileField(blank=True, null=True, upload_to='documents/meetings/minutes/', validators=[utils.validators.validate_file_extension]), field=models.FileField(blank=True, null=True, upload_to='documents/meetings/minutes/', validators=[]),
), ),
] ]
...@@ -3,7 +3,6 @@ from __future__ import unicode_literals ...@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -22,8 +21,8 @@ class Migration(migrations.Migration): ...@@ -22,8 +21,8 @@ class Migration(migrations.Migration):
('created', models.DateTimeField(auto_now_add=True, verbose_name='created')), ('created', models.DateTimeField(auto_now_add=True, verbose_name='created')),
('last_updated', models.DateTimeField(auto_now=True, verbose_name='last updated')), ('last_updated', models.DateTimeField(auto_now=True, verbose_name='last updated')),
('category', models.CharField(choices=[('annual', 'Annual document'), ('association', 'Association document'), ('minutes', 'Minutes'), ('misc', 'Miscellaneous document')], default='misc', max_length=40, verbose_name='category')), ('category', models.CharField(choices=[('annual', 'Annual document'), ('association', 'Association document'), ('minutes', 'Minutes'), ('misc', 'Miscellaneous document')], default='misc', max_length=40, verbose_name='category')),
('file_en', models.FileField(upload_to='documents/', validators=[utils.validators.validate_file_extension], verbose_name='file (EN)')), ('file_en', models.FileField(upload_to='documents/', validators=[], verbose_name='file (EN)')),
('file_nl', models.FileField(upload_to='documents/', validators=[utils.validators.validate_file_extension], verbose_name='file (NL)')), ('file_nl', models.FileField(upload_to='documents/', validators=[], verbose_name='file (NL)')),
('members_only', models.BooleanField(default=False, verbose_name='members only')), ('members_only', models.BooleanField(default=False, verbose_name='members only')),
], ],
options={ options={
......
...@@ -4,7 +4,6 @@ import os ...@@ -4,7 +4,6 @@ import os
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import utils.validators
def make_assocation_documents(apps, schema_editor): def make_assocation_documents(apps, schema_editor):
......
...@@ -4,7 +4,6 @@ import os ...@@ -4,7 +4,6 @@ import os
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
......
# Generated by Django 2.1.7 on 2019-03-27 18:48
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('documents', '0010_auto_20181219_2146'),
]
operations = [
migrations.AlterField(
model_name='document',
name='file_en',
field=models.FileField(upload_to='documents/', validators=[django.core.validators.FileExtensionValidator(['.txt', '.pdf', '.jpg', '.jpeg', '.png'])], verbose_name='file (EN)'),
),
migrations.AlterField(
model_name='document',
name='file_nl',
field=models.FileField(upload_to='documents/', validators=[django.core.validators.FileExtensionValidator(['.txt', '.pdf', '.jpg', '.jpeg', '.png'])], verbose_name='file (NL)'),
),
]
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator, FileExtensionValidator
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from utils.translation import ModelTranslateMeta, MultilingualField from utils.translation import ModelTranslateMeta, MultilingualField
from utils.validators import validate_file_extension
class Document(models.Model, metaclass=ModelTranslateMeta): class Document(models.Model, metaclass=ModelTranslateMeta):
...@@ -49,7 +48,8 @@ class Document(models.Model, metaclass=ModelTranslateMeta): ...@@ -49,7 +48,8 @@ class Document(models.Model, metaclass=ModelTranslateMeta):
models.FileField, models.FileField,
verbose_name=_('file'), verbose_name=_('file'),
upload_to='documents/', upload_to='documents/',
validators=[validate_file_extension], validators=[FileExtensionValidator(
['.txt', '.pdf', '.jpg', '.jpeg', '.png'])],
) )
members_only = models.BooleanField( members_only = models.BooleanField(
......
...@@ -4,8 +4,6 @@ from __future__ import unicode_literals ...@@ -4,8 +4,6 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -19,7 +17,7 @@ class Migration(migrations.Migration): ...@@ -19,7 +17,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('file', models.FileField(upload_to='members/', validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to='members/', validators=[])),
], ],
), ),
] ]
...@@ -5,7 +5,6 @@ from __future__ import unicode_literals ...@@ -5,7 +5,6 @@ from __future__ import unicode_literals
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import thabloid.models import thabloid.models
import utils.validators
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -22,7 +21,7 @@ class Migration(migrations.Migration): ...@@ -22,7 +21,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('year', models.IntegerField(validators=[django.core.validators.MinValueValidator(1990)])), ('year', models.IntegerField(validators=[django.core.validators.MinValueValidator(1990)])),
('issue', models.IntegerField()), ('issue', models.IntegerField()),
('file', models.FileField(upload_to=thabloid.models.thabloid_filename, validators=[utils.validators.validate_file_extension])), ('file', models.FileField(upload_to=thabloid.models.thabloid_filename, validators=[])),
], ],
), ),
migrations.AlterUniqueTogether( migrations.AlterUniqueTogether(
......
# Generated by Django 2.1.7 on 2019-03-27 18:48
import django.core.validators
from django.db import migrations, models
import thabloid.models
class Migration(migrations.Migration):
dependencies = [
('thabloid', '0003_auto_20180307_2047'),
]
operations = [
migrations.AlterField(
model_name='thabloid',
name='file',
field=models.FileField(upload_to=thabloid.models.thabloid_filename, validators=[django.core.validators.FileExtensionValidator(['.txt', '.pdf', '.jpg', '.jpeg', '.png'])]),
),
]
...@@ -5,13 +5,12 @@ from itertools import zip_longest ...@@ -5,13 +5,12 @@ from itertools import zip_longest
from PIL import Image from PIL import Image
from django.conf import settings from django.conf import settings
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator, FileExtensionValidator
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.text import slugify from django.utils.text import slugify
from utils.threading import PopenAndCall from utils.threading import PopenAndCall
from utils.validators import validate_file_extension
def thabloid_filename(instance, filename): def thabloid_filename(instance, filename):
...@@ -29,7 +28,8 @@ class Thabloid(models.Model): ...@@ -29,7 +28,8 @@ class Thabloid(models.Model):
file = models.FileField( file = models.FileField(
upload_to=thabloid_filename, upload_to=thabloid_filename,
validators=[validate_file_extension], validators=[FileExtensionValidator(
['.txt', '.pdf', '.jpg', '.jpeg', '.png'])],
) )
class Meta: class Meta:
......
...@@ -8,7 +8,7 @@ from django.test import TestCase, override_settings ...@@ -8,7 +8,7 @@ from django.test import TestCase, override_settings
from django.utils import translation from django.utils import translation
from utils.translation import ModelTranslateMeta, MultilingualField from utils.translation import ModelTranslateMeta, MultilingualField
from utils import snippets, validators from utils import snippets
LANGUAGES = [ LANGUAGES = [
('en', 'English'), ('en', 'English'),
...@@ -23,8 +23,6 @@ def load_tests(_loader, tests, _ignore): ...@@ -23,8 +23,6 @@ def load_tests(_loader, tests, _ignore):
""" """
# Adds the doctests in snippets # Adds the doctests in snippets
tests.addTests(doctest.DocTestSuite(snippets)) tests.addTests(doctest.DocTestSuite(snippets))
# Adds the doctests in validators
tests.addTests(doctest.DocTestSuite(validators))
return tests return tests
......
"""Validators for form fields"""
import os
from django.core.exceptions import ValidationError
def validate_file_extension(file, exts=None):
"""
Checks if a file has a certain allowed extension. Raises a
:class:`~django.core.exceptions.ValidationError` if that's not the case.
:param file: the file to validate
:param list(str) exts: the list of acceptable types.
Default: ``.txt``, ``.pdf``, ``.jpg``, ``.jpeg``, ``.png``.
:raises ValidationError: if the extension is not allowed.
>>> class File(object):
... pass
>>> f = File()
>>> f.name = 'foo.jpeg'
>>> validate_file_extension(f)
>>> f.name = 'foo.exe'
>>> validate_file_extension(f)
Traceback (most recent call last):
...
django.core.exceptions.ValidationError: ['File extension not allowed.']
>>> validate_file_extension(f, ['.exe'])
"""
if exts is None:
exts = ['.txt', '.pdf', '.jpg', '.jpeg', '.png']
ext = os.path.splitext(file.name)[1]
if not ext.lower() in exts:
raise ValidationError("File extension not allowed.")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment