Move class and form class functions to the utils module, issue #56: remove whilelist and blacklist support
This commit is contained in:
+30
-29
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user