Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
from wagtail.admin.panels import FieldPanel, InlinePanel, ObjectList, TabbedInterface
from wagtail.models import Page, Orderable, RevisionMixin
from wagtail.snippets.models import register_snippet
from modelcluster.fields import ParentalKey
## imports truncated so may not be 100% complete
class CourseDepartment(Orderable):
course = ParentalKey('core.CoursePage', related_name='coursedepartments', on_delete=models.CASCADE)
department = models.ForeignKey('core.Department', related_name='coursedepartments', on_delete=models.CASCADE)
@cnk
cnk / README.txt
Last active December 16, 2022 17:14
Changes to make reports section of Wagtail admin multitenanted
Background info for this snippet.
Each site in our system is completely independent of the others. Even if you have admin permissions on more than one site, when logged in to site A, you do not see any content for site B. Each sie has 2 groups - an admin group and an editor group.
One of the main things we had to patch is the filters on the reports page. We do not want users on one site to even know there are other users in the system. This is implemented in the site_specific_get_users_for_filter method.
Although our non-page models all have site_ids, it was not possible to filter ModelLogEntries in site, so we settled for hiding that report from everyone except superusers.
@cnk
cnk / models.py
Created November 28, 2022 17:27
Controlling caching by overriding serve
from django.template.response import TemplateResponse
from wagtail.core.models import Page, PageViewRestriction
DEFAULT_PAGE_CACHE_TIME = 60 * 5 # 5 minutes
class BasePage(Page):
"""
This model contains methods we want added to all (or nearly all) of our custom Page types. Right
now that is our Cache-Control headers. The values for these are a work in progress and for now
@cnk
cnk / document_importer.py
Created November 16, 2022 21:25
Import documents from a directory into Wagtail CMS
# core./jobs/document_importer.py
import hashlib
import os
from django.core.files import File
from wagtail.documents import get_document_model
from wagtail.models import Collection
from catalog.logging import logger
@cnk
cnk / fields.py
Created October 14, 2022 21:34
Import code for changing references in rich text from their html versions into draftail references
import json
import re
import uuid
from django.db import connection
from core.logging import logger
from .utils import (
get_document_by_import_id,
get_image_by_import_id,
load_page_by_import_id,
from django.db import models
class RobotsTxtMixin(models.Model):
"""
Always mix this class in BEFORE wagtailcore.Page. Otherwise, its override of get_sitemap_urls() won't get called.
"""
hide_from_search_engines = models.BooleanField(
default=False,
## 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