Ejemplo n.º 1
0
def save_chapter(request, book_id, chapter_id):
    if request.method == 'POST':
        bc = BookComponent(book_id)
        if not bc.is_my_book(request.user):
            raise Http404("不正なリクエストです")

        form = forms.ChapterForm(request.POST)
        if form.is_valid():
            if chapter_id == 0:
                chapter = Chapter(chapter=form.cleaned_data['chapter'],
                                  order=form.cleaned_data['order'],
                                  book=Book.objects.get(id=book_id))
                chapter.save()
                section = Section(text="",
                                  order=1,
                                  update_date=timezone.now(),
                                  chapter=Chapter.objects.get(id=chapter.id))
                section.save()
                page = bc.get_page(section.id)
                return HttpResponseRedirect(
                    reverse('disp_book', args=[book_id, page]))
            else:
                chapter = Chapter.objects.get(id=chapter_id)
                chapter.chapter = form.cleaned_data['chapter']
                chapter.order = form.cleaned_data['order']
                chapter.book = Book.objects.get(id=book_id)
                chapter.save()
                #section_id = request.session['section_id']
                page = bc.get_chapter_top_page(chapter_id)
            return HttpResponseRedirect(
                reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 2
0
def under_chapter(request, book_id, chapter_id, page):
    # チャプターを上下を入れ替える
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    bc.swap_chapter(chapter_id, False)

    return HttpResponseRedirect(reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 3
0
def edit_section(request, book_id, chapter_id, section_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    record = Section.objects.get(id=section_id)
    if record.text == "":
        record.text = "ここに記事を書きます"
    section_form = forms.SectionForm(initial={'text': record.text,
                                              'order': record.order})

    data = {'section_form': section_form,
            'book_id': book_id, 'chapter_id': chapter_id, 'section_id': section_id}

    return book_header(request, 'pyarticle/admin/section/section.html', bc, data)
Ejemplo n.º 4
0
def add_chapter(request, book_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    # チャプターのorderの最大値を求める
    order_max = Chapter.objects.filter(book=book_id).aggregate(Max('order'))
    if 'order__max' in order_max:
        order_max = order_max['order__max'] + 1
    else:
        order_max = 1

    form = forms.ChapterForm(initial={'order': order_max})
    data = {'chapter_form': form, 'book_id': book_id, 'chapter_id': 0}
    return book_header(request, 'pyarticle/admin/chapter/chapter.html', bc,
                       data)
Ejemplo n.º 5
0
def save_section(request, book_id, chapter_id, section_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    if request.method == 'POST':
        bc = BookComponent(book_id)
        form = forms.SectionForm(request.POST)
        if form.is_valid():
            if section_id == 0:
                section_id = bc.create_section(chapter_id, form.cleaned_data['text'], form.cleaned_data['order'])
            else:
                bc.update_section(section_id, chapter_id, form.cleaned_data['text'], form.cleaned_data['order'])

        page = bc.get_page(section_id)
        return HttpResponseRedirect(reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 6
0
def delete_section(request, book_id, chapter_id, section_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    # 現在のページ取得
    page = bc.get_page(section_id)
    # セクションを削除する
    bc.delete_section(section_id)

    if not bc.is_exists_chapter_section(chapter_id):
        # チャプターのセクションがすべて削除されたときは空のセクションを自動的に作成する
        new_section_id = bc.create_section(chapter_id, "記事を書きます", 1)
        page = bc.get_page(new_section_id)
    else:
        total_page = bc.get_page_count()
        if total_page < page:
            page = total_page

    return HttpResponseRedirect(reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 7
0
def add_section(request, book_id, chapter_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    items = Section.objects.filter(chapter=chapter_id).aggregate(Max('order'))
    if 'order__max' in items:
        order_max = items['order__max'] + 1
    else:
        order_max = 1

    section_form = forms.SectionForm(initial={'text': "記事を書きます",
                                              'order': order_max})
    new_section_image_form = forms.SectionImageForm()

    data = {'section_form': section_form, 'section_image_datas': [],
            'new_section_image_form': new_section_image_form,
            'book_id': book_id, 'chapter_id': chapter_id, 'section_id': 0}

    return book_header(request, 'pyarticle/admin/section/section.html', bc, data)
Ejemplo n.º 8
0
def delete_chapter(request, book_id, chapter_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    bc.delete_chapter(chapter_id)
    page = 1
    if not bc.is_exists_chapters():
        # チャプターをすべて削除すると、ページがなくなってしまうので
        # 空のページを生成する
        bc.create_empty_book()

    return HttpResponseRedirect(reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 9
0
def edit_chapter(request, book_id, chapter_id):
    bc = BookComponent(book_id)
    if not bc.is_my_book(request.user):
        raise Http404("不正なリクエストです")

    record = Chapter.objects.get(id=chapter_id)
    form = forms.ChapterForm(initial={
        'chapter': record.chapter,
        'order': record.order
    })
    section_records = Section.objects.filter(chapter=Chapter.objects.get(
        id=chapter_id))
    data = {
        'chapter_form': form,
        'section_records': section_records,
        'book_id': book_id,
        'chapter_id': chapter_id
    }
    return book_header(request, 'pyarticle/admin/chapter/chapter.html', bc,
                       data)
Ejemplo n.º 10
0
def ajax_save_chapter(request):
    if request.method == 'POST':
        chapter_list = request.POST.get('chapter_list', None)
        book_id = request.POST.get('book_id', None)
        bc = BookComponent(book_id)
        if not bc.is_my_book(request.user):
            ret = {"result": -1, "message": "不正な値です"}
        else:
            chapter_list = json.loads(chapter_list)
            try:
                with transaction.atomic():
                    for i, html_chapter_id in enumerate(chapter_list):
                        chapter_id = html_chapter_id.split("_")[0]
                        bc.update_chapter_order(chapter_id, i + 1)

                ret = {"result": 0, "message": "正常"}
            except DatabaseError:
                ret = {"result": -1, "message": "DBの更新に失敗しました"}

    return JsonResponse(ret)
Ejemplo n.º 11
0
def index(request):
    # マイページには自分の投稿記事とプロフィールを表示する
    book_records = Book.objects.order_by('create_date').reverse().all()
    # 本全体のアクセス数をカウントする
    books = []
    for book in book_records:
        bc = BookComponent(book.id)
        if bc.draft == 1:
            books.append(bc.book_info())

    # 検索フォームの作成
    search_form = SearchForm()

    # 人気記事top10
    popular_books = []
    popular_records = Book.objects.order_by('good_count').reverse().all()[:10]
    for book in popular_records:
        bc = BookComponent(book.id)
        if bc.draft == 1:
            popular_books.append(bc.book_info())

    data = {
        'books': books,
        'search_form': search_form,
        'popular_books': popular_books
    }
    return custom_render(request, 'pyarticle/index.html', data)
Ejemplo n.º 12
0
def index_old(request):
    """
    登録されている本一覧をカテゴリごとに表示する
    :param request:
    :return:なし
    """
    # 検索フォームの作成
    search_form = SearchForm()
    # カテゴリの取得
    categories = Category.objects.all().order_by('category_name')
    records = {}
    for category in categories:
        book_records = Book.objects.filter(
            category=category).order_by('id').reverse()
        if len(book_records) == 0:
            continue
        records[category.category_name] = []

        # カテゴリごとに本をまとめる
        for book_record in book_records:
            bc = BookComponent(book_record.id)
            acc = bc.get_book_access_count()
            if len(book_record.description) > 10:
                book_record.description = book_record.description[0:100]

            first_chapter = Chapter.objects.filter(
                book=book_record).order_by('order').first()
            first_section = Section.objects.filter(
                chapter=first_chapter).order_by('order').first()
            records[category.category_name].append(
                [book_record, acc, first_chapter, first_section])

    return custom_render(request, 'pyarticle/index.html', {
        'book_records': records,
        'search_form': search_form
    })
Ejemplo n.º 13
0
def search_books(key_word):
    results = []  # score, 見つかった文字列, 本のID, ページ番号

    book_records = Book.objects.filter(title__contains=key_word, draft=1)
    if len(book_records) > 0:
        for record in book_records:
            results.append({
                'score': 10,
                'title': record.title,
                'text': record.description,
                'book_id': record.id,
                'page': 1
            })

    book_records = Book.objects.filter(description__contains=key_word, draft=1)
    if len(book_records) > 0:
        for record in book_records:
            results.append({
                'score': 8,
                'title': record.title,
                'text': record.description,
                'book_id': record.id,
                'page': 1
            })

    chapter_records = Chapter.objects.filter(chapter__contains=key_word)
    if len(chapter_records) > 0:
        for record in chapter_records:
            book_id = record.book.id
            bc = BookComponent(book_id)
            if bc.draft == 1:
                page = bc.get_chapter_top_page(record.id)
                results.append({
                    'score': 5,
                    'title': bc.title,
                    'text': record.chapter,
                    'book_id': book_id,
                    'page': page
                })

    section_records = Section.objects.filter(text__contains=key_word)
    if len(section_records) > 0:
        for record in section_records:
            book_id = record.chapter.book.id
            bc = BookComponent(book_id)
            if bc.draft == 1:
                page = bc.get_page(record.id)
                results.append({
                    'score': 3,
                    'title': bc.title,
                    'text': record.text,
                    'book_id': book_id,
                    'page': page
                })

    return results
Ejemplo n.º 14
0
def set_good(request, book_id):
    """★をクリックしたとき"""
    bc = BookComponent(book_id)
    bc.set_good(request)
    count = bc.get_book_good_count()
    return JsonResponse({'good_count': count})
Ejemplo n.º 15
0
def chapter(request, book_id, chapter_id):
    bc = BookComponent(book_id)
    page = bc.get_chapter_top_page(chapter_id)

    return HttpResponseRedirect(reverse('disp_book', args=[book_id, page]))
Ejemplo n.º 16
0
def book(request, book_id, page):
    """
    本を表示する
    :param request:
    :param book_id:
    :param page:
    :return:
    """
    """
    if 'HTTP_REFERER' in request.META:
        print(request.META['HTTP_REFERER'])
        print(request.META['REMOTE_ADDR'])
        print(request.META['OS'])
        print(request.META['HTTP_USER_AGENT'])

        for meta in request.META:
            print("{}/{}".format(meta,request.META[meta]))
    else:
        print("ありません")
    """
    bc = BookComponent(book_id)

    if not bc.is_my_book(request.user):
        is_my_page = False
        print("私のページではありません")
    else:
        is_my_page = True
        print("私のページです")

    bc = BookComponent(book_id)
    if (not is_my_page) and (bc.draft == 0):
        raise Http404("不正なリクエストです")

    total_page = bc.get_page_count()
    if total_page == 0:
        # ページが一個もなかったら空のページを作成する
        bc.create_empty_book()
        total_page = bc.get_page_count()

    # セクションのアクセス数を更新して保存する
    bc.update_access_count(request, page)
    # アクセス数を取得する
    acc = bc.get_book_access_count()

    # ナビゲーション用データの作成
    top_chapter_list = bc.get_chapter_list()
    chapter_list = []
    for chapter_record in top_chapter_list:
        # セクションのうち、見出し1で始まるものを取得する
        sub_chapter_list = bc.get_chapter_in_section(chapter_record)
        chapter_list.append([chapter_record, sub_chapter_list])

    chapter_record, section_record = bc.get_chapter_and_section(page)
    _, prev_section = bc.get_chapter_and_section(page - 1)
    _, next_section = bc.get_chapter_and_section(page + 1)

    # 添付ファイル一覧
    attach_file_list = bc.get_attach_list()

    # ユーザが★を押しているかどうか
    is_press_good = bc.is_already_good(request)
    if is_press_good:
        good_image = 'star_on.png'
    else:
        good_image = 'star_off.png'

    daily_list = []
    if bc.book.article_type == 1:
        daily_list = bc.get_chapter_list(order=1)

    data = {
        'book': bc.book,
        'chapter': chapter_record,
        'chapter_list': chapter_list,
        'attach_file_form': bc.attach_file_form,
        'comment_form': bc.comment_form,
        'acc': acc,
        'prev_page': page - 1,
        'section': section_record,
        'next_page': page + 1,
        'total_page': total_page,
        'now_page': page,
        'attach_file_list': attach_file_list,
        'profile': bc.profile,
        'is_my_page': is_my_page,
        'good_image': good_image,
        'daily_list': daily_list
    }

    access_log = AccessLog()
    access_log.write_log(request, bc.book.user, bc.book, chapter_record,
                         section_record)

    return book_header(request, 'pyarticle/book.html', bc, data)