Skip to content

Instantly share code, notes, and snippets.

View dfalk's full-sized avatar

Dmitry Falk dfalk

  • freelancer
  • Russia, Yoshkar-Ola
View GitHub Profile
@dfalk
dfalk / gist:954924
Created May 4, 2011 08:24
django <1.2 date_based object_list
I was working on a problem similar to this yesterday, and I found the best solution for me personally was to use the object_list generic view for all date-based pages, but pass a filtered queryset, as follows:
import datetime, time
def post_archive_month(request, year, month, page=0, template_name='post_archive_month.html', **kwargs):
# Convert date to numeric format
date = datetime.date(*time.strptime('%s-%s' % (year, month), '%Y-%b')[:3])
return list_detail.object_list(
request,
queryset = Post.objects.filter(publish__year=date.year, publish__date.month).order_by('-publish',),
@dfalk
dfalk / gradient.html
Created May 16, 2011 12:32
jsgradient
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
@dfalk
dfalk / onlinenow.py
Created December 13, 2011 13:17 — forked from ericflo/onlinenow.py
django online users
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth.models import User
ONLINE_THRESHOLD = getattr(settings, 'ONLINE_THRESHOLD', 60 * 15)
ONLINE_MAX = getattr(settings, 'ONLINE_MAX', 50)
def get_online_now(self):
return User.objects.filter(id__in=self.online_now_ids or [])
@dfalk
dfalk / gist:1527620
Created December 28, 2011 11:23
django admin utf8 usernames
# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
import re
@dfalk
dfalk / gist:1532789
Created December 29, 2011 07:58
django 404 context
# File handlers.py
from django.shortcuts import render_to_response
from django.template import RequestContext, Context
from functools import partial
def base_error(request, template_name=None):
try:
context = RequestContext(request)
except:
@dfalk
dfalk / gist:1536295
Created December 29, 2011 21:31
django subdomains
class SubdomainMiddleware:
""" Make the subdomain publicly available to classes """
def process_request(self, request):
domain_parts = request.get_host().split('.')
if (len(domain_parts) > 2):
subdomain = domain_parts[0]
if (subdomain.lower() == 'www'):
subdomain = None
domain = '.'.join(domain_parts[1:])
@dfalk
dfalk / __init__.py
Created February 4, 2012 22:18 — forked from SmileyChris/__init__.py
a Django site_settings app
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.cache import cache
from project.apps.site_settings import models
KEY = 'site-settings-%d' % settings.SITE_ID
def get_settings(from_cache=True):
"""
@dfalk
dfalk / gist:1740720
Created February 4, 2012 22:29
django site in context
#myproject/context_processors.py:
from django.contrib.sites.models import Site
def site(request):
'''
A context processor to add the current site to the current Context
'''
try:
site = Site.objects.get_current()
@dfalk
dfalk / abstract.py
Created February 7, 2012 04:46 — forked from patrys/abstract.py
Parametrized apps for Django
class AbstractMixin(object):
_classcache = {}
@classmethod
def contribute(cls):
return {}
@classmethod
def construct(cls, *args, **kwargs):
attrs = cls.contribute(*args, **kwargs)
@dfalk
dfalk / postprocess_sekizai.py
Created February 28, 2012 04:43 — forked from timmyomahony/postprocess_sekizai.py
django-sekizai postprocessor for enabling django-compressor compatibility
"""
Get django-sekizai, django-compessor (and django-cms) playing nicely together
re: https://github.com/ojii/django-sekizai/issues/4
using: https://github.com/jezdez/django_compressor.git
and: https://github.com/ojii/[email protected]
"""
from compressor.templatetags.compress import CompressorNode
from django.template.base import *
def compress(data, name):