summaryrefslogtreecommitdiff
path: root/project/kiwipycon/proceedings/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'project/kiwipycon/proceedings/views.py')
-rw-r--r--project/kiwipycon/proceedings/views.py169
1 files changed, 139 insertions, 30 deletions
diff --git a/project/kiwipycon/proceedings/views.py b/project/kiwipycon/proceedings/views.py
index 3ebdd04..6a21691 100644
--- a/project/kiwipycon/proceedings/views.py
+++ b/project/kiwipycon/proceedings/views.py
@@ -1,20 +1,46 @@
-# -*- coding: utf-8 -*-
+ # -*- 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 project.kiwipycon.proceedings.models import Paper
from project.kiwipycon.user.forms import RegisterForm
from project.kiwipycon.user.models import UserProfile
+from project.kiwipycon.utils import set_message_cookie
+from project.kiwipycon.proceedings.booklet import mk_scipy_paper
from project.kiwipycon.proceedings.forms import ProceedingsForm
+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')
+ authors = proceedings_form_data.get('authors')
+
+ if rst_file:
+ destination = open('some/file/name.txt', 'wb+')
+ for chunk in rst_file.chunks():
+ destination.write(chunk)
+ destination.close()
+
+ return title, abstract, body, authors
+
+
@login_required
-def submit(request, template = 'proceedings/submit.html'):
+def submit(request, id=None, template='proceedings/submit.html'):
"""View to submit the proceedings paper.
"""
+
user = request.user
if user.is_authenticated():
try:
@@ -26,10 +52,7 @@ def submit(request, template = 'proceedings/submit.html'):
message = None
if request.method == 'POST':
- proceedings_form = ProceedingsForm(data=request.POST)
-
- register_form = RegisterForm(data=request.POST,
- files=request.FILES)
+ register_form = RegisterForm(data=request.POST)
if request.POST.get('action', None) == 'login':
login_form = AuthenticationForm(data=request.POST)
@@ -47,28 +70,49 @@ def submit(request, template = 'proceedings/submit.html'):
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():
- title = proceedings_form.data.get('title')
-
- # Saved, ... redirect back to account
- redirect_to = reverse('kiwipycon_account')
- return set_message_cookie(redirect_to,
- msg = u'Thanks, your paper has been submitted.')
+ # Data from reSt file is appended to the data in fields
+ title, abstract, body, authors = handleUploadedFile(
+ proceedings_form.cleaned_data, request.FILES.get('file'))
+
+ 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.')
-
+ return set_message_cookie(
+ redirect_to, msg = u'Something is wrong here.')
else:
- proceedings_form = ProceedingsForm()
- register_form = RegisterForm()
- login_form = AuthenticationForm()
+ 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,
+ 'authors': ', '.join([
+ author.username for author in paper.authors.all()])
+ })
+ else:
+ # Otherwise create a new form
+ proceedings_form = ProceedingsForm()
-
- proceedings_form = ProceedingsForm()
- register_form = RegisterForm()
- login_form = AuthenticationForm()
+ register_form = RegisterForm()
+ login_form = AuthenticationForm()
context = RequestContext(request, {
'proceedings_form': proceedings_form,
@@ -77,18 +121,83 @@ def submit(request, template = 'proceedings/submit.html'):
'login_form' : login_form
})
+ context['id'] = id if id else None
+
return render_to_response(template, context)
-def edit(request, id, template = 'proceedings/edit.html'):
+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.
"""
- context = RequestContext(request, {
- 'proceedings_form': proceedings_form,
- 'register_form' : register_form,
- 'message' : message,
- 'login_form' : login_form
- })
+ paper = Paper.objects.get(id=id)
- return render_to_response(template, context)
+ paper.title = kwargs.get('title')
+ paper.abstract = kwargs.get('abstract')
+ paper.body = kwargs.get('body')
+ authors = kwargs.get('authors')
+
+ if authors:
+ authors = authors.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)
+
+ paper_data = {
+ 'paper_abstract': paper.abstract,
+ 'authors': [
+ {'first_names': author.first_name,
+ 'surname': author.last_name,
+ 'address': 'XXX',
+ 'country': 'XXX',
+ 'email_address': 'XXX@xx.com',
+ 'institution': 'XXX'
+ } 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
+
+ outfilename = '/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/paper.pdf'
+ attach_dir = os.path.dirname('/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/')
+ mk_scipy_paper.mk_abstract_preview(abstract, outfilename, attach_dir)
+
+ from django.http import HttpResponse
+ return HttpResponse('Machi')
+
+ \ No newline at end of file