Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
@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
@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)