From 3398e7349e2bafddaa491c01f4c16c6dae14cd00 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Mon, 5 Feb 2018 13:38:59 -0600 Subject: abstracted the next prev links into utils so now they work for every model that implements a get_%S_admin_link method and loads the JS --- app/books/admin.py | 2 ++ app/books/models.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) (limited to 'app/books') diff --git a/app/books/admin.py b/app/books/admin.py index 1896067..29a885f 100644 --- a/app/books/admin.py +++ b/app/books/admin.py @@ -7,6 +7,8 @@ class BookAdmin(admin.ModelAdmin): search_fields = ['title', 'body_markdown'] list_filter = ('rating', 'read_date') + class Media: + js = ('next-prev-links.js',) class BookHighlightAdmin(admin.ModelAdmin): list_display = ('book', 'page', 'date_added') diff --git a/app/books/models.py b/app/books/models.py index f888480..ebd3a2a 100644 --- a/app/books/models.py +++ b/app/books/models.py @@ -56,6 +56,19 @@ class Book(models.Model): def ratings_range(cls): return range(1, 6) + @property + def get_previous_admin_url(self): + n = self.get_previous_by_pub_date() + return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) + + @property + def get_next_admin_url(self): + model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name) + try: + return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_pub_date().pk] ) + except model.DoesNotExist: + return '' + def admin_thumbnail(self): return force_text('' % (self.get_image_url())) admin_thumbnail.allow_tags = True -- cgit v1.2.3-70-g09d2