すみません自分用メモですorz

# encoding = utf-8

from django.shortcuts import render_to_response
from tutorial.wiki60.models import Keyword
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django import forms

#from django.utils.simplejson import dumps

#from django import template
#register = template.Library()

import sha
import base64

def uri(value):
    s = sha.new(value)
    return base64.urlsafe_b64encode(s.digest())

def index(request):
    keywords = [keyword for keyword in Keyword.objects.order_by('keyword')]
   
    return render_to_response('wiki60/index.html', dict(keywords=keywords))
   
def keyword(request, uri):
    try:
        keyword = Keyword.objects.select_related().get(uri=uri)
    except Keyword.DoesNotExist:
        raise Http404

    return render_to_response('wiki60/keyword.html', dict(keyword=keyword))

def edit(request, uri):
    keyword = Keyword.objects.select_related().get(uri=uri)
   
    return render_to_response('wiki60/edit.html', dict(keyword=keyword))

#def save(request):
#    keyword = request.POST['keyword']
#    data = request.POST['data']
#   
#    keyword = Keyword.objects.select_related().get(pk=keyword)
#    keyword.data = data
#    keyword.save()
#   
#    return HttpResponseRedirect('/wiki60/')

class KeywordAddManipulator(forms.Manipulator):
    def __init__(self):
        self.fields = (
            forms.TextField(field_name="keyword", maxlength=255, is_required=True),
            forms.LargeTextField(field_name="data"),
        )
       
#class keywordEditManipulator(forms.Manipulator):
#    def __init__(self):
#        self.fields = (
#            forms.HiddenField(field_name),
#            forms.TextField(field_name)
#        )

def create(request):
    manipulator = KeywordAddManipulator()

    if request.POST:
        new_data = request.POST.copy()
        keyword = request.POST['keyword']
        data = request.POST['data']
       
        obj, created = Keyword.objects.get_or_create(keyword=keyword, uri=uri(keyword))
       
        if created:
            obj.data = data

            return HttpResponseRedirect('/wiki60/%s/keyword/' % obj.uri)
        else:
            error = {}
            error['message'] = 'This keyword is already existing.' 
       
    else:
        error = new_data = {}

    form = forms.FormWrapper(manipulator, new_data, {})
   
    return render_to_response('wiki60/create.html', dict(form=form, error=error))
  1. uriフィールドをprimary_keyにする
  2. edit部分をManipulatorをカスタムして作成
  3. Pytstを使用したkeyword反映

とりあえずはこの3つを今週中に実装。

wikiを作るのはとても勉強になることがよくわかった。
そしてDjangoで作ると楽しいのがよくわかった。