Skip to content

Instantly share code, notes, and snippets.

"""
Patches the database wrapper and template engine to throw an exception if a query is executed inside of a template.
In your urls.py, enable it like so:
>>> import monkey
>>> monkey.patch_templates()
"""
import logging
@jsoa
jsoa / gist:959372
Created May 6, 2011 17:24
Elegant way to parse arguments, keyword arguments and/or return variable for a django template tag
def do_sometag(parser, token):
"""
{% tagname arg1 arg2 arg3 ... with kwg1=v kwg2=v kwg3=v ... as varname %}
"""
argv = token.contents.split()
# get the index of 'as' or the total length of the arguments
a = 'as' in argv and argv.index('as') or len(argv)
# get the index from 'with' to 'as' or the total length of arguments
anonymous
anonymous / fsfds.py
Created June 15, 2011 17:43
debugging context manager
import time, logging
class debug_watch:
def __init__(self, name, logger=logging.info):
self.name = name
self.logger = logger
def __enter__(self):
self.t = time.time()
self.logger("Starting %s" % self.name)
@Zapotek
Zapotek / arachni_script.rb
Created June 20, 2012 15:54
Scripting with Arachni
# Helps to see status messages when developing.
#require 'arachni/ui/cli/output'
# Only works with the latest code from the experimental branch.
require 'arachni'
# to avoid having to prefix every object's name with it
include Arachni
include Utilities
from django.db.models.query import QuerySet
from django.db import models
class QuerySetManager(QuerySet):
'''
Subclass this class to define custom Managers with chainable, custom
QuerySet calls.
For example, define your queryset subclassing this object:
@Tomanow
Tomanow / radiolist for x-editable
Created December 30, 2013 21:59
Modified radio / radiolist gist. Requires x-editable. Adds 'radiolist' option to x-editable where you can use radio inputs.
/**
* X-Editable Radiolist extension for Bootstrap 3
* @requires X-Editable, jquery, etc.
* @example:
$('.editable-earn-method').editable({
name: 'earn_method',
source: [
{value: 'swipes', text: 'Number of swipes'},
{value: 'spend', text: 'Spend Amount ($USD)'}

SVG icon loader with an API

Just copy and paste HTML code to your project. You have the {pan} variable in global scope and {setProgress} function to set the current progress. Unminified version http://goo.gl/sq2gDY

A Pen by Lego Mushroom on CodePen.

License.

@primoz-k
primoz-k / merge_model_objects.py
Last active March 13, 2016 22:17 — forked from nick-merrill/merge_model_objects.py
A variation on a snippet that handles one-to-one relationships by recursively migrating those relationships' field data to the `primary_object`'s related object.
# -*- coding: utf-8 -*-
from django.db.models import Model
from django.contrib.contenttypes.fields import GenericForeignKey
from django.db.utils import IntegrityError
from django.db import transaction
from urllib import parse
try:
from django.apps import apps
class QuerySetDoubleIteration(Exception):
"A QuerySet was iterated over twice, you probably want to list() it."
pass
# "Skinny" here means we use iterator by default, rather than
# ballooning in memory.
class SkinnyManager(Manager):
def get_query_set(self):
return SkinnyQuerySet(self.model, using=self._db)
@prestontimmons
prestontimmons / testcase.py
Created October 24, 2011 22:19
Lazy man's Django testcase
"""
Decrease the verbosity of writing view tests.
Old way:
self.client.get(reverse("my-view"))
self.client.post(reverse("my-view"), data={"key": "value"})
self.client.login("username", "password")
self.client.get(reverse("my-other-view"))
self.client.logout()