Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@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
@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 / cookies.js
Last active April 4, 2020 15:54
cookie functions
/*
cookies.get() gets all cookies by name
cookies.get('name') gets single cookie
cookies.set('name', 'value', {max_age, expires, path, domain})
cookies.del('name', {path, domain})
https://javascript-minifier.com/ = 731 bytes
window.cookies=function(){function e(e){return e.replace(/[;,\s]+/g,e=>escape(e))}var n=unescape,c=document,i="; ",o="",u="path",s="domain",p="max",t="age",r="expires",a="secure",h="samesite",f="=",g=p+"_"+t;function m(n,m,d){if(n){var l=[e(n),f,e(m||o)];(d=d||{})[u]&&l.push(i,u,f,d[u]),d[s]&&l.push(i,s,f,d[s]),d[g]?l.push(i,p,"-",t,f,d[g]):d[r]&&l.push(i,r,f,d[r].toUTCString()),d[a]&&l.push(i,a),d[h]&&l.push(i,h,f,d[h]),c.cookie=l.join(o)}}return{get:function(i){return i?new RegExp("\\b"+e(i).replace(/\W/g,"\\$&")+"=([^;]*)").exec(c.cookie)[1]:c.cookie.split("; ").map(e=>/([^=]+)=?(.*)/.exec(e).slice(1,3)).map(e=>[n(e[0]),n(e[1])]).reduce((e,n)=>(e[n[0]]=n[1],e),{})},set:m,del:function(e,n){(n=n||{})[g]=-1,m(e,o,n)}}}();
*/
@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 / jsonxml.py
Last active June 8, 2017 23:53
Convert between XML and JSON
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 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)