def update_book(request, publisher_id, book_id): book = get_object_or_404(Book, pk=book_id) if request.method == 'POST': form = BookForm(request.POST, request.FILES, instance=book) if form.is_valid(): form.save() book.save_categories(request.POST) if form.cleaned_data.has_key('file_upload'): uploaded_file = request.FILES['file_upload'] try: file_upload = FileUpload.objects.get( publication_type=Publication.BOOK, publication_id=book_id) pre_filename = file_upload.uploaded_file.name post_filename = _complete_filename(uploaded_file.name, pre_filename, update=True) except FileUpload.DoesNotExist: file_upload = FileUpload(publication_type=Publication.BOOK, publication_id=book_id) pre_filename = 'u%s_b%s' % (request.user.id, book_id) post_filename = _complete_filename(uploaded_file.name, pre_filename) file_upload.uploader = request.user file_upload.uploaded_file.save(post_filename, uploaded_file) file_upload.save() return redirect('publication-show-book', publisher_id=publisher_id, book_id=book_id) else: form = BookForm(instance=book, initial={'book_id': book_id}) categories = Category.objects.all() book_categories = [] for category in book.categories.all(): book_categories.append(category.id) return render(request, 'publication/book_form.html', {'form': form, 'book': book, 'book_categories': book_categories, 'categories': categories})
def create_book(request, publisher_id): if request.method == 'POST': form = BookForm(request.POST, request.FILES) if form.is_valid(): book = form.save(commit=False) book.publisher = Publisher.objects.get(pk=publisher_id) book.save() book.save_categories(request.POST) uploaded_file = request.FILES['file_upload'] pre_filename = 'u%s_b%s' % (request.user.id, book.id) post_filename = _complete_filename(uploaded_file.name, pre_filename) file_upload = FileUpload.objects.create( uploader=request.user, publication_type=Publication.BOOK, publication_id=book.id) file_upload.uploaded_file.save(post_filename, uploaded_file) return redirect('publication-show-book', publisher_id=publisher_id, book_id=book.id) else: form = BookForm() categories = Category.objects.all() publisher = get_object_or_404(Publisher, pk=publisher_id) return render(request, 'publication/book_form.html', {'form': form, 'publisher': publisher, 'categories': categories})