forked from wagtail/wagtail-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from torchbox-forks/support/wagtail-60
Wagtail 6.0
- Loading branch information
Showing
7 changed files
with
170 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
wagtailautocomplete/static/wagtailautocomplete/autocomplete-widget-controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AutoCompleteWidgetController extends window.StimulusModule.Controller { | ||
connect() { | ||
initAutoCompleteWidget(this.element.id); | ||
} | ||
} | ||
|
||
window.wagtail.app.register('autocomplete-widget', AutoCompleteWidgetController); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,127 @@ | ||
import json | ||
|
||
from django import forms | ||
from wagtail import VERSION as WAGTAIL_VERSION | ||
from wagtail.admin.staticfiles import versioned_static | ||
from wagtail.utils.widgets import WidgetWithScript | ||
|
||
from .views import render_page | ||
|
||
if WAGTAIL_VERSION >= (6, 0): # type: ignore | ||
|
||
class Autocomplete(WidgetWithScript): | ||
template_name = 'wagtailautocomplete/autocomplete.html' | ||
|
||
def __init__(self, target_model, can_create=False, is_single=True, attrs=None): | ||
super().__init__(attrs) | ||
|
||
self.target_model = target_model | ||
self.can_create = can_create | ||
self.is_single = is_single | ||
|
||
def get_context(self, name, value, attrs): | ||
context = super().get_context(name, value, attrs) | ||
context['widget']['target_model'] = self.target_model._meta.label | ||
context['widget']['can_create'] = self.can_create | ||
context['widget']['is_single'] = self.is_single | ||
return context | ||
|
||
def format_value(self, value): | ||
if not value: | ||
return 'null' | ||
if isinstance(value, list): | ||
return json.dumps([ | ||
render_page(page) | ||
for page in self.target_model.objects.filter(pk__in=value) | ||
]) | ||
else: | ||
return json.dumps(render_page(self.target_model.objects.get(pk=value))) | ||
|
||
def value_from_datadict(self, data, files, name): | ||
# treat empty value as None to prevent deserialization error | ||
original_value = super().value_from_datadict(data, files, name) | ||
if not original_value: | ||
from django.forms.widgets import Widget | ||
|
||
|
||
class Autocomplete(Widget): # type: ignore | ||
template_name = 'wagtailautocomplete/autocomplete.html' | ||
|
||
def __init__(self, target_model, can_create=False, is_single=True, attrs=None): | ||
super().__init__(attrs) | ||
|
||
self.target_model = target_model | ||
self.can_create = can_create | ||
self.is_single = is_single | ||
|
||
def get_context(self, name, value, attrs): | ||
context = super().get_context(name, value, attrs) | ||
context['widget']['target_model'] = self.target_model._meta.label | ||
context['widget']['can_create'] = self.can_create | ||
context['widget']['is_single'] = self.is_single | ||
return context | ||
|
||
def format_value(self, value): | ||
if not value: | ||
return 'null' | ||
if isinstance(value, list): | ||
return json.dumps([ | ||
render_page(page) | ||
for page in self.target_model.objects.filter(pk__in=value) | ||
]) | ||
else: | ||
return json.dumps(render_page(self.target_model.objects.get(pk=value))) | ||
|
||
def value_from_datadict(self, data, files, name): | ||
# treat empty value as None to prevent deserialization error | ||
original_value = super().value_from_datadict(data, files, name) | ||
if not original_value: | ||
return None | ||
|
||
value = json.loads(original_value) | ||
|
||
if isinstance(value, list): | ||
return [obj['pk'] for obj in value if 'pk' in obj] | ||
if isinstance(value, dict): | ||
return value.get('pk', None) | ||
return None | ||
|
||
def build_attrs(self, *args, **kwargs): | ||
attrs = super().build_attrs(*args, **kwargs) | ||
attrs["data-controller"] = "autocomplete-widget" | ||
return attrs | ||
|
||
@property | ||
def media(self): | ||
return forms.Media( | ||
css={ | ||
'all': [versioned_static('wagtailautocomplete/dist.css')], | ||
}, | ||
js=[versioned_static('wagtailautocomplete/dist.js'), versioned_static('wagtailautocomplete/autocomplete-widget-controller.js')], | ||
) | ||
else: | ||
from wagtail.utils.widgets import WidgetWithScript | ||
|
||
|
||
class Autocomplete(WidgetWithScript): | ||
template_name = 'wagtailautocomplete/autocomplete.html' | ||
|
||
def __init__(self, target_model, can_create=False, is_single=True, attrs=None): | ||
super().__init__(attrs) | ||
|
||
self.target_model = target_model | ||
self.can_create = can_create | ||
self.is_single = is_single | ||
|
||
def get_context(self, name, value, attrs): | ||
context = super().get_context(name, value, attrs) | ||
context['widget']['target_model'] = self.target_model._meta.label | ||
context['widget']['can_create'] = self.can_create | ||
context['widget']['is_single'] = self.is_single | ||
return context | ||
|
||
def format_value(self, value): | ||
if not value: | ||
return 'null' | ||
if isinstance(value, list): | ||
return json.dumps([ | ||
render_page(page) | ||
for page in self.target_model.objects.filter(pk__in=value) | ||
]) | ||
else: | ||
return json.dumps(render_page(self.target_model.objects.get(pk=value))) | ||
|
||
def value_from_datadict(self, data, files, name): | ||
# treat empty value as None to prevent deserialization error | ||
original_value = super().value_from_datadict(data, files, name) | ||
if not original_value: | ||
return None | ||
|
||
value = json.loads(original_value) | ||
|
||
if isinstance(value, list): | ||
return [obj['pk'] for obj in value if 'pk' in obj] | ||
if isinstance(value, dict): | ||
return value.get('pk', None) | ||
return None | ||
|
||
value = json.loads(original_value) | ||
|
||
if isinstance(value, list): | ||
return [obj['pk'] for obj in value if 'pk' in obj] | ||
if isinstance(value, dict): | ||
return value.get('pk', None) | ||
return None | ||
|
||
def render_js_init(self, id_, name, value): | ||
return "initAutoCompleteWidget({id});".format( | ||
id=json.dumps(id_), | ||
) | ||
|
||
@property | ||
def media(self): | ||
return forms.Media( | ||
css={ | ||
'all': [versioned_static('wagtailautocomplete/dist.css')], | ||
}, | ||
js=[versioned_static('wagtailautocomplete/dist.js')], | ||
) | ||
def render_js_init(self, id_, name, value): | ||
return "initAutoCompleteWidget({id});".format( | ||
id=json.dumps(id_), | ||
) | ||
|
||
@property | ||
def media(self): | ||
return forms.Media( | ||
css={ | ||
'all': [versioned_static('wagtailautocomplete/dist.css')], | ||
}, | ||
js=[versioned_static('wagtailautocomplete/dist.js')], | ||
) |