Esempio n. 1
0
def book_edit(request, book_id=None):
    # return HttpResponse('도서 수정')

    if book_id:
        book = get_object_or_404(Book, pk=book_id)

    else:
        book = Book()

    if request.method == 'POST':  # POST
        # POST된 request 데이터를 가지고 Form 생성
        form = BookForm(request.POST, instance=book)
        # save()
        if form.is_valid():
            writer = request.session['user_id']
            book.publisher = writer
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')

    else:  # GET
        # book instance에서 Form 생성
        form = BookForm(instance=book)
        return render(request, 'cms/book_edit.html',
                      dict(form=form, book_id=book_id))
Esempio n. 2
0
def book_search(request):
    """"書籍の検索"""
    # return HttpResponse('書籍の検索')
    book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            # print(type(book))
            # print(type(str(book)))
            search = str(book).split()
            # print(search)
            if len(search) > 1:
                isbn = search[1]
                isbn = isbn.replace("-", "")
                headers = {"content-type": "application/json"}
                res = requests.get('https://www.googleapis.com/books/v1/volumes?q=isbn:%s' % isbn, headers=headers)
                data = res.json()
                if data['totalItems'] > 0:
                    title = data['items'][0]['volumeInfo']['title']
                    publish_date = data['items'][0]['volumeInfo']['publishedDate']
                    isbn = data['items'][0]['volumeInfo']['industryIdentifiers'][1]['identifier']
                    authors = data['items'][0]['volumeInfo']['authors']
                    page =  data['items'][0]['volumeInfo']['pageCount']
                    form = BookForm(initial={'name': title, 'publisher': authors, 'page': page, 'publish_date': publish_date, 'isbn': isbn})
                    # return redirect('cms:book_search_result')
                    return render(request, 'cms/book_edit.html', {'form': form,})
            elif len(search) == 1:
                title = search[0]
                headers = {"content-type": "application/json"}
                res = requests.get('https://www.googleapis.com/books/v1/volumes?q=%s' % title, headers=headers)
                data = res.json()
                books = []
                if data['totalItems'] > 0:
                    for i in range(len(data['items'])):
                        try:
                            name = data['items'][i]['volumeInfo']['title']
                            author = data['items'][i]['volumeInfo']['authors']
                            pagecount = data['items'][i]['volumeInfo']['pageCount']
                            publish_date = data['items'][i]['volumeInfo']['publishedDate']
                            isbn = data['items'][i]['volumeInfo']['industryIdentifiers'][1]['identifier']
                            info = {'name': name, 'author': author, 'pageCount': pagecount, 'publish_date': publish_date, 'isbn': isbn}
                            books.append(info)
                        except:
                            continue
                    # booksの中の辞書配列の内容をclass Bookに入れて返したい
                    # books = BookForm(initial={'name': title, 'publisher': authors, 'page': page, 'publish_date': publish_date, 'isbn': isbn})

                    # return redirect('cms:book_search_result')
                    return render(request, 'cms/book_search_list.html', {'books': books,})

            else:
                form = BookForm(instance=book)
    else:
        form = BookForm(instance=book)

    return render(request, 'cms/book_search.html', dict(form=form))
Esempio n. 3
0
def book_edit(request, book_id=None):
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        form = BookForm(instance=book)

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 4
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    if book_id:  # book_id が指定されている (修正時)
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_id が指定されていない (追加時)
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)  # POST された request データからフォームを作成
        if form.is_valid():  # フォームのバリデーション
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:  # GET の時
        form = BookForm(instance=book)  # book インスタンスからフォームを作成

    return render(request, 'cms/book_edit.html', dict(form=form, book_id=book_id))
Esempio n. 5
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    if book_id:  # book_id が指定されている (修正時)
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_id が指定されていない (追加時)
        book = Book()
    if request.method == 'POST':
        form = BookForm(request.POST,
                        instance=book)  # POST された request データからフォームを作成
        if form.is_valid():  # フォームのバリデーション
            form.save()
            return redirect('cms:book_list')
    else:  # GET の時
        form = BookForm(instance=book)  # book インスタンスからフォームを作成

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 6
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        form = BookForm(instance=book)

    return render(request, 'cms/book_edit.html', dict(form=form, book_id=book_id))
Esempio n. 7
0
def book_edit(request, book_id=None):
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == "POST":
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect("cms:book_list")
    else:
        form = BookForm(instance=book)

    return render_to_response(
        "cms/book_edit.html", dict(form=form, book_id=book_id), context_instance=RequestContext(request)
    )
Esempio n. 8
0
def book_edit(request, book_id=None):
	"""書籍の編集"""
	# return HttpResponse("書籍の編集")
	if book_id: #book_id が指定されている(フォーム修正時)
		book = get_object_or_404(Book, pk=book_id)
	else: # book_idが指定されていない(フォームに追加時)
		book = Book()

	if request.method=="POST": # POSTの時
		form = BookForm(request.POST, instance=book)  # POSTされたrequestデータからフォームを生成
		if form.is_valid():
			book = form.save(commit=False)
			book.save()
			return redirect("cms:book_list")
	else: # GETの時
		form = BookForm(instance=book) # bookインスタンスからフォームを生成

	return render(request, "cms/book_edit.html", dict(form=form, book_id=book_id))
