Advertisement
Guest User

Untitled

a guest
Jan 8th, 2020
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. from django.contrib import admin
  2. from .models import AddressBookAttribute
  3. from django.utils.html import format_html
  4. from django.conf.urls import url
  5. from django.urls import reverse
  6. from django.utils.translation import gettext_lazy as _
  7.  
  8.  
  9. @admin.register(AddressBookAttribute)
  10. class AddressBookAttributeAdmin(admin.ModelAdmin):
  11.     readonly_fields = ('attribute_actions',)
  12.     list_display = ('attribute', 'visible', 'available_for_group', 'sorting_index', 'attribute_actions')
  13.     fieldsets = (
  14.         (None, {'fields': ('attribute', 'available_for_group', 'sorting_index', 'visible')}),
  15.     )
  16.  
  17.     def get_urls(self):
  18.         urls = super().get_urls()
  19.         custom_urls = [
  20.             url(
  21.                 r'^(?P<attrinbute_id>.+)/up/$',
  22.                 self.admin_site.admin_view(self.sorted_index_up),
  23.                 name='sorted-index-up',
  24.             ),
  25.             url(
  26.                 r'^(?P<attrinbute_id>.+)/down/$',
  27.                 self.admin_site.admin_view(self.sorted_index_down),
  28.                 name='sorted-index-down',
  29.             ),
  30.         ]
  31.         return custom_urls + urls
  32.  
  33.     def attribute_actions(self, obj):
  34.         return format_html(
  35.             '<a class="button" href="{}">UP</a>&nbsp;'
  36.             '<a class="button" href="{}">DOWN</a>',
  37.             reverse('admin:sorted-index-up', args=[obj.pk]),
  38.             reverse('admin:sorted-index-down', args=[obj.pk]),
  39.         )
  40.  
  41.     attribute_actions.short_description = _('Attribute Actions')
  42.     attribute_actions.allow_tags = True
  43.  
  44.     def sorted_index_up(self, *args, **kwargs):
  45.         print('up index')
  46.         return 'up'
  47.  
  48.     def sorted_index_down(self, *args, **kwargs):
  49.         print('down index')
  50.         return 'text'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement