Skip to content

Instantly share code, notes, and snippets.

mgd020 mgd020

Block or report user

Report or block mgd020

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mgd020
mgd020 / renderTemplate.js
Last active Jun 30, 2016
javascript dom html template rendering
View renderTemplate.js
/*
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 Jul 7, 2016
allow url encoding inside django template
View urlencode_tags.py
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 Jul 26, 2016
movable django app template
View __init__.py
default_app_config = __name__ + '.apps.MyAppConfig'
@mgd020
mgd020 / formset_data_factory.py
Last active Aug 11, 2016
create formset data with dicts, and modelformset data with models
View formset_data_factory.py
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 Sep 8, 2016
email regex pattern (python format)
View email regex
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 Sep 16, 2016
Django template tag that allows inline code.
View code.py
"""
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 Sep 27, 2016
a little python enum gist
View enum.py
class Enum(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@mgd020
mgd020 / multilocationfield.py
Created Sep 28, 2016
A haystack search field for specifying multiple locations for a single document.
View multilocationfield.py
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 Oct 25, 2016
Fix wagtails built-in FillOperation to center on the focal point
View crop.py
"""
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 Oct 25, 2016
Registers render_streamfield tag to add current context to streamfield block templates.
View render_streamfield.py
'''
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
You can’t perform that action at this time.