Skip to content

Instantly share code, notes, and snippets.

View dchaplinsky's full-sized avatar

Dmitry Chaplinsky dchaplinsky

View GitHub Profile
@dchaplinsky
dchaplinsky / example.diff
Created October 25, 2010 11:14
Patch for coffin extensions example
--- misc.py~ 2010-10-25 14:07:30.000000000 +0300
+++ misc.py 2010-10-25 14:13:56.000000000 +0300
@@ -1,5 +1,6 @@
from coffin import template
-from jinja2 import Markup
+from jinja2 import Markup, nodes
+from jinja2.ext import Extension
register = template.Library()
@register.filter(jinja2_only=True)
@dchaplinsky
dchaplinsky / jinja2_loaders.py
Created October 27, 2010 11:40
Jinja2 loaders for django 1.2
from coffin.template import Template as JTemplate
from django.template.loaders import app_directories, filesystem
from django.template import TemplateDoesNotExist
# integrate jinja instead of django template language to take advantage of
# all django helper function like direct_to_template
def _render_or_die(source, origin):
"""helper function for loaders below. Makes a simple check if
passed template seems to be jinja one and raising exception otherwise"""
@dchaplinsky
dchaplinsky / orm_patch.py
Created October 29, 2010 12:50
Patch for django ORM to display all simple SELECT requests which makes lookup by one key
from django.db.models.query import QuerySet
old_get = QuerySet.get
def new_get(self, *args, **kwargs):
if len(kwargs) == 1:
for field_name in kwargs:
if "__" in field_name:
field_name, kind = field_name.split("__")
if kind != 'exact':
continue
@dchaplinsky
dchaplinsky / amazonproduct_patch.py
Created October 31, 2010 21:28
Patch for python implementation of amazonproduct API to enable caching and preserve some debug info.
from amazonproduct import API
import os
from hashlib import md5
old_fetch = API._fetch
old_build_url = API._build_url
old_init = API.__init__
def new_fetch(self, url):
@dchaplinsky
dchaplinsky / jinja2.diff
Created November 1, 2010 12:52
Patch for jinja2 loader to enable debug_toolbar support (original implementation made by Dmitri Kharlamov)
We couldn’t find that file to show.
@dchaplinsky
dchaplinsky / upload_utils.py
Created November 3, 2010 12:58
Custom upload_to callback which using given names of fields to obtain access to storage and input data to create a slug
import os
from django.template.defaultfilters import slugify
try: # make it python2.4 compatible!
from hashlib import md5
except:
import md5
def picture_filename(path, field_name, field_to_slug = None):
def upload_to(instance, filename):
@dchaplinsky
dchaplinsky / orm_patch.py
Created November 9, 2010 13:30
Deep injection into the django ORM to find select requests suitable for Handler Socket
old_iterator = QuerySet.iterator
def new_iterator(self):
def drilldown(node, negated):
NEGATE_TABLE = {
'exact': 'nexact', # bogus, I know
'lt': 'gte',
'lte': 'gt',
'gte': 'lt',
'gt': 'lte',
@dchaplinsky
dchaplinsky / _pagination.html
Created November 12, 2010 10:25
Port of django_pagination package to make it works with jinja2
{% macro render_pagination(pages) -%}
{% if pages.is_paginated %}
<div class="pagination">
{% if pages.page_obj.has_previous() %}
<a href="?page={{ pages.page_obj.previous_page_number() }}{{ pages.getvars }}{{ pages.hashtag }}" class="prev">&lsaquo;&lsaquo; previous</a>
{% else %}
<span class="disabled prev">&lsaquo;&lsaquo; previous</span>
{% endif %}
{% for page in pages.pages %}
{% if page %}
@dchaplinsky
dchaplinsky / sort_optimization.py
Created November 16, 2010 12:32
Method to avoid heavy filesort
class FoobarManager(models.Manager):
def latest(self, limit=10):
""" more effecient version of query to avoid filesort """
ids = list(self.values_list('id', flat=True).order_by('-created')[:limit])
return self.filter(id__in=ids).extra(
select={'manual': 'FIELD(id,%s)' % ','.join(map(str, ids))},
order_by=['manual'])
@dchaplinsky
dchaplinsky / simple_test.py
Created December 22, 2010 00:33
Simple test of handlersocket and mysql python working together
#!/usr/bin/env python
from MySQLdb import connect
from pyhs.manager import Manager
from hashlib import md5
from datetime import datetime
DB_NAME = 'simple_test'
DB_USER = 'root'
DB_PASSWORD = ''