diff --git a/chapeau/kepi/migrations/0002_acitem_serial.py b/chapeau/kepi/migrations/0002_acitem_serial.py new file mode 100644 index 0000000..3b8c728 --- /dev/null +++ b/chapeau/kepi/migrations/0002_acitem_serial.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.4 on 2019-11-15 15:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('kepi', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='acitem', + name='serial', + field=models.IntegerField(default=0), + ), + ] diff --git a/chapeau/kepi/models/item.py b/chapeau/kepi/models/item.py index bf70753..950d7ba 100644 --- a/chapeau/kepi/models/item.py +++ b/chapeau/kepi/models/item.py @@ -3,6 +3,7 @@ from . import acobject, audience from .. import PUBLIC_IDS from django.conf import settings import logging +import random logger = logging.getLogger(name='chapeau') @@ -20,6 +21,32 @@ class AcItem(acobject.AcObject): blank=True, ) + serial = models.IntegerField( + default = 0, + ) + + def _generate_id(self): + if not self.f_attributedTo: + logger.info(' -- new object has no f_attributedTo') + return super()._generate_id() + + max_so_far = AcItem.objects.filter( + f_attributedTo = self.f_attributedTo, + ).aggregate(models.Max('serial'))['serial__max'] + + if max_so_far is None: + max_so_far = 0 + + self.serial = max_so_far + random.randint(0, 256) + + logger.info(' -- max serial so far is %d; using serial %d', + max_so_far, self.serial) + + # TODO: For now, we return the same IDs as the superclass. + # When I'm sure the serial numbers work and the tests pass, + # we'll have IDs in the form "/username/serial". + return super()._generate_id() + @property def visibility(self):