Skip to content

Instantly share code, notes, and snippets.

@yuchant
yuchant / bad_idea.py
Created February 23, 2011 04:07
A Terrible Idea
class ModelAdmin(admin.ModelAdmin)
"""
Refrain from prolonged exposure.
"""
def render_change_form(self, request, context, *args, **kwargs):
def get_queryset(original_func):
import inspect, itertools
def wrapped_func():
if inspect.stack()[1][3] == '__iter__':
return itertools.repeat(None)
@yuchant
yuchant / has_exact_m2m_match.py
Created February 24, 2011 23:21
Find an exact object-m2m match in Python -- no SQL.
"""
Find Exact M2M - Match
=========
Find an exact m2m match for a model.
Just did for fun in response to a stackoverflow question for a data importing issue....
By Yuji Tomita
2/24/2011
@yuchant
yuchant / jquery.crossfade.js
Created February 11, 2012 05:51
jQuery cross fade..
/*
Cross Fade Any Element
Version 0.01
By Yuji Tomita
February 10, 2012.
*/
(function( $ ){
$.fn.crossFadeTo = function( fadeToElement, speed, options) {
@yuchant
yuchant / jquery.stickyelement.js
Created February 26, 2012 20:55
jQuery - stick element to top upon scrolling
/*
Stick element to top
Version 0.01
Usage:
$("#product_preview_container").stickyElement({ // apply to element to stick.
stopPoint: $("#products_see_more"), // integer or jquery element to stop fading at
stopPointOffset: 100, // offset the stop point
@yuchant
yuchant / sorl_pil_engine.py
Created February 27, 2012 01:29
Sorl Thumbnail PIL Engine that accepts PNG conversion to JPG with background color
"""
Sorl Thumbnail Engine that accepts background color
---------------------------------------------------
Created on Sunday, February 2012 by Yuji Tomita
"""
from PIL import Image, ImageColor
from sorl.thumbnail.engines.pil_engine import Engine
@yuchant
yuchant / sorl_pil_engine.py
Created February 27, 2012 01:29
Sorl Thumbnail PIL Engine that accepts PNG conversion to JPG with background color
"""
Sorl Thumbnail Engine that accepts background color
---------------------------------------------------
Created on Sunday, February 2012 by Yuji Tomita
"""
from PIL import Image, ImageColor
from sorl.thumbnail.engines.pil_engine import Engine
@yuchant
yuchant / resolve.py
Created February 28, 2012 00:15
Django resolve variable with variable value tag
"""
Resolve lookup based on variable value
--------------------------------------
{% resolve foovar foodict %} # does django's Variable(foovar).resolve(foodict)
Created on Monday, February 2012 by Yuji Tomita
"""
from django import template
from django.template.base import Variable, VariableDoesNotExist
@yuchant
yuchant / capture.py
Created February 28, 2012 00:16
Django capture contents of block - variable assignment in template like Shopify
"""
Capture contents of block into context
--------------------------------------
Use case: variable accessing based on current variable values.
{% capture foo %}{{ foo.value }}-suffix{% endcapture %}
{% if foo in bar %}{% endif %}
Created on Monday, February 2012 by Yuji Tomita
@yuchant
yuchant / template_injector.py
Created April 5, 2012 01:31
Django template context variable injector decorator (inject variables into templates based on URL, not view)
#from django.core.urlresolvers import reverse, reverse_lazy, NoReverseMatch
# from django.utils.functional import lazy
# CONTEXT_INJECTOR_REVERSE_DEBUG = getattr(settings, 'CONTEXT_INJECTOR_REVERSE_DEBUG', False)
class TemplateContextInjectorMiddleware(object):
lazy_url_func_pairs = []
def generate_default_sql(app, created_models, verbosity, **kwargs):
"""
Generate default SQL
"""
db_fields = filter(lambda x: hasattr(x, 'db_default'), OrderShipLog._meta.fields)
modelname = OrderShipLog._meta.object_name
sql_dir = os.path.join(os.path.dirname(__file__), '../sql/')
sql_filename = os.path.join(sql_dir, '{modelname}.postgresql_psycopg2.sql'.format(
modelname=modelname.lower()))