Skip to content

Commit

Permalink
Let new publications be first to display
Browse files Browse the repository at this point in the history
Overwrite wagtailorderable.models.Orderable.save() to initialize new
objects with a lower sort order than existing objects, so that the new
objects appear at the top of the list.
  • Loading branch information
donald committed Aug 2, 2021
1 parent 5a9d4c6 commit 69c7945
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mpicms/publications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@

from modelcluster.models import ClusterableModel

class LatestFirstOrderable(Orderable):
def save(self, *args, **kwargs):
if self.pk is None:
self.sort_order = self.__class__.objects.aggregate(models.Min('sort_order'))['sort_order__min'] - 1
super(Orderable, self).save(*args, **kwargs)
class Meta:
abstract = True

@register_snippet
class Publication(index.Indexed, ClusterableModel, Orderable):
class Publication(index.Indexed, ClusterableModel, LatestFirstOrderable):
title = RichTextField(_('title'), features=['bold', 'italic', 'link'])
groups = RichTextField(_('groups'), features=['bold', 'italic', 'link'], blank=True)
authors = RichTextField(_('authors'), features=['bold', 'italic', 'link'], blank=True)
Expand Down

0 comments on commit 69c7945

Please sign in to comment.