project/kiwipycon/proceedings/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 11 May 2010 03:48:47 +0530
changeset 96 3ece0de11641
parent 94 e86755df35da
child 101 fb27be005a5c
permissions -rw-r--r--
Modify views and templates for proceedings to work.

  # -*- coding: utf-8 -*-

import os

from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils import simplejson as json

from project.kiwipycon.proceedings.booklet import mk_scipy_paper
from project.kiwipycon.proceedings.forms import ProceedingsForm
from project.kiwipycon.proceedings.models import Paper
from project.kiwipycon.user.forms import RegisterForm
from project.kiwipycon.user.models import UserProfile
from project.kiwipycon.user.utils import kiwipycon_createuser
from project.kiwipycon.utils import set_message_cookie
from project import settings


def handleUploadedFile(proceedings_form_data, rst_file):
    """Handles the uploaded file content and process the form
    """

    title = proceedings_form_data.get('title')
    abstract = proceedings_form_data.get('abstract')
    body = proceedings_form_data.get('body')

    if rst_file:
        # TODO: using stating only for testing purposes.
        destination = open('some/file/name.txt', 'wb+')
        for chunk in rst_file.chunks():
            destination.write(chunk)
        destination.close()

    return title, abstract, body


@login_required
def submit(request, id=None, template='proceedings/submit.html'):
    """View to submit the proceedings paper.
    """

    context = RequestContext(request, {})

    user = request.user
    if user.is_authenticated():
        try:
            profile = user.get_profile()
        except:
            profile, new = UserProfile.objects.get_or_create(user=user)
            if new:
                profile.save()
    message = None

    if request.method == 'POST':
        register_form = RegisterForm(data=request.POST)

        if request.POST.get('action', None) == 'login':
            login_form = AuthenticationForm(data=request.POST)
            if login_form.is_valid():

                login(request, login_form.get_user())

                redirect_to = reverse('kiwipycon_submit_proceedings')
                return set_message_cookie(redirect_to,
                        msg = u'You have been logged in.')

        if request.POST.get('action', None) == 'register':
            # add the new user
            if register_form.is_valid():
                user = kiwipycon_createuser(request, register_form.data)

        proceedings_form = ProceedingsForm(data=request.POST,
                                           files=request.FILES)

        if proceedings_form.is_valid():
            if user.is_authenticated():
                # Data from reSt file is appended to the data in fields
                title, abstract, body = handleUploadedFile(
                    proceedings_form.cleaned_data, request.FILES.get('file'))
                authors = request.POST.get('as_values_authors')

                paper = edit(id, title=title,
                    abstract=abstract, body=body,
                    authors=authors) if id else create(title=title,
                    abstract=abstract, body=body,
                    authors=authors)

                # Successfully saved. So get back to the edit page.
                redirect_to = reverse('kiwipycon_submit_proceedings',
                                  args=[paper.id])
                return set_message_cookie(
                redirect_to, msg = u'Thanks, your paper has been submitted.')
            else:
                # This is impossible. Something was wrong so return back
                # to submit page
                redirect_to = reverse('kiwipycon_submit_proceedings')
                return set_message_cookie(
                redirect_to, msg = u'Something is wrong here.')          
    else:
        if id:
            # If id exists initialize the form with old values
            paper = Paper.objects.get(id=id)
            proceedings_form = ProceedingsForm(
                initial={'title': paper.title,
                         'abstract': paper.abstract,
                         'body': paper.body,
                })
            context['initial_authors'] = json.dumps([
                {'name': str(author.username),
                 'value': '%s (%s)' % (author.get_full_name(), author.username)
                } for author in paper.authors.all()])
        else:
            # Otherwise create a new form
            proceedings_form = ProceedingsForm()

        register_form = RegisterForm()
        login_form = AuthenticationForm()

    context['proceedings_form'] = proceedings_form
    context['register_form'] = register_form
    context['message'] = message
    context['login_form'] = login_form
    context['id'] = id if id else None

    return render_to_response(template, context)


def create(**kwargs):
    """View to create a new proceedings.
    """

    title = kwargs.get('title')
    abstract = kwargs.get('abstract')
    body = kwargs.get('body')
    authors = kwargs.get('authors')

    paper = Paper(title=title, abstract=abstract, body=body)
    paper.save()

    if authors:
        authors = authors.split(',')
        for author in authors:
            user = User.objects.get(username=author.strip())
            paper.authors.add(user)

    return paper


def edit(id, **kwargs):
    """View to edit the proceedings paper.
    """

    paper = Paper.objects.get(id=id)

    paper.title = kwargs.get('title')
    paper.abstract = kwargs.get('abstract')
    paper.body = kwargs.get('body')
    authors = kwargs.get('authors')

    if authors:
        authors = authors.strip(' ,').split(',')
        for author in authors:
            user = User.objects.get(username=author.strip())
            paper.authors.add(user)

    paper.save()

    return paper


def show_paper(request, id):
    """Display the thumbnail of the rendered paper for download
    """

    paper = Paper.objects.get(id=id)

    # TODO: Address and country should be required field in the user forms
    # henceforth. Also contact numbers.
    paper_data = {
      'paper_abstract': paper.abstract,
      'authors': [
          {'first_names': author.first_name,
            'surname': author.last_name,
            'address': 'None',
            'country': 'India',
            'email_address': author.email,
            'institution': author.registration_set.get().organisation,
            'city': author.registration_set.get().city
           } for author in paper.authors.all()],
      'title': paper.title
      }

    abstract = mk_scipy_paper.Bunch(**paper_data)
    abstract.authors = [mk_scipy_paper.Bunch(**a) for a in abstract.authors]

    abstract['paper_text'] = paper.body

    out_dir = attach_dir = os.path.join(settings.STATIC_ROOT,
                                        'proceedings', 'output')
    outfilename = os.path.join(out_dir, 'paper%d.pdf' % (paper.id))
    mk_scipy_paper.mk_abstract_preview(abstract, outfilename, attach_dir)

    context = {'paper_id': paper.id,
               'out_path': '/static/proceedings/output' }
    template = 'proceedings/show_paper_preview.html'

    return render_to_response(template, context)