Gitlab CSE Unil
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
M. Chardon
ADIM
Commits
6cffed8e
Commit
6cffed8e
authored
Mar 23, 2018
by
M. Chardon
Browse files
correction api : ajout verif fichier image par json image/png etc
parent
7d901131
Changes
3
Hide whitespace changes
Inline
Side-by-side
adim_project/adim_app/forms.py
View file @
6cffed8e
# coding=utf-8
from
__future__
import
unicode_literals
from
django
import
forms
from
django.forms.utils
import
ErrorDict
from
utils.validators
import
validate_json_file
class
UploadImageFileForm
(
forms
.
Form
):
"""
This form is used to validate the image files submitted
for upload
"""
image_file
=
forms
.
ImageField
(
allow_empty_file
=
False
)
image_file
=
forms
.
ImageField
(
allow_empty_file
=
False
,
validators
=
[
validate_json_file
]
)
name
=
forms
.
CharField
(
max_length
=
125
,
required
=
False
)
sharing_opts
=
None
allow_public_publishing
=
None
\ No newline at end of file
allow_public_publishing
=
None
def
_clean_fields
(
self
):
super
(
UploadImageFileForm
,
self
).
_clean_fields
()
# Add control json import
errors
=
self
.
errors
if
errors
and
errors
.
get
(
'image_file'
):
fileError
=
errors
.
get
(
'image_file'
)
if
fileError
:
data
=
fileError
.
data
if
data
and
len
(
data
)
==
1
:
validatorError
=
data
[
0
]
paramserror
=
validatorError
.
params
if
validatorError
.
code
==
"invalid_extension"
and
paramserror
[
'extension'
]
==
""
or
validatorError
.
code
==
"invalid_json_extension"
:
self
.
_errors
=
ErrorDict
()
\ No newline at end of file
adim_project/adim_app/utils/validators.py
0 → 100644
View file @
6cffed8e
from
django.core.exceptions
import
ValidationError
from
django.utils.translation
import
gettext_lazy
as
_
def
validate_json_file
(
value
):
message
=
_
(
"File extension '%(extension)s' is not allowed. "
"Allowed extensions are: '%(allowed_extensions)s'."
)
code
=
'invalid_json_extension'
if
value
.
content_type
:
options
=
{
# the file types which are going to be allowed for upload
# must be a mimetype
"acceptedformats"
:
(
"image/jpeg"
,
"image/jpg"
,
"image/png"
,
)
}
# allowed file type
extension
=
value
.
content_type
if
extension
not
in
options
[
"acceptedformats"
]:
raise
ValidationError
(
message
,
code
=
code
,
params
=
{
'extension'
:
extension
,
'allowed_extensions'
:
', '
.
join
(
options
[
"acceptedformats"
])
}
)
\ No newline at end of file
adim_project/adim_app/views.py
View file @
6cffed8e
...
...
@@ -30,10 +30,8 @@ from .forms import UploadImageFileForm
from
sendfile
import
sendfile
from
.utils
import
add_image_border
,
create_image_thumbnail
logger
=
logging
.
getLogger
(
__name__
)
def
home
(
request
):
"""
Home page
...
...
@@ -229,15 +227,15 @@ def annotate(request, anobj_uuid=None):
owner_membership
=
None
context
.
update
({
'display_shared_annotations'
:
(
anobj
.
sharing_mode
!=
SHARING_MODE_NONE
)
and
(
anobj
.
is_owned
(
request
.
user
.
id
)
or
anobj
.
allow_public_publishing
or
(
owner_membership
and
owner_membership
.
publish_mode
==
2
)
)
})
(
anobj
.
sharing_mode
!=
SHARING_MODE_NONE
)
and
(
anobj
.
is_owned
(
request
.
user
.
id
)
or
anobj
.
allow_public_publishing
or
(
owner_membership
and
owner_membership
.
publish_mode
==
2
)
)
})
# ----- Environment specific settings
template_path
=
[
'adim'
]
...
...
@@ -323,10 +321,13 @@ def upload_file(request, anobj_uuid=None):
response_data
[
'error'
]
=
"invalid"
if
'application/json'
in
request
.
META
.
get
(
'HTTP_ACCEPT'
,
''
):
return
HttpResponse
(
json
.
dumps
(
response_data
),
content_type
=
response_type
)
httpResponse
=
HttpResponse
(
json
.
dumps
(
response_data
),
content_type
=
response_type
)
if
response_data
[
'error'
]
==
"invalid"
:
httpResponse
.
status_code
=
400
return
httpResponse
else
:
return
HttpResponseRedirect
(
response_data
[
'next'
])
def
_validate_uploaded_file
(
image_file
):
options
=
{
...
...
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