Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# Record work @ current time with message
if [ ! -n "$1" ]
then
echo "give a message!"
exit
fi
@minism
minism / middleware.py
Created February 13, 2012 22:45
maintenance mode middleware
from django import http
from mainsite import settings
class MaintenanceMiddleware(object):
"""Serve a temporary redirect to a maintenance url in maintenance mode"""
def process_request(self, request):
if getattr(settings, 'MAINTENANCE_MODE', False) == True and hasattr(settings, 'MAINTENANCE_URL'):
return http.HttpResponseRedirect(settings.MAINTENANCE_URL)
return None
@minism
minism / gist:1954294
Created March 2, 2012 00:35
modes.py
def rotate(seq, n):
return seq[n:] + seq[:n]
diatonic = [2, 2, 1, 2, 2, 2, 1]
ionian = rotate(diatonic, 0)
dorian = rotate(diatonic, 1)
phrygian = rotate(diatonic, 2)
lydian = rotate(diatonic, 3)
mixolydian = rotate(diatonic, 4)
@minism
minism / gist:2832403
Created May 30, 2012 01:39
Fix CKEditor not letting you paste something before typing
editor.on('paste', function(e) {
if (e.editor.getData() == "")
e.editor.setData(e.data.html)
})
local binary = require 'shared.binary'
-- Datagram format
--
-- uint32 seq Sequence number of the packet
-- uint32 ack Last acknowledged sequence number of remote channel
-- uint16 fragment_ofs Offset of fragment
-- uint16 fragment_len Length of fragment, zero if single message
-- uint8 cmd Command (see command.lua)
--- binary data Arbitrary data
class NestedLazyModelField(serializers.ModelSerializer):
"""
Use this class for defining a nested relationship based on a funciton
instead of an attribute.
For example, 'get_profile' method of user
"""
def field_to_native(self, obj, field_name):
obj = getattr(obj, self.source or field_name)
@minism
minism / resources.py
Created November 8, 2012 20:26
tastypie ModelResource that respects 'blank' attribute on Model fields
class BaseModelResource(ModelResource):
@classmethod
def get_fields(cls, fields=None, excludes=None):
"""
Unfortunately we must override this method because tastypie ignores 'blank' attribute
on model fields.
Here we invoke an insane workaround hack due to metaclass inheritance issues:
http://stackoverflow.com/questions/12757468/invoking-super-in-classmethod-called-from-metaclass-new
"""
@minism
minism / serializers.py
Created November 27, 2012 18:43
Tastypie serializer to return 400 on bad JSON request
from tastypie.serializers import Serializer
from tastypie.exceptions import BadRequest
class JSONClientErrorSerializer(Serializer):
"""
Small override to tastypie's internal serializer to provide a client error
on a malformed JSON request body
"""
def from_json(self, *args, **kwargs):
try:
@minism
minism / forms.py
Created November 27, 2012 18:51
Tastypie ModelForm validation that only validates submitted fields on PUT requests
from tastypie.validation import CleanedDataFormValidation
def pick(d, keys):
"""Based on http://underscorejs.org/#pick"""
return dict((key, val) for key, val in d.items() if key in keys)
class BaseFormValidation(CleanedDataFormValidation):
"""
@minism
minism / decorators.py
Last active December 10, 2015 14:38
Cached method by related model save
from django.db import models
from django.db.models.signals import post_save
from django.db.models.loading import get_model
from cachemodel.decorators import cached_method
def model2label(obj):
return ("%s.%s" % (obj._meta.app_label, obj._meta.object_name))