Gitlab CSE Unil

Commit d1ae5d8d authored by M. Chardon's avatar M. Chardon
Browse files

Merge branch 'django' into 'javascript'

Django

See merge request !6
parents 51f46844 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
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
......@@ -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 = {
......
......@@ -21,7 +21,7 @@
<div class="navbar-header">
{% block navbar_brand %}
<a class="navbar-brand" href="{% url "adim.app:home" %}">ADIM</a>
{% if debug %}
{% if adim_prod == false %}
<span style="color:red;font-weight: bold;font-size: large;"><i class="fa fa-exclamation-triangle"></i> ENVIRONNEMENT DE TEST&nbsp;</span>
{% endif %}
{% endblock %}
......
Markdown is supported
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