0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Remove AdminChooser widget class

This commit is contained in:
Matt Westcott 2023-01-30 17:30:44 +00:00 committed by Matt Westcott
parent 981d02ff1a
commit 1e4417ae0c

View File

@ -1,5 +1,4 @@
import json
import warnings
from django import forms
from django.core.exceptions import ImproperlyConfigured
@ -15,78 +14,9 @@ from wagtail.admin.staticfiles import versioned_static
from wagtail.coreutils import resolve_model_string
from wagtail.models import Page
from wagtail.telepath import register
from wagtail.utils.deprecation import RemovedInWagtail50Warning
from wagtail.utils.widgets import WidgetWithScript
from wagtail.widget_adapters import WidgetAdapter
class AdminChooser(WidgetWithScript, widgets.Input):
choose_one_text = _("Choose an item")
choose_another_text = _("Choose another item")
clear_choice_text = _("Clear choice")
link_to_chosen_text = _("Edit this item")
show_edit_link = True
show_clear_link = True
# when looping over form fields, this one should appear in visible_fields, not hidden_fields
# despite the underlying input being type="hidden"
input_type = "hidden"
is_hidden = False
def __init__(self, **kwargs):
warnings.warn(
"wagtail.admin.widgets.chooser.AdminChooser is deprecated. "
"Custom chooser subclasses should inherit from wagtail.admin.widgets.chooser.BaseChooser instead",
category=RemovedInWagtail50Warning,
)
# allow choose_one_text / choose_another_text to be overridden per-instance
if "choose_one_text" in kwargs:
self.choose_one_text = kwargs.pop("choose_one_text")
if "choose_another_text" in kwargs:
self.choose_another_text = kwargs.pop("choose_another_text")
if "clear_choice_text" in kwargs:
self.clear_choice_text = kwargs.pop("clear_choice_text")
if "link_to_chosen_text" in kwargs:
self.link_to_chosen_text = kwargs.pop("link_to_chosen_text")
if "show_edit_link" in kwargs:
self.show_edit_link = kwargs.pop("show_edit_link")
if "show_clear_link" in kwargs:
self.show_clear_link = kwargs.pop("show_clear_link")
super().__init__(**kwargs)
def get_instance(self, model_class, value):
# helper method for cleanly turning 'value' into an instance object.
# DEPRECATED - subclasses should override WidgetWithScript.get_value_data instead
if value is None:
return None
try:
return model_class.objects.get(pk=value)
except model_class.DoesNotExist:
return None
def get_instance_and_id(self, model_class, value):
# DEPRECATED - subclasses should override WidgetWithScript.get_value_data instead
if value is None:
return (None, None)
elif isinstance(value, model_class):
return (value, value.pk)
else:
try:
return (model_class.objects.get(pk=value), value)
except model_class.DoesNotExist:
return (None, None)
def value_from_datadict(self, data, files, name):
# treat the empty string as None
result = super().value_from_datadict(data, files, name)
if result == "":
return None
else:
return result
class BaseChooser(widgets.Input):
choose_one_text = _("Choose an item")
choose_another_text = _("Choose another item")