project/kiwipycon/proceedings/views.py
changeset 94 87e77aa18610
parent 93 e86755df35da
child 95 f94e0cd9a862
equal deleted inserted replaced
93:e86755df35da 94:87e77aa18610
     1   # -*- coding: utf-8 -*-
       
     2 
       
     3 import os
       
     4 
       
     5 from django.contrib.auth import login
       
     6 from django.contrib.auth.decorators import login_required
       
     7 from django.contrib.auth.forms import AuthenticationForm
       
     8 from django.contrib.auth.models import User
       
     9 from django.core.urlresolvers import reverse
       
    10 from django.shortcuts import render_to_response
       
    11 from django.template import RequestContext
       
    12 
       
    13 from project.kiwipycon.proceedings.models import Paper
       
    14 from project.kiwipycon.user.forms import RegisterForm
       
    15 from project.kiwipycon.user.models import UserProfile
       
    16 from project.kiwipycon.utils import set_message_cookie
       
    17 from project.kiwipycon.proceedings.booklet import mk_scipy_paper
       
    18 from project.kiwipycon.proceedings.forms import ProceedingsForm
       
    19 
       
    20 
       
    21 def handleUploadedFile(proceedings_form_data, rst_file):
       
    22     """Handles the uploaded file content and process the form
       
    23     """
       
    24 
       
    25     title = proceedings_form_data.get('title')
       
    26     abstract = proceedings_form_data.get('abstract')
       
    27     body = proceedings_form_data.get('body')
       
    28     authors = proceedings_form_data.get('authors')
       
    29 
       
    30     if rst_file:
       
    31         destination = open('some/file/name.txt', 'wb+')
       
    32         for chunk in rst_file.chunks():
       
    33             destination.write(chunk)
       
    34         destination.close()
       
    35 
       
    36     return title, abstract, body, authors
       
    37 
       
    38 
       
    39 @login_required
       
    40 def submit(request, id=None, template='proceedings/submit.html'):
       
    41     """View to submit the proceedings paper.
       
    42     """
       
    43 
       
    44     user = request.user
       
    45     if user.is_authenticated():
       
    46         try:
       
    47             profile = user.get_profile()
       
    48         except:
       
    49             profile, new = UserProfile.objects.get_or_create(user=user)
       
    50             if new:
       
    51                 profile.save()
       
    52     message = None
       
    53 
       
    54     if request.method == 'POST':
       
    55         register_form = RegisterForm(data=request.POST)
       
    56 
       
    57         if request.POST.get('action', None) == 'login':
       
    58             login_form = AuthenticationForm(data=request.POST)
       
    59             if login_form.is_valid():
       
    60 
       
    61                 login(request, login_form.get_user())
       
    62 
       
    63                 redirect_to = reverse('kiwipycon_submit_proceedings')
       
    64                 return set_message_cookie(redirect_to,
       
    65                         msg = u'You have been logged in.')
       
    66 
       
    67         if request.POST.get('action', None) == 'register':
       
    68             # add the new user
       
    69             if register_form.is_valid():
       
    70 
       
    71                 user = kiwipycon_createuser(request, register_form.data)
       
    72 
       
    73         proceedings_form = ProceedingsForm(data=request.POST,
       
    74                                            files=request.FILES)
       
    75   
       
    76         if proceedings_form.is_valid():
       
    77             if user.is_authenticated():
       
    78                 # Data from reSt file is appended to the data in fields
       
    79                 title, abstract, body, authors = handleUploadedFile(
       
    80                     proceedings_form.cleaned_data, request.FILES.get('file'))
       
    81 
       
    82                 paper = edit(id, title=title,
       
    83                     abstract=abstract, body=body,
       
    84                     authors=authors) if id else create(title=title,
       
    85                     abstract=abstract, body=body,
       
    86                     authors=authors)
       
    87 
       
    88                 # Successfully saved. So get back to the edit page.
       
    89                 redirect_to = reverse('kiwipycon_submit_proceedings',
       
    90                                   args=[paper.id])
       
    91                 return set_message_cookie(
       
    92                 redirect_to, msg = u'Thanks, your paper has been submitted.')
       
    93             else:
       
    94                 # This is impossible. Something was wrong so return back
       
    95                 # to submit page
       
    96                 redirect_to = reverse('kiwipycon_submit_proceedings')
       
    97                 return set_message_cookie(
       
    98                 redirect_to, msg = u'Something is wrong here.')          
       
    99     else:
       
   100         if id:
       
   101             # If id exists initialize the form with old values
       
   102             paper = Paper.objects.get(id=id)
       
   103             proceedings_form = ProceedingsForm(
       
   104                 initial={'title': paper.title,
       
   105                          'abstract': paper.abstract,
       
   106                          'body': paper.body,
       
   107                          'authors': ', '.join([
       
   108                              author.username for author in paper.authors.all()])
       
   109                 })
       
   110         else:
       
   111             # Otherwise create a new form
       
   112             proceedings_form = ProceedingsForm()
       
   113 
       
   114         register_form = RegisterForm()
       
   115         login_form = AuthenticationForm()
       
   116 
       
   117     context = RequestContext(request, {
       
   118         'proceedings_form': proceedings_form,
       
   119         'register_form' : register_form,
       
   120         'message' : message,
       
   121         'login_form' : login_form
       
   122         })
       
   123 
       
   124     context['id'] = id if id else None
       
   125 
       
   126     return render_to_response(template, context)
       
   127 
       
   128 
       
   129 def create(**kwargs):
       
   130     """View to create a new proceedings.
       
   131     """
       
   132 
       
   133     title = kwargs.get('title')
       
   134     abstract = kwargs.get('abstract')
       
   135     body = kwargs.get('body')
       
   136     authors = kwargs.get('authors')
       
   137 
       
   138     paper = Paper(title=title, abstract=abstract, body=body)
       
   139     paper.save()
       
   140 
       
   141     if authors:
       
   142         authors = authors.split(',')
       
   143         for author in authors:
       
   144             user = User.objects.get(username=author.strip())
       
   145             paper.authors.add(user)
       
   146 
       
   147     return paper
       
   148 
       
   149 
       
   150 def edit(id, **kwargs):
       
   151     """View to edit the proceedings paper.
       
   152     """
       
   153 
       
   154     paper = Paper.objects.get(id=id)
       
   155 
       
   156     paper.title = kwargs.get('title')
       
   157     paper.abstract = kwargs.get('abstract')
       
   158     paper.body = kwargs.get('body')
       
   159     authors = kwargs.get('authors')
       
   160 
       
   161     if authors:
       
   162         authors = authors.split(',')
       
   163         for author in authors:
       
   164             user = User.objects.get(username=author.strip())
       
   165             paper.authors.add(user)
       
   166 
       
   167     paper.save()
       
   168 
       
   169     return paper
       
   170 
       
   171 
       
   172 def show_paper(request, id):
       
   173     """Display the thumbnail of the rendered paper for download
       
   174     """
       
   175     
       
   176     paper = Paper.objects.get(id=id)
       
   177 
       
   178     paper_data = {
       
   179       'paper_abstract': paper.abstract,
       
   180       'authors': [
       
   181           {'first_names': author.first_name,
       
   182             'surname': author.last_name,
       
   183             'address': 'XXX',
       
   184             'country': 'XXX',
       
   185             'email_address': 'XXX@xx.com',
       
   186             'institution': 'XXX'
       
   187            } for author in paper.authors.all()],
       
   188       'title': paper.title
       
   189       }
       
   190     
       
   191     abstract = mk_scipy_paper.Bunch(**paper_data)
       
   192     abstract.authors = [mk_scipy_paper.Bunch(**a) for a in abstract.authors]
       
   193 
       
   194     abstract['paper_text'] = paper.body
       
   195 
       
   196     outfilename = '/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/paper.pdf'
       
   197     attach_dir = os.path.dirname('/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/')
       
   198     mk_scipy_paper.mk_abstract_preview(abstract, outfilename, attach_dir)
       
   199 
       
   200     from django.http import HttpResponse
       
   201     return HttpResponse('Machi')
       
   202 
       
   203