Skip to content

Commit 86b252c

Browse files
committed
Some refactoring of the agenda and minutes validation work.
- Legacy-Id: 13850
1 parent 46fc7b7 commit 86b252c

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

ietf/meeting/views.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from ietf.utils.pipe import pipe
6565
from ietf.utils.pdf import pdf_pages
6666
from ietf.utils.text import xslugify
67-
from ietf.utils.textupload import ( validate_file_size, validate_mime_type,
67+
from ietf.utils.validators import ( validate_file_size, validate_mime_type,
6868
validate_file_extension, validate_no_html_frame, )
6969

7070
from .forms import (InterimMeetingModelForm, InterimAnnounceForm, InterimSessionModelForm,
@@ -1136,8 +1136,8 @@ class UploadBlueSheetForm(forms.Form):
11361136

11371137
def clean_file(self):
11381138
file = self.cleaned_data['file']
1139-
validate_mime_type(file.read(), settings.MEETING_VALID_BLUESHEET_MIME_TYPES)
1140-
validate_file_extension(file.name, settings.MEETING_VALID_BLUESHEET_EXTENSIONS)
1139+
validate_mime_type(file, settings.MEETING_VALID_BLUESHEET_MIME_TYPES)
1140+
validate_file_extension(file, settings.MEETING_VALID_BLUESHEET_EXTENSIONS)
11411141
return file
11421142

11431143
@role_required('Area Director', 'Secretariat', 'IRTF Chair', 'WG Chair')
@@ -1218,12 +1218,11 @@ def __init__(self, show_apply_to_all_checkbox, *args, **kwargs):
12181218

12191219
def clean_file(self):
12201220
file = self.cleaned_data['file']
1221-
validate_file_size(file._size)
1222-
ext = validate_file_extension(file.name, settings.MEETING_VALID_MINUTES_EXTENSIONS)
1223-
content = file.read()
1224-
mime_type, encoding = validate_mime_type(content, settings.MEETING_VALID_MINUTES_MIME_TYPES)
1221+
validate_file_size(file)
1222+
ext = validate_file_extension(file, settings.MEETING_VALID_MINUTES_EXTENSIONS)
1223+
mime_type, encoding = validate_mime_type(file, settings.MEETING_VALID_MINUTES_MIME_TYPES)
12251224
if ext in ['.html', '.htm'] or mime_type in ['text/html', ]:
1226-
validate_no_html_frame(content)
1225+
validate_no_html_frame(file)
12271226
return file
12281227

12291228
def upload_session_minutes(request, session_id, num):
@@ -1316,12 +1315,11 @@ def __init__(self, show_apply_to_all_checkbox, *args, **kwargs):
13161315

13171316
def clean_file(self):
13181317
file = self.cleaned_data['file']
1319-
validate_file_size(file._size)
1320-
ext = validate_file_extension(file.name, settings.MEETING_VALID_AGENDA_EXTENSIONS)
1321-
content = file.read()
1322-
mime_type, encoding = validate_mime_type(content, settings.MEETING_VALID_AGENDA_MIME_TYPES)
1318+
validate_file_size(file)
1319+
ext = validate_file_extension(file, settings.MEETING_VALID_AGENDA_EXTENSIONS)
1320+
mime_type, encoding = validate_mime_type(file, settings.MEETING_VALID_AGENDA_MIME_TYPES)
13231321
if ext in ['.html', '.htm'] or mime_type in ['text/html', ]:
1324-
validate_no_html_frame(content)
1322+
validate_no_html_frame(file)
13251323
return file
13261324

13271325
def upload_session_agenda(request, session_id, num):
@@ -1427,8 +1425,8 @@ def __init__(self, show_apply_to_all_checkbox, *args, **kwargs):
14271425

14281426
def clean_file(self):
14291427
file = self.cleaned_data['file']
1430-
validate_file_size(file._size)
1431-
validate_file_extension(file.name, settings.MEETING_VALID_SLIDES_EXTENSIONS)
1428+
validate_file_size(file)
1429+
validate_file_extension(file, settings.MEETING_VALID_SLIDES_EXTENSIONS)
14321430
return file
14331431

14341432
def upload_session_slides(request, session_id, num, name):

ietf/utils/textupload.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import re
2-
import os
3-
import magic
4-
from pyquery import PyQuery
52

6-
from django import forms
7-
from django.conf import settings
83
from django.core.exceptions import ValidationError
9-
from django.template.defaultfilters import filesizeformat
104

115
import debug # pyflakes:ignore
126

@@ -54,39 +48,3 @@ def get_cleaned_text_file_content(uploaded_file):
5448
content = content.replace("\r\n", "\n").replace("\r", "\n")
5549

5650
return content.encode("utf-8")
57-
58-
def get_mime_type(content):
59-
# try to fixup encoding
60-
if hasattr(magic, "open"):
61-
m = magic.open(magic.MAGIC_MIME)
62-
m.load()
63-
filetype = m.buffer(content)
64-
else:
65-
m = magic.Magic()
66-
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
67-
magic.magic_load(m.cookie, None)
68-
filetype = m.from_buffer(content)
69-
70-
return filetype.split('; ', 1)
71-
72-
def validate_file_size(size):
73-
if size > settings.SECR_MAX_UPLOAD_SIZE:
74-
raise forms.ValidationError('Please keep filesize under %s. Requested upload size was %s' % (filesizeformat(settings.SECR_MAX_UPLOAD_SIZE), filesizeformat(size)))
75-
76-
def validate_mime_type(content, valid):
77-
mime_type, encoding = get_mime_type(content)
78-
if not mime_type in valid:
79-
raise forms.ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' %
80-
(mime_type, ', '.join(valid) ))
81-
return mime_type, encoding
82-
83-
def validate_file_extension(name, valid):
84-
name, ext = os.path.splitext(name)
85-
if ext.lower() not in valid:
86-
raise forms.ValidationError('Found an unexpected extension: %s. Expected one of %s' % (ext, ','.join(valid)))
87-
return ext
88-
89-
def validate_no_html_frame(content):
90-
q = PyQuery(content)
91-
if q("frameset") or q("frame") or q("iframe"):
92-
raise forms.ValidationError('Found content with html frames. Please upload a file that does not use frames')

ietf/utils/validators.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
# Copyright The IETF Trust 2007, All Rights Reserved
33
from __future__ import unicode_literals
44

5+
import os
56
import re
7+
import magic
8+
from pyquery import PyQuery
69

10+
from django.conf import settings
711
from django.core.exceptions import ValidationError
812
from django.core.validators import RegexValidator
13+
from django.template.defaultfilters import filesizeformat
914
from django.utils.deconstruct import deconstructible
1015

16+
import debug # pyflakes:ignore
17+
1118
# Note that this is an instantiation of the regex validator, _not_ the
1219
# regex-string validator defined right below
1320
validate_no_control_chars = RegexValidator(
@@ -46,3 +53,40 @@ def __ne__(self, other):
4653

4754
validate_regular_expression_string = RegexStringValidator()
4855

56+
def get_mime_type(content):
57+
# try to fixup encoding
58+
if hasattr(magic, "open"):
59+
m = magic.open(magic.MAGIC_MIME)
60+
m.load()
61+
filetype = m.buffer(content)
62+
else:
63+
m = magic.Magic()
64+
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
65+
magic.magic_load(m.cookie, None)
66+
filetype = m.from_buffer(content)
67+
68+
return filetype.split('; ', 1)
69+
70+
def validate_file_size(file):
71+
if file._size > settings.SECR_MAX_UPLOAD_SIZE:
72+
raise ValidationError('Please keep filesize under %s. Requested upload size was %s' % (filesizeformat(settings.SECR_MAX_UPLOAD_SIZE), filesizeformat(file._size)))
73+
74+
def validate_mime_type(file, valid):
75+
file.open()
76+
mime_type, encoding = get_mime_type(file.read())
77+
if not mime_type in valid:
78+
raise ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' %
79+
(mime_type, ', '.join(valid) ))
80+
return mime_type, encoding
81+
82+
def validate_file_extension(file, valid):
83+
name, ext = os.path.splitext(file.name)
84+
if ext.lower() not in valid:
85+
raise ValidationError('Found an unexpected extension: %s. Expected one of %s' % (ext, ','.join(valid)))
86+
return ext
87+
88+
def validate_no_html_frame(file):
89+
file.open()
90+
q = PyQuery(file.read())
91+
if q("frameset") or q("frame") or q("iframe"):
92+
raise ValidationError('Found content with html frames. Please upload a file that does not use frames')

0 commit comments

Comments
 (0)