Source

djangobook-cn / djangobook / views.py

Full commit
# coding=utf-8
#!/usr/bin/env python
#
# The Django Book: Japanese translation of free online Django books.
#
# @package	djangobook
# @identifier	djangobook/views.py
# @version	1.0
# @rights	GPLv3 - http://www.fsf.org/licensing/licenses/gpl.html
# @creator	Kosei Kitahara - http://surgo.jp/
# @issued	2009-03-01
# @modified	2009-03-01
# @references	http://djangobook.appspot.com/
#
# Copyright (C) 2009 Kosei Kitahara. - http://surgo.jp/
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see http://www.gnu.org/licenses/.

import os
import datetime
import cPickle as pickle

import django.views.static
from django.conf import settings
from django.core import urlresolvers
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.template import RequestContext
from django.views.generic.create_update import create_object, delete_object, update_object
from ragendja.dbutils import get_object_or_404
from django.shortcuts import render_to_response
import configs

def index(request):
    return HttpResponseRedirect(
        urlresolvers.reverse('book_toc', kwargs={
            'title': '2.0',
        })
    )

def book(request, lang='ja', title='2.0', url=''):
    if lang != 'ja' or title not in configs.BOOKS: raise Http404()
    # First look for [url]/index.fpickle, then for [url].fpickle
    bookroot = configs.BOOKS_RESOURCE_PATH+title+configs.BOOKS_PICKLE_PATH
    bookindex = bookroot+url+'/index.fpickle'
    bookpage = bookroot+url+'.fpickle'
    if os.path.exists(bookindex):
        page = bookindex
    else:
        if os.path.exists(bookpage):
            page = bookpage
        else:
            raise Http404()
    template_names = [
        '%s.html' % str(url).replace('/', '-'),
        'book.html',
    ]
    #return HttpResponse(pickle.load(open(page, 'rb')))
    return render_to_response(
        template_names,
        RequestContext(
            request,
            {
                'book': pickle.load(open(page, 'rb')),
                'env': pickle.load(open(bookroot+'globalcontext.pickle', 'rb')),
                'update_date': datetime.datetime.fromtimestamp(os.stat(bookroot+'last_build').st_mtime),
                'update_date': datetime.datetime.fromtimestamp(os.stat(bookroot+'last_build').st_mtime),
                'home': urlresolvers.reverse('book_toc', kwargs={'title':title}),
                'home': urlresolvers.reverse('book_search', kwargs={'title':title}),
                'redirect_from': request.GET.get('from', None),
            },
        )
    )

def search(request, lang='ja', title='2.0', url=''):
    return HttpResponse('search')

def source(request, lang='ja', title='2.0', path=''):
    if lang != 'ja' or title not in configs.BOOKS: raise Http404()
    return django.views.static.serve(
        request,
        document_root = os.path.join(
            configs.BOOKS_RESOURCE_PATH+title+configs.BOOKS_SOURCE_PATH
        ),
        path = path,
    )