Skip to content

Commit

Permalink
[fix] Fixed re-ordering templates on device add page #434
Browse files Browse the repository at this point in the history
Closes #434
  • Loading branch information
pandafy committed Feb 6, 2024
1 parent 226442c commit cd31571
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions openwisp_controller/config/sortedm2m/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
create_sorted_many_related_manager as base_create_sorted_many_related_manager,
)

from .forms import SortedMultipleChoiceField


def create_sorted_many_related_manager(superclass, rel, *args, **kwargs):
BaseSortedRelatedManager = base_create_sorted_many_related_manager(
Expand Down Expand Up @@ -64,3 +66,12 @@ class SortedManyToManyField(BaseSortedManyToManyField):
def contribute_to_class(self, cls, name, **kwargs):
super().contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, SortedManyToManyDescriptor(self))

def formfield(self, **kwargs):
# TODO: Remove this when stacked inline admin is fixed in django-sortedm2m
# https://github.com/jazzband/django-sortedm2m/pull/213
defaults = {}
if self.sorted:
defaults['form_class'] = SortedMultipleChoiceField
defaults.update(kwargs)
return super().formfield(**defaults)
24 changes: 24 additions & 0 deletions openwisp_controller/config/sortedm2m/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sortedm2m.forms import (
SortedCheckboxSelectMultiple as BaseSortedCheckboxSelectMultiple,
)
from sortedm2m.forms import SortedMultipleChoiceField as BaseSortedMultipleChoiceField


class SortedCheckboxSelectMultiple(BaseSortedCheckboxSelectMultiple):
class Media(BaseSortedCheckboxSelectMultiple.Media):
# The django-sortedm2m library has a bug when the widget is
# use in StackedInline Admin, see
# https://github.com/jazzband/django-sortedm2m/pull/213
# The workaround in sortedm2m/patch_sortedm2m.js fixes the bug.
# TODO: Remove this workaround when a new version of django-sortedm2m
# is released.
js = (
'admin/js/jquery.init.js',
'sortedm2m/widget.js',
'sortedm2m/jquery-ui.js',
'sortedm2m/patch_sortedm2m.js',
)


class SortedMultipleChoiceField(BaseSortedMultipleChoiceField):
widget = SortedCheckboxSelectMultiple
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
(function ($) {
$(document).ready( function () {
$('.sortedm2m-items').sortable('destroy');
});
}(django.jQuery));

0 comments on commit cd31571

Please sign in to comment.