Skip to content

Instantly share code, notes, and snippets.

View dpnova's full-sized avatar
🥸

David Novakovic dpnova

🥸
View GitHub Profile
#!/usr/bin/env python
# coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# Copyright (c) 2008 Drew Smathers
# See LICENSE for details.
"""
Memcache Multi-Client Proxy and Node Locator components.
"""
from twisted.internet import defer
from twisted.internet.protocol import ReconnectingClientFactory, ClientFactory
from twisted.python import failure
@dpnova
dpnova / hash.py
Created September 24, 2010 06:53
# Copyright (c) 2008 Drew Smathers
# See LICENSE for details.
_zlib = True
try:
import zlib
except ImportError:
_zlib = False
_hashlib = True
@dpnova
dpnova / mail.py
Created April 7, 2011 10:15
Email Mixin for Tornado - I can't remember who originally wrote this :(
#!/usr/bin/env python
import asyncore
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE
from email.utils import formatdate
import logging
import simplejson as json
import lxml
class objectJSONEncoder(json.JSONEncoder):
"""A specialized JSON encoder that can handle simple lxml objectify types
>>> from lxml import objectify
>>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")
>>> objectJSONEncoder().encode(obj)
'{"price": 1.5, "author": "W. Shakespeare"}'
"""
@dpnova
dpnova / gist:1223933
Created September 17, 2011 13:26
invalidate cache_page entries in django
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None, method="GET"):
"""
This function allows you to invalidate any view-level cache.
view_name: view function you wish to invalidate or it's named url pattern
args: any arguments passed to the view function
namepace: optioal, if an application namespace is needed
key prefix: for the @cache_page decorator for the function (if any)
from: http://stackoverflow.com/questions/2268417/expire-a-view-cache-in-django
added: method to request to get the key generating properly
@dpnova
dpnova / hack.sh
Created June 3, 2012 09:16 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@dpnova
dpnova / gist:5225027
Last active February 9, 2016 14:54
Adding reversion to the Base PageAdmin in Mezzanine.
from django.contrib import admin
import reversion
from mezzanine.pages.admin import PageAdmin
from mezzanine.pages.models import Page
class NewPageAdmin(PageAdmin, reversion.VersionAdmin):
pass
admin.site.unregister(Page)
admin.site.register(Page, NewPageAdmin)
(function(foo, $, undefined) {
foo.csrf= {
csrftoken: $.cookie('csrftoken'),
csrfSafeMethod: function(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
},
sameOrigin: function(url) {
// test that a given url is a same-origin URL
// url could be relative or scheme relative or absolute
from django.http import HttpResponse
EMPTY_GIF_BYTES = 'GIF89a\x01\x00\x01\x00\xf0\x00\x00\xb0\x8cZ\x00\x00\x00!\xf9\x04\x00\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
def record_page_view(request):
response = HttpResponse(EMPTY_GIF_BYTES) #need 1pixel gif here
response['Content-Type'] = 'image/gif'
return response