Move class and form class functions to the utils module, issue #56: remove whilelist and blacklist support

This commit is contained in:
Roberto Rosario
2014-10-21 03:21:32 -04:00
parent f8fe2ada0e
commit 0d6b7dfbc6
5 changed files with 153 additions and 71 deletions

View File

@@ -10,7 +10,6 @@ from documents.forms import DocumentForm
from .models import (IMAPEmail, POP3Email, SourceTransformation,
StagingFolderSource, WebFormSource, WatchFolderSource)
from .utils import validate_whitelist_blacklist
logger = logging.getLogger(__name__)
@@ -70,8 +69,6 @@ class WebFormForm(DocumentForm):
def clean_file(self):
data = self.cleaned_data['file']
validate_whitelist_blacklist(data.name, self.source.whitelist.split(','), self.source.blacklist.split(','))
return data

View File

@@ -0,0 +1,116 @@
# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Deleting field 'Source.whitelist'
db.delete_column(u'sources_source', 'whitelist')
# Deleting field 'Source.blacklist'
db.delete_column(u'sources_source', 'blacklist')
def backwards(self, orm):
# Adding field 'Source.whitelist'
db.add_column(u'sources_source', 'whitelist',
self.gf('django.db.models.fields.TextField')(default='', blank=True),
keep_default=False)
# Adding field 'Source.blacklist'
db.add_column(u'sources_source', 'blacklist',
self.gf('django.db.models.fields.TextField')(default='', blank=True),
keep_default=False)
models = {
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
u'documents.documenttype': {
'Meta': {'ordering': "['name']", 'object_name': 'DocumentType'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'})
},
u'sources.emailbasemodel': {
'Meta': {'ordering': "('title',)", 'object_name': 'EmailBaseModel', '_ormbases': [u'sources.IntervalBaseModel']},
'host': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
u'intervalbasemodel_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.IntervalBaseModel']", 'unique': 'True', 'primary_key': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '96'}),
'port': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}),
'ssl': ('django.db.models.fields.BooleanField', [], {}),
'username': ('django.db.models.fields.CharField', [], {'max_length': '96'})
},
u'sources.imapemail': {
'Meta': {'ordering': "('title',)", 'object_name': 'IMAPEmail', '_ormbases': [u'sources.EmailBaseModel']},
u'emailbasemodel_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.EmailBaseModel']", 'unique': 'True', 'primary_key': 'True'}),
'mailbox': ('django.db.models.fields.CharField', [], {'default': "'INBOX'", 'max_length': '64'})
},
u'sources.interactivesource': {
'Meta': {'ordering': "('title',)", 'object_name': 'InteractiveSource', '_ormbases': [u'sources.Source']},
u'source_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.Source']", 'unique': 'True', 'primary_key': 'True'})
},
u'sources.intervalbasemodel': {
'Meta': {'ordering': "('title',)", 'object_name': 'IntervalBaseModel', '_ormbases': [u'sources.OutOfProcessSource']},
'document_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['documents.DocumentType']", 'null': 'True', 'blank': 'True'}),
'interval': ('django.db.models.fields.PositiveIntegerField', [], {'default': '60'}),
u'outofprocesssource_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.OutOfProcessSource']", 'unique': 'True', 'primary_key': 'True'}),
'uncompress': ('django.db.models.fields.CharField', [], {'max_length': '1'})
},
u'sources.outofprocesssource': {
'Meta': {'ordering': "('title',)", 'object_name': 'OutOfProcessSource', '_ormbases': [u'sources.Source']},
u'source_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.Source']", 'unique': 'True', 'primary_key': 'True'})
},
u'sources.pop3email': {
'Meta': {'ordering': "('title',)", 'object_name': 'POP3Email', '_ormbases': [u'sources.EmailBaseModel']},
u'emailbasemodel_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.EmailBaseModel']", 'unique': 'True', 'primary_key': 'True'}),
'timeout': ('django.db.models.fields.PositiveIntegerField', [], {'default': '60'})
},
u'sources.source': {
'Meta': {'ordering': "('title',)", 'object_name': 'Source'},
'enabled': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '64'})
},
u'sources.sourcetransformation': {
'Meta': {'ordering': "('order',)", 'object_name': 'SourceTransformation'},
'arguments': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
'order': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'null': 'True', 'db_index': 'True', 'blank': 'True'}),
'transformation': ('django.db.models.fields.CharField', [], {'max_length': '128'})
},
u'sources.stagingfoldersource': {
'Meta': {'ordering': "('title',)", 'object_name': 'StagingFolderSource', '_ormbases': [u'sources.InteractiveSource']},
'delete_after_upload': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'folder_path': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
u'interactivesource_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.InteractiveSource']", 'unique': 'True', 'primary_key': 'True'}),
'preview_height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
'preview_width': ('django.db.models.fields.IntegerField', [], {}),
'uncompress': ('django.db.models.fields.CharField', [], {'max_length': '1'})
},
u'sources.watchfoldersource': {
'Meta': {'ordering': "('title',)", 'object_name': 'WatchFolderSource', '_ormbases': [u'sources.OutOfProcessSource']},
'delete_after_upload': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'folder_path': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'interval': ('django.db.models.fields.PositiveIntegerField', [], {}),
u'outofprocesssource_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.OutOfProcessSource']", 'unique': 'True', 'primary_key': 'True'}),
'uncompress': ('django.db.models.fields.CharField', [], {'max_length': '1'})
},
u'sources.webformsource': {
'Meta': {'ordering': "('title',)", 'object_name': 'WebFormSource', '_ormbases': [u'sources.InteractiveSource']},
u'interactivesource_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['sources.InteractiveSource']", 'unique': 'True', 'primary_key': 'True'}),
'uncompress': ('django.db.models.fields.CharField', [], {'max_length': '1'})
}
}
complete_apps = ['sources']

