project/kiwipycon/proceedings/views.py
changeset 96 3ece0de11641
parent 94 e86755df35da
child 101 fb27be005a5c
equal deleted inserted replaced
95:34e28994fec5 96:3ece0de11641
     7 from django.contrib.auth.forms import AuthenticationForm
     7 from django.contrib.auth.forms import AuthenticationForm
     8 from django.contrib.auth.models import User
     8 from django.contrib.auth.models import User
     9 from django.core.urlresolvers import reverse
     9 from django.core.urlresolvers import reverse
    10 from django.shortcuts import render_to_response
    10 from django.shortcuts import render_to_response
    11 from django.template import RequestContext
    11 from django.template import RequestContext
    12 
    12 from django.utils import simplejson as json
       
    13 
       
    14 from project.kiwipycon.proceedings.booklet import mk_scipy_paper
       
    15 from project.kiwipycon.proceedings.forms import ProceedingsForm
    13 from project.kiwipycon.proceedings.models import Paper
    16 from project.kiwipycon.proceedings.models import Paper
    14 from project.kiwipycon.user.forms import RegisterForm
    17 from project.kiwipycon.user.forms import RegisterForm
    15 from project.kiwipycon.user.models import UserProfile
    18 from project.kiwipycon.user.models import UserProfile
       
    19 from project.kiwipycon.user.utils import kiwipycon_createuser
    16 from project.kiwipycon.utils import set_message_cookie
    20 from project.kiwipycon.utils import set_message_cookie
    17 from project.kiwipycon.proceedings.booklet import mk_scipy_paper
    21 from project import settings
    18 from project.kiwipycon.proceedings.forms import ProceedingsForm
       
    19 
    22 
    20 
    23 
    21 def handleUploadedFile(proceedings_form_data, rst_file):
    24 def handleUploadedFile(proceedings_form_data, rst_file):
    22     """Handles the uploaded file content and process the form
    25     """Handles the uploaded file content and process the form
    23     """
    26     """
    24 
    27 
    25     title = proceedings_form_data.get('title')
    28     title = proceedings_form_data.get('title')
    26     abstract = proceedings_form_data.get('abstract')
    29     abstract = proceedings_form_data.get('abstract')
    27     body = proceedings_form_data.get('body')
    30     body = proceedings_form_data.get('body')
    28     authors = proceedings_form_data.get('authors')
       
    29 
    31 
    30     if rst_file:
    32     if rst_file:
       
    33         # TODO: using stating only for testing purposes.
    31         destination = open('some/file/name.txt', 'wb+')
    34         destination = open('some/file/name.txt', 'wb+')
    32         for chunk in rst_file.chunks():
    35         for chunk in rst_file.chunks():
    33             destination.write(chunk)
    36             destination.write(chunk)
    34         destination.close()
    37         destination.close()
    35 
    38 
    36     return title, abstract, body, authors
    39     return title, abstract, body
    37 
    40 
    38 
    41 
    39 @login_required
    42 @login_required
    40 def submit(request, id=None, template='proceedings/submit.html'):
    43 def submit(request, id=None, template='proceedings/submit.html'):
    41     """View to submit the proceedings paper.
    44     """View to submit the proceedings paper.
    42     """
    45     """
       
    46 
       
    47     context = RequestContext(request, {})
    43 
    48 
    44     user = request.user
    49     user = request.user
    45     if user.is_authenticated():
    50     if user.is_authenticated():
    46         try:
    51         try:
    47             profile = user.get_profile()
    52             profile = user.get_profile()
    65                         msg = u'You have been logged in.')
    70                         msg = u'You have been logged in.')
    66 
    71 
    67         if request.POST.get('action', None) == 'register':
    72         if request.POST.get('action', None) == 'register':
    68             # add the new user
    73             # add the new user
    69             if register_form.is_valid():
    74             if register_form.is_valid():
    70 
       
    71                 user = kiwipycon_createuser(request, register_form.data)
    75                 user = kiwipycon_createuser(request, register_form.data)
    72 
    76 
    73         proceedings_form = ProceedingsForm(data=request.POST,
    77         proceedings_form = ProceedingsForm(data=request.POST,
    74                                            files=request.FILES)
    78                                            files=request.FILES)
    75   
    79 
    76         if proceedings_form.is_valid():
    80         if proceedings_form.is_valid():
    77             if user.is_authenticated():
    81             if user.is_authenticated():
    78                 # Data from reSt file is appended to the data in fields
    82                 # Data from reSt file is appended to the data in fields
    79                 title, abstract, body, authors = handleUploadedFile(
    83                 title, abstract, body = handleUploadedFile(
    80                     proceedings_form.cleaned_data, request.FILES.get('file'))
    84                     proceedings_form.cleaned_data, request.FILES.get('file'))
       
    85                 authors = request.POST.get('as_values_authors')
    81 
    86 
    82                 paper = edit(id, title=title,
    87                 paper = edit(id, title=title,
    83                     abstract=abstract, body=body,
    88                     abstract=abstract, body=body,
    84                     authors=authors) if id else create(title=title,
    89                     authors=authors) if id else create(title=title,
    85                     abstract=abstract, body=body,
    90                     abstract=abstract, body=body,
   102             paper = Paper.objects.get(id=id)
   107             paper = Paper.objects.get(id=id)
   103             proceedings_form = ProceedingsForm(
   108             proceedings_form = ProceedingsForm(
   104                 initial={'title': paper.title,
   109                 initial={'title': paper.title,
   105                          'abstract': paper.abstract,
   110                          'abstract': paper.abstract,
   106                          'body': paper.body,
   111                          'body': paper.body,
   107                          'authors': ', '.join([
       
   108                              author.username for author in paper.authors.all()])
       
   109                 })
   112                 })
       
   113             context['initial_authors'] = json.dumps([
       
   114                 {'name': str(author.username),
       
   115                  'value': '%s (%s)' % (author.get_full_name(), author.username)
       
   116                 } for author in paper.authors.all()])
   110         else:
   117         else:
   111             # Otherwise create a new form
   118             # Otherwise create a new form
   112             proceedings_form = ProceedingsForm()
   119             proceedings_form = ProceedingsForm()
   113 
   120 
   114         register_form = RegisterForm()
   121         register_form = RegisterForm()
   115         login_form = AuthenticationForm()
   122         login_form = AuthenticationForm()
   116 
   123 
   117     context = RequestContext(request, {
   124     context['proceedings_form'] = proceedings_form
   118         'proceedings_form': proceedings_form,
   125     context['register_form'] = register_form
   119         'register_form' : register_form,
   126     context['message'] = message
   120         'message' : message,
   127     context['login_form'] = login_form
   121         'login_form' : login_form
       
   122         })
       
   123 
       
   124     context['id'] = id if id else None
   128     context['id'] = id if id else None
   125 
   129 
   126     return render_to_response(template, context)
   130     return render_to_response(template, context)
   127 
   131 
   128 
   132 
   157     paper.abstract = kwargs.get('abstract')
   161     paper.abstract = kwargs.get('abstract')
   158     paper.body = kwargs.get('body')
   162     paper.body = kwargs.get('body')
   159     authors = kwargs.get('authors')
   163     authors = kwargs.get('authors')
   160 
   164 
   161     if authors:
   165     if authors:
   162         authors = authors.split(',')
   166         authors = authors.strip(' ,').split(',')
   163         for author in authors:
   167         for author in authors:
   164             user = User.objects.get(username=author.strip())
   168             user = User.objects.get(username=author.strip())
   165             paper.authors.add(user)
   169             paper.authors.add(user)
   166 
   170 
   167     paper.save()
   171     paper.save()
   170 
   174 
   171 
   175 
   172 def show_paper(request, id):
   176 def show_paper(request, id):
   173     """Display the thumbnail of the rendered paper for download
   177     """Display the thumbnail of the rendered paper for download
   174     """
   178     """
   175     
   179 
   176     paper = Paper.objects.get(id=id)
   180     paper = Paper.objects.get(id=id)
   177 
   181 
       
   182     # TODO: Address and country should be required field in the user forms
       
   183     # henceforth. Also contact numbers.
   178     paper_data = {
   184     paper_data = {
   179       'paper_abstract': paper.abstract,
   185       'paper_abstract': paper.abstract,
   180       'authors': [
   186       'authors': [
   181           {'first_names': author.first_name,
   187           {'first_names': author.first_name,
   182             'surname': author.last_name,
   188             'surname': author.last_name,
   183             'address': 'XXX',
   189             'address': 'None',
   184             'country': 'XXX',
   190             'country': 'India',
   185             'email_address': 'XXX@xx.com',
   191             'email_address': author.email,
   186             'institution': 'XXX'
   192             'institution': author.registration_set.get().organisation,
       
   193             'city': author.registration_set.get().city
   187            } for author in paper.authors.all()],
   194            } for author in paper.authors.all()],
   188       'title': paper.title
   195       'title': paper.title
   189       }
   196       }
   190     
   197 
   191     abstract = mk_scipy_paper.Bunch(**paper_data)
   198     abstract = mk_scipy_paper.Bunch(**paper_data)
   192     abstract.authors = [mk_scipy_paper.Bunch(**a) for a in abstract.authors]
   199     abstract.authors = [mk_scipy_paper.Bunch(**a) for a in abstract.authors]
   193 
   200 
   194     abstract['paper_text'] = paper.body
   201     abstract['paper_text'] = paper.body
   195 
   202 
   196     outfilename = '/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/paper.pdf'
   203     out_dir = attach_dir = os.path.join(settings.STATIC_ROOT,
   197     attach_dir = os.path.dirname('/media/python/workspace/kiwipycon/project/kiwipycon/proceedings/booklet/output/')
   204                                         'proceedings', 'output')
       
   205     outfilename = os.path.join(out_dir, 'paper%d.pdf' % (paper.id))
   198     mk_scipy_paper.mk_abstract_preview(abstract, outfilename, attach_dir)
   206     mk_scipy_paper.mk_abstract_preview(abstract, outfilename, attach_dir)
   199 
   207 
   200     from django.http import HttpResponse
   208     context = {'paper_id': paper.id,
   201     return HttpResponse('Machi')
   209                'out_path': '/static/proceedings/output' }
   202 
   210     template = 'proceedings/show_paper_preview.html'
   203  
   211 
       
   212     return render_to_response(template, context)