예제 #1
0
def update_issue(request, publisher_id, periodical_id, issue_id):
    issue = get_object_or_404(Issue, pk=issue_id)
    if request.method == 'POST':
        form = IssueForm(request.POST, request.FILES, instance=issue)
        if form.is_valid():
            form.save()
            if form.cleaned_data.has_key('file_upload'):
                uploaded_file = request.FILES['file_upload']
                try:
                    file_upload = FileUpload.objects.get(
                                        publication_type=Publication.PERIODICAL,
                                        publication_id=issue_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.PERIODICAL,
                                             publication_id=issue_id)
                    pre_filename = 'u%s_p%s_s%s' % (request.user.id, periodical_id, issue_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-issue',
                        publisher_id=publisher_id, periodical_id=periodical_id, issue_id=issue_id)
    else:
        form = IssueForm(instance=issue, initial={'issue_id': issue_id})
    return render(request, 'publication/issue_form.html', {'form': form, 'issue': issue})
예제 #2
0
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})