Skip to content

Instantly share code, notes, and snippets.

View tarkatronic's full-sized avatar

Joey Wilhelm tarkatronic

View GitHub Profile
@tarkatronic
tarkatronic / analytics.html
Created December 9, 2009 16:59
A mashup of the analytics demo and linechart plugin for gRaphael
<html>
<head>
<script type="text/javascript" src="raphael-1.2.5.js"></script>
<script type="text/javascript" src="raphael.path.methods.js"></script>
<script type="text/javascript" src="raphael.primitives.js"></script>
<script type="text/javascript" src="g.raphael-0.4.js"></script>
<script type="text/javascript" src="g.analytics.js"></script>
<script type="text/javascript">
$(function () {
$("#data").css({
@tarkatronic
tarkatronic / slicehost_zone_batch_update.py
Created August 25, 2010 21:17
Shift A records to a new IP for any number of domains at a time using Slicehost's API. Requires http://code.google.com/p/pyactiveresource/
from pyactiveresource.activeresource import ActiveResource
from pyactiveresource.connection import UnauthorizedAccess
#### CONFIGURATION ####
from_ip = 'xxx.xxx.xxx.xxx'
to_ip = 'xxx.xxx.xxx.xxx'
update_domains = ('domain1.com.','domain2.net.','otherdomain.org.')
api_key = 'xxxxx_YOUR_API_KEY_HERE_xxxxx'
api_url = 'https://%s@api.slicehost.com/' % api_key
@tarkatronic
tarkatronic / transfer_slicehost.py
Created July 21, 2011 22:23
Transferring DNS records from Slicehost to 6sync via API calls
from pyactiveresource.activeresource import ActiveResource
from biscuit.api import APIHandler
ip_translations = {'aaa.bbb.ccc.ddd': 'xxx.yyy.zzz.qqq',
'eee.fff.ggg.hhh': 'ppp.qqq.rrr.sss'}
slice_api_key = 'xxxxx_YOUR_SLICEHOST_API_KEY_HERE_xxxxx'
slice_api_url = 'https://%s@api.slicehost.com/' % slice_api_key
@tarkatronic
tarkatronic / admin.py
Created March 13, 2012 18:08
Django row-level permissions
"""This is just an example of how to provide permissions on a per-object basis in the admin"""
from django.contrib import admin
from django.contrib.auth.models import Permission
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from account.models import ObjectGroupPermission, ObjectUserPermission
from myapp.models import MyModel
@tarkatronic
tarkatronic / redis_session_backend.py
Created August 14, 2012 16:23 — forked from mikeyk/redis_session_backend.py
A redis backend for Django Sessions, tested on Django 1.3+
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.conf import settings
from django.utils.encoding import force_unicode
import redis
class SessionStore(SessionBase):
""" Redis store for sessions"""
def __init__(self, session_key=None):
self.redis = redis.Redis(
def search_list(term, words, deep):
result = []
idx = 0
while idx < len(words):
try:
idx = words.index(term, idx)
result.append(idx)
idx += 1 # Start from the next element
if not deep:
break
@tarkatronic
tarkatronic / gist:b6d3f6036c13cd54d1a8
Created April 8, 2015 21:17
Mock Django database calls
"""
This is for a case where I was accessing the database connection/cursors directly,
and had need to mock up a result from a database call. The real trick here is
returning the cursor MagicMock object as a side effect of the initial patch.
"""
from unittest import mock
cursor = mock.MagicMock(
@tarkatronic
tarkatronic / filters.py
Created March 25, 2016 16:47
django-filters TimeSpanFilter
import re
import time
from datetime import datetime, timedelta
import django_filters
from django.utils import timezone
from .lookups import Now
@tarkatronic
tarkatronic / base.html
Last active June 25, 2017 20:35
Django Bootstrap Messages
{% if messages %}
{% for message in messages %}
<div class="alert{% if message.tags %}{% if message.tags == "error" %} alert-danger{% else %} alert-{{ message.tags }}{% endif %}{% endif %}" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
{{ message }}
</div>
{% endfor %}
{% endif %}
@tarkatronic
tarkatronic / pygments_filter.py
Created November 28, 2017 22:10
A Django jsonify template filter
import json
from django import template
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
register = template.Library()