Created
September 28, 2020 20:41
-
-
Save cnk/b090bcdd021e681754d19a166cc77254 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class NewsPageForm(WagtailAdminPageForm): | |
""" | |
This form exists to make the Summary, Header, and Body fields optional if the external_url field is populated. | |
""" | |
def clean(self): | |
cleaned_data = super().clean() | |
# If there's an external_url, remove any errors that might have been added for the summary, header, and body. | |
if cleaned_data.get('external_url'): | |
summary_error = self._errors.pop('summary', None) | |
# Since the summary field is required at the database level, we need it to have at least a placeholder. | |
if summary_error: | |
cleaned_data['summary'] = 'placeholder' | |
header_error = self._errors.pop('header', None) | |
body_error = self._errors.pop('body', None) | |
# This is here to handle a weird use case. If the user already saved the page with blocks in the header | |
# and body fields, but then removed them on a subsequent save, this code is required to ensure that the | |
# form actually saves blank data to those fields, rather than just leaving their data intact. | |
if header_error: | |
cleaned_data['header'] = '' | |
if body_error: | |
cleaned_data['body'] = '' | |
# Don't let people remove the publication_date from a live article; Unfortunately the field 'live' defaults | |
# to True for a never saved article. But having a live_revision_id appears to be an accurate indicator. | |
# Note: this isn't really needed since publishing will fill in an empty publication_date. But removing a | |
# previous publication date rather than editing it might make the date jump in an unexpected way. | |
if self.instance.live_revision_id and not cleaned_data.get('publication_date'): | |
self.add_error( | |
'publication_date', | |
ValidationError('You may not remove the publication date from a live news article') | |
) | |
return cleaned_data | |
class NewsPage(Page): | |
# lots of fields.. | |
################### | |
# EDIT FORM CONFIG | |
################### | |
content_panels = [ | |
FieldPanel('title', classname='full title'), | |
FieldPanel('external_url'), | |
FieldPanel('writer'), | |
FieldPanel('contact'), | |
StreamFieldPanel('related_links'), | |
StreamFieldPanel('header'), | |
StreamFieldPanel('body') | |
] | |
publishing_panels = [ | |
PublishingPanel(), | |
FieldPanel('publication_date'), | |
FieldPanel('display_locations', widget=forms.CheckboxSelectMultiple), | |
] | |
edit_handler = TabbedInterface( | |
[ | |
ObjectList(content_panels, heading='Content'), | |
ObjectList(publishing_panels, heading='Publishing'), | |
], | |
base_form_class=NewsPageForm | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment