Skip to content

Instantly share code, notes, and snippets.

@kaedroho
kaedroho / README.md
Last active July 6, 2022 12:03
Django Structured Fields

django-structured-fields

A package containing a set of structural field types for Django.

This package is inspired by Wagtail's StreamField, but with the following changes:

  • Allows any Django field type to be used
  • Provides a separate "enum" type
  • structs/enums/lists can be added directly to models
from contextlib import contextmanager
from django.core.cache import caches
from django.core.cache.backends.dummy import DummyCache
class AlwaysDummyCache:
def __getitem__(self, key):
return DummyCache(None)
import logging
import os
from bakery.views import BuildableMixin
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from wagtail.wagtailcore.models import Page, Site
from wagtail.api.v2.endpoints import PagesAPIEndpoint
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.pagination import WagtailPagination
@kaedroho
kaedroho / fill-filter.md
Last active December 8, 2017 11:10
Attempt at describing Wagtail's "fill" filter

The "fill" filter crops the image so that the final image dimensions exactly match the specified dimentions.

Note: If the image is very small, this filter may generate an image that is smaller than the specified dimentions but the aspect ratio will always be the same.

Examples:

  • fill-200x200 (generates 200x200 px thumbnail)
  • fill-200x200-c75 (generates 200x200 px thumbnail. Zoomed in to the focal point 75%)

It's most common use is generating thumbnails which must be an exact size. If the original image's aspect ratio doesn't

angle = sqrt((radius**2 - (radius - tolerance)**2)*4) / radius
use std::fmt::Debug;
trait Atom: Debug {
fn render(&self);
}
#[derive(Debug)]
struct TextAtom {
from wagtail.wagtailsearch.backends.elasticsearch import ElasticsearchSearchBackend, ElasticsearchSearchQuery
class ElasticSearchQueryWithFuzziness(ElasticsearchSearchQuery):
def get_inner_query(self):
if self.query_string is not None:
fields = self.fields or ['_all', '_partials']
if len(fields) == 1:
query = {
def randomly_order_queryset_deterministically(queryset, salt):
"""
Applies a pseudo-random ordering on the queryset. For each object, a number
is generated by performing a CRC32 function on the object's ID and a salt.
So if you use the same salt, you get the same ordering.
We need to implement it this way so the same random ordering is preserved for
the subsequent AJAX requests.
"""
return queryset.extra(
from django.utils import timezone
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch, ElasticSearchQuery
from wagtail.wagtailcore.models import Page
from wagtail.wagtailimages.models import AbstractImage
from wagtail.wagtaildocs.models import Document
class ElasticSearchQueryWithRecency(ElasticSearchQuery):
BUNDLES
tag_css
<link rel="stylesheet" href="{% static 'wagtailadmin/css/vendor/jquery.tagit.css' %}">
wagtail/wagtailadmin/templates/wagtailadmin/shared/tag_field_css.html
tag_js
<script src="{% static 'wagtailadmin/js/vendor/tag-it.js' %}"></script>