Skip to content

Instantly share code, notes, and snippets.

View mrts's full-sized avatar

Mart Sõmermaa mrts

  • Tallinn, Estonia (EET, UTC +2 hours)
View GitHub Profile
@mrts
mrts / raw_id_fields_widget_in_custom_ModelForm.py
Created January 29, 2010 22:34
Use raw_id_fields widget in custom ModelForm
from django.contrib.admin import widgets
from django import forms
from django.contrib import admin
# create wrappers for overriding the queryset
class ToWrapper(object):
def __init__(self, to, manager):
self.to = to
self._default_manager = manager
# see http://groups.google.com/group/django-users/browse_thread/thread/259c1679ddaaceb8
# for the problem and http://code.djangoproject.com/ticket/12780 for the admin/options.py patch
# required to get this working
from django.forms.util import ErrorList
class ClipAdmin(admin.ModelAdmin):
def formsets_are_valid(self, formsets, form, form_is_valid, instance,
request):
valid = super(ClipAdmin, self).formsets_are_valid(formsets,
"""
Thread-safe Django cache backend for pylibmc.
Tested on Python 2.6, should work on 2.5 as well.
Use it by setting CACHE_BACKEND in settings.py, e.g.:
CACHE_BACKEND = 'projdir.utils.pylibmcd://127.0.0.1:11211/'
And here's how to properly install pylibmcd in Ubuntu for mod_wsgi:
@mrts
mrts / fabfile.py
Created January 7, 2011 00:31
Automatic remote deployment with Fabric.
"""
This fabfile automates deployment of and moving data between Django apps
in development (devel), staging (stage), and production (live)
environments.
Use it as:
fab -H user@host:port deploy:stage
Requirements
@mrts
mrts / .gitignore
Created January 17, 2011 20:45
Script that uses the Google Calendar API for automated time reports.
*.pyc
*.swp
timetrack_conf.py
@mrts
mrts / github.py
Created January 30, 2011 18:08
GitHub API with descriptors
# coding: utf-8
"""
To my great disappointment, Dustin Sallings's otherwise excellent py-github
does not work with repositories that have / in branch names, as this breaks
the XML-based parser. Unfortunately, many projects use / for simulating
directory structure. See e.g.
http://github.com/api/v2/xml/repos/show/django/django/branches
for the error.
What follows is a comprehensive interface to the GitHub JSON API that does not
@mrts
mrts / descriptors.py
Created January 30, 2011 18:58
Example of chaining descriptors and sharing a cache between them.
"""
Example of chaining descriptors and sharing a cache between them.
"""
class LeafDescriptor(object):
def __get__(self, obj, cls):
return obj.parent._cache
class Bar(object):
baz = LeafDescriptor()
import os
from django.core.management.base import CommandError, LabelCommand
from django.utils.datastructures import SortedDict
from django_commands.utils import parse_apps_and_models
class Command(LabelCommand):
args = '<upload-path> <appname.Model> [appname.Model] ...>'
help = ("Cleans orphaned file field files from <upload-path>.\n"
"'Orphaned' is defined as existing under <upload-path> "
@mrts
mrts / lazycacheddescriptor.py
Created March 9, 2011 19:12
Thread safe descriptor that combines memoizing (caching) and lazy loading
class LazyCachedDescriptor(object):
def __init__(self, fn, *args, **kwargs):
self.cache = None
self.fn = fn
self.args = args
self.kwargs = kwargs
def __get__(self, obj, objtype):
if self.cache is None:
result = [self.fn(*self.args, **self.kwargs)]
@mrts
mrts / gist:4041421
Created November 8, 2012 20:37
copy_if in C++03 and C++11
#include <algorithm>
#include <vector>
#include <iostream>
// for C++03
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
using namespace std;