Add transformation copy support and update sources models to use it
This commit is contained in:
@@ -12,6 +12,36 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TransformationManager(models.Manager):
|
class TransformationManager(models.Manager):
|
||||||
|
def copy(self, source, targets):
|
||||||
|
"""
|
||||||
|
Copy transformation from source to all targets
|
||||||
|
"""
|
||||||
|
|
||||||
|
content_type = ContentType.objects.get_for_model(source)
|
||||||
|
|
||||||
|
# Get transformations
|
||||||
|
transformations = self.filter(content_type=content_type, object_id=source.pk).values('name', 'arguments', 'order')
|
||||||
|
logger.debug('source transformations: %s', transformations)
|
||||||
|
|
||||||
|
# Get all targets from target QS
|
||||||
|
targets_dict = map(lambda entry:{'content_type': entry[0], 'object_id': entry[1]}, zip(ContentType.objects.get_for_models(*targets).values(), targets.values_list('pk')))
|
||||||
|
logger.debug('targets: %s', targets_dict)
|
||||||
|
|
||||||
|
# Combine the two
|
||||||
|
results = []
|
||||||
|
for instance in targets_dict:
|
||||||
|
for transformation in transformations:
|
||||||
|
result = instance.copy()
|
||||||
|
result.update(transformation)
|
||||||
|
results.append(dict(result))
|
||||||
|
|
||||||
|
logger.debug('results: %s', results)
|
||||||
|
|
||||||
|
# Bulk create for a single DB query
|
||||||
|
self.bulk_create(
|
||||||
|
map(lambda entry: self.model(**entry), results),
|
||||||
|
)
|
||||||
|
|
||||||
def get_for_model(self, obj, as_classes=False):
|
def get_for_model(self, obj, as_classes=False):
|
||||||
"""
|
"""
|
||||||
as_classes == True returns the transformation classes from .classes
|
as_classes == True returns the transformation classes from .classes
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
from model_utils.managers import InheritanceManager
|
from model_utils.managers import InheritanceManager
|
||||||
|
|
||||||
from converter.literals import DIMENSION_SEPARATOR
|
from converter.literals import DIMENSION_SEPARATOR
|
||||||
|
from converter.models import Transformation
|
||||||
from djcelery.models import PeriodicTask, IntervalSchedule
|
from djcelery.models import PeriodicTask, IntervalSchedule
|
||||||
from documents.models import Document, DocumentType
|
from documents.models import Document, DocumentType
|
||||||
from metadata.api import save_metadata_list
|
from metadata.api import save_metadata_list
|
||||||
@@ -50,9 +51,6 @@ class Source(models.Model):
|
|||||||
def fullname(self):
|
def fullname(self):
|
||||||
return ' '.join([self.class_fullname(), '"%s"' % self.title])
|
return ' '.join([self.class_fullname(), '"%s"' % self.title])
|
||||||
|
|
||||||
def get_transformation_list(self):
|
|
||||||
return SourceTransformation.objects.get_for_object_as_list(self)
|
|
||||||
|
|
||||||
def upload_document(self, file_object, label, description=None, document_type=None, expand=False, language=None, metadata_dict_list=None, user=None):
|
def upload_document(self, file_object, label, description=None, document_type=None, expand=False, language=None, metadata_dict_list=None, user=None):
|
||||||
new_versions = Document.objects.new_document(
|
new_versions = Document.objects.new_document(
|
||||||
description=description,
|
description=description,
|
||||||
@@ -64,18 +62,19 @@ class Source(models.Model):
|
|||||||
user=user
|
user=user
|
||||||
)
|
)
|
||||||
|
|
||||||
transformations, errors = self.get_transformation_list()
|
|
||||||
for new_version in new_versions:
|
for new_version in new_versions:
|
||||||
new_version.apply_default_transformations(transformations)
|
Transformation.objects.copy(source=Source.objects.get_subclass(pk=self.pk), targets=new_version.pages.all())
|
||||||
|
|
||||||
if metadata_dict_list:
|
if metadata_dict_list:
|
||||||
save_metadata_list(metadata_dict_list, new_version.document, create=True)
|
save_metadata_list(metadata_dict_list, new_version.document, create=True)
|
||||||
|
|
||||||
def get_upload_file_object(self, form_data):
|
def get_upload_file_object(self, form_data):
|
||||||
pass
|
pass
|
||||||
|
# TODO: Should raise NotImplementedError()?
|
||||||
|
|
||||||
def clean_up_upload_file(self, upload_file_object):
|
def clean_up_upload_file(self, upload_file_object):
|
||||||
pass
|
pass
|
||||||
|
# TODO: Should raise NotImplementedError()?
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ('title',)
|
ordering = ('title',)
|
||||||
|
|||||||
Reference in New Issue
Block a user