def edit_paper(request, paper_id):
    """
    Allow paper submitters to edit their paper. Only the user that submitted
    the paper is allowed to edit it.
    """
    try:
        paper = Paper.objects.get(id=paper_id)
    except Paper.DoesNotExist:
        return HttpResponseRedirect(reverse("django_conference_home"))
    notice = ""
    paper_form = None
    if paper.submitter != request.user:
        email = settings.DJANGO_CONFERENCE_CONTACT_EMAIL
        notice = 'According to our records, you are not the submitter for '+\
            'the paper "%s" and thus may not edit it. If you believe this '+\
            'to be an error, please contact <a href="mailto:%s">%s</a>.'
        notice = notice % (unicode(paper), email, email)
    else:
        paper_form = PaperForm(request.POST or None, instance=paper)
        if request.POST and paper_form.is_valid():
            paper_form.save(request.user)
            notice = "Your paper has been updated"

    return render_to_response('django_conference/edit_paper.html', {
        'paper_form': paper_form,
        'notice': notice,
    }, context_instance=RequestContext(request))
def submit_session_papers(request):
    meeting = Meeting.current()
    if not meeting.can_submit_session() or \
        'session_data' not in request.session:
        return HttpResponseRedirect(reverse("django_conference_home"))

    session_data = request.session['session_data']
    num = int(session_data['num_papers'])
    forms = []
    for i in range(num):
        forms.append(PaperPresenterForm(request.POST or None, prefix=i))
        forms.append(PaperForm(request.POST or None, prefix=i))

    if request.POST and all([x.is_valid() for x in forms]):
        session_form = SessionForm(session_data)
        session = session_form.save(meeting=meeting, submitter=request.user)

        organizer_form = SessionCadreForm(session_data)
        organizer = organizer_form.save()
        session.organizers.add(organizer)

        chair_form = SessionCadreForm(session_data, prefix="chair")
        chair = chair_form.save()
        session.chairs.add(chair)

        commentator_form = SessionCadreForm(session_data, prefix="commentator")
        commentator_form.is_valid()
        if commentator_form.has_entered_info():
            commentator = commentator_form.save()
            session.commentators.add(commentator)

        position = 1
        for i in range(0, num * 2, 2):
            paper_presenter = forms[i].save()
            paper = forms[i + 1].save(request.user, paper_presenter)
            SessionPapers.objects.create(session=session, paper=paper,
                position=position)
            position += 1

        session.save()
        session.send_submission_email()
        kwargs = {'id': session.id}
        url = reverse('django_conference_submission_success', kwargs=kwargs)
        return HttpResponseRedirect(url)

    return render_to_response('django_conference/submit_paper.html', {
        'forms': forms,
        'meeting': meeting,
    }, context_instance=RequestContext(request))
def submit_paper(request):
    """
    Allow users to submit a paper for approval
    """
    meeting = current_meeting_or_none()
    if not meeting or not meeting.can_submit_paper():
        return HttpResponseRedirect(reverse("django_conference_home"))

    paper_form = PaperForm(request.POST or None)
    paper_presenter_form = PaperPresenterForm(request.POST or None)

    if request.POST and paper_form.is_valid() and \
        paper_presenter_form.is_valid():
        presenter = paper_presenter_form.save()
        paper = paper_form.save(request.user, presenter)
        paper.send_submission_email()
        kwargs = {'id': paper.id}
        url = reverse('django_conference_submission_success', kwargs=kwargs)
        return HttpResponseRedirect(url)

    return render_to_response('django_conference/submit_paper.html', {
        'forms': [paper_presenter_form, paper_form],
        'meeting': meeting,
    }, context_instance=RequestContext(request))