From 37f74a3f26d880f7fec11399fad8c7b5d4fb12ed Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 8 Feb 2011 19:46:46 -0400 Subject: [PATCH] Moved the converter to its own app --- apps/converter/__init__.py | 5 ++++ .../convert.py => converter/api.py} | 11 +++++---- apps/converter/models.py | 3 +++ apps/converter/tests.py | 23 +++++++++++++++++++ apps/converter/views.py | 1 + apps/documents/views.py | 5 ++-- settings.py | 1 + 7 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 apps/converter/__init__.py rename apps/{documents/convert.py => converter/api.py} (75%) create mode 100644 apps/converter/models.py create mode 100644 apps/converter/tests.py create mode 100644 apps/converter/views.py diff --git a/apps/converter/__init__.py b/apps/converter/__init__.py new file mode 100644 index 0000000000..e5c4e1ea01 --- /dev/null +++ b/apps/converter/__init__.py @@ -0,0 +1,5 @@ +import tempfile + +from documents.conf import settings as documents_settings + +TEMPORARY_DIRECTORY = documents_settings.TEMPORARY_DIRECTORY if documents_settings.TEMPORARY_DIRECTORY else tempfile.mkdtemp() diff --git a/apps/documents/convert.py b/apps/converter/api.py similarity index 75% rename from apps/documents/convert.py rename to apps/converter/api.py index 58a1c8872d..df3c5a790e 100644 --- a/apps/documents/convert.py +++ b/apps/converter/api.py @@ -4,13 +4,14 @@ import subprocess import tempfile #from django.core.files.base import File -from documents.conf.settings import TEMPORARY_DIRECTORY +#from documents.conf.settings import TEMPORARY_DIRECTORY +from converter import TEMPORARY_DIRECTORY def in_cache(input_filepath, size, page=0, format='jpg'): - temp_directory = TEMPORARY_DIRECTORY if TEMPORARY_DIRECTORY else tempfile.mkdtemp() + #temp_directory = TEMPORARY_DIRECTORY if TEMPORARY_DIRECTORY else tempfile.mkdtemp() temp_filename, separator = os.path.splitext(os.path.basename(input_filepath)) - temp_path = os.path.join(temp_directory, temp_filename) + temp_path = os.path.join(TEMPORARY_DIRECTORY, temp_filename) output_arg = '%s_%s%s%s' % (temp_path, size, os.extsep, format) input_arg = '%s[%s]' % (input_filepath, page) if os.path.exists(output_arg): @@ -20,13 +21,13 @@ def in_cache(input_filepath, size, page=0, format='jpg'): def convert(input_filepath, size, cache=True, page=0, format='jpg'): - temp_directory = TEMPORARY_DIRECTORY if TEMPORARY_DIRECTORY else tempfile.mkdtemp() + #temp_directory = TEMPORARY_DIRECTORY if TEMPORARY_DIRECTORY else tempfile.mkdtemp() #TODO: generate output file using lightweight hash function on #file name or file content #descriptor, temp_filepath = tempfile.mkstemp() temp_filename, separator = os.path.splitext(os.path.basename(input_filepath)) - temp_path = os.path.join(temp_directory, temp_filename) + temp_path = os.path.join(TEMPORARY_DIRECTORY, temp_filename) output_arg = '%s_%s%s%s' % (temp_path, size, os.extsep, format) input_arg = '%s[%s]' % (input_filepath, page) if os.path.exists(output_arg): diff --git a/apps/converter/models.py b/apps/converter/models.py new file mode 100644 index 0000000000..71a8362390 --- /dev/null +++ b/apps/converter/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/apps/converter/tests.py b/apps/converter/tests.py new file mode 100644 index 0000000000..2247054b35 --- /dev/null +++ b/apps/converter/tests.py @@ -0,0 +1,23 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/apps/converter/views.py b/apps/converter/views.py new file mode 100644 index 0000000000..60f00ef0ef --- /dev/null +++ b/apps/converter/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/apps/documents/views.py b/apps/documents/views.py index 3969e6b762..d336871de8 100644 --- a/apps/documents/views.py +++ b/apps/documents/views.py @@ -12,9 +12,10 @@ from django.forms.formsets import formset_factory from django.core.files.base import File from filetransfers.api import serve_file +from converter.api import convert, in_cache +from common.utils import pretty_size -from convert import convert, in_cache -from utils import from_descriptor_to_tempfile, pretty_size +from utils import from_descriptor_to_tempfile from models import Document, DocumentMetadata, DocumentType, MetadataType from forms import DocumentTypeSelectForm, DocumentCreateWizard, \ diff --git a/settings.py b/settings.py index d5511d3d0f..7d9d2c5cb9 100644 --- a/settings.py +++ b/settings.py @@ -124,6 +124,7 @@ INSTALLED_APPS = ( 'pagination', 'dynamic_search', 'filetransfers', + 'converter', ) TEMPLATE_CONTEXT_PROCESSORS = (