Initial commit of the converter image transformation refactor

This commit is contained in:
Roberto Rosario
2011-07-15 06:16:14 -04:00
parent 358216fac5
commit 743ae0fce0
17 changed files with 303 additions and 114 deletions

View File

@@ -6,3 +6,6 @@ class SourceTransformationManager(models.Manager):
def get_for_object(self, obj):
ct = ContentType.objects.get_for_model(obj)
return self.model.objects.filter(content_type=ct).filter(object_id=obj.pk)
def get_for_object_as_list(self, obj):
return list([{'transformation': transformation['transformation'], 'arguments': eval(transformation['arguments'])} for transformation in self.get_for_object(obj).values('transformation', 'arguments')])

View File

@@ -6,7 +6,8 @@ from django.contrib.contenttypes import generic
from documents.models import DocumentType
from documents.managers import RecentDocumentManager
from metadata.models import MetadataType
from converter.api import backend
from converter.api import get_available_transformations_choices
from converter.literals import DIMENSION_SEPARATOR
from sources.managers import SourceTransformationManager
@@ -118,7 +119,7 @@ class StagingFolder(InteractiveBaseModel):
if self.preview_height:
dimensions.append(unicode(self.preview_height))
return u'x'.join(dimensions)
return DIMENSION_SEPARATOR.join(dimensions)
class Meta(InteractiveBaseModel.Meta):
verbose_name = _(u'staging folder')
@@ -162,8 +163,8 @@ class SourceTransformation(models.Model):
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
order = models.PositiveIntegerField(default=0, blank=True, null=True, verbose_name=_(u'order'), db_index=True)
transformation = models.CharField(choices=backend.get_available_transformations_labels(), max_length=128, verbose_name=_(u'transformation'))
arguments = models.TextField(blank=True, null=True, verbose_name=_(u'arguments'), help_text=_(u'Use dictionaries to indentify arguments, example: {\'degrees\':90}'))
transformation = models.CharField(choices=get_available_transformations_choices(), max_length=128, verbose_name=_(u'transformation'))
arguments = models.TextField(blank=True, null=True, verbose_name=_(u'arguments'), help_text=_(u'Use dictionaries to indentify arguments, example: %s') % u'{\'degrees\':90}')
objects = SourceTransformationManager()

View File

@@ -106,16 +106,15 @@ class StagingFile(object):
def upload(self):
"""
Return a StagingFile encapsulated in a File class instance to
allow for easier upload a staging files
allow for easier upload of staging files
"""
try:
return File(file(self.filepath, 'rb'), name=self.filename)
except Exception, exc:
raise Exception(ugettext(u'Unable to upload staging file: %s') % exc)
def delete(self, preview_size):
# tranformation_string, errors = get_transformation_string(DEFAULT_TRANSFORMATIONS)
cache_cleanup(self.filepath, size=preview_size)# , extra_options=tranformation_string)
def delete(self, preview_size, transformations):
cache_cleanup(self.filepath, size=preview_size, transformations=transformations)
try:
os.unlink(self.filepath)
except OSError, exc:
@@ -124,24 +123,7 @@ class StagingFile(object):
else:
raise OSError(ugettext(u'Unable to delete staging file: %s') % exc)
def preview(self, preview_size):
def preview(self, preview_size, transformations):
errors = []
# tranformation_string, errors = get_transformation_string(DEFAULT_TRANSFORMATIONS)
# output_file = convert(self.filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string, cleanup_files=False)
output_file = convert(self.filepath, size=preview_size, cleanup_files=False)
output_file = convert(self.filepath, size=preview_size, cleanup_files=False, transformations=transformations)
return output_file, errors
def get_transformation_string(transformations):
transformation_list = []
errors = []
#for transformation in transformations:
# try:
# if transformation['name'] in TRANFORMATION_CHOICES:
# output = TRANFORMATION_CHOICES[transformation['name']] % eval(transformation['arguments'])
# transformation_list.append(output)
# except Exception, e:
# errors.append(e)
#tranformation_string = ' '.join(transformation_list)
return tranformation_string, errors

View File

@@ -285,7 +285,10 @@ def staging_file_preview(request, source_type, source_id, staging_file_id):
staging_folder = get_object_or_404(StagingFolder, pk=source_id)
StagingFile = create_staging_file_class(request, staging_folder.folder_path)
try:
output_file, errors = StagingFile.get(staging_file_id).preview(staging_folder.get_preview_size())
output_file, errors = StagingFile.get(staging_file_id).preview(
preview_size=staging_folder.get_preview_size(),
transformations=SourceTransformation.objects.get_for_object_as_list(staging_folder)
)
if errors and (request.user.is_staff or request.user.is_superuser):
for error in errors:
messages.warning(request, _(u'Staging file transformation error: %(error)s') % {
@@ -318,7 +321,10 @@ def staging_file_delete(request, source_type, source_id, staging_file_id):
if request.method == 'POST':
try:
staging_file.delete(staging_folder.get_preview_size())
staging_file.delete(
preview_size=staging_folder.get_preview_size(),
transformations=SourceTransformation.objects.get_for_object_as_list(staging_folder)
)
messages.success(request, _(u'Staging file delete successfully.'))
except Exception, e:
messages.error(request, e)
@@ -509,6 +515,8 @@ def setup_source_transformation_edit(request, transformation_id):
form = SourceTransformationForm(instance=source_transformation, data=request.POST)
if form.is_valid():
try:
# Test the validity of the argument field
eval(form.cleaned_data['arguments'])
form.save()
messages.success(request, _(u'Source transformation edited successfully'))
return HttpResponseRedirect(next)
@@ -598,6 +606,8 @@ def setup_source_transformation_create(request, source_type, source_id):
form = SourceTransformationForm_create(request.POST)
if form.is_valid():
try:
# Test the validity of the argument field
eval(form.cleaned_data['arguments'])
source_tranformation = form.save(commit=False)
source_tranformation.content_object = source
source_tranformation.save()