Esempio n. 9
0
def book_edit(request, book_id=None):
    # return HttpResponse('書籍の編集')
    if book_id:  # 書籍の修正
        book = get_object_or_404(Book, pk=book_id)
    else:  # 書籍の追加
        book = Book()

        # POSTでもGETでもformを作る
    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)  # POSTされたデータからform作成
        if form.is_valid():  # エラーがなければ保存 -> 一覧へ
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:  # GETの時
        form = BookForm(instance=book)

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 10
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    # return HttpResponse('書籍の編集')
    if book_id:  # book_idが指定されている
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_idが指定されていない
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        form = BookForm(instance=book)

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 11
0
def book_edit(request, book_id=None):
    #return HttpResponse(u"書籍の編集")
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()
    #
    if request.method=="POST":
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        form = BookForm(instance=book)
    #
    return render_to_response('cms/book_edit.html',
                              dict(form=form, book_id=book_id),
                              context_instance=RequestContext(request))
Esempio n. 12
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    # return HttpResponse('書籍の編集')
    if book_id:  # book_id が指定されている(修正時)
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_id が指定されていない(追加時)
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST,
                        instance=book)  # POST された request データからフォームを作成
        if form.is_valid():  # フォームのバリデーション
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:  # GET の時
        form = BookForm(instance=book)  # book インスタンスからフォームを作成

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 13
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    # Book ID is specified (to modify)
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        # Validation of form
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        # Create an instance from BookForm
        form = BookForm(instance=book)

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 14
0
def book_edit(request, book_id=None):
    '''書籍の編集'''
    # return HttpResponse(u'書籍の編集')
    if book_id:  # book_id が指定されている (修正時)
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_id が指定されていない (追加時)
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST,
                        instance=book)  # POST された request データからフォームを作成
        if form.is_valid():  # フォームのバリデーション
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:  # GET の時
        form = BookForm(instance=book)  # book インスタンスからフォームを作成

    return render_to_response('cms/book_edit.html',
                              dict(form=form, book_id=book_id),
                              context_instance=RequestContext(request))
Esempio n. 15
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
        if form.is_valid():
            # (質問)直接保存しない意図は?
            book = form.save(commit=False)
            book.save()
            return redirect('cms:book_list')
    else:
        form = BookForm(instance=book)

    return render(request,
                  'cms/book_edit.html',
                  # book_id、edit時のためにそのままtemplateへ返してる。
                  # 編集を埋めでやるのではなく、formのactionのリンクをmod/add分岐させてる。
                  dict(form=form, book_id=book_id),
    )
Esempio n. 16
0
def book_edit(request, book_id=None):
    # return HttpResponse('도서 수정')

    if book_id:
        book = get_object_or_404(Book, pk=book_id)
    else:
        book = Book()

    if request.method == 'POST':  # POST
        # POST된 request 데이터를 가지고 Form 생성
        form = BookForm(request.POST, instance=book)
        # save()
        if form.is_valid():
            book = form.save(commit=False)
            book.save()
            return redirect(
                'cms:book_list')  #단순히 페이지 요청만 필요하다. request정보 안가지고 감.

    else:  # GET
        # book instance에서 Form 생성
        form = BookForm(instance=book)
        return render(request, 'cms/book_edit.html',
                      dict(form=form, book_id=book_id))
Esempio n. 17
0
def book_edit(request, book_id=None):
    """書籍の編集"""
    if book_id:
        # book_id が指定されているとき、修正する Book オブジェクトを取得
        book = get_object_or_404(Book, pk=book_id)
    else:
        # book_id が指定されていないとき、新しく追加する Book オブジェクトを作成
        book = Book()

    if request.method == 'POST':
        # POST されたデータからフォームを作成
        form = BookForm(request.POST, instance=book)
        if form.is_valid():     # フォームのバリデーション
            # Book オブジェクトを保存
            book = form.save(commit=False)
            book.save()
            # 書籍一覧のページにリダイレクト
            return redirect('cms:book_list')
    else:
        # GET のとき、Book インスタンスからフォームを作成
        form = BookForm(instance=book)

    # 編集ページを出力
    return render(request, 'cms/book_edit.html', dict(form=form, book_id=book_id))
Esempio n. 18
0
def book_edit(request, book_id=None):
    pprint('リクエストの表示')
    pprint(request)
    """書籍の編集"""
    #     return HttpResponse('書籍の編集')
    if book_id:  # book_id が指定されている (修正時)
        # get_object_or_404はdjangoの関数
        book = get_object_or_404(Book, pk=book_id)
    else:  # book_id が指定されていない (追加時)
        book = Book()

    if request.method == 'POST':
        form = BookForm(request.POST,
                        instance=book)  # POST された request データからフォームを作成
        if form.is_valid():  # フォームのバリデーション
            book = form.save(commit=False)
            book.save()
            return redirect(
                'cms:book_list')  #urlsに定義しているnameでリダイレクト先を指定することができる
    else:  # GET の時
        form = BookForm(instance=book)  # book インスタンスからフォームを作成

    return render(request, 'cms/book_edit.html',
                  dict(form=form, book_id=book_id))
Esempio n. 19
0
 def test_valid(self):
     """正常な入力を行えばエラーにならないことを検証"""
     params = dict(name='書籍タイトル', publisher='出版社', page=0)
     book = Book()  # book_idの指定なし(追加時)
     form = BookForm(params, instance=book)
     self.assertTrue(form.is_valid())
 def test_either1(self):
     """何も入力しなければエラー"""
     params = dict()
     book = Book()
     form = BookForm(params, instance=book)
     self.assertFalse(form.is_valid())
 def test_valid(self):
     """正常な入力を行えばエラーにならない"""
     params = dict(name='title', publisher="publisher", page=114514)
     book = Book()
     form = BookForm(params, instance=book)
     self.assertTrue(form.is_valid())
Esempio n. 22
0
 def test_either1(self):
     """何も入力しなければエラーになることを検証"""
     params = dict()
     book = Book()  # book_idの指定なし(追加時)
     form = BookForm(params, instance=book)
     self.assertFalse(form.is_valid())