Skip to content
Snippets Groups Projects
Commit e0d1feee authored by Saurabh Kumar's avatar Saurabh Kumar Committed by Fredrik Jonsson
Browse files

fix(admin): attached forms displayed in the rounds admin page

Fixed by re-implement ReadOnlyInlinePanel. Simplified the implementation of ReadOnlyInlinePanel to follow the single
responsibility principle. It now just renders all the related items in a 
list. 

The responsibility of rendering items can be overridden using the panels 
passed to this ReadOnlyInlinePanel.
parent 453bd8d6
No related branches found
No related tags found
No related merge requests found
......@@ -38,18 +38,21 @@ class DisplayField(Field):
class ReadOnlyPanel(Panel):
def __init__(self, attr, **kwargs):
def __init__(self, attr: str, **kwargs):
self.attr = attr
super().__init__(**kwargs)
self.heading = pretty_name(self.attr) if not self.heading else self.heading
def clone(self):
return self.__class__(
attr=self.attr,
heading=self.heading,
classname=self.classname,
help_text=self.help_text,
)
def clone_kwargs(self):
"""
Return a dictionary of keyword arguments that can be used to create a clone of this panel definition.
"""
return {
"attr": self.attr,
"heading": self.heading,
"classname": self.classname,
"help_text": self.help_text,
}
class BoundPanel(Panel.BoundPanel):
field_template_name = 'wagtailadmin/shared/field.html'
......@@ -63,7 +66,7 @@ class ReadOnlyPanel(Panel):
def context(self):
try:
value = getattr(self.instance, self.attr)
value = getattr(self.instance, self.panel.attr)
except AttributeError:
self.attr = '__'.join(
[self.instance._meta.model_name, str(self.instance.id)]
......@@ -76,10 +79,10 @@ class ReadOnlyPanel(Panel):
# Add initial value only when an object is present. Display nothing when a new page is being
# created. As it is a read-only panel and creates confusion when default values are displayed.
if self.instance.id:
self.form.initial[self.attr] = value
self.form.initial[self.panel.attr] = value
else:
self.form.initial[self.attr] = '-'
self.bound_field = DisplayField().get_bound_field(self.form, self.attr)
self.form.initial[self.panel.attr] = '-'
self.bound_field = DisplayField().get_bound_field(self.form, self.panel.attr)
return {
'self': self,
'field': self.bound_field,
......@@ -87,26 +90,6 @@ class ReadOnlyPanel(Panel):
}
class ReadOnlyInlinePanel(ReadOnlyPanel):
template_name = 'wagtailadmin/panels/multi_field_panel.html'
def get_child_edit_handler(self):
child_edit_handler = ReadOnlyPanel(self.attr)
model = getattr(self.instance, self.attr)
return child_edit_handler.bind_to(model=model)
class BoundPanel(ReadOnlyPanel.BoundPanel):
def on_instance_bound(self):
values = getattr(self.instance, self.attr).all()
child_panel = self.get_child_edit_handler()
self.children = [
child_panel.bind_to(
instance=value, form=self.form, request=self.request
)
for value in values
]
class FilteredFieldPanel(FieldPanel):
def __init__(self, *args, filter_query=dict(), **kwargs):
self.filter_query = filter_query
......
......@@ -36,8 +36,10 @@ from wagtail.fields import RichTextField
from wagtail.models import Page, PageManager
from wagtail.query import PageQuerySet
from hypha.core.wagtail.admin.panels import ReadOnlyInlinePanel
from ..admin_forms import RoundBasePageAdminForm, WorkflowFormAdminForm
from ..edit_handlers import ReadOnlyInlinePanel, ReadOnlyPanel
from ..edit_handlers import ReadOnlyPanel
from ..workflow import OPEN_CALL_PHASES
from .submissions import ApplicationSubmission
from .utils import (
......@@ -177,24 +179,36 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore
]),
], heading=_('Dates')),
FieldPanel('reviewers', widget=forms.SelectMultiple(attrs={'size': '16'})),
ReadOnlyPanel('get_workflow_name_display', heading=_('Workflow'), help_text=_('Copied from the fund.')),
ReadOnlyPanel(
'get_workflow_name_display',
heading=_('Workflow'),
help_text=_('Copied from the fund.'),
),
# Forms comes from parental key in models/forms.py
ReadOnlyInlinePanel(
'forms',
panels=[ReadOnlyPanel("name")],
heading=_('Application forms'),
help_text=_('Copied from the fund.'),
heading=_('Application forms')
),
ReadOnlyInlinePanel(
'review_forms',
panels=[ReadOnlyPanel("name")],
heading=_('Internal Review Form'),
help_text=_('Copied from the fund.'),
heading=_('Internal Review Form')
),
ReadOnlyInlinePanel(
'external_review_forms',
panels=[ReadOnlyPanel("name")],
help_text=_('Copied from the fund.'),
heading=_('External Review Form'),
),
ReadOnlyInlinePanel(
'determination_forms',
panels=[ReadOnlyPanel("name")],
help_text=_('Copied from the fund.'),
heading=_('External Review Form')
heading=_('Determination Form'),
),
ReadOnlyInlinePanel('determination_forms', help_text=_('Copied from the fund.')),
]
edit_handler = TabbedInterface([
......
{% load i18n l10n wagtailadmin_tags %}
{{ self.formset.management_form }}
<ul class="multiple" id="id_{{ self.formset.prefix }}-FORMS">
{% if self.formset.non_form_errors %}
<li class="error-message">
{% for error in self.formset.non_form_errors %}
<span>{{ error|escape }}</span>
{% endfor %}
</li>
{% endif %}
{% for child in self.children %}
<li data-inline-panel-child id="inline_child_{{ child.form.prefix }}" data-contentpath-disabled>
{{ child.render_form_content }}
</li>
{% empty %}
<li>
---
</li>
{% endfor %}
</ul>
from wagtail.admin.panels import InlinePanel
class ReadOnlyInlinePanel(InlinePanel):
"""
Behaves same as InlinePanel, but removes UI for adding new item and
deleting an existing item in the formset.
"""
class BoundPanel(InlinePanel.BoundPanel):
template_name = "core/wagtail/panels/inline_panel_readonly.html"
......@@ -49,6 +49,7 @@ INSTALLED_APPS = [
'hypha.cookieconsent',
'hypha.images',
'hypha.core',
'hypha.apply.activity',
'hypha.apply.categories',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment