Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
stephenmcd / metrics.py
Created January 23, 2014 05:20
Baseline system metrics daemon for statsd
import os
import time
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand
from django.db.models import Sum
import psutil
import redis
import statsd
@stephenmcd
stephenmcd / rtl_field.py
Created December 11, 2013 19:54
Calculated RTL field handling in Django Rest Framework.
from unicodedata import bidirectional
from django.template.defaultfilters import striptags
from rest_framework import serializers
class RTLField(serializers.BooleanField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("read_only", True)
super(RTLField, self).__init__(*args, **kwargs)
@stephenmcd
stephenmcd / linkedin_viewers_rss.py
Created December 7, 2013 21:28
Convert linkedin profile viewers to RSS.
import cgi
import getpass
import mechanize
import json
import SimpleHTTPServer
import SocketServer
import uuid
@stephenmcd
stephenmcd / deep_force_unicode.py
Created August 5, 2012 00:37
Recursive force_unicode
from django.utils.encoding import force_unicode
from django.utils.functional import Promise
def deep_force_unicode(value):
"""
Recursively call force_unicode on value.
"""
if isinstance(value (list, tuple, set)):
value = type(value)(map(deep_force_unicode, value))
elif isinstance(value, dict):
@stephenmcd
stephenmcd / cache_stats.py
Created August 3, 2012 02:40
Quick cache stats scraper using django-debug-toolbar's cache panel.
#!/usr/bin/env python
import os
import sys
import urllib
name, url, num = sys.argv[1:]
print name, url, num
first = not os.path.exists("output.csv")
f = open("output.csv", "a")
@stephenmcd
stephenmcd / trans_stats.py
Created June 18, 2012 11:33
A super-messy Django translation file parser.
import os
translators = []
languages = []
for root, dirs, files in os.walk("mezz_current/mezzanine"):
if root.endswith("locale"):
languages.extend(dirs)
for name in files:
if name == "django.po":
@stephenmcd
stephenmcd / requirements_report.py
Created June 18, 2012 00:45
Build a report of Python requirements for multiple repositories.
#!/usr/bin/env python
"""
Given the current directory contains multiple repositories, each with a
requirements/project.txt file, build a report of all requirements.
"""
import os
reqs = {}
@stephenmcd
stephenmcd / patch_extend_node.py
Created May 8, 2012 12:00
Allow circular Django template inheritance, so projects can both override and extend a reusable app's templates.
from django.template.loader_tags import ExtendsNode
def get_extends_parent(self, context):
"""
Patched onto Django's ``ExtendsNode.get_parent`` which is
responsible for loading the template to extend from with
the ``extends`` template tag.
This patch allows the template foo/bar.html to extend
foo/bar.html, given that there is another version of it that
@stephenmcd
stephenmcd / richtextfield_clean.py
Created April 26, 2012 22:19
Patch Mezzanine's RichTextField without upgrading
"""
XSS privilege escalation by malicious non-superuser admin users.
Fixed in Mezzanine 1.0.9:
https://bitbucket.org/stephenmcd/mezzanine/changeset/40cbc47b8d8a
If an admin user was to create their own POST submit to any forms with a
RichTextField, they could include JavaScript that does the following:
@stephenmcd
stephenmcd / slumber_django_testclient.py
Created February 8, 2012 04:58
Subclass of ``slumber.API`` that patches ``requests.request`` with Django test client compatible HTTP requests.
from collections import namedtuple
from django.test import TestCase
from requests.auth import HTTPBasicAuth
import slumber
class SlumberTestClientAPI(slumber.API):
"""
Subclass of ``slumber.API`` that patches ``requests.request``