Skip to content

Instantly share code, notes, and snippets.

@yuchant
yuchant / to_base64.py
Created December 8, 2012 00:59
Convert file to base64. Using for fonts > b64 string.
"""
python to_base64.py some_file
"""
import sys
import os
def main(path):
with open(path) as f:
sys.stdout.write(f.read().encode('base64').replace('\n', ''))
<script type="text/javascript">
$(function() {
var API_KEY = '2132d453ab3cc055308fb40c708ea412';
$(".set").each(function() {
var set_id = $(this).find('a.flickr-set').attr('rel');
url = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=' + API_KEY + '&per_page=48&page=1&format=json&jsoncallback=?&photoset_id=' + set_id;
var selector = $(this);
data = $.getJSON(url,
@yuchant
yuchant / django_db_defaults.py
Created June 7, 2012 04:59
Django: set database level defaults with postgresql_psycopg2 from the model file.
def db_default(field, db_default):
"""
Set attribute __db_default for signal to set default values with.
"""
if db_default == '':
db_default = "''"
field.__db_default = db_default
return field
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()))
@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 = []
@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 / 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 / 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 / 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