Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
@cnk
cnk / logs.sh
Created October 22, 2020 16:25
frontend |
frontend | > wagtail@1.0.0 start /code/wagtail
frontend | > npm run watch
frontend |
frontend |
frontend | > wagtail@1.0.0 watch /code/wagtail
frontend | > npm-run-all --parallel gulp:dev:watch webpack:dev:watch
frontend |
web | Watching for file changes with StatReloader
web | Performing system checks...
@cnk
cnk / news.py
Created September 28, 2020 20:41
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'):
#################################################################################################################
# Patch the wagtail.admin.forms.pages.PageViewRestrictionForm class to filter groups to only those available
# to the current site.
#################################################################################################################
def page_view_restriction_init(self, *args, **kwargs):
super(PageViewRestrictionForm, self).__init__(*args, **kwargs)
self.fields['groups'].widget = forms.CheckboxSelectMultiple()
# BEGIN PATCH
current_site = Site.find_for_request(get_current_request())
#################################################################################################################
# Patch the wagtaildocs.views.documents.edit view so that we keep the same file name if the user uploads a replacement
# with the same filename. The default appends 7 random characters to make the filename unique, which changes the URL.
# 2020-07-10 rrollins: Works with Wagtail 2.9.
# 2020-09-03 cnk: should work with Wagtail 2.11a
# #################################################################################################################
@permission_checker.require('change')
def edit_without_changing_filename(request, document_id):
Document = get_document_model()
DocumentForm = get_document_form(Document)
@cnk
cnk / blocks.py
Created July 22, 2020 14:56
Wagtail link block requiring exactly one of 3 options
class AirspaceRequiredLinkBlock(blocks.StructBlock):
"""
Allows a user to create a link to a Page, a Document, or a relative or absolute URL.
This version requires that you provide one and only one of the destinations above.
NOTE: Due to limitations in CSS, callers of LinkBlock() must not specify a label in the construction arguments.
See the comment in the Meta class for why.
NOTE: Within a template, checking for the existence of `self.link` will always return True because the LinkBlock
object is not falsy, even if it has no contents. To retrieve the value of a LinkBlock, use the {% link_url %}

Restricting some fields in a model

We would like to be able to add images to events - or tag them with display locations such as "Home Page". But we do not want oridinary Event Planners to be able to do either of those things. Wagtail's admin interface is defined at the class level, so omitting those fields for just a subset of users would be hard to do in the panel definition. Instead we hide those fields during panel instantiation.

First, in the EventPage model, we create a MultiFieldPanel with a specific CSS class name:

Restricting some fields in a model

We would like to be able to add images to events - or tag them with display locations such as "Home Page". But we do not want oridinary Event Planners to be able to do either of those things. Wagtail's admin interface is defined at the class level, so omitting those fields for just a subset of users would be hard to do in the panel definition. Instead we hide those fields during panel instantiation.

First, in the EventPage model, we create a MultiFieldPanel with a specific CSS class name:

template2 = PersonPage2Template(
title='Faculty Template',
)
index.add_child(instance=template2)
template2.header = StreamValue(
template2.header.stream_block,
[{"type": "AirspaceSimplePageHeaderBlock", "value": {"replacement_title": ""}}],
is_lazy=True)
template2.save()
from wagtail.core import blocks, hooks
def get_block_tuple(block_inst):
"""
Returns the canned block tuple for use in StreamField and StreamBlock definitions (but NOT in StructBlocks!).
We use this everywhere so that our code consistently generates the same tuple at every organiziational level.
"""
try:
return block_inst.get_block_tuple()
except AttributeError:
### NewsHubPage is the parent for all NewsPages
### get_news_for_site is a query for all pages belonging to a specific site
### It returns a queryset of NewsPage objects
class NewsTeasersListingBlock(blocks.StructBlock):
"""
Displays a row or grid of teasers for news pages - optionally filtered by tag
"""
STYLES = (
('horizontal', 'Horizontal Layout'),