Skip to content

Instantly share code, notes, and snippets.

View specialunderwear's full-sized avatar

Voxin Muyli specialunderwear

View GitHub Profile
@specialunderwear
specialunderwear / exceptionhandler.py
Created April 2, 2015 09:58
Django rest api exception handler, returns exceptions as json and logs exception.
import json
import logging
import traceback
from django.http import HttpResponseServerError
from oscarapi.middleware import IsApiRequest
logger = logging.getLogger(__name__)
@specialunderwear
specialunderwear / exclusive.py
Created May 29, 2015 17:08
Enforce exclusive running of uwsgi cron jobs on aws - where uwsgi legion does not work - using a database lock
"""
Ensure that only 1 process can be running a managment command at the same
time, even across multiple application servers.
Uses a database lock on a table row, in a separate database connection.
"""
import logging
from django.conf import settings
from django.db import models
@specialunderwear
specialunderwear / polymorphicserializer.py
Created July 29, 2015 08:37
Polymorphic serializer
from rest_framework import serializers
from rest_framework.utils.serializer_helpers import BindingDict
class PolymorphicModelSerializer(serializers.ModelSerializer):
"""
Serializer that serializes a polymorphic model
Since the whole point of polymorphism is to have models
with different properties in the same queryset, the ``fields`` meta
specification in rest_framework becomes rather useless. That is
# this file is in a package named verboten
from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseForbidden
from django.template import RequestContext, loader
from django.db import models
from django.conf import settings
class LanguagePermissions(models.Model):
"""Only used to define permissions"""
@specialunderwear
specialunderwear / inspect.py
Created August 30, 2011 14:33
template tags for inspecting variables inside templates
from django import template
register = template.Library()
@register.simple_tag
def props(obj):
result = []
for key in dir(obj):
try:
result.append("%s -> %s<br/>" % (key, getattr(obj, key)))
except Exception as e:
@specialunderwear
specialunderwear / meta.py
Created November 1, 2011 15:19
metaclass for google.appengine.ext.db.Model with i18n properties.
from copy import deepcopy
from google.appengine.ext import db
import settings
class I18nModelMetaClass(db.PropertiedClass):
"""
Metaclass for adding language specific attributes to a
AuthorizationRef authorizationRef;
OSStatus status;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authorizationRef);
// suppose we've got the new hosts file in an NSString called new_hosts_file
NSString *overwrite_hosts = [NSString stringWithFormat:@"echo %s > /private/etc/hosts", new_hosts_file];
char *args[] = {[overwrite_hosts cstring]};
status = AuthorizationExecuteWithPrivileges(authorizationRef, "/bin/sh",
kAuthorizationFlagDefaults, args, NULL);
@specialunderwear
specialunderwear / README.rst
Created January 12, 2012 14:48
virtualenvwrapper postactivate hook with buildout shortcuts

Quickly navigate your buildout.

Requires virtualenv and virtualenvwrapper.

cdco # cd to buildout root
cds voxin # (will autocomplete with tab and will cd into the project named voxin in the src dir)
buildout # will run buildout, but from the buildout root.
@specialunderwear
specialunderwear / one2.js
Created January 29, 2012 23:37
new scope
> var one = 1;
> var two = new one;
> two;
1
> one = 2;
> two;
1
> one;
2
@specialunderwear
specialunderwear / newfunction.js
Created January 29, 2012 23:46
next function
> var next = function(input) {
> return input + 1;
> }
> var three = new next;
> three;
[Function]
> three(2);
3