Skip to content

Commit

Permalink
Fix jrief#257 Add django-hashid-field integration
Browse files Browse the repository at this point in the history
  • Loading branch information
trottomv committed Dec 21, 2020
1 parent a2a8dc8 commit fb60c09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion adminsortable2/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from django.http import (
HttpResponse, HttpResponseBadRequest,
HttpResponseNotAllowed, HttpResponseForbidden)
from . import encoders

__all__ = ['SortableAdminMixin', 'SortableInlineAdminMixin']

Expand Down Expand Up @@ -89,6 +90,11 @@ class SortableAdminMixin(SortableAdminBase):
BACK, FORWARD, FIRST, LAST, EXACT = range(5)
action_form = MovePageActionForm

if encoders.HAS_HASHID:
encoder = encoders.HashidJSONEncoder
else:
encoder = DjangoJSONEncoder

@property
def change_list_template(self):
opts = self.model._meta
Expand Down Expand Up @@ -244,7 +250,7 @@ def update_order(self, request):
startorder = int(request.POST.get('startorder'))
endorder = int(request.POST.get('endorder', 0))
moved_items = list(self._move_item(request, startorder, endorder))
return HttpResponse(json.dumps(moved_items, cls=DjangoJSONEncoder), content_type='application/json;charset=UTF-8')
return HttpResponse(json.dumps(moved_items, cls=self.encoder), content_type='application/json;charset=UTF-8')

def save_model(self, request, obj, form, change):
if not change:
Expand Down
14 changes: 14 additions & 0 deletions adminsortable2/encoders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.core.serializers.json import DjangoJSONEncoder

try:
from hashid_field import Hashid
HAS_HASHID = True
except ModuleNotFoundError:
HAS_HASHID = False
pass

class HashidJSONEncoder(DjangoJSONEncoder):
def default(self, o):
if isinstance(o, Hashid):
return str(o)
return super().default(o)

0 comments on commit fb60c09

Please sign in to comment.