From f484e1a9c3902e2ba696aa8e417c6ea1b1215e7d Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 22 Feb 2012 11:57:00 -0400 Subject: [PATCH] PEP8 and general cleanups --- apps/django_gpg/api.py | 2 +- apps/django_gpg/forms.py | 1 - apps/django_gpg/runtime.py | 2 +- apps/django_gpg/views.py | 9 ++++----- apps/document_comments/views.py | 2 +- apps/document_indexing/api.py | 8 +++----- apps/document_indexing/tools.py | 2 +- apps/document_indexing/widgets.py | 5 ++--- apps/document_signatures/managers.py | 4 ++-- apps/linking/forms.py | 2 -- apps/linking/views.py | 1 - apps/mimetype/api.py | 3 ++- apps/ocr/__init__.py | 1 + apps/ocr/views.py | 2 -- apps/signaler/management/commands/collectstatic.py | 2 +- apps/sources/management/commands/bulk_upload.py | 13 +++++++------ apps/sources/models.py | 14 +++++++------- apps/sources/views.py | 8 ++++---- .../management/commands/import_users.py | 12 +++++++----- 19 files changed, 44 insertions(+), 49 deletions(-) diff --git a/apps/django_gpg/api.py b/apps/django_gpg/api.py index 38e60fde76..6d2feea14b 100644 --- a/apps/django_gpg/api.py +++ b/apps/django_gpg/api.py @@ -178,7 +178,7 @@ class GPG(object): self.keyservers = keyservers self.gpg = gnupg.GPG(**kwargs) - + def verify_file(self, file_input, detached_signature=None, fetch_key=False): """ Verify the signature of a file. diff --git a/apps/django_gpg/forms.py b/apps/django_gpg/forms.py index 9a9514fdb3..930298d4b1 100644 --- a/apps/django_gpg/forms.py +++ b/apps/django_gpg/forms.py @@ -1,6 +1,5 @@ from django import forms from django.utils.translation import ugettext_lazy as _ -from django.utils.translation import ugettext class KeySearchForm(forms.Form): diff --git a/apps/django_gpg/runtime.py b/apps/django_gpg/runtime.py index afb5d73867..257ba8e3fd 100644 --- a/apps/django_gpg/runtime.py +++ b/apps/django_gpg/runtime.py @@ -8,6 +8,6 @@ from .conf.settings import KEYSERVERS, GPG_HOME try: gpg = GPG(home=GPG_HOME, keyservers=KEYSERVERS) except Exception, e: - gpg = GPG(keyservers=KEYSERVERS) + gpg = GPG(keyservers=KEYSERVERS) sys.stderr.write(u'ERROR: GPG initialization error: %s\n' % e) sys.stderr.write(u'INFO: Initializating GPG with system default home\n') diff --git a/apps/django_gpg/views.py b/apps/django_gpg/views.py index 09a74d0a4d..b390dee333 100644 --- a/apps/django_gpg/views.py +++ b/apps/django_gpg/views.py @@ -9,12 +9,11 @@ from django.template import RequestContext from django.contrib import messages from permissions.models import Permission -from common.utils import (urlquote, encapsulate) +from common.utils import encapsulate -from .api import Key, SIGNATURE_STATES +from .api import Key from .runtime import gpg -from .exceptions import (GPGVerificationError, KeyFetchingError, - KeyImportError) +from .exceptions import KeyFetchingError, KeyImportError from .forms import KeySearchForm from .permissions import (PERMISSION_KEY_VIEW, PERMISSION_KEY_DELETE, PERMISSION_KEYSERVER_QUERY, PERMISSION_KEY_RECEIVE) @@ -40,7 +39,7 @@ def key_receive(request, key_id): except (KeyImportError, KeyError, TypeError), e: messages.error( request, - _(u'Unable to import key id: %(key_id)s; %(error)s') % + _(u'Unable to import key id: %(key_id)s; %(error)s') % { 'key_id': key_id, 'error': e, diff --git a/apps/document_comments/views.py b/apps/document_comments/views.py index 7c089b3cda..4a87435fa3 100644 --- a/apps/document_comments/views.py +++ b/apps/document_comments/views.py @@ -34,7 +34,7 @@ def comment_delete(request, comment_id=None, comment_id_list=None): if not comments: messages.error(request, _(u'Must provide at least one comment.')) - return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) + return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', '/'))) diff --git a/apps/document_indexing/api.py b/apps/document_indexing/api.py index 7112f1d3a2..49f262f35a 100644 --- a/apps/document_indexing/api.py +++ b/apps/document_indexing/api.py @@ -2,13 +2,11 @@ from __future__ import absolute_import from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext -from django.core.urlresolvers import reverse from django.template.defaultfilters import slugify from metadata.classes import MetadataClass -from .models import (Index, IndexTemplateNode, IndexInstanceNode, - DocumentRenameCount) +from .models import Index, IndexInstanceNode, DocumentRenameCount from .conf.settings import (AVAILABLE_INDEXING_FUNCTIONS, MAX_SUFFIX_COUNT, SLUGIFY_PATHS) from .filesystem import (fs_create_index_directory, @@ -94,7 +92,7 @@ def cascade_eval(eval_dict, document, template_node, parent_index_instance=None) fs_create_index_directory(index_instance) except Exception, exc: warnings.append(_(u'Error updating document index, expression: %(expression)s; %(exception)s') % { - 'expression': template_node.expression, 'exception': exc}) + 'expression': template_node.expression, 'exception': exc}) if template_node.link_documents: suffix = find_lowest_available_suffix(index_instance, document) @@ -109,7 +107,7 @@ def cascade_eval(eval_dict, document, template_node, parent_index_instance=None) fs_create_document_link(index_instance, document, suffix) except Exception, exc: warnings.append(_(u'Error updating document index, expression: %(expression)s; %(exception)s') % { - 'expression': template_node.expression, 'exception': exc}) + 'expression': template_node.expression, 'exception': exc}) index_instance.documents.add(document) diff --git a/apps/document_indexing/tools.py b/apps/document_indexing/tools.py index 71837b0930..cc83183dae 100644 --- a/apps/document_indexing/tools.py +++ b/apps/document_indexing/tools.py @@ -10,7 +10,7 @@ from .api import update_indexes def do_rebuild_all_indexes(): for index in Index.objects.all(): fs_delete_directory_recusive(index) - + IndexInstanceNode.objects.all().delete() DocumentRenameCount.objects.all().delete() for document in Document.objects.all(): diff --git a/apps/document_indexing/widgets.py b/apps/document_indexing/widgets.py index e678feebb7..d1dea28b90 100644 --- a/apps/document_indexing/widgets.py +++ b/apps/document_indexing/widgets.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import -#from django.utils.safestring import mark_safe -from django.utils.html import conditional_escape, mark_safe +from django.utils.html import mark_safe from .models import IndexInstanceNode @@ -73,7 +72,7 @@ def get_breadcrumbs(index_instance, simple=False, single_link=False, include_cou else: output.insert(0, u' / '.join(result)) return mark_safe(u' '.join(output)) - + def node_level(x): """ diff --git a/apps/document_signatures/managers.py b/apps/document_signatures/managers.py index e812f1935f..1052d6d988 100644 --- a/apps/document_signatures/managers.py +++ b/apps/document_signatures/managers.py @@ -69,7 +69,7 @@ class DocumentVersionSignatureManager(models.Manager): document_descriptor.close() if detached_signature: detached_signature.close() - + def clear_detached_signature(self, document): document_signature = self.get_document_signature(document) if not document_signature.signature_file: @@ -77,4 +77,4 @@ class DocumentVersionSignatureManager(models.Manager): document_signature.delete_detached_signature_file() document_signature.signature_file = None - document_signature.save() + document_signature.save() diff --git a/apps/linking/forms.py b/apps/linking/forms.py index 53c9639f53..46e92454d6 100644 --- a/apps/linking/forms.py +++ b/apps/linking/forms.py @@ -1,12 +1,10 @@ from __future__ import absolute_import from django import forms -from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext from django.core.urlresolvers import reverse from django.utils.safestring import mark_safe from django.template.defaultfilters import capfirst -from django.conf import settings from documents.widgets import document_html_widget from tags.widgets import get_tags_inline_widget diff --git a/apps/linking/views.py b/apps/linking/views.py index 538badd895..e02fdabc29 100644 --- a/apps/linking/views.py +++ b/apps/linking/views.py @@ -127,7 +127,6 @@ def smart_link_list(request): except PermissionDenied: qs = AccessEntry.objects.filter_objects_by_access(PERMISSION_SMART_LINK_VIEW, request.user, qs) - return render_to_response('generic_list.html', { 'title': _(u'smart links'), 'object_list': qs, diff --git a/apps/mimetype/api.py b/apps/mimetype/api.py index 5790bce8a2..cc7e93db32 100644 --- a/apps/mimetype/api.py +++ b/apps/mimetype/api.py @@ -83,10 +83,11 @@ def get_error_icon_file_path(): else: return os.path.join(settings.STATIC_ROOT, MIMETYPE_ICONS_DIRECTORY_NAME, ERROR_FILE_NAME) + def get_error_icon_url(): return os.path.join(MIMETYPE_ICONS_DIRECTORY_NAME, ERROR_FILE_NAME) - + def get_mimetype(file_description, filepath, mimetype_only=False): """ Determine a file's mimetype by calling the system's libmagic diff --git a/apps/ocr/__init__.py b/apps/ocr/__init__.py index 3a3b0ac7c9..22b55a9ae5 100644 --- a/apps/ocr/__init__.py +++ b/apps/ocr/__init__.py @@ -95,6 +95,7 @@ def document_post_save(sender, instance, **kwargs): # logger.debug('got call_queue signal: %s' % kwargs) # task_process_document_queues() + @receiver(post_syncdb, dispatch_uid='create_default_queue', sender=ocr_models) def create_default_queue_signal_handler(sender, **kwargs): create_default_queue() diff --git a/apps/ocr/views.py b/apps/ocr/views.py index fbeafc7b3a..92772ad03e 100644 --- a/apps/ocr/views.py +++ b/apps/ocr/views.py @@ -1,7 +1,5 @@ from __future__ import absolute_import -import socket - from django.http import HttpResponseRedirect from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext diff --git a/apps/signaler/management/commands/collectstatic.py b/apps/signaler/management/commands/collectstatic.py index e33b4d0ec1..28b63cd9f2 100644 --- a/apps/signaler/management/commands/collectstatic.py +++ b/apps/signaler/management/commands/collectstatic.py @@ -10,4 +10,4 @@ class Command(collectstatic.Command): def handle_noargs(self, *args, **kwargs): pre_collectstatic.send(sender=self) - super(Command, self).handle_noargs(*args, **kwargs) + super(Command, self).handle_noargs(*args, **kwargs) diff --git a/apps/sources/management/commands/bulk_upload.py b/apps/sources/management/commands/bulk_upload.py index 47c0a179d1..8fa11f652e 100644 --- a/apps/sources/management/commands/bulk_upload.py +++ b/apps/sources/management/commands/bulk_upload.py @@ -1,10 +1,11 @@ from __future__ import absolute_import -import os, sys +import os +import sys from optparse import make_option from django.core.management.base import BaseCommand, CommandError, LabelCommand -from django.utils.simplejson import loads, dumps +from django.utils.simplejson import loads from metadata.api import convert_dict_to_dict_list from documents.models import DocumentType @@ -25,7 +26,7 @@ class Command(LabelCommand): make_option('--document_type', action='store', dest='document_type_name', help='The document type to apply to the uploaded documents.'), ) - + def handle_label(self, label, **options): if not os.access(label, os.R_OK): raise CommandError("File '%s' is not readable." % label) @@ -53,10 +54,10 @@ class Command(LabelCommand): print 'Using the metadata values:' for key, value in metadata_dict.items(): print '%s: %s' % (key, value) - + if document_type: print 'Uploaded document will be of type: %s' % options['document_type_name'] - + source = OutOfProcess() fd = open(label) try: @@ -71,7 +72,7 @@ class Command(LabelCommand): else: print 'Cancelled.' - + def _confirm(interactive): if not interactive: return 'yes' diff --git a/apps/sources/models.py b/apps/sources/models.py index ad20758bff..0941abd00d 100644 --- a/apps/sources/models.py +++ b/apps/sources/models.py @@ -9,7 +9,6 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic from django.core.exceptions import ValidationError from django.db import transaction -from django.db.utils import DatabaseError from converter.api import get_available_transformations_choices from converter.literals import DIMENSION_SEPARATOR @@ -91,7 +90,7 @@ class BaseModel(models.Model): @transaction.commit_on_success def upload_single_file(self, file_object, filename=None, use_file_name=False, document_type=None, metadata_dict_list=None, user=None, document=None, new_version_data=None): new_document = not document - + if not document: document = Document() if document_type: @@ -99,7 +98,7 @@ class BaseModel(models.Model): document.save() apply_default_acls(document, user) - + if user: document.add_as_recent_document_for_user(user) create_history(HISTORY_DOCUMENT_CREATED, document, {'user': user}) @@ -113,7 +112,7 @@ class BaseModel(models.Model): if not new_version_data: new_version_data = {} - + try: new_version = document.new_version(file=file_object, **new_version_data) except Exception: @@ -121,7 +120,7 @@ class BaseModel(models.Model): # document.delete() transaction.rollback() raise - + if filename: document.rename(filename) @@ -129,11 +128,11 @@ class BaseModel(models.Model): new_version.apply_default_transformations(transformations) #TODO: new HISTORY for version updates - + if metadata_dict_list and new_document: # Only do for new documents save_metadata_list(metadata_dict_list, document, create=True) - warnings = update_indexes(document) + warnings = update_indexes(document) class Meta: ordering = ('title',) @@ -288,6 +287,7 @@ class SourceTransformation(models.Model): class OutOfProcess(BaseModel): is_interactive = False + class Meta(BaseModel.Meta): verbose_name = _(u'out of process') verbose_name_plural = _(u'out of process') diff --git a/apps/sources/views.py b/apps/sources/views.py index 0c107d7365..775ddf2917 100644 --- a/apps/sources/views.py +++ b/apps/sources/views.py @@ -165,13 +165,13 @@ def upload_interactive(request, source_type=None, source_id=None, document_pk=No else: if result['is_compressed'] == None: messages.success(request, _(u'File uploaded successfully.')) - + if result['is_compressed'] == True: messages.success(request, _(u'File uncompressed successfully and uploaded as individual files.')) - + if result['is_compressed'] == False: messages.warning(request, _(u'File was not a compressed file, uploaded as it was.')) - + return HttpResponseRedirect(request.get_full_path()) except Exception, e: if settings.DEBUG: @@ -240,7 +240,7 @@ def upload_interactive(request, source_type=None, source_id=None, document_pk=No if result['is_compressed'] == True: messages.success(request, _(u'Staging file: %s, uncompressed successfully and uploaded as individual files.') % staging_file.filename) - + if result['is_compressed'] == False: messages.warning(request, _(u'Staging file: %s, was not compressed, uploaded as a single file.') % staging_file.filename) diff --git a/apps/user_management/management/commands/import_users.py b/apps/user_management/management/commands/import_users.py index b02d08d5b4..9ccfbe09dc 100644 --- a/apps/user_management/management/commands/import_users.py +++ b/apps/user_management/management/commands/import_users.py @@ -1,10 +1,11 @@ from __future__ import absolute_import -import csv, os, sys +import csv +import os +import sys from optparse import make_option from django.core.management.base import BaseCommand, CommandError, LabelCommand -from django.utils.simplejson import loads, dumps from django.contrib.auth.models import User from django.db.utils import IntegrityError @@ -17,11 +18,12 @@ def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): # decode UTF-8 back to Unicode, cell by cell: yield [unicode(cell, 'utf-8') for cell in row] + def utf_8_encoder(unicode_csv_data): for line in unicode_csv_data: yield line.encode('utf-8') - + class Command(LabelCommand): args = '' help = 'Import users from a CSV file with the field order: username, firstname, lastname, email.' @@ -34,7 +36,7 @@ class Command(LabelCommand): make_option('--skip-repeated', action='store_true', dest='skip_repeated', default=False, help='Don\'t exit if the user already exists.'), ) - + def handle_label(self, label, **options): if not os.access(label, os.R_OK): raise CommandError("File '%s' is not readable." % label) @@ -68,7 +70,7 @@ class Command(LabelCommand): sys.exit() except csv.Error, e: - sys.exit('file %s, line %d: %s' % (label, reader.line_num, e)) + sys.exit('file %s, line %d: %s' % (label, reader.line_num, e)) else: print 'Finish.' else: