Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
concrexit
Commits
8e7c4258
Verified
Commit
8e7c4258
authored
Mar 26, 2019
by
Sébastiaan Versteeg
Browse files
Replace FileValidator and remove old validator from previous migrations
parent
c19fde14
Changes
18
Hide whitespace changes
Inline
Side-by-side
docs/utils.rst
View file @
8e7c4258
...
...
@@ -59,12 +59,4 @@ utils.translation module
:undoc-members:
:show-inheritance:
utils.validators module
-----------------------
.. automodule:: utils.validators
:members:
:undoc-members:
:show-inheritance:
website/documents/migrations/0001_initial.py
View file @
8e7c4258
...
...
@@ -4,7 +4,6 @@ from __future__ import unicode_literals
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -20,7 +19,7 @@ class Migration(migrations.Migration):
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'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
)),
],
),
...
...
@@ -28,7 +27,7 @@ class Migration(migrations.Migration):
name
=
'GeneralMeeting'
,
fields
=
[
(
'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
()),
(
'location'
,
models
.
CharField
(
max_length
=
200
)),
],
...
...
@@ -37,7 +36,7 @@ class Migration(migrations.Migration):
name
=
'GeneralMeetingDocument'
,
fields
=
[
(
'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'
)),
],
),
...
...
@@ -46,7 +45,7 @@ class Migration(migrations.Migration):
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'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
)),
],
),
...
...
website/documents/migrations/0002_auto_20160723_1247.py
View file @
8e7c4258
...
...
@@ -4,7 +4,6 @@ from __future__ import unicode_literals
import
django.core.validators
from
django.db
import
migrations
,
models
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -19,9 +18,9 @@ class Migration(migrations.Migration):
fields
=
[
(
'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
)])),
(
'policy_document'
,
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
=
[
utils
.
validators
.
validate_file_extension
])),
(
'financial_report'
,
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
=
[])),
(
'financial_report'
,
models
.
FileField
(
blank
=
True
,
upload_to
=
'documents/association/'
,
validators
=
[])),
],
),
migrations
.
DeleteModel
(
...
...
website/documents/migrations/0003_auto_20160724_1314.py
View file @
8e7c4258
...
...
@@ -3,7 +3,6 @@
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -18,7 +17,7 @@ class Migration(migrations.Migration):
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'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
)),
],
),
...
...
website/documents/migrations/0005_auto_20160813_1116.py
View file @
8e7c4258
...
...
@@ -3,7 +3,6 @@
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
import
utils.validators
def
meetingdocument_upload_to
(
instance
,
filename
):
return
'documents/meetings/{}/files/{}'
.
format
(
instance
.
meeting
.
pk
,
...
...
@@ -20,6 +19,6 @@ class Migration(migrations.Migration):
migrations
.
AlterField
(
model_name
=
'generalmeetingdocument'
,
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
=
[]),
),
]
website/documents/migrations/0006_auto_20160907_1934.py
View file @
8e7c4258
...
...
@@ -3,7 +3,6 @@
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -16,6 +15,6 @@ class Migration(migrations.Migration):
migrations
.
AlterField
(
model_name
=
'generalmeeting'
,
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
=
[]),
),
]
website/documents/migrations/0007_auto_20160930_1447.py
View file @
8e7c4258
...
...
@@ -3,7 +3,6 @@
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -16,6 +15,6 @@ class Migration(migrations.Migration):
migrations
.
AlterField
(
model_name
=
'generalmeeting'
,
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
=
[]),
),
]
website/documents/migrations/0008_0_refactor_documents.py
View file @
8e7c4258
...
...
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -22,8 +21,8 @@ class Migration(migrations.Migration):
(
'created'
,
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'created'
)),
(
'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'
)),
(
'file_en'
,
models
.
FileField
(
upload_to
=
'documents/'
,
validators
=
[
utils
.
validators
.
validate_file_extension
],
verbose_name
=
'file (EN)'
)),
(
'file_nl'
,
models
.
FileField
(
upload_to
=
'documents/'
,
validators
=
[
utils
.
validators
.
validate_file_extension
],
verbose_name
=
'file (NL)'
)),
(
'file_en'
,
models
.
FileField
(
upload_to
=
'documents/'
,
validators
=
[],
verbose_name
=
'file (EN)'
)),
(
'file_nl'
,
models
.
FileField
(
upload_to
=
'documents/'
,
validators
=
[],
verbose_name
=
'file (NL)'
)),
(
'members_only'
,
models
.
BooleanField
(
default
=
False
,
verbose_name
=
'members only'
)),
],
options
=
{
...
...
website/documents/migrations/0008_1_refactor_documents.py
View file @
8e7c4258
...
...
@@ -4,7 +4,6 @@ import os
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
utils.validators
def
make_assocation_documents
(
apps
,
schema_editor
):
...
...
website/documents/migrations/0008_2_refactor_documents.py
View file @
8e7c4258
...
...
@@ -4,7 +4,6 @@ import os
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
website/documents/migrations/0011_auto_20190327_1948.py
0 → 100644
View file @
8e7c4258
# 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)'
),
),
]
website/documents/models.py
View file @
8e7c4258
from
django.core.validators
import
MinValueValidator
from
django.core.validators
import
MinValueValidator
,
FileExtensionValidator
from
django.db
import
models
from
django.urls
import
reverse
from
django.utils
import
timezone
from
django.utils.translation
import
ugettext_lazy
as
_
from
utils.translation
import
ModelTranslateMeta
,
MultilingualField
from
utils.validators
import
validate_file_extension
class
Document
(
models
.
Model
,
metaclass
=
ModelTranslateMeta
):
...
...
@@ -49,7 +48,8 @@ class Document(models.Model, metaclass=ModelTranslateMeta):
models
.
FileField
,
verbose_name
=
_
(
'file'
),
upload_to
=
'documents/'
,
validators
=
[
validate_file_extension
],
validators
=
[
FileExtensionValidator
(
[
'.txt'
,
'.pdf'
,
'.jpg'
,
'.jpeg'
,
'.png'
])],
)
members_only
=
models
.
BooleanField
(
...
...
website/members/migrations/0002_becomeamemberdocument.py
View file @
8e7c4258
...
...
@@ -4,8 +4,6 @@ from __future__ import unicode_literals
from
django.db
import
migrations
,
models
import
utils.validators
class
Migration
(
migrations
.
Migration
):
...
...
@@ -19,7 +17,7 @@ class Migration(migrations.Migration):
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'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
=
[])),
],
),
]
website/thabloid/migrations/0001_initial.py
View file @
8e7c4258
...
...
@@ -5,7 +5,6 @@ from __future__ import unicode_literals
import
django.core.validators
from
django.db
import
migrations
,
models
import
thabloid.models
import
utils.validators
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'
)),
(
'year'
,
models
.
IntegerField
(
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
1990
)])),
(
'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
(
...
...
website/thabloid/migrations/0004_auto_20190327_1948.py
0 → 100644
View file @
8e7c4258
# 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'
])]),
),
]
website/thabloid/models.py
View file @
8e7c4258
...
...
@@ -5,13 +5,12 @@ from itertools import zip_longest
from
PIL
import
Image
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.urls
import
reverse
from
django.utils.text
import
slugify
from
utils.threading
import
PopenAndCall
from
utils.validators
import
validate_file_extension
def
thabloid_filename
(
instance
,
filename
):
...
...
@@ -29,7 +28,8 @@ class Thabloid(models.Model):
file
=
models
.
FileField
(
upload_to
=
thabloid_filename
,
validators
=
[
validate_file_extension
],
validators
=
[
FileExtensionValidator
(
[
'.txt'
,
'.pdf'
,
'.jpg'
,
'.jpeg'
,
'.png'
])],
)
class
Meta
:
...
...
website/utils/tests.py
View file @
8e7c4258
...
...
@@ -8,7 +8,7 @@ from django.test import TestCase, override_settings
from
django.utils
import
translation
from
utils.translation
import
ModelTranslateMeta
,
MultilingualField
from
utils
import
snippets
,
validators
from
utils
import
snippets
LANGUAGES
=
[
(
'en'
,
'English'
),
...
...
@@ -23,8 +23,6 @@ def load_tests(_loader, tests, _ignore):
"""
# Adds the doctests in snippets
tests
.
addTests
(
doctest
.
DocTestSuite
(
snippets
))
# Adds the doctests in validators
tests
.
addTests
(
doctest
.
DocTestSuite
(
validators
))
return
tests
...
...
website/utils/validators.py
deleted
100644 → 0
View file @
c19fde14
"""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."
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment