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 / django_orm_inner_join.py
Created February 7, 2012 21:43
Django ORM INNER JOIN SQL test
from django.db import models
class Banana(models.Model):
name = models.CharField(max_length=255)
father = models.ForeignKey('self', null=True)
def __unicode__(self):
return self.name
@c4urself
c4urself / jquery.tooltip.js
Created February 2, 2012 09:33
jquery tooltip
define([ 'jquery' ], function($) {
var Tooltip = function(element, options) {
this.$element = $(element);
var tooltip = this,
title = this.$element.attr('title');
this.options = $.extend({}, $.fn.tooltip.defaults, options);
this.text = this.$element.removeAttr('title') && title || this.options.text;
@c4urself
c4urself / truncatewords_by_chars.py
Created January 27, 2012 10:18
Django truncate words by characters
from django import template
register = template.Library()
@register.filter
def truncatewords_by_chars(value, arg):
"""
Truncate words based on the number of characters
based on original truncatewords filter code
@c4urself
c4urself / styles.less
Created December 15, 2011 10:48
General styles.less
@yellow: yellow;
@green: green;
.pie {
behavior: url(PIE.htc);
}
@import "css/mixins";
@import "css/base";
@import "css/tables";
@c4urself
c4urself / jquery.notification.js
Created December 14, 2011 06:28
jQuery notifications
define([ 'jquery' ], function($) {
$.fn.notification = function(msg_type, msg, options) {
var container = this;
var defaults = {
autoHide: true,
hideSpeed: 3000,
fadeInSpeed: 'fast',
hideOthers: true
};
@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.
@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 / 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 / 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 / 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>