Add auto increment order for transformations.

This commit is contained in:
Roberto Rosario
2015-07-11 03:45:38 -04:00
parent 72bbc3f4e9
commit b5ede7c2d3
4 changed files with 33 additions and 13 deletions

View File

@@ -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

View File

@@ -15,7 +15,7 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='transformation', model_name='transformation',
name='arguments', 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, preserve_default=True,
), ),
] ]

View File

@@ -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')]),
),
]

View File

@@ -5,6 +5,7 @@ import logging
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
from django.db.models import Max
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@@ -26,7 +27,7 @@ class Transformation(models.Model):
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id') 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')) 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')) 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): def __str__(self):
return self.get_name_display() 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: class Meta:
ordering = ('order',) ordering = ('order',)
unique_together = ('content_type', 'object_id', 'order')
verbose_name = _('Transformation') verbose_name = _('Transformation')
verbose_name_plural = _('Transformations') verbose_name_plural = _('Transformations')