def book_create(request): if request.method == 'POST': form = BookForm(request.POST) else: form = BookForm() return save_book_form(request, form, 'books/includes/partial_book_create.html')
def create_book(request): book_form = BookForm(request.POST) if book_form.is_valid(): return initialize_book(book_form) return create(request, book_form=book_form)
def book_update(request, pk): book = get_object_or_404(Book, pk=pk) if request.method == 'POST': form = BookForm(request.POST, instance=book) else: form = BookForm(instance=book) return save_book_form(request, form, 'books/includes/partial_book_update.html')
def createBook(request): form = BookForm() context = {'form': form} if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): Book.create(request.POST['name'], request.POST['description'], request.POST['count'], request.POST['authors']) return redirect("/books") return render(request, 'book_form.html', context)
def edit(request, pk): book = Book.objects.get(pk=pk) if request.method == 'GET': book_form = BookForm(initial=book.__dict__) return create(request, book_form, 'edit', pk=pk) else: book_form = BookForm(request.POST) if book_form.is_valid(): initialize_book(book_form, pk) return redirect(index) return create(request, book_form=book_form, form_action='edit', pk=pk)
def book(request): if request.method == "GET": books = Book.objects.all() context = {'form': BookForm, 'book1': books} return render(request, 'book.html', context) else: form = BookForm(request.POST) if form.is_valid(): data = form.save() data.save() return redirect('book') return render(request, 'book.html')
def update_book(request, book_id): book_id = int(book_id) try: book_query = Book.objects.get(id=book_id) except Book.DoesNotExist: return redirect('index') book_form = BookForm(request.POST or None, instance=book_query) if book_form.is_valid(): book_form.save() return redirect('index') return render(request, 'book/upload_form.html', {'book_form': book_form})
def get(self, request): book_form = BookForm() context = { 'title': 'New Book', 'book_form': book_form, } return render(request, 'book/book_page/create.html', context)
def get(self, request, book_id): book = get_object_or_404(Book, pk=book_id) book_form = BookForm(instance=book) context = { 'title': 'Edit Book', 'book_form': book_form, } return render(request, 'book/book_page/create.html', context)
def create(request, book_form=None, form_action='create_book', pk=None): if not book_form: book_form = BookForm() context = { 'book_form': book_form, 'form_action': form_action, 'pk': pk, } return render(request, 'create.html', context)
def export_excel(request): if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): response = HttpResponse(content_type="application/ms-excel") response['Content-Disposition'] = 'attachment; filename=template.xls' rb = open_workbook( filename='resource/template.xls', formatting_info=True) wb = copy(rb) s = wb.get_sheet(0) cd = form.cleaned_data s.write(0, 0, "BOOK NAME") s.write(0, 1, "AUTHOR") s.write(1, 0, cd['book_name']) s.write(1, 1, cd['author']) wb.save(response) return response return render(request, 'book_form.html', {'form': BookForm()})
def updateBook(request, pk): book = Book.get_by_id(pk) form = BookForm(instance=book) if request.method == 'POST': form = BookForm(request.POST, instance=book) if form.is_valid(): form.save() return redirect("/books") context = {'form': form} return render(request, 'book_form.html', context)
def add_book(request): template_name = 'book/add_book.html' data={} if request.method == "POST": form = BookForm(request.POST, request.FILES) if form.is_valid(): user = User.objects.get(username=request.user.username) user_profile = UserProfile.objects.get(user=user) user_book = UserBook.objects.get(user=user_profile) title = form.cleaned_data.get('title') author = form.cleaned_data.get('author') editorial = form.cleaned_data.get('editorial') book_type = form.cleaned_data.get('book_type') genre = form.cleaned_data.get('genre') language = form.cleaned_data.get('language') original = form.cleaned_data.get('original') transaction = form.cleaned_data.get('transaction') price = form.cleaned_data.get('price') comment = form.cleaned_data.get('comment') number_of_pages = form.cleaned_data.get('number_of_pages') picture = form.cleaned_data.get('picture') book = Book.objects.create(user=user_book, title=title, author=author, editorial=editorial, book_type=book_type, genre=genre, language=language, original=original, transaction=transaction, price=price, comment=comment, number_of_pages=number_of_pages, picture=picture) book.save return redirect('list_book') else: form = BookForm() data['form'] = form return render(request, template_name, data)
def create_book(request): global form if request.method == 'POST': form = BookForm(request.POST) is_valid = form.is_valid() if is_valid: form.save() return HttpResponseRedirect('/books/list/') elif request.method == 'GET': form = BookForm() context = {'book_form': form} return render(request, 'create_book.html', context=context)
def update_book(request, pk): book = get_object_or_404(Book, pk=pk) if request.method == 'POST': form = BookForm(request.POST, instance=book) is_valid = form.is_valid() if is_valid: form.save() return HttpResponseRedirect('/books/list/') elif request.method == 'GET': form = BookForm(instance=book) context = {'book_form': form} return render(request, 'create_book.html', context=context)
def create(request): book_form = BookForm() if request.method == 'POST': book_form = BookForm(request.POST, request.FILES) if book_form.is_valid(): book_form.save() return redirect('index') else: return HttpResponse("Something wrong with your form!") else: return render(request, 'book/upload_form.html', {'book_form': book_form})
def create(request): print(request.POST) if request.method=="POST": form=BookForm(request.POST) form.save() return redirect("/book") else: form=BookForm() return render(request,"book/create.html",{'form':form})
def update(request,id): book=Book.objects.get(book_id=id) if request.method=="POST": form=BookForm(request.POST,instance=book) form.save() return redirect("/book") else: form=BookForm(instance=book) return render(request,"book/edit.html",{'form':form})
def book_edit(request, book_id=None): # 書籍の編集 if book_id: book = get_object_or_404(Book, pk=book_id) else: book = Book() # POST リクエスト # POST されたデータからフォームを作成する if request.method == 'POST': form = BookForm(request.POST, instance=book) if form.is_valid(): book = form.save(commit=False) book.save() return redirect('book_list') # GET リクエストの時 else: form = BookForm(instance=book) return render_to_response('book_edit.html', dict(form=form, book_id=book_id), context_instance=RequestContext(request))
def createBook(request): if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): form.save(commit=True) #messages.success(request, 'book successfully registered') return HttpResponseRedirect(reverse('getAllBooks')) else: #messages.error(request, 'fill out the form correctly') return HttpResponseRedirect(reverse('createBook')) form = BookForm() context = {'form': form} return render(request, 'book/create.html', context=context)
def bookUpdate(request, bookId): book = get_object_or_404(Book, id=bookId) template = 'book/bookCreateUpdate.html' if request.method == 'GET': bookForm = BookForm(instance=book) return render(request, template, {'bookForm': bookForm}) # POST bookForm = BookForm(request.POST, instance=book) if not bookForm.is_valid(): return render(request, template, {'bookForm': bookForm}) bookForm.save() messages.success(request, '文章已修改') return redirect('book:bookRead', bookId=bookId)
def post(self, request, book_id): book = get_object_or_404(Book, pk=book_id) book_form = BookForm(request.POST or None, request.FILES or None, instance=book) if book_form.is_valid(): instance = book_form.save(commit=False) instance.save() book_form.save_m2m() return HttpResponseRedirect(instance.get_absolute_url()) return self.get(request, book_id)
def edit_book(request,id): template_name = "book/add_book.html" data = {} book = Book.objects.get(id=id) if request.method == "GET": data['form'] = BookForm(instance=book) else: form_book= BookForm(request.POST,request.FILES, instance=book) if form_book.is_valid(): form_book.save() return redirect('list_book') return render(request, template_name, data)
def post(self, request): book_form = BookForm(request.POST or None, request.FILES or None) if book_form.is_valid(): instance = book_form.save(commit=False) instance.save() book_form.save_m2m() return HttpResponseRedirect(instance.get_absolute_url()) context = { 'title': 'New Book', 'book_form': book_form, } return render(request, 'book/book_page/create.html', context)
def add_book(request): context = dict() if request.method == 'POST': if 'url' in request.POST: url = request.POST['url'] context = parse_book_by_url(url) return render(request, 'book/add.html', context) if 'bookid' in request.POST: bookid = request.POST['bookid'] context = parse_book_by_id(bookid) book = Book() book.title = context['title'] book.publisher = context['publisher'] book.isbn = context['isbn10'] if 'isbn10' in context else context[ 'isbn13'] book.url = context['url'] book.ispersonal = int(True) book.ownerid = request.user.id book.summary = context['summary'] book.price = context['price'] book.numraters = 0 book.averageRate = 3 book.created_date = datetime.datetime.now() book.updated_date = datetime.datetime.now() book.author = context['author'][0] book.pubdate = context['pubdate'] book.ispublic = int(True) book.imgurl = context['images'] book.save() #save tags of book for val in context['tags']: tag = Tag() tag.value = val tag.createdate = datetime.datetime.now() tag.save() rel = Booktag() rel.tagid = tag.id rel.bookid = book.id rel.save() return redirect('/book/library', context) else: form = BookForm() context['form'] = form return render(request, 'book/add.html', context)
def add_book(request, id): if request.method == "GET": books = Book.objects.get(id=id) form = BookForm(instance=books) context = {'form': form} return render(request, 'add_book.html', context) else: i = int(request.POST.get("includeid")) form = BookForm(request.POST, id) books = Book.objects.get(id=id) books.available = F('available') + i books.stock = F('stock') + i books.save() if form.is_valid(): form.save() return redirect('book') return render(request, 'book.html')
def updateBook(request, idBook): try: book = Book.objects.get(pk=idBook) except ObjectDoesNotExist: #messages.error(request, 'book does not exist') return HttpResponseRedirect(reverse('getAllBooks')) if request.method == 'POST': form = BookForm(request.POST, instance=book) if form.is_valid(): form.save(commit=True) #messages.success(request, 'book updated successfully') return HttpResponseRedirect(reverse('getAllBooks')) else: #messages.error(request, 'fill out the form correctly') return HttpResponseRedirect(reverse('updateBook', args=[idBook])) form = BookForm(instance=book) context = {'form': form, 'book': book} return render(request, 'book/update.html', context=context)
def create(request): if request.method == "GET": context = { "form": BookForm(), } return render(request, "create.html", context) else: form = BookForm(request.POST) if form.is_valid(): form.save() return redirect("index") context = { "form": form, } return render(request, "create.html", context)
def edit(request, pk): book = BookModel.objects.get(pk=pk) if request.method == "GET": form = BookForm(instance=book) context = { "form": form, } return render(request, "edit.html", context) else: form = BookForm(request.POST, instance=book) if form.is_valid(): form.save() return redirect("index") context = { "form": form, } return render(request, "edit.html", context)
def bookCreate(request): ''' Create a new article instance 1. If method is GET, render an empty form 2. If method is POST, perform form validation and display error messages if the form is invalid 3. Save the form to the model and redirect the user to the article page ''' template = 'book/bookCreateUpdate.html' if request.method == 'GET': return render(request, template, {'bookForm': BookForm()}) # POST bookForm = BookForm(request.POST) if not bookForm.is_valid(): return render(request, template, {'bookForm': bookForm}) bookForm.save() messages.success(request, '文章已新增') return redirect('book:book')
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('book:book_list') else: # GET の時 form = BookForm(instance=book) # book インスタンスからフォームを作成 return render(request, 'book/book_edit.html', dict(form=form, book_id=book_id))
def save_js(request): date_format = '%d/%m/%Y' response = {} status = 405 if request.is_ajax() and request.method == 'POST': the_book = json.loads(request.POST['the_book']) the_chapters = the_book.pop('chapters') # if the_book['cover_image']==False: # the_book.pop('cover_image') the_book['metadata'] = json.dumps(the_book['metadata']) the_book['settings'] = json.dumps(the_book['settings']) if the_book['id'] == 0: # We are dealing with a new book that still has not obtained an # ID. the_book['owner'] = request.user.pk # Now we check the augmented form against the modelform form = BookForm(the_book) if form.is_valid(): # The form was valid, so we save the instance in the database, # and return the id that was assigned back to the client. form.save() status = 201 the_book['id'] = form.instance.id response['id'] = the_book['id'] date_obj = dateutil.parser.parse(str(form.instance.added)) response['added'] = date_obj.strftime(date_format) date_obj = dateutil.parser.parse(str(form.instance.updated)) response['updated'] = date_obj.strftime(date_format) status = add_chapters(form.instance, the_chapters, status, request.user) else: response['errors'] = form.errors else: book = Book.objects.get(pk=the_book['id']) the_book['owner'] = book.owner.id the_book['updated'] = timezone.now() if book.owner == request.user: form = BookForm(the_book, instance=book) if form.is_valid(): form.save() status = 200 date_obj = dateutil.parser.parse(str( form.instance.updated)) response['updated'] = date_obj.strftime(date_format) form.instance.chapter_set.all().delete() status = add_chapters(form.instance, the_chapters, status, request.user) else: response['errors'] = form.errors status = 422 else: # We are not dealing with the owner, so we need to check if the # current user has the right to save the book if len( book.bookaccessright_set.filter(user=request.user, rights=u'write')) > 0: form = BookForm(the_book, instance=book) if form.is_valid(): form.save() date_obj = dateutil.parser.parse( str(form.instance.updated)) response['updated'] = date_obj.strftime(date_format) status = 200 form.instance.chapter_set.all().delete() status = add_chapters(form.instance, the_chapters, status, request.user) else: status = 422 else: status = 403 return JsonResponse(response, status=status)
def save_js(request): date_format = '%d/%m/%Y' response = {} status = 405 if request.is_ajax() and request.method == 'POST': the_book = json.loads(request.POST['the_book']) the_chapters = the_book.pop('chapters') # if the_book['cover_image']==False: # the_book.pop('cover_image') the_book['metadata']=json.dumps(the_book['metadata']) the_book['settings']=json.dumps(the_book['settings']) if the_book['id'] == 0: # We are dealing with a new book that still has not obtained an # ID. the_book['owner'] = request.user.pk # Now we check the augmented form against the modelform form = BookForm(the_book) if form.is_valid(): # The form was valid, so we save the instance in the database, # and return the id that was assigned back to the client. form.save() status = 201 the_book['id'] = form.instance.id response['id'] = the_book['id'] date_obj = dateutil.parser.parse(str(form.instance.added)) response['added'] = date_obj.strftime(date_format) date_obj = dateutil.parser.parse(str(form.instance.updated)) response['updated'] = date_obj.strftime(date_format) status = add_chapters(form.instance, the_chapters, status, request.user) else: response['errors'] = form.errors else: book = Book.objects.get(pk=the_book['id']) the_book['owner']=book.owner.id the_book['updated'] = timezone.now() if book.owner==request.user: form = BookForm(the_book,instance=book) if form.is_valid(): form.save() status = 200 date_obj = dateutil.parser.parse(str(form.instance.updated)) response['updated'] = date_obj.strftime(date_format) form.instance.chapter_set.all().delete() status = add_chapters(form.instance,the_chapters, status, request.user) else: response['errors'] = form.errors status = 422 else: # We are not dealing with the owner, so we need to check if the # current user has the right to save the book if len(book.bookaccessright_set.filter(user=request.user,rights=u'w'))>0: form = BookForm(the_book,instance=book) if form.is_valid(): form.save() date_obj = dateutil.parser.parse(str(form.instance.updated)) response['updated'] = date_obj.strftime(date_format) status = 200 form.instance.chapter_set.all().delete() status = add_chapters(form.instance,the_chapters, status, request.user) else: status = 422 else: status = 403 return HttpResponse( json.dumps(response), content_type = 'application/json; charset=utf8', status=status )
def save_js(request): date_format = "%d/%m/%Y" response = {} status = 405 if request.is_ajax() and request.method == "POST": the_book = json.loads(request.POST["the_book"]) the_chapters = the_book.pop("chapters") # if the_book['cover_image']==False: # the_book.pop('cover_image') the_book["metadata"] = json.dumps(the_book["metadata"]) the_book["settings"] = json.dumps(the_book["settings"]) if the_book["id"] == 0: # We are dealing with a new book that still has not obtained an # ID. the_book["owner"] = request.user.pk # Now we check the augmented form against the modelform form = BookForm(the_book) if form.is_valid(): # The form was valid, so we save the instance in the database, # and return the id that was assigned back to the client. form.save() status = 201 the_book["id"] = form.instance.id response["id"] = the_book["id"] date_obj = dateutil.parser.parse(str(form.instance.added)) response["added"] = date_obj.strftime(date_format) date_obj = dateutil.parser.parse(str(form.instance.updated)) response["updated"] = date_obj.strftime(date_format) status = add_chapters(form.instance, the_chapters, status, request.user) else: response["errors"] = form.errors else: book = Book.objects.get(pk=the_book["id"]) the_book["owner"] = book.owner.id the_book["updated"] = timezone.now() if book.owner == request.user: form = BookForm(the_book, instance=book) if form.is_valid(): form.save() status = 200 date_obj = dateutil.parser.parse(str(form.instance.updated)) response["updated"] = date_obj.strftime(date_format) form.instance.chapter_set.all().delete() status = add_chapters(form.instance, the_chapters, status, request.user) else: response["errors"] = form.errors status = 422 else: # We are not dealing with the owner, so we need to check if the # current user has the right to save the book if len(book.bookaccessright_set.filter(user=request.user, rights=u"write")) > 0: form = BookForm(the_book, instance=book) if form.is_valid(): form.save() date_obj = dateutil.parser.parse(str(form.instance.updated)) response["updated"] = date_obj.strftime(date_format) status = 200 form.instance.chapter_set.all().delete() status = add_chapters(form.instance, the_chapters, status, request.user) else: status = 422 else: status = 403 return JsonResponse(response, status=status)