|
1 # -*- coding: utf-8 -*- |
|
2 from __future__ import absolute_import |
|
3 |
|
4 # python imports |
|
5 from urlparse import urlparse |
|
6 |
|
7 # django |
|
8 from django.conf import settings |
|
9 from django.shortcuts import render_to_response |
|
10 from django.template import RequestContext |
|
11 from django.core.urlresolvers import reverse |
|
12 from django.views.generic.list_detail import object_list |
|
13 from django.views.generic.list_detail import object_detail |
|
14 |
|
15 from django.contrib.auth.decorators import login_required |
|
16 from django.contrib.auth.forms import AuthenticationForm |
|
17 from django.contrib.auth.models import User |
|
18 |
|
19 # PIL |
|
20 from PIL import Image |
|
21 |
|
22 # tagging |
|
23 from tagging.models import Tag |
|
24 |
|
25 #kiwipycon |
|
26 from project.kiwipycon.utils import set_message_cookie |
|
27 from project.kiwipycon.utils import slugify |
|
28 from project.kiwipycon.user.models import UserProfile |
|
29 from project.kiwipycon.user.forms import RegisterForm |
|
30 from project.kiwipycon.user.utils import kiwipycon_createuser |
|
31 |
|
32 from .models import Talk |
|
33 from .forms import TalkSubmitForm |
|
34 from .forms import TalkEditForm |
|
35 from .models import DURATION_CHOICES |
|
36 from .models import AUDIENCE_CHOICES |
|
37 |
|
38 def list_talks(request): |
|
39 objects = Talk.objects.filter(approved=True) |
|
40 extra_context = dict(count=objects.count()) |
|
41 return object_list(request, objects, extra_context=extra_context) |
|
42 |
|
43 def talk(request, id): |
|
44 objects = Talk.objects.filter(approved=True) |
|
45 audience = {} |
|
46 for choice in AUDIENCE_CHOICES: |
|
47 audience[choice[0]] = choice[1] |
|
48 extra_context = dict(choices=audience) |
|
49 return object_detail(request, objects, id, extra_context=extra_context) |
|
50 |
|
51 @login_required |
|
52 def edit_talk(request, id, template_name='talk/edit-talk.html'): |
|
53 '''Allows users that submitted a talk to edit it until the talk is approved. |
|
54 ''' |
|
55 talk = Talk.objects.get(pk=id) |
|
56 |
|
57 if talk.approved == True: |
|
58 redirect_to = reverse('kiwipycon_account') |
|
59 return set_message_cookie(redirect_to, |
|
60 msg = u'Sorry but you cannot edit the talk once'\ |
|
61 + ' it has been accepted.') |
|
62 if talk.speaker != request.user: |
|
63 redirect_to = reverse('kiwipycon_account') |
|
64 return set_message_cookie(redirect_to, |
|
65 msg = u'Redirected to account because the talk you selected' \ |
|
66 + ' is not your own.') |
|
67 |
|
68 if request.method == 'POST': |
|
69 form = TalkEditForm(data=request.POST) |
|
70 if form.is_valid(): |
|
71 talk.slug = slugify(form.data.get('title')) |
|
72 talk.authors_bio = form.data.get('authors_bio') |
|
73 talk.contact = form.data.get('contact') |
|
74 talk.title = form.data.get('title') |
|
75 talk.abstract = form.data.get('abstract') |
|
76 talk.outline = form.data.get('outline') |
|
77 talk.topic = form.data.get('topic') |
|
78 talk.topic_other = form.data.get('topic_other') |
|
79 talk.duration = form.data.get('duration') |
|
80 talk.audience = form.data.get('audience') |
|
81 talk.audience_other = form.data.get('audience_other') |
|
82 talk.tags = form.data.get('tags') |
|
83 talk.save() |
|
84 # Saved.. redirect |
|
85 redirect_to = reverse('kiwipycon_account') |
|
86 return set_message_cookie(redirect_to, |
|
87 msg = u'Your changes have been saved.') |
|
88 else: |
|
89 form = TalkEditForm(initial={ |
|
90 'id' : id, |
|
91 'authors_bio' : talk.authors_bio, |
|
92 'contact' : talk.contact, |
|
93 'title' : talk.title, |
|
94 'abstract' : talk.abstract, |
|
95 'outline' : talk.outline, |
|
96 'topic' : talk.topic, |
|
97 'topic_other' : talk.topic_other, |
|
98 'duration' : talk.duration, |
|
99 'audience' : talk.audience, |
|
100 'audience_other' : talk.audience_other, |
|
101 'tags' : talk.tags, |
|
102 }) |
|
103 |
|
104 return render_to_response(template_name, RequestContext(request, locals())) |
|
105 |
|
106 @login_required() |
|
107 def submit_talk(request, template_name='talk/submit-talk.html'): |
|
108 '''Allows user to edit profile |
|
109 ''' |
|
110 user = request.user |
|
111 if user.is_authenticated(): |
|
112 try: |
|
113 profile = user.get_profile() |
|
114 except: |
|
115 profile, new = UserProfile.objects.get_or_create(user=user) |
|
116 if new: |
|
117 profile.save() |
|
118 message = None |
|
119 |
|
120 if request.method == 'POST': |
|
121 talk_form = TalkSubmitForm(data=request.POST) |
|
122 |
|
123 register_form = RegisterForm(data=request.POST, |
|
124 files=request.FILES) |
|
125 |
|
126 if request.POST.get('action', None) == 'login': |
|
127 login_form = AuthenticationForm(data=request.POST) |
|
128 if login_form.is_valid(): |
|
129 |
|
130 from django.contrib.auth import login |
|
131 login(request, login_form.get_user()) |
|
132 |
|
133 redirect_to = reverse('kiwipycon_submit_talk') |
|
134 return set_message_cookie(redirect_to, |
|
135 msg = u'You have been logged in.') |
|
136 |
|
137 if request.POST.get('action', None) == 'register': |
|
138 # add the new user |
|
139 if register_form.is_valid(): |
|
140 |
|
141 user = kiwipycon_createuser(request, register_form.data) |
|
142 |
|
143 if talk_form.is_valid(): |
|
144 if user.is_authenticated(): |
|
145 title = talk_form.data.get('title') |
|
146 talk = Talk.objects.create( |
|
147 slug = slugify(title), |
|
148 speaker = User.objects.get(pk=user.id), |
|
149 authors_bio = talk_form.data.get('authors_bio'), |
|
150 contact = talk_form.data.get('contact'), |
|
151 title = talk_form.data.get('title'), |
|
152 abstract = talk_form.data.get('abstract'), |
|
153 outline = talk_form.data.get('outline'), |
|
154 topic = talk_form.data.get('topic'), |
|
155 topic_other = talk_form.data.get('topic_other'), |
|
156 duration = talk_form.data.get('duration'), |
|
157 audience = talk_form.data.get('audience'), |
|
158 audience_other = talk_form.data.get('audience_other'), |
|
159 approved = False, |
|
160 tags = talk_form.data.get('tags')) |
|
161 talk.save() |
|
162 # Saved, ... redirect back to account |
|
163 redirect_to = reverse('kiwipycon_account') |
|
164 return set_message_cookie(redirect_to, |
|
165 msg = u'Thanks, your talk has been submitted.') |
|
166 else: |
|
167 redirect_to = reverse('kiwipycon_submit_talk') |
|
168 return set_message_cookie(redirect_to, |
|
169 msg = u'Something is wrong here.') |
|
170 |
|
171 else: |
|
172 talk_form = TalkSubmitForm() |
|
173 register_form = RegisterForm() |
|
174 login_form = AuthenticationForm() |
|
175 |
|
176 |
|
177 return render_to_response(template_name, RequestContext(request, { |
|
178 'talk_form': talk_form, |
|
179 'register_form' : register_form, |
|
180 'message' : message, |
|
181 'login_form' : login_form |
|
182 })) |
|
183 |