From b5ede7c2d3e3580ff37a62a256fe9de94e82feff Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 11 Jul 2015 03:45:38 -0400 Subject: [PATCH] Add auto increment order for transformations. --- mayan/apps/converter/forms.py | 11 --------- .../migrations/0006_auto_20150708_0120.py | 2 +- .../migrations/0008_auto_20150711_0723.py | 24 +++++++++++++++++++ mayan/apps/converter/models.py | 9 ++++++- 4 files changed, 33 insertions(+), 13 deletions(-) delete mode 100644 mayan/apps/converter/forms.py create mode 100644 mayan/apps/converter/migrations/0008_auto_20150711_0723.py diff --git a/mayan/apps/converter/forms.py b/mayan/apps/converter/forms.py deleted file mode 100644 index 6c897cf27a..0000000000 --- a/mayan/apps/converter/forms.py +++ /dev/null @@ -1,11 +0,0 @@ -from __future__ import unicode_literals - -from django import forms - -from .models import Transformation - - -class TransformationForm(forms.ModelForm): - class Meta: - fields = ('order', 'name', 'arguments') - model = Transformation diff --git a/mayan/apps/converter/migrations/0006_auto_20150708_0120.py b/mayan/apps/converter/migrations/0006_auto_20150708_0120.py index ca240ed57f..d77d536010 100644 --- a/mayan/apps/converter/migrations/0006_auto_20150708_0120.py +++ b/mayan/apps/converter/migrations/0006_auto_20150708_0120.py @@ -15,7 +15,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='transformation', name='arguments', - field=models.TextField(help_text='Enter the arguments for the transformation as a YAML dictionary. ie: {"degrees": 180}', blank=True, verbose_name='Arguments', validators=converter.models.validators), + field=models.TextField(help_text='Enter the arguments for the transformation as a YAML dictionary. ie: {"degrees": 180}', blank=True, verbose_name='Arguments', validators=getattr(converter.models, 'validators', [])), preserve_default=True, ), ] diff --git a/mayan/apps/converter/migrations/0008_auto_20150711_0723.py b/mayan/apps/converter/migrations/0008_auto_20150711_0723.py new file mode 100644 index 0000000000..ccda029b85 --- /dev/null +++ b/mayan/apps/converter/migrations/0008_auto_20150711_0723.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('converter', '0007_auto_20150711_0656'), + ] + + operations = [ + migrations.AlterField( + model_name='transformation', + name='order', + field=models.PositiveIntegerField(default=0, help_text='Order in which the transformations will be executed.', db_index=True, verbose_name='Order', blank=True), + preserve_default=True, + ), + migrations.AlterUniqueTogether( + name='transformation', + unique_together=set([('content_type', 'object_id', 'order')]), + ), + ] diff --git a/mayan/apps/converter/models.py b/mayan/apps/converter/models.py index 4056a0f756..df52ad6747 100644 --- a/mayan/apps/converter/models.py +++ b/mayan/apps/converter/models.py @@ -5,6 +5,7 @@ import logging from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from django.db import models +from django.db.models import Max from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -26,7 +27,7 @@ class Transformation(models.Model): object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') - order = models.PositiveIntegerField(blank=True, db_index=True, default=0, help_text=_('Order in which the transformations will be executed.'), null=True, verbose_name=_('Order')) + order = models.PositiveIntegerField(blank=True, db_index=True, default=0, help_text=_('Order in which the transformations will be executed. If left unchanged, an automatic order value will be assigned.'), verbose_name=_('Order')) name = models.CharField(choices=BaseTransformation.get_transformation_choices(), max_length=128, verbose_name=_('Name')) arguments = models.TextField(blank=True, help_text=_('Enter the arguments for the transformation as a YAML dictionary. ie: {"degrees": 180}'), validators=[YAMLValidator()], verbose_name=_('Arguments')) @@ -35,7 +36,13 @@ class Transformation(models.Model): def __str__(self): return self.get_name_display() + def save(self, *args, **kwargs): + if not self.order: + self.order = Transformation.objects.filter(content_type=self.content_type, object_id=self.object_id).aggregate(Max('order'))['order__max'] + 1 + super(Transformation, self).save(*args, **kwargs) + class Meta: ordering = ('order',) + unique_together = ('content_type', 'object_id', 'order') verbose_name = _('Transformation') verbose_name_plural = _('Transformations')