Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
## within our "create_site" method, we make an admin and editor group for each site.
## This method has already created a collecton named for the site
admins = Group.objects.create(name=f'{site.hostname} Admins')
apply_default_permissions(admins, site, 'admin')
admins.save()
editors = Group.objects.create(name=f'{site.hostname} Editors')
apply_default_permissions(editors, site, 'editor')
editors.save()
@cnk
cnk / monkey_patchess.py
Created May 4, 2022 15:37
Site-specific search for wagtail 2.16
#################################################################################################################
# Patch the wagtail.admin.views.pages.search.search method to filter by the current site.
# I asked Torchbox to add a hook that would let us delete this patch: https://github.com/wagtail/wagtail/issues/6235
# 2020-09-03 cnk: updated to work with 2.11a
#################################################################################################################
@vary_on_headers('X-Requested-With')
@user_passes_test(user_has_any_page_permission)
def single_site_search(request):
# BEGIN PATCH
pages = all_pages = Page.objects.in_site(Site.find_for_request(request)).prefetch_related('content_type').specific()
@cnk
cnk / models.py
Last active September 15, 2021 16:19
Example of drag and drop sorting of a category hierarchy
from django import forms
from django.conf import settings
from django.urls import re_path
from django.contrib.admin.utils import quote, unquote
from django.core.exceptions import PermissionDenied
from django.core.validators import MinLengthValidator
from django.db import models
from django.http import JsonResponse, HttpResponseNotAllowed
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
@cnk
cnk / Gemfile
Created January 6, 2021 23:03
Running H4LA Site natively
source 'https://rubygems.org'
gem 'github-pages', group: :jekyll_plugins
@cnk
cnk / logs.sh
Created October 22, 2020 16:25
frontend |
frontend | > [email protected] start /code/wagtail
frontend | > npm run watch
frontend |
frontend |
frontend | > [email protected] 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: