July 2011
1 post
4 tags
Dynamic ModelForm creation
That looks amazing!
def get_model_form_class(model_class, fields_list=None, exclude_list=None):
class form_class(forms.ModelForm):
class Meta:
model = model_class
fields = fields_list
exclude = exclude_list
return form_class
The idea taken from:
http://stackoverflow.com/questions/297383/dynamically-update-modelforms-meta-class/297478#297478
4 tags
Shortcut for printing user's full name or username...
That’s it:
from django import template
@register.filter
def nice_name(user):
"""
Example::
Hi, {{ user|nice_name }}
"""
return user.get_full_name() or user.username
5 tags
Great setup example for Nginx + FastCGI +... →
It also uses fabfile for automation of start / restart process.
3 tags
Seems like i’m in trend. The idea to build decentralized network comes to me again and again.
And it seems, we’ll see first p2p social networks soon. Wuala is running. Diaspora is coming. Actually, their start is amazing: they’ve raised investments on Kickstarter and made friends with Pivotal Labs.
I’m working on social network prototype at the moment. The key feature...
June 2010
3 posts
3 tags
thecodefarm team →
I’m going to play with Dajax.
It’s created by Jorge Bastida from thecodefarm team. I like them. :)
4 tags
How to extract html page title by URL
Actually the subject can be divided into two tasks:
retreive data
extract information from it
There’s standard library urllib2 in Python for retreiving data over HTTP and a number of libraries for parsing HTML data. I’ll use html5lib in this example.
First iteration of retrieving data
import urllib2
def read_url(url):
try:
response = urllib2.urlopen(url)
except...
4 tags
Django Widget for CommaSeparatedIntegerField with... →
Assume, we need a registration form for some event with time frame for 3 days, starting at Monday. User can select any of 3 days, so we need to show 3 checkboxes.
Here’s the basic example for such form:
class EventRegistrationForm(forms.Form):
days = forms.CharField(widget=NumbersSelectionWidget(
['Mon', 'Tue', 'Wed'], range(1, 4)))
May 2010
4 posts
2 tags
Unicode 'funny characters'
There’re characters that sometimes cause strange behaviour, when trying to print them to console.
It seems that depends on environment and Python compilation. I’ve tested it on Windows Vista and it failed, than it worked on some *nix machines and failed on others. I used Python 2.6.x version. So, it’s possible you will be unable to rebpoduce it!
An example
Russian character...
4 tags
Next order value for Django model instance
Assume, we have Django model with special weight integer field for ordering. We may want to assign its value automatically on save. Here’s the snippet implementing such behaviour:
class MyModel(models.Model):
# some fields...
weight = models.IntegerField(default=0)
class Meta:
ardering = ('weight',)
def save(self, *args, **kwargs):
self.weight =...
5 tags
Smarter Django cart
Introduction
django-carting is a basic online store application for Django. It is designed in a sketchy manner to be like a rewritable application.
Demo is available:
http://carting-demo.05bit.com
Concept
It’s conceptually differs both from Satchmo and LFS projects. Basically, that’s just a “cart application” with utilities, which can used to build full-featured...
3 tags
Smarter Django project configuration
Two points
Generally we need different settings for Django project in development environment on localhost and on production environment. Settings may differ also across different development environments when application is developed by many programmers.
Project source is good to be stored in some VCS repository, so settings should also be stored there. Settings modifications that are common...
April 2010
5 posts
4 tags
Python logging example and helper
When trying to find simple example on how to use Python logging module for writing logs to file, I became frustrated, as there are only a few useful examples. Here’s one that really works and covers most of use cases: see it on mechanicalcat.net.
And here’s simple helper to open log file, based on that example:
import re
import logging
def openlog(filename, logger_name=None,...
3 tags
Flavoured Markdown
Markdown is good, but it has 2 features that’s hard to explain to inexperienced user:
it doesn’t convert urls to links automatically
it doesn’t convert line breaks to html <br> tags
It seems, for 80% cases this would better be done. Python Markdown supports extensions and thus can be “flavoured”.
So, there are extensaions:
autobr for converting line...
3 tags
Automatically converting urls to html links
Here’s simple Python snippet for converting urls to links in single text line:
import re
link_re = re.compile('(\s+\(?|^)((http|ftp|https)://[-\w\#$%&~/.;:=,?@+]+)', re.IGNORECASE)
def autolinks(line):
return link_re.sub(r'\1<a href="\2" target="_blank">\2</a>', line)
It’s simple yet smart enough:
doesn’t touch urls inside html links <a...
4 tags
Highway to brilliant wiki, part 1. Brillixy.
Hey!
I am unsatisfied.
It’s 21st century, the age of perfect web-frameworks and rapid development. But there’s no good wiki-like engine to satisfy reasonable requirements.
There are Trac, DokuWiki and many others and others. By the way, I really appreciate Trac and DokuWiki. But I need less. And more.
So, I just need an engine which:
has wiki-like basis: users can create,...
4 tags
Rewritable Django apps
The common good approach in designing applications for Django is write them to be reusable. Another approach is to build rewritable applications on top of reusable ones.
What’s the point?
There’re number of specific tasks, which can be splitted to generic blocks. But it seems, some “generic” blocks better not to be generic by theirs nature, because they need “a...