View File

@@ -40,9 +40,6 @@ logger = logging.getLogger(__name__)
class Source(models.Model):
title = models.CharField(max_length=64, verbose_name=_(u'Title'))
enabled = models.BooleanField(default=True, verbose_name=_(u'Enabled'))
# TODO: remove whitelist and blacklists
whitelist = models.TextField(blank=True, verbose_name=_(u'Whitelist'), editable=False)
blacklist = models.TextField(blank=True, verbose_name=_(u'Blacklist'), editable=False)
objects = InheritanceManager()

View File

@@ -1,35 +1,36 @@
import re
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext
from .forms import (POP3EmailSetupForm, IMAPEmailSetupForm,
StagingFolderSetupForm, WatchFolderSetupForm,
WebFormSetupForm)
from .literals import (SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3,
SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH,
SOURCE_CHOICE_WEB_FORM)
from .models import (IMAPEmail, POP3Email, StagingFolderSource,
WatchFolderSource, WebFormSource)
# 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]):
for okpattern in item_list:
if re.findall(okpattern.replace('*', '\S+'), value, re.I):
# match!
if reject:
return False
else:
return True
# default is to accept all
return default_accept
def get_class(source_type):
if source_type == SOURCE_CHOICE_WEB_FORM:
return WebFormSource
elif source_type == SOURCE_CHOICE_STAGING:
return StagingFolderSource
elif source_type == SOURCE_CHOICE_WATCH:
return WatchFolderSource
elif source_type == SOURCE_CHOICE_EMAIL_POP3:
return POP3Email
elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
return IMAPEmail
def validate_whitelist_blacklist(value, whitelist, blacklist):
if not accept_item(value, whitelist, blacklist):
raise ValidationError(ugettext(u'Whitelist Blacklist validation error.'))
def get_form_class(source_type):
if source_type == SOURCE_CHOICE_WEB_FORM:
return WebFormSetupForm
elif source_type == SOURCE_CHOICE_STAGING:
return StagingFolderSetupForm
elif source_type == SOURCE_CHOICE_WATCH:
return WatchFolderSetupForm
elif source_type == SOURCE_CHOICE_EMAIL_POP3:
return POP3EmailSetupForm
elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
return IMAPEmailSetupForm

View File

@@ -23,47 +23,18 @@ from documents.permissions import (PERMISSION_DOCUMENT_CREATE,
from metadata.api import decode_metadata_from_url, metadata_repr_as_list
from permissions.models import Permission
from .forms import (POP3EmailSetupForm, IMAPEmailSetupForm, StagingDocumentForm,
StagingFolderSetupForm, SourceTransformationForm,
SourceTransformationForm_create, WatchFolderSetupForm,
WebFormForm, WebFormSetupForm)
from .literals import (SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3,
SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH,
SOURCE_CHOICE_WEB_FORM, SOURCE_UNCOMPRESS_CHOICE_ASK,
SOURCE_UNCOMPRESS_CHOICE_Y)
from .models import (IMAPEmail, POP3Email, Source, StagingFolderSource,
SourceTransformation, WatchFolderSource, WebFormSource)
from .forms import (StagingDocumentForm, SourceTransformationForm,
SourceTransformationForm_create, WebFormForm)
from .literals import (SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WEB_FORM,
SOURCE_UNCOMPRESS_CHOICE_ASK, SOURCE_UNCOMPRESS_CHOICE_Y)
from .models import (Source, StagingFolderSource, SourceTransformation,
WebFormSource)
from .permissions import (PERMISSION_SOURCES_SETUP_CREATE,
PERMISSION_SOURCES_SETUP_DELETE,
PERMISSION_SOURCES_SETUP_EDIT,
PERMISSION_SOURCES_SETUP_VIEW)
from .tasks import task_upload_document
def get_class(source_type):
if source_type == SOURCE_CHOICE_WEB_FORM:
return WebFormSource
elif source_type == SOURCE_CHOICE_STAGING:
return StagingFolderSource
elif source_type == SOURCE_CHOICE_WATCH:
return WatchFolderSource
elif source_type == SOURCE_CHOICE_EMAIL_POP3:
return POP3Email
elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
return IMAPEmail
def get_form_class(source_type):
if source_type == SOURCE_CHOICE_WEB_FORM:
return WebFormSetupForm
elif source_type == SOURCE_CHOICE_STAGING:
return StagingFolderSetupForm
elif source_type == SOURCE_CHOICE_WATCH:
return WatchFolderSetupForm
elif source_type == SOURCE_CHOICE_EMAIL_POP3:
return POP3EmailSetupForm
elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
return IMAPEmailSetupForm
from .utils import get_class, get_form_class
def document_create_siblings(request, document_id):