-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andy Babic
committed
May 4, 2023
1 parent
b93a7f4
commit 62719e5
Showing
1 changed file
with
17 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,32 @@ | ||
import logging | ||
|
||
from generic_chooser.widgets import AdminChooser | ||
from wagtail.admin.widgets.chooser import BaseChooser | ||
|
||
from .api import records_client | ||
from .models import Record | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RecordChooser(AdminChooser): | ||
class RecordChooser(BaseChooser): | ||
choose_one_text = "Choose a record" | ||
choose_another_text = "Choose another record" | ||
link_to_chosen_text = "Edit this record" | ||
choose_modal_url_name = "record_chooser:choose" | ||
chooser_modal_url_name = "record_chooser:choose" | ||
show_edit_link = False | ||
|
||
def get_value_data(self, value): | ||
# Given a data value (which may be None or an value such as a pk to pass to get_instance), | ||
# extract the necessary data for rendering the widget with that value. | ||
# In the case of StreamField (in Wagtail >=2.13), this data will be serialised via | ||
# telepath https://wagtail.github.io/telepath/ to be rendered client-side, which means it | ||
# cannot include model instances. Instead, we return the raw values used in rendering - | ||
# namely: value, title and edit_item_url | ||
|
||
instance = None | ||
|
||
if isinstance(value, Record): | ||
instance = value | ||
value = value.iaid | ||
elif value: | ||
try: | ||
instance = self.get_instance(value) | ||
except Exception: | ||
logger.exception(f"Error fetching Record '{value}'. Using dummy value.") | ||
return { | ||
"value": value, | ||
"title": "Record currently unavailable", | ||
"edit_item_url": None, | ||
} | ||
|
||
if instance is None: | ||
return { | ||
"value": None, | ||
"title": "", | ||
"edit_item_url": None, | ||
} | ||
def get_instance(self, pk): | ||
"""Fetch related instance on edit form.""" | ||
try: | ||
return records_client.fetch(iaid=pk) | ||
except Exception: | ||
logger.exception(f"Error fetching Record '{pk}'.") | ||
return None | ||
|
||
def get_value_data_from_instance(self, instance): | ||
return { | ||
"value": value, | ||
"title": self.get_title(instance), | ||
"edit_item_url": self.get_edit_item_url(instance), | ||
"id": instance.pk, | ||
self.display_title_key: instance.summary_title, | ||
"description": instance.listing_description, | ||
"reference_number": instance.reference_number, | ||
"edit_item_url": None, | ||
} | ||
|
||
def get_instance(self, pk): | ||
"""Fetch related instance on edit form.""" | ||
return records_client.fetch(iaid=pk) |