Initial whitelist/blacklist support
This commit is contained in:
@@ -6,6 +6,7 @@ from documents.forms import DocumentForm
|
|||||||
|
|
||||||
from sources.models import WebForm, StagingFolder
|
from sources.models import WebForm, StagingFolder
|
||||||
from sources.widgets import FamFamRadioSelect
|
from sources.widgets import FamFamRadioSelect
|
||||||
|
from sources.utils import validate_whitelist_blacklist
|
||||||
|
|
||||||
|
|
||||||
class StagingDocumentForm(DocumentForm):
|
class StagingDocumentForm(DocumentForm):
|
||||||
@@ -16,6 +17,7 @@ class StagingDocumentForm(DocumentForm):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
cls = kwargs.pop('cls')
|
cls = kwargs.pop('cls')
|
||||||
show_expand = kwargs.pop('show_expand', False)
|
show_expand = kwargs.pop('show_expand', False)
|
||||||
|
self.source = kwargs.pop('source')
|
||||||
super(StagingDocumentForm, self).__init__(*args, **kwargs)
|
super(StagingDocumentForm, self).__init__(*args, **kwargs)
|
||||||
try:
|
try:
|
||||||
self.fields['staging_file_id'].choices = [
|
self.fields['staging_file_id'].choices = [
|
||||||
@@ -44,7 +46,9 @@ class StagingDocumentForm(DocumentForm):
|
|||||||
class WebFormForm(DocumentForm):
|
class WebFormForm(DocumentForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
show_expand = kwargs.pop('show_expand', False)
|
show_expand = kwargs.pop('show_expand', False)
|
||||||
|
self.source = kwargs.pop('source')
|
||||||
super(WebFormForm, self).__init__(*args, **kwargs)
|
super(WebFormForm, self).__init__(*args, **kwargs)
|
||||||
|
print self.instance
|
||||||
|
|
||||||
if show_expand:
|
if show_expand:
|
||||||
self.fields['expand'] = forms.BooleanField(
|
self.fields['expand'] = forms.BooleanField(
|
||||||
@@ -52,6 +56,12 @@ class WebFormForm(DocumentForm):
|
|||||||
help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents')
|
help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def clean_file(self):
|
||||||
|
data = self.cleaned_data['file']
|
||||||
|
validate_whitelist_blacklist(data.name, self.source.whitelist.split(','), self.source.blacklist.split(','))
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class WebFormSetupForm(forms.ModelForm):
|
class WebFormSetupForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|||||||
37
apps/sources/utils.py
Normal file
37
apps/sources/utils.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.utils.translation import ugettext
|
||||||
|
|
||||||
|
# From http://www.peterbe.com/plog/whitelist-blacklist-logic
|
||||||
|
def accept_item(value, whitelist, blacklist, default_accept=True):
|
||||||
|
""" return true if this item is either whitelisted or
|
||||||
|
not blacklisted """
|
||||||
|
if not whitelist:
|
||||||
|
whitelist = []
|
||||||
|
|
||||||
|
if not blacklist:
|
||||||
|
blacklist = []
|
||||||
|
|
||||||
|
# note the order
|
||||||
|
for reject, item_list in ([False, whitelist], [True, blacklist]):
|
||||||
|
print 'item_list: %s' % item_list
|
||||||
|
print 'reject: %s' % reject
|
||||||
|
for okpattern in item_list:
|
||||||
|
print 'okpattern: %s' % okpattern
|
||||||
|
if re.findall(okpattern.replace('*','\S+'), value, re.I):
|
||||||
|
# match!
|
||||||
|
print 'MATCH'
|
||||||
|
if reject:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# default is to accept all
|
||||||
|
return default_accept
|
||||||
|
|
||||||
|
|
||||||
|
def validate_whitelist_blacklist(value, whitelist, blacklist):
|
||||||
|
print 'blacklist', blacklist
|
||||||
|
if not accept_item(value, whitelist, blacklist):
|
||||||
|
raise ValidationError(ugettext(u'Whitelist Blacklist validation error.'))
|
||||||
@@ -113,7 +113,8 @@ def upload_interactive(request, source_type=None, source_id=None):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = WebFormForm(request.POST, request.FILES,
|
form = WebFormForm(request.POST, request.FILES,
|
||||||
document_type=document_type,
|
document_type=document_type,
|
||||||
show_expand=(web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK)
|
show_expand=(web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||||
|
source=web_form
|
||||||
)
|
)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
try:
|
try:
|
||||||
@@ -136,7 +137,11 @@ def upload_interactive(request, source_type=None, source_id=None):
|
|||||||
|
|
||||||
return HttpResponseRedirect(request.get_full_path())
|
return HttpResponseRedirect(request.get_full_path())
|
||||||
else:
|
else:
|
||||||
form = WebFormForm(show_expand=(web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK), document_type=document_type)
|
form = WebFormForm(
|
||||||
|
show_expand=(web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||||
|
document_type=document_type,
|
||||||
|
source=web_form
|
||||||
|
)
|
||||||
|
|
||||||
subtemplates_list.append({
|
subtemplates_list.append({
|
||||||
'name': 'generic_form_subtemplate.html',
|
'name': 'generic_form_subtemplate.html',
|
||||||
@@ -152,7 +157,8 @@ def upload_interactive(request, source_type=None, source_id=None):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = StagingDocumentForm(request.POST, request.FILES,
|
form = StagingDocumentForm(request.POST, request.FILES,
|
||||||
cls=StagingFile, document_type=document_type,
|
cls=StagingFile, document_type=document_type,
|
||||||
show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK)
|
show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||||
|
source=staging_folder
|
||||||
)
|
)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
try:
|
try:
|
||||||
@@ -182,7 +188,8 @@ def upload_interactive(request, source_type=None, source_id=None):
|
|||||||
else:
|
else:
|
||||||
form = StagingDocumentForm(cls=StagingFile,
|
form = StagingDocumentForm(cls=StagingFile,
|
||||||
document_type=document_type,
|
document_type=document_type,
|
||||||
show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK)
|
show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||||
|
source=staging_folder
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
staging_filelist = StagingFile.get_all()
|
staging_filelist = StagingFile.get_all()
|
||||||
|
|||||||
Reference in New Issue
Block a user