1. youngking
  2. djangobook-cn

Source

djangobook-cn / docs / views.py

# coding=utf-8
#!/usr/bin/env python
#
# The Django Book: Japanese translation of free online Django books.
#
# @package	docs
# @identifier	docs/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 document(request, lang='ja', url=''):
    if lang != 'ja': raise Http404()

    # First look for <bits>/index.fpickle, then for <bits>.fpickle
    bits = url.strip('/').split('/') + ['index.fpickle']
    doc = configs.DOCS_PICKLE_PATH + '/' + '/'.join(bits)
    if not os.path.exists(doc):
        bits = bits[:-2] + ['%s.fpickle' % bits[-2]]
        doc = configs.DOCS_PICKLE_PATH + '/' + '/'.join(bits)
        if not os.path.exists(doc):
            raise Http404(u"'%s' を見つけることができませんでした" % doc)

    bits[-1] = bits[-1].replace('.fpickle', '')

    template_names = [
        'docs/%s.html' % '-'.join([b for b in bits if b]),
        'docs/doc.html',
    ]
    return render_to_response(
        template_names,
        RequestContext(
            request,
            {
                'doc': pickle.load(open(doc, 'rb')),
                'env': pickle.load(
                        open(os.path.join(configs.DOCS_PICKLE_PATH,'globalcontext.pickle'), 'rb')
                    ),
                'toc': urlresolvers.reverse(
                        'document_detail',
                        kwargs={'url':'/'.join(bits[:-1])}
                    ),
                'update_date': datetime.datetime.fromtimestamp(
                        os.stat(os.path.join(configs.DOCS_PICKLE_PATH, 'last_build')).st_mtime
                    ),
                'redirect_from': request.GET.get('from', None),
            },
        )
    )

def search(request, lang='ja', url=''):
    #if lang != 'ja': raise Http404()
    raise Http404()

def images(request, lang='ja', path=''):
    if lang != 'ja': raise Http404()
    return django.views.static.serve(
        request,
        document_root = configs.DOCS_IMAGE_PATH,
        path = path,
    )

def static(request, lang='ja', path=''):
    if lang != 'ja': raise Http404()
    return django.views.static.serve(
        request,
        document_root = configs.DOCS_STATIC_PATH,
        path = path,
    )

def source(request, lang='ja', path=''):
    if lang != 'ja': raise Http404()
    return django.views.static.serve(
        request,
        document_root = configs.DOCS_SOURCE_PATH,
        path = path,
    )