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 / 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
@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 / cookie.js
Last active Nov 16, 2018
cookie functions
View cookie.js
@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 / jsonxml.py
Last active Jun 8, 2017
Convert between XML and JSON
View jsonxml.py
from __future__ import absolute_import, division, print_function, unicode_literals
from xml import sax
from xml.sax import saxutils
class XMLParser(sax.handler.ContentHandler):
def __init__(self, cdata_key):
self.cdata_key = cdata_key
@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)
You can’t perform that action at this time.