Skip to content

Instantly share code, notes, and snippets.

View c4urself's full-sized avatar

Christian Verkerk c4urself

  • Tubular Labs
  • Mountain View
View GitHub Profile
@c4urself
c4urself / search_in_models.py
Created May 9, 2011 15:48
Search in multiple django models
import operator
from django.db.models.query import QuerySet
from django.utils.encoding import smart_str
from django.db import models
def search_in_models(search_query, model_fields, queryset=False):
"""
Search in multiple models:
`model_fields` is are tuples within a tuple defining model_class, search_field pairs
@c4urself
c4urself / settings.py
Created May 9, 2011 15:57
Django settings file as loader for server specific settings files
#
# 1. Imports base settings from `/serversettings/base.py`
# 2. Imports settings based on server name, unless:
# a. The environmental setting `SERVER_TYPE` has been set in the `django.wsgi` script
# b. The name of the server corresponds to a name in the `DEVELOPMENT_MACHINES` tuple
#
import os
import platform
import sys
import warnings
@c4urself
c4urself / ccarousel.py
Created May 10, 2011 13:42
Simple jQuery Click Carousel
// szs Shizzle JS Selector
ccarousel.init = function(szsItems, szsButtonPrev, szsButtonNext) {
var numVisible = 4;
var aAllItems = $(szsItems);
renewList = function() {
aAllItems = $(szsItems);
};
aItems = $(szsItems);
oLeft = $(szsButtonPrev);
@c4urself
c4urself / cshowhidemenu.py
Created May 10, 2011 13:46
Simple jQuery Menu Show/hide
handleShowMenu = function(sMainLiClass, sSubUlClass) {
$(sMainLiClass).mouseover(function() {
if(current !== undefined && this !== current) {
$(sSubUlClass, current).fadeOut();
}
current = this;
var oSubmenu = $(sSubUlClass, this);
clearTimeout(timer);
@c4urself
c4urself / innershiv_load.js
Created May 12, 2011 09:59
HTML5 for Ajax loaded content in IE 7 and 8
// http://www.changer.nl
// http://jdbartlett.github.com/innershiv
window.innerShiv = (function() {
var d, r;
return function(h, u) {
if (!d) {
d = document.createElement('div');
r = document.createDocumentFragment();
@c4urself
c4urself / message.html
Created May 17, 2011 10:17
Highlight and Hide your Django messages
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.12.custom.min.js"></script>
</head>
<body>
<header>
@c4urself
c4urself / html_tag.py
Created May 19, 2011 08:54
make an html tag in python
#tag('ul',''.join([tag('li',i) for i in xrange(1,6)]),{'class':'required'})
#>>>'<ul class="required"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>'
def tag(tag_name='div', content='', attrs_dict={}):
"""
Makes an html tag.
"""
template = '<%s>' % ('%s>%s</%s' % ('%s' % (tag_name + '%s'),'%s','%s' % tag_name))
return template % (' '.join([' %s="%s"' %(k,v) for k,v in attrs_dict.iteritems()]), content)
@c4urself
c4urself / get_first_or_none.py
Created June 16, 2011 08:30
Get First Or None
def get_first_or_none(qs):
"""
Gets first item of queryset or None,
queryset can be QuerySet, RelatedManager,
Model, or object/Model instance.
Examples:
class Pizza(models.Model):
name = models.CharField(max_length=50)
toppings = models.ForeignKey('test.Topping')
@c4urself
c4urself / url_patterns.py
Created June 16, 2011 08:39
URL Patterns with Optional Arguments
(r'^articles/(?P<year>\d{4}/?$, 'main.views.year'),
# When a use case comes up that a month needs to be involved as
# well, you add an argument in your regex:
(r'^articles/(?P<year>\d{4}/(?P<month>\d{2})/?$, 'main.views.year_month'),
# That works fine, unless of course you want to show something
# different for just the year, in which case the following case can be
# used, making separate views based on the arguments as djangoproject
@c4urself
c4urself / alternative_ssr_template
Created October 1, 2011 18:43
Alternative ssr template
def alternate_ssr_template(ssr_template, default_args_pos):
"""
Decorator which changes the template and context for non-ajax
server-side requests.
This decorator MUST be first because it uses func_defaults.
`ssr_template`: Replacement template.
`default_args_pos`: The position of the 'template' argument that will
be replaced in the list of default arguments
for the function.