Skip to content

Instantly share code, notes, and snippets.

View cnk's full-sized avatar

Cynthia Kiser cnk

View GitHub Profile
@register_snippet
class Author(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
site = models.ForeignKey('wagtailcore.Site', on_delete=models.CASCADE)
panels = [
MultiFieldPanel([
FieldPanel('first_name', classname='col6 first-name'),
FieldPanel('last_name', classname='col6 last-name'),
def site_authors(request):
return Author.objects.filter(site_id=request.site.id)
class Authorships(Orderable, models.Model):
author = models.ForeignKey('Author', related_name='author_paper_relationship', on_delete=models.CASCADE)
paper = ParentalKey('Page', related_name='paper_author_relationship', on_delete=models.CASCADE)
panels = [
SnippetChooserPanel('author', qs_restriction=site_authors, editable=False)
]
# These are the logging settings we used. I know we had to fiddle with log level for django.db.backends and qinspect.
# I think unless they were DEBUG, the logging just didn't happen;but this may not be accurate - esp as this was a Django 1.8 project.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
import os
import requests
import traceback
from io import BytesIO
from collections import OrderedDict
from django.core.files.images import ImageFile
from wagtail.images import get_image_model
from djunk.utils import get_or_generate
from core.logging import logger
#################################################################################################################
# Monkey patch the wagtaildocs.views.documents.edit view so that we keep the same file name if the user uploads
# a file with the same name. (Default appends 7 random alphanumeics to make filename unique)
# 2018-10-26 cmalek: should be fine with wagtail-2.3
# #################################################################################################################
@permission_checker.require('change')
def edit_without_changing_filename(request, document_id):
Document = get_document_model()
DocumentForm = get_document_form(Document)
# Shell plus session
In [3]: reverse('public:survey')
Out[3]: '/commuter_survey/'
In [4]: reverse('admin:survey-index')
---------------------------------------------------------------------------
NoReverseMatch Traceback (most recent call last)
<ipython-input-4-21bbf73985d4> in <module>
----> 1 reverse('admin:survey-index')
### 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'),
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:
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()

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: