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