Exemplo n.º 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})
Exemplo n.º 2
0
def create_issue(request, publisher_id, periodical_id):
    periodical = get_object_or_404(Periodical, pk=periodical_id)
    if request.method == 'POST':
        form = IssueForm(request.POST, request.FILES)
        if form.is_valid():
            issue = form.save(commit=False)
            issue.periodical = periodical
            issue.save()

            uploaded_file = request.FILES['file_upload']
            pre_filename = 'u%d_p%d_s%d' % (request.user.id, periodical.id, issue.id)
            post_filename = _complete_filename(uploaded_file.name, pre_filename)
            file_upload = FileUpload.objects.create(uploader=request.user,
                                publication_type=Publication.PERIODICAL,
                                publication_id=issue.id)
            file_upload.uploaded_file.save(post_filename, uploaded_file)
            return redirect('publication-show-issue',
                        publisher_id=publisher_id, periodical_id=periodical_id, issue_id=issue.id)
    else:
        form = IssueForm()
    return render(request, 'publication/issue_form.html', {'form': form, 'periodical': periodical})