Some refactoring of the agenda and minutes validation work.

- Legacy-Id: 13850
This commit is contained in:
Henrik Levkowetz 2017-07-13 10:42:48 +00:00
parent 46fc7b77fd
commit 86b252c90e
3 changed files with 57 additions and 57 deletions

View file

@ -64,7 +64,7 @@ from ietf.utils.mail import send_mail_message
from ietf.utils.pipe import pipe
from ietf.utils.pdf import pdf_pages
from ietf.utils.text import xslugify
from ietf.utils.textupload import ( validate_file_size, validate_mime_type,
from ietf.utils.validators import ( validate_file_size, validate_mime_type,
validate_file_extension, validate_no_html_frame, )
from .forms import (InterimMeetingModelForm, InterimAnnounceForm, InterimSessionModelForm,
@ -1136,8 +1136,8 @@ class UploadBlueSheetForm(forms.Form):
def clean_file(self):
file = self.cleaned_data['file']
validate_mime_type(file.read(), settings.MEETING_VALID_BLUESHEET_MIME_TYPES)
validate_file_extension(file.name, settings.MEETING_VALID_BLUESHEET_EXTENSIONS)
validate_mime_type(file, settings.MEETING_VALID_BLUESHEET_MIME_TYPES)
validate_file_extension(file, settings.MEETING_VALID_BLUESHEET_EXTENSIONS)
return file
@role_required('Area Director', 'Secretariat', 'IRTF Chair', 'WG Chair')
@ -1218,12 +1218,11 @@ class UploadMinutesForm(forms.Form):
def clean_file(self):
file = self.cleaned_data['file']
validate_file_size(file._size)
ext = validate_file_extension(file.name, settings.MEETING_VALID_MINUTES_EXTENSIONS)
content = file.read()
mime_type, encoding = validate_mime_type(content, settings.MEETING_VALID_MINUTES_MIME_TYPES)
validate_file_size(file)
ext = validate_file_extension(file, settings.MEETING_VALID_MINUTES_EXTENSIONS)
mime_type, encoding = validate_mime_type(file, settings.MEETING_VALID_MINUTES_MIME_TYPES)
if ext in ['.html', '.htm'] or mime_type in ['text/html', ]:
validate_no_html_frame(content)
validate_no_html_frame(file)
return file
def upload_session_minutes(request, session_id, num):
@ -1316,12 +1315,11 @@ class UploadAgendaForm(forms.Form):
def clean_file(self):
file = self.cleaned_data['file']
validate_file_size(file._size)
ext = validate_file_extension(file.name, settings.MEETING_VALID_AGENDA_EXTENSIONS)
content = file.read()
mime_type, encoding = validate_mime_type(content, settings.MEETING_VALID_AGENDA_MIME_TYPES)
validate_file_size(file)
ext = validate_file_extension(file, settings.MEETING_VALID_AGENDA_EXTENSIONS)
mime_type, encoding = validate_mime_type(file, settings.MEETING_VALID_AGENDA_MIME_TYPES)
if ext in ['.html', '.htm'] or mime_type in ['text/html', ]:
validate_no_html_frame(content)
validate_no_html_frame(file)
return file
def upload_session_agenda(request, session_id, num):
@ -1427,8 +1425,8 @@ class UploadSlidesForm(forms.Form):
def clean_file(self):
file = self.cleaned_data['file']
validate_file_size(file._size)
validate_file_extension(file.name, settings.MEETING_VALID_SLIDES_EXTENSIONS)
validate_file_size(file)
validate_file_extension(file, settings.MEETING_VALID_SLIDES_EXTENSIONS)
return file
def upload_session_slides(request, session_id, num, name):

View file

@ -1,12 +1,6 @@
import re
import os
import magic
from pyquery import PyQuery
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.template.defaultfilters import filesizeformat
import debug # pyflakes:ignore
@ -54,39 +48,3 @@ def get_cleaned_text_file_content(uploaded_file):
content = content.replace("\r\n", "\n").replace("\r", "\n")
return content.encode("utf-8")
def get_mime_type(content):
# try to fixup encoding
if hasattr(magic, "open"):
m = magic.open(magic.MAGIC_MIME)
m.load()
filetype = m.buffer(content)
else:
m = magic.Magic()
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
magic.magic_load(m.cookie, None)
filetype = m.from_buffer(content)
return filetype.split('; ', 1)
def validate_file_size(size):
if size > settings.SECR_MAX_UPLOAD_SIZE:
raise forms.ValidationError('Please keep filesize under %s. Requested upload size was %s' % (filesizeformat(settings.SECR_MAX_UPLOAD_SIZE), filesizeformat(size)))
def validate_mime_type(content, valid):
mime_type, encoding = get_mime_type(content)
if not mime_type in valid:
raise forms.ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' %
(mime_type, ', '.join(valid) ))
return mime_type, encoding
def validate_file_extension(name, valid):
name, ext = os.path.splitext(name)
if ext.lower() not in valid:
raise forms.ValidationError('Found an unexpected extension: %s. Expected one of %s' % (ext, ','.join(valid)))
return ext
def validate_no_html_frame(content):
q = PyQuery(content)
if q("frameset") or q("frame") or q("iframe"):
raise forms.ValidationError('Found content with html frames. Please upload a file that does not use frames')

View file

@ -2,12 +2,19 @@
# Copyright The IETF Trust 2007, All Rights Reserved
from __future__ import unicode_literals
import os
import re
import magic
from pyquery import PyQuery
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.template.defaultfilters import filesizeformat
from django.utils.deconstruct import deconstructible
import debug # pyflakes:ignore
# Note that this is an instantiation of the regex validator, _not_ the
# regex-string validator defined right below
validate_no_control_chars = RegexValidator(
@ -46,3 +53,40 @@ class RegexStringValidator(object):
validate_regular_expression_string = RegexStringValidator()
def get_mime_type(content):
# try to fixup encoding
if hasattr(magic, "open"):
m = magic.open(magic.MAGIC_MIME)
m.load()
filetype = m.buffer(content)
else:
m = magic.Magic()
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
magic.magic_load(m.cookie, None)
filetype = m.from_buffer(content)
return filetype.split('; ', 1)
def validate_file_size(file):
if file._size > settings.SECR_MAX_UPLOAD_SIZE:
raise ValidationError('Please keep filesize under %s. Requested upload size was %s' % (filesizeformat(settings.SECR_MAX_UPLOAD_SIZE), filesizeformat(file._size)))
def validate_mime_type(file, valid):
file.open()
mime_type, encoding = get_mime_type(file.read())
if not mime_type in valid:
raise ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' %
(mime_type, ', '.join(valid) ))
return mime_type, encoding
def validate_file_extension(file, valid):
name, ext = os.path.splitext(file.name)
if ext.lower() not in valid:
raise ValidationError('Found an unexpected extension: %s. Expected one of %s' % (ext, ','.join(valid)))
return ext
def validate_no_html_frame(file):
file.open()
q = PyQuery(file.read())
if q("frameset") or q("frame") or q("iframe"):
raise ValidationError('Found content with html frames. Please upload a file that does not use frames')