1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 import os |
2 |
4 |
3 from django.contrib.auth import login |
5 from django.contrib.auth import login |
4 from django.contrib.auth.decorators import login_required |
6 from django.contrib.auth.decorators import login_required |
5 from django.contrib.auth.forms import AuthenticationForm |
7 from django.contrib.auth.forms import AuthenticationForm |
|
8 from django.contrib.auth.models import User |
|
9 from django.core.urlresolvers import reverse |
6 from django.shortcuts import render_to_response |
10 from django.shortcuts import render_to_response |
7 from django.template import RequestContext |
11 from django.template import RequestContext |
8 |
12 |
|
13 from project.kiwipycon.proceedings.models import Paper |
9 from project.kiwipycon.user.forms import RegisterForm |
14 from project.kiwipycon.user.forms import RegisterForm |
10 from project.kiwipycon.user.models import UserProfile |
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 |
11 from project.kiwipycon.proceedings.forms import ProceedingsForm |
18 from project.kiwipycon.proceedings.forms import ProceedingsForm |
12 |
19 |
13 |
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 |
14 @login_required |
39 @login_required |
15 def submit(request, template = 'proceedings/submit.html'): |
40 def submit(request, id=None, template='proceedings/submit.html'): |
16 """View to submit the proceedings paper. |
41 """View to submit the proceedings paper. |
17 """ |
42 """ |
|
43 |
18 user = request.user |
44 user = request.user |
19 if user.is_authenticated(): |
45 if user.is_authenticated(): |
20 try: |
46 try: |
21 profile = user.get_profile() |
47 profile = user.get_profile() |
22 except: |
48 except: |
45 # add the new user |
68 # add the new user |
46 if register_form.is_valid(): |
69 if register_form.is_valid(): |
47 |
70 |
48 user = kiwipycon_createuser(request, register_form.data) |
71 user = kiwipycon_createuser(request, register_form.data) |
49 |
72 |
|
73 proceedings_form = ProceedingsForm(data=request.POST, |
|
74 files=request.FILES) |
|
75 |
50 if proceedings_form.is_valid(): |
76 if proceedings_form.is_valid(): |
51 if user.is_authenticated(): |
77 if user.is_authenticated(): |
52 title = proceedings_form.data.get('title') |
78 # Data from reSt file is appended to the data in fields |
53 |
79 title, abstract, body, authors = handleUploadedFile( |
54 # Saved, ... redirect back to account |
80 proceedings_form.cleaned_data, request.FILES.get('file')) |
55 redirect_to = reverse('kiwipycon_account') |
81 |
56 return set_message_cookie(redirect_to, |
82 paper = edit(id, title=title, |
57 msg = u'Thanks, your paper has been submitted.') |
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.') |
58 else: |
93 else: |
|
94 # This is impossible. Something was wrong so return back |
|
95 # to submit page |
59 redirect_to = reverse('kiwipycon_submit_proceedings') |
96 redirect_to = reverse('kiwipycon_submit_proceedings') |
60 return set_message_cookie(redirect_to, |
97 return set_message_cookie( |
61 msg = u'Something is wrong here.') |
98 redirect_to, msg = u'Something is wrong here.') |
62 |
|
63 else: |
99 else: |
64 proceedings_form = ProceedingsForm() |
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 |
65 register_form = RegisterForm() |
114 register_form = RegisterForm() |
66 login_form = AuthenticationForm() |
115 login_form = AuthenticationForm() |
67 |
|
68 |
|
69 proceedings_form = ProceedingsForm() |
|
70 register_form = RegisterForm() |
|
71 login_form = AuthenticationForm() |
|
72 |
116 |
73 context = RequestContext(request, { |
117 context = RequestContext(request, { |
74 'proceedings_form': proceedings_form, |
118 'proceedings_form': proceedings_form, |
75 'register_form' : register_form, |
119 'register_form' : register_form, |
76 'message' : message, |
120 'message' : message, |
77 'login_form' : login_form |
121 'login_form' : login_form |
78 }) |
122 }) |
79 |
123 |
|
124 context['id'] = id if id else None |
|
125 |
80 return render_to_response(template, context) |
126 return render_to_response(template, context) |
81 |
127 |
82 |
128 |
83 def edit(request, id, template = 'proceedings/edit.html'): |
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): |
84 """View to edit the proceedings paper. |
151 """View to edit the proceedings paper. |
85 """ |
152 """ |
86 |
153 |
87 context = RequestContext(request, { |
154 paper = Paper.objects.get(id=id) |
88 'proceedings_form': proceedings_form, |
155 |
89 'register_form' : register_form, |
156 paper.title = kwargs.get('title') |
90 'message' : message, |
157 paper.abstract = kwargs.get('abstract') |
91 'login_form' : login_form |
158 paper.body = kwargs.get('body') |
92 }) |
159 authors = kwargs.get('authors') |
93 |
160 |
94 return render_to_response(template, context) |
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 |