コード例 #1
0
ファイル: views.py プロジェクト: pv/mediasnake
def bookmark(request, id, chapter):
    try:
        ebook = Ebook.objects.get(pk=id)
    except Ebook.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        try:
            paragraph = int(request.POST['paragraph'])
        except (ValueError, KeyError):
            return HttpResponse("400 Bad request", status=400)

        try:
            bookmark = Bookmark.objects.get(ebook=ebook)
        except Bookmark.DoesNotExist:
            bookmark = Bookmark(ebook=ebook)

        bookmark.chapter = chapter
        bookmark.paragraph = paragraph
        bookmark.save()
    else:
        try:
            bookmark = Bookmark.objects.get(ebook=ebook, chapter=chapter)
        except Bookmark.DoesNotExist:
            raise Http404

    content = json.dumps(dict(paragraph=bookmark.paragraph, error=False))
    return HttpResponse(content, content_type="application/json")
コード例 #2
0
ファイル: views.py プロジェクト: pv/mediasnake
def ebook(request, id, chapter):
    ebook, epub, chapters, paragraphs, chapter = _get_epub(id, chapter)

    languages = [dict(code=lang.code, dict_url=lang.dict_url)
                 for lang in Language.objects.order_by('code').all()]

    context = {
        'ebook': ebook,
        'paragraphs': paragraphs,
        'chapters': chapters,
        'chapter': chapter,
        'next': chapter + 1 if chapter + 1 < len(chapters) else None,
        'prev': chapter - 1 if chapter > 0 else None,
        'languages': languages,
        }

    try:
        bookmark = Bookmark.objects.get(ebook=ebook)
    except Bookmark.DoesNotExist:
        if chapter != 0:
            bookmark = Bookmark(ebook=ebook)
            bookmark.chapter = 0
            bookmark.paragraph = 0
            bookmark.save()

    return render(request, "mediasnakebooks/ebook.html", context)