mise-en-production
[auf_framonde.git] / project / cmsplugin_contact_plus / inline_ordering / models.py
1 from django.db import models
2
3 class Orderable(models.Model):
4
5 """Add extra field and default ordering column for and inline orderable model"""
6
7 inline_ordering_position = models.IntegerField(blank = True,
8 null = True,
9 editable = True)
10
11 class Meta:
12 abstract = True
13 ordering = ('inline_ordering_position',)
14
15 def save(self, force_insert=False, force_update=False, using=None):
16 """Calculate position (max+1) for new records"""
17 if not self.inline_ordering_position:
18 max = self.__class__.objects.filter().aggregate(models.Max('inline_ordering_position'))
19 try:
20 self.inline_ordering_position = max['inline_ordering_position__max'] + 1
21 except TypeError:
22 self.inline_ordering_position = 1
23 return super(Orderable, self).save(force_insert=force_insert, force_update=force_update, using=using)