Import and PEP8 cleanups
This commit is contained in:
@@ -3,11 +3,9 @@ from __future__ import absolute_import
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from navigation.api import (bind_links,
|
||||
register_model_list_columns, Link)
|
||||
register_model_list_columns)
|
||||
from common.utils import encapsulate
|
||||
from project_setup.api import register_setup
|
||||
from documents.permissions import (PERMISSION_DOCUMENT_NEW_VERSION,
|
||||
PERMISSION_DOCUMENT_CREATE)
|
||||
from scheduler.api import register_interval_job
|
||||
from documents.models import Document
|
||||
|
||||
@@ -15,18 +13,15 @@ from .staging import StagingFile
|
||||
from .models import (WebForm, StagingFolder, SourceTransformation,
|
||||
WatchFolder, POP3Email, IMAPEmail)
|
||||
from .widgets import staging_file_thumbnail
|
||||
from .permissions import (PERMISSION_SOURCES_SETUP_VIEW,
|
||||
PERMISSION_SOURCES_SETUP_EDIT, PERMISSION_SOURCES_SETUP_DELETE,
|
||||
PERMISSION_SOURCES_SETUP_CREATE)
|
||||
from .tasks import task_fetch_pop3_emails, task_fetch_imap_emails
|
||||
from .conf.settings import EMAIL_PROCESSING_INTERVAL
|
||||
from .links import (staging_file_preview, staging_file_delete, setup_sources,
|
||||
from .links import (staging_file_delete, setup_sources,
|
||||
setup_web_form_list, setup_staging_folder_list, setup_watch_folder_list,
|
||||
setup_pop3_email_list, setup_imap_email_list, setup_source_edit,
|
||||
setup_source_delete, setup_source_create, setup_source_log_list,
|
||||
setup_source_transformation_list, setup_source_transformation_create,
|
||||
setup_source_transformation_edit, setup_source_transformation_delete,
|
||||
source_list, upload_version, document_create_multiple)
|
||||
upload_version, document_create_multiple)
|
||||
|
||||
bind_links([StagingFile], [staging_file_delete])
|
||||
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
"""Configuration options for the sources app"""
|
||||
import os
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.conf import settings
|
||||
|
||||
from smart_settings.api import Setting, SettingNamespace
|
||||
|
||||
namespace = SettingNamespace('sources', _(u'Sources'), module='sources.conf.settings')
|
||||
|
||||
POP3_DEFAULT_TIMEOUT = 10 # for POP3 only not POP3_SSL
|
||||
POP3_DEFAULT_TIMEOUT = 10 # for POP3 only not POP3_SSL
|
||||
DEFAULT_EMAIL_PROCESSING_INTERVAL = 60
|
||||
DEFAULT_POP3_EMAIL_LOG_COUNT = 10 # Max log entries to store
|
||||
DEFAULT_POP3_EMAIL_LOG_COUNT = 10 # Max log entries to store
|
||||
|
||||
Setting(
|
||||
namespace=namespace,
|
||||
|
||||
@@ -38,11 +38,11 @@ class SourceLogManager(models.Manager):
|
||||
to_delete = self.model.objects.filter(content_type=content_type, object_id=source.pk).order_by('-creation_datetime')[LOG_SIZE:]
|
||||
for recent_to_delete in to_delete:
|
||||
recent_to_delete.delete()
|
||||
|
||||
|
||||
def get_for_source(self, source):
|
||||
content_type = ContentType.objects.get_for_model(source)
|
||||
return self.model.objects.filter(content_type=content_type, object_id=source.pk).order_by('-creation_datetime')
|
||||
|
||||
|
||||
def get_latest_for(self, source):
|
||||
content_type = ContentType.objects.get_for_model(source)
|
||||
return self.model.objects.filter(content_type=content_type, object_id=source.pk).latest().creation_datetime
|
||||
|
||||
@@ -21,7 +21,6 @@ from django.contrib.contenttypes import generic
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import transaction
|
||||
from django.core.files import File
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from converter.api import get_available_transformations_choices
|
||||
from converter.literals import DIMENSION_SEPARATOR
|
||||
@@ -38,9 +37,9 @@ from .literals import (SOURCE_CHOICES, SOURCE_CHOICES_PLURAL,
|
||||
SOURCE_ICON_CHOICES, SOURCE_CHOICE_WATCH, SOURCE_UNCOMPRESS_CHOICES,
|
||||
SOURCE_UNCOMPRESS_CHOICE_Y,
|
||||
POP3_PORT, POP3_SSL_PORT,
|
||||
SOURCE_CHOICE_POP3_EMAIL, DEFAULT_POP3_INTERVAL,
|
||||
SOURCE_CHOICE_POP3_EMAIL, DEFAULT_POP3_INTERVAL,
|
||||
IMAP_PORT, IMAP_SSL_PORT,
|
||||
SOURCE_CHOICE_IMAP_EMAIL, DEFAULT_IMAP_INTERVAL,
|
||||
SOURCE_CHOICE_IMAP_EMAIL, DEFAULT_IMAP_INTERVAL,
|
||||
IMAP_DEFAULT_MAILBOX)
|
||||
from .compressed_file import CompressedFile, NotACompressedFile
|
||||
from .conf.settings import POP3_TIMEOUT
|
||||
@@ -169,14 +168,14 @@ class SourceLog(models.Model):
|
||||
source = generic.GenericForeignKey('content_type', 'object_id')
|
||||
creation_datetime = models.DateTimeField(verbose_name=_(u'date time'))
|
||||
status = models.TextField(verbose_name=_(u'status'))
|
||||
|
||||
|
||||
objects = SourceLogManager()
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.pk:
|
||||
self.creation_datetime = datetime.datetime.now()
|
||||
return super(SourceLog, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name = _(u'source log')
|
||||
verbose_name_plural = _(u'sources logs')
|
||||
@@ -194,8 +193,8 @@ class InteractiveBaseModel(BaseModel):
|
||||
|
||||
class Meta(BaseModel.Meta):
|
||||
abstract = True
|
||||
|
||||
|
||||
|
||||
|
||||
class PseudoFile(File):
|
||||
def __init__(self, file, name):
|
||||
self.name = name
|
||||
@@ -203,8 +202,8 @@ class PseudoFile(File):
|
||||
self.file.seek(0, os.SEEK_END)
|
||||
self.size = self.file.tell()
|
||||
self.file.seek(0)
|
||||
|
||||
|
||||
|
||||
|
||||
class Attachment(File):
|
||||
def __init__(self, part, name):
|
||||
self.name = name
|
||||
@@ -251,7 +250,7 @@ class EmailBaseModel(IntervalBaseModel):
|
||||
counter += 1
|
||||
|
||||
logger.debug('filename: %s' % filename)
|
||||
|
||||
|
||||
document_file = Attachment(part, name=filename)
|
||||
source.upload_file(document_file, expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y), document_type=source.document_type)
|
||||
|
||||
@@ -274,7 +273,7 @@ class POP3Email(EmailBaseModel):
|
||||
else:
|
||||
difference = datetime.datetime.now() - last_check
|
||||
initial_trigger = False
|
||||
|
||||
|
||||
if difference >= datetime.timedelta(seconds=self.interval) or initial_trigger:
|
||||
try:
|
||||
logger.debug('Starting POP3 email fetch')
|
||||
@@ -293,28 +292,28 @@ class POP3Email(EmailBaseModel):
|
||||
mailbox.user(self.username)
|
||||
mailbox.pass_(self.password)
|
||||
messages_info = mailbox.list()
|
||||
|
||||
|
||||
logger.debug('messages_info:')
|
||||
logger.debug(messages_info)
|
||||
logger.debug('messages count: %s' % len(messages_info[1]))
|
||||
|
||||
|
||||
for message_info in messages_info[1]:
|
||||
message_number, message_size = message_info.split()
|
||||
logger.debug('message_number: %s' % message_number)
|
||||
logger.debug('message_size: %s' % message_size)
|
||||
|
||||
|
||||
complete_message = '\n'.join(mailbox.retr(message_number)[1])
|
||||
|
||||
EmailBaseModel.process_message(source=self, message=complete_message)
|
||||
mailbox.dele(message_number)
|
||||
|
||||
|
||||
mailbox.quit()
|
||||
SourceLog.objects.save_status(source=self, status='Successful connection.')
|
||||
|
||||
except Exception, exc:
|
||||
logger.error('Unhandled exception: %s' % exc)
|
||||
SourceLog.objects.save_status(source=self, status='Error: %s' % exc)
|
||||
|
||||
|
||||
class Meta(EmailBaseModel.Meta):
|
||||
verbose_name = _(u'POP email')
|
||||
verbose_name_plural = _(u'POP email')
|
||||
@@ -322,7 +321,7 @@ class POP3Email(EmailBaseModel):
|
||||
|
||||
class IMAPEmail(EmailBaseModel):
|
||||
source_type = SOURCE_CHOICE_IMAP_EMAIL
|
||||
|
||||
|
||||
mailbox = models.CharField(max_length=64, blank=True, verbose_name=_(u'mailbox'), help_text=_(u'Mail from which to check for messages with attached documents. If none is specified, the default mailbox is %s') % IMAP_DEFAULT_MAILBOX)
|
||||
|
||||
# http://www.doughellmann.com/PyMOTW/imaplib/
|
||||
@@ -336,7 +335,7 @@ class IMAPEmail(EmailBaseModel):
|
||||
else:
|
||||
difference = datetime.datetime.now() - last_check
|
||||
initial_trigger = False
|
||||
|
||||
|
||||
if difference >= datetime.timedelta(seconds=self.interval) or initial_trigger:
|
||||
try:
|
||||
logger.debug('Starting IMAP email fetch')
|
||||
@@ -353,7 +352,7 @@ class IMAPEmail(EmailBaseModel):
|
||||
|
||||
mailbox.login(self.username, self.password)
|
||||
mailbox.select(self.mailbox or IMAP_DEFAULT_MAILBOX)
|
||||
|
||||
|
||||
status, data = mailbox.search(None, 'NOT', 'DELETED')
|
||||
if data:
|
||||
messages_info = data[0].split()
|
||||
@@ -369,7 +368,7 @@ class IMAPEmail(EmailBaseModel):
|
||||
mailbox.close()
|
||||
mailbox.logout()
|
||||
SourceLog.objects.save_status(source=self, status='Successful connection.')
|
||||
|
||||
|
||||
except Exception, exc:
|
||||
logger.error('Unhandled exception: %s' % exc)
|
||||
SourceLog.objects.save_status(source=self, status='Error: %s' % exc)
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from datetime import timedelta, datetime
|
||||
import platform
|
||||
import logging
|
||||
|
||||
from django.db.models import Q
|
||||
|
||||
from job_processor.api import process_job
|
||||
from lock_manager import Lock, LockError
|
||||
|
||||
from .models import POP3Email, IMAPEmail
|
||||
@@ -20,7 +15,7 @@ def task_fetch_single_pop3_email(pop3_email):
|
||||
try:
|
||||
lock_id = u'task_fetch_pop3_email-%d' % pop3_email.pk
|
||||
logger.debug('trying to acquire lock: %s' % lock_id)
|
||||
lock = Lock.acquire_lock(lock_id, POP3_TIMEOUT + 60) # Lock expiration = POP3 timeout + 60 seconds
|
||||
lock = Lock.acquire_lock(lock_id, POP3_TIMEOUT + 60) # Lock expiration = POP3 timeout + 60 seconds
|
||||
logger.debug('acquired lock: %s' % lock_id)
|
||||
try:
|
||||
pop3_email.fetch_mail()
|
||||
@@ -31,7 +26,7 @@ def task_fetch_single_pop3_email(pop3_email):
|
||||
except LockError:
|
||||
logger.error('unable to obtain lock')
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def task_fetch_pop3_emails():
|
||||
logger.debug('executing')
|
||||
@@ -57,7 +52,7 @@ def task_fetch_single_imap_email(imap_email):
|
||||
except LockError:
|
||||
logger.error('unable to obtain lock')
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def task_fetch_imap_emails():
|
||||
logger.debug('executing')
|
||||
|
||||
@@ -35,6 +35,6 @@ urlpatterns = patterns('sources.views',
|
||||
url(r'^setup/interactive/(?P<source_type>\w+)/(?P<source_id>\d+)/transformation/create/$', 'setup_source_transformation_create', (), 'setup_source_transformation_create'),
|
||||
url(r'^setup/interactive/source/transformation/(?P<transformation_id>\d+)/edit/$', 'setup_source_transformation_edit', (), 'setup_source_transformation_edit'),
|
||||
url(r'^setup/interactive/source/transformation/(?P<transformation_id>\d+)/delete/$', 'setup_source_transformation_delete', (), 'setup_source_transformation_delete'),
|
||||
|
||||
url(r'^setup/source/(?P<source_type>\w+)/(?P<source_pk>\d+)/log/list/$', 'setup_source_log_list', (), 'setup_source_log_list'),
|
||||
|
||||
url(r'^setup/source/(?P<source_type>\w+)/(?P<source_pk>\d+)/log/list/$', 'setup_source_log_list', (), 'setup_source_log_list'),
|
||||
)
|
||||
|
||||
@@ -73,7 +73,7 @@ def get_active_tab_links(document=None):
|
||||
staging_folders = StagingFolder.objects.filter(enabled=True)
|
||||
for staging_folder in staging_folders:
|
||||
tab_links.append(get_tab_link_for_source(staging_folder, document))
|
||||
|
||||
|
||||
return {
|
||||
'tab_links': tab_links,
|
||||
SOURCE_CHOICE_WEB_FORM: web_forms,
|
||||
@@ -480,7 +480,7 @@ def setup_source_edit(request, source_type, source_id):
|
||||
elif source_type == SOURCE_CHOICE_IMAP_EMAIL:
|
||||
cls = IMAPEmail
|
||||
form_class = IMAPEmailSetupForm
|
||||
|
||||
|
||||
source = get_object_or_404(cls, pk=source_id)
|
||||
next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/')))
|
||||
|
||||
@@ -623,7 +623,7 @@ def setup_source_log_list(request, source_type, source_pk):
|
||||
cls = POP3Email
|
||||
elif source_type == SOURCE_CHOICE_IMAP_EMAIL:
|
||||
cls = IMAPEmail
|
||||
|
||||
|
||||
source = get_object_or_404(cls, pk=source_pk)
|
||||
|
||||
context = {
|
||||
@@ -659,7 +659,7 @@ def setup_source_transformation_list(request, source_type, source_id):
|
||||
cls = POP3Email
|
||||
elif source_type == SOURCE_CHOICE_IMAP_EMAIL:
|
||||
cls = IMAPEmail
|
||||
|
||||
|
||||
source = get_object_or_404(cls, pk=source_id)
|
||||
|
||||
context = {
|
||||
@@ -765,7 +765,7 @@ def setup_source_transformation_create(request, source_type, source_id):
|
||||
cls = POP3Email
|
||||
elif source_type == SOURCE_CHOICE_IMAP_EMAIL:
|
||||
cls = IMAPEmail
|
||||
|
||||
|
||||
source = get_object_or_404(cls, pk=source_id)
|
||||
|
||||
redirect_view = reverse('setup_source_transformation_list', args=[source.source_type, source.pk])
|
||||
|
||||
Reference in New Issue
Block a user