Skip to content

Instantly share code, notes, and snippets.

View patrys's full-sized avatar

Patryk Zawadzki patrys

View GitHub Profile
@patrys
patrys / jquery.placeholder.js
Created August 3, 2010 14:43
Fallback code for the HTML5 "placeholder" attribute
$(function() {
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder], textarea[placeholder]').each(function() {
var text = this.getAttribute('placeholder');
var fld = $(this);
function setPlaceholder() {
if (fld.val() == text || fld.val() == '') {
fld.addClass('jqPlaceholder');
fld.val(text);
@patrys
patrys / abstract.py
Created September 17, 2010 11:59
Parametrized apps for Django
class AbstractMixin(object):
_classcache = {}
@classmethod
def contribute(cls):
return {}
@classmethod
def construct(cls, *args, **kwargs):
attrs = cls.contribute(*args, **kwargs)
{% load liczba_mnoga %}
<p>{% liczba_mnoga user_count "jedna osoba" "%(n)s osoby" "%(n)s osób" %}</p>
@patrys
patrys / gist:784810
Created January 18, 2011 17:38
Proper translation for gender-related messages
{% load i18n %}
{% if u.sex == 'm' %}
{% blocktrans with u.get_full_name as male %}
{{ male }} set up us a bomb.
{% endblocktrans %}
{% else %}
{% blocktrans with u.get_full_name as female %}
{{ female }} set up us a bomb.
{% endblocktrans %}
@patrys
patrys / stinson.py
Created June 22, 2011 09:33
Implements the awesome pattern
class Barney(object):
def __enter__(self):
print 'legen...'
def __exit__(self, exc_type, exc_value, traceback):
print '...dary'
with Barney():
print 'wait for it'
@patrys
patrys / base.py
Created July 20, 2011 11:59
A custom sqlite3 database backend for Django that turns synchronous mode off
"""
sqlite3 database backend with non-synchronous mode.
It gives you a huge performance boost by turning off the overzealous
synchronous operation mode. The tradeoff is less data protection in case of
power outages but come on, you were not going to use it in production, were you?
"""
from django.db.backends.sqlite3.base import (
DatabaseWrapper as Sqlite3Wrapper,
DatabaseError,
@patrys
patrys / forms.py
Created August 3, 2011 22:11 — forked from octaflop/forms.py
satchless model example
from django import forms
from satchless.forms.widgets import DecimalInput
from satchless.product.forms import BaseVariantForm
from . import models
class ProductPriceForm(forms.ModelForm):
class Meta:
widgets = {
'price': DecimalInput(min_decimal_places=2),
@patrys
patrys / rebrand.py
Created December 13, 2012 14:42 — forked from jimmac/rebrand.rb
import glob
import os
from xml.etree import ElementTree
TEMP = './tmp.svg'
def rebrand(fname, brand):
svg = ElementTree.parse(fname)
for e in svg.iterfind(".//{http://www.w3.org/2000/svg}rect[@id='background']"):
e.set('style', 'fill:url(#%s);' % (brand,))
@patrys
patrys / such_db_optimization.py
Last active August 29, 2015 14:03
wroc.py / haskell
def query_db(ids):
print('Querying the database:', ids)
# fake some data
results = []
for id_ in ids:
results.extend(range(id_ + 2, id_ + 5))
# simulate DISTINCT()
return set(results)
@patrys
patrys / hack.patch
Last active August 29, 2015 14:22
Expression filter hack
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index 4306193..bf965c1 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -340,11 +340,27 @@ def reverse_ordering(self):
return self
+class ExpressionFilter(object):
+ def __init__(self, expression, value, lookup):