Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@mgd020
mgd020 / renderTemplate.js
Last active June 30, 2016 00:37
javascript dom html template rendering
/*
renderTemplate(template_id, context) -> string
Renders a template declared in the DOM with a given context.
Template tags:
<% ... %>: code
<%= ... %>: added to output
@mgd020
mgd020 / urlencode_tags.py
Created July 7, 2016 01:39
allow url encoding inside django template
from django import template
from django.conf import settings
from django.utils.http import urlencode
register = template.Library()
@register.simple_tag(name='urlencode')
def urlencode_(*args, **kwargs):
@mgd020
mgd020 / __init__.py
Created July 26, 2016 06:14
movable django app template
default_app_config = __name__ + '.apps.MyAppConfig'
@mgd020
mgd020 / formset_data_factory.py
Last active August 11, 2016 01:09
create formset data with dicts, and modelformset data with models
from django.forms import formsets, model_to_dict
from django.http.request import QueryDict
def formset_data_factory(formset_class, dicts, extra_fields=None):
data = QueryDict(mutable=True)
form = formset_class.form
prefix = (form.prefix or formset_class.get_default_prefix()) + '-'
fields = form.declared_fields.viewkeys() | form.base_fields.viewkeys()
@mgd020
mgd020 / email regex
Created September 8, 2016 02:31
email regex pattern (python format)
https://regex101.com/r/nV4rB0/1
^(([A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+)*)|\"([A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~\.\(\)\,\:\;\<\>\@\[\] ]|\\\\|\\\")+\")(\.(([A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+)*)|\"([A-Za-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~\.\(\)\,\:\;\<\>\@\[\] ]|\\\\|\\\")+\"))*@(([A-Za-z0-9]+(\-[A-Za-z0-9]+)*)(\.([A-Za-z0-9]+(\-[A-Za-z0-9]+)*))*|\[((\d{1,3}(\.\d{1,3}){3})|IPv6(\:[a-zA-F0-9]{0,4}){1,7})\])$
@mgd020
mgd020 / code.py
Last active September 16, 2016 00:03
Django template tag that allows inline code.
"""
Add {% code %}{% endcode %} tags to template.
Note: it will only run if it is in a block that is rendered.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import re
import sys
@mgd020
mgd020 / enum.py
Created September 27, 2016 06:34
a little python enum gist
class Enum(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@mgd020
mgd020 / multilocationfield.py
Created September 28, 2016 00:23
A haystack search field for specifying multiple locations for a single document.
from __future__ import absolute_import, division, print_function, unicode_literals
import six
from django.contrib.gis.geos import Point
from haystack import indexes
from haystack.exceptions import SpatialError
from haystack.utils.geo import ensure_geometry, ensure_point
@mgd020
mgd020 / crop.py
Created October 25, 2016 04:17
Fix wagtails built-in FillOperation to center on the focal point
"""
Install via:
from __future__ import absolute_import, division, print_function, unicode_literals
from wagtail.wagtailcore import hooks
from wagtail.wagtailimages.models import Filter
from .crop import CropOperation
@mgd020
mgd020 / render_streamfield.py
Last active October 25, 2016 05:34
Registers render_streamfield tag to add current context to streamfield block templates.
'''
Registers render_streamfield tag to add current page context to all block templates.
Usage: {% render_streamfield a_streamfield %}
Author: github.com/mgd020
'''
from django import template
from wagtail.wagtailcore.blocks.base import Block