Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
@cnk
cnk / wagtail-document-importer.py
Created August 2, 2023 17:12
Import documents from file + yml data
import os
import requests
from io import BytesIO
from collections import OrderedDict
from django.core.files import File
from wagtail.models import Collection
from wagtail.documents import get_document_model
from djunk.utils import get_or_generate
from core.logging import logger
@cnk
cnk / course_changes_importer.py
Last active July 10, 2023 21:36
I want a ManyToMany relationship between courses and departments - where the order of those mappings matters. The courses.py models work just fine in the admin UI but we are struggling with creating a script that updates the mappings and still leaves the pages editable in the Wagtail admin.
class CourseChangesImporter(object):
def sync_with_cataloger(self, catalog_label, json_data, course_listing_page, dry_run=False):
courses = json_data.get('courses', [])
for course in courses:
proposal_type = course['proposal_type']
course_number, course_letters = self._split_course_number(course['course_number'])
if proposal_type == "CHANGE":
course_page = self._find_current_course_page(course_listing_page, course['copied_from'], dry_run)
course_page = course_page.get_latest_revision_as_object()
@cnk
cnk / courses.py
Created July 10, 2023 20:36
I want a ManyToMany relationship between courses and departments - where the order of those mappings matters.
class CoursePage(Page, ClusterableModel):
course_number = models.CharField(
verbose_name='Course Number',
max_length=256,
help_text="Only the numeric part",
)
course_letters = models.CharField(
verbose_name='Course Letters',
max_length=256,
blank=True,
@cnk
cnk / Notes.txt
Created June 8, 2023 19:19
Custom page copy that does NOT copy the original page's revisions
The tricky bit is then getting that view used for the "copy".
We do a lot of patching so it's hard for me to extract a good example of that
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,