リファクタリング三昧

ueBLOGに影響を受けて、自分が書いたTropyを少しリファクタリングしてみました。
ちゃんとJavaScriptもHTMLカラーリングされます。

/tropy/views.py
http://pastebin.com/827399

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django import forms 

from django.views.generic import simple

from tropy.models import Tropy

import random
import string

def random_redirect_to(request):
    object = random.choice(Tropy.objects.all())

    return simple.redirect_to(request, url=object.get_absolute_url())

def create_object(request):
    url = ''.join(random.choice(string.digits) for i in range(8))
    Tropy.objects.get_or_create(url=url)
    
    manipulator = Tropy.ChangeManipulator(url)

    if request.POST:
        new_data = request.POST.copy()
        errors = manipulator.get_validation_errors(new_data)

        if not errors:
            manipulator.do_html2python(new_data)
            new_object = manipulator.save(new_data)

            return HttpResponseRedirect(new_object.get_absolute_url())
    else:
        new_data = errors = {}
    
    form = forms.FormWrapper(manipulator, new_data, errors)

    return render_to_response('tropy/tropy_create.html', dict(form=form))

/tropy/urls.py
http://pastebin.com/827400

from django.conf.urls.defaults import *
from django.views.generic import (list_detail, create_update)
from django.conf import settings

from tropy.models import Tropy
from tropy.views import (random_redirect_to, create_object)

query_dict = dict(queryset=Tropy.objects.all())
model_dict = dict(model=Tropy)

urlpatterns = patterns('',
    (r'^$', random_redirect_to),

    (r'^random/$', random_redirect_to),

    (r'^(?P<object_id>\d+)/$', list_detail.object_detail, query_dict),

    (r'^create/$', create_object),
    
    (r'^edit/(?P<object_id>\d+)/$', create_update.update_object, 
        dict(model=Tropy, template_name='tropy/tropy_edit.html')),
)

一応Python2.5で書いてます。