1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
from urlparse import urlparse
import simplejson as json
import os
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from PIL import Image
from project.scipycon.base.models import Event
from project.scipycon.registration.models import Registration
from project.scipycon.registration.models import Wifi
from project.scipycon.registration.forms import WifiForm
from project.scipycon.talk.models import Talk
from project.scipycon.user.forms import EditProfileForm
from project.scipycon.user.forms import RegisterForm
from project.scipycon.user.forms import UsernameForm
from project.scipycon.user.utils import handle_uploaded_photo
from project.scipycon.user.utils import scipycon_createuser
from project.scipycon.utils import set_message_cookie
#User_dump Http404 Error
from django.http import Http404
#for user_dump creation
from project.scipycon.registration.models import Accommodation
#Pdf badge generation
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.platypus import Image as reportlabImage
from django.core.exceptions import ObjectDoesNotExist
@login_required
def account(request, scope, template_name="user/account.html"):
"""Displays the main screen of the current user's account.
"""
user = request.user
profile = user.get_profile()
talks = Talk.objects.filter(speaker=user)
try:
registration = Registration.objects.get(registrant=user)
except ObjectDoesNotExist:
registration = None
try:
wifiobj = Wifi.objects.get(user=user)
except ObjectDoesNotExist:
wifiobj = None
event = Event.objects.get(scope=scope)
if profile.photo:
photo = os.path.join(settings.USER_MEDIA_URL, profile.photo)
else:
photo = '/img/user-default.png'
return render_to_response(template_name, RequestContext(request, {
'params': {'scope': scope},
'user' : user,
'profile' : profile,
'photo' : photo,
'talks' : talks,
'registration' : registration,
'event': event}))
@login_required
def edit_profile(request, scope, template_name="user/editprofile.html"):
"""Allows user to edit profile
"""
user = request.user
profile = user.get_profile()
if request.method == "POST":
form = EditProfileForm(data=request.POST,
files=request.FILES)
if form.is_valid():
photo = request.FILES.get('photo', None)
filename= None
if photo:
filename = handle_uploaded_photo(user, request.FILES['photo'])
if filename:
profile.photo = filename
user.email = form.data.get("email")
user.first_name = form.data.get("first_name")
user.last_name = form.data.get("last_name")
user.save()
profile.url = form.data.get("url")
profile.about = form.data.get("about")
profile.save()
redirect_to = reverse('scipycon_account',
kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u'Your profile has been changed.')
else:
form = EditProfileForm(
initial={
'email' : user.email,
'email2' : user.email, # hidden field
'first_name' : user.first_name,
'last_name' : user.last_name,
'url' : profile.url,
'about' : profile.about,
})
return render_to_response(template_name, RequestContext(request, {
'params': {'scope': scope},
'form': form
}))
def login(request, scope, template_name="user/login.html"):
"""Custom view to login or register/login a user.
Integration of register and login form
It uses Django's standard AuthenticationForm, though.
"""
user = request.user
if user.is_authenticated():
redirect_to = reverse("scipycon_account", kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u"Redirected to account from login form.")
# Using Djangos default AuthenticationForm
login_form = AuthenticationForm()
register_form = RegisterForm()
if request.POST.get("action") == "login":
login_form = AuthenticationForm(data=request.POST)
if login_form.is_valid():
redirect_to = request.POST.get("next")
# Light security check -- make sure redirect_to isn't garbage.
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
redirect_to = reverse('scipycon_account',
kwargs={'scope': scope})
from django.contrib.auth import login
login(request, login_form.get_user())
return set_message_cookie(redirect_to, msg = u"You have been logged in.")
elif request.POST.get("action") == "register":
register_form = RegisterForm(data=request.POST)
if register_form.is_valid():
user = scipycon_createuser(request, register_form.data, scope)
redirect_to = request.POST.get("next")
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
redirect_to = reverse('scipycon_account',
kwargs={'scope': scope})
return set_message_cookie(
redirect_to, msg = u"You have been registered and logged in.")
# Get next_url
next_url = request.REQUEST.get("next")
if next_url is None:
next_url = request.META.get("HTTP_REFERER")
if next_url is None:
next_url = reverse('scipycon_account', kwargs={'scope': scope})
# Get just the path of the url.
# See django.contrib.auth.views.login for more
next_url = urlparse(next_url)
next_url = next_url[2]
try:
login_form_errors = login_form.errors["__all__"]
except KeyError:
login_form_errors = None
return render_to_response(template_name, RequestContext(request, {
'params': {'scope': scope},
'login_form' : login_form,
'login_form_errors' : login_form_errors,
'register_form' : register_form,
'next_url' : next_url,
}))
def logout(request, scope):
"""Custom method to logout a user.
The reason to use a custom logout method is just to provide a login and a
logoutmethod on one place.
"""
from django.contrib.auth import logout
logout(request)
redirect_to = '/%s' % (scope)
return set_message_cookie(redirect_to, msg = u"You have been logged out.")
@login_required
def password(request, scope, template_name='user/password.html'):
"""Changes the password of current user.
"""
if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
form.save()
redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u'Your password has been changed.')
else:
form = PasswordChangeForm(request.user)
return render_to_response(template_name, RequestContext(request, {
'params': {'scope': scope},
'form' : form
}))
@login_required
def username(request, scope, template_name='user/username.html'):
"""Saves the username from the data form.
"""
if request.method == 'POST':
username_form = UsernameForm(
initial={'username' : request.user.username},
data=request.POST)
if username_form.is_valid():
request.user.username = username_form.cleaned_data.get("username")
request.user.save()
redirect_to = reverse('scipycon_account',
kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u"Your username has been changed.")
else:
username_form = UsernameForm(initial={"username" : request.user.username})
return render_to_response(template_name, RequestContext(request, {
'params': {'scope': scope},
'form': username_form
}))
def get_usernames(request, scope):
"""Returns in json the list of ten possible usernames
starting with the last pattern in the comma separated string
"""
get_params = request.GET
authors_str = get_params.get('input')
if not authors_str:
return HttpResponse(json.dumps(''))
authors = authors_str.split(',')
search_author = authors[-1].strip()
users = User.objects.filter(
Q(username__istartswith=search_author) | Q(
first_name__istartswith=search_author) | Q(
last_name__istartswith=search_author))
results = [{'id': '',
'info': 'plugin_header',
'value': 'User Names'
}]
for user in users:
results.append(
{'id': 'author_name',
'info': str(user.get_full_name()),
'value': str(user.username)
})
json_response = {'results': results}
return HttpResponse(json.dumps(json_response))
@login_required
def get_user_dump(request, scope,template_name='user/dump.html'):
""" Gets a general dump of user related info
"""
print request.user.is_staff
if request.user.is_staff:
qs=Registration.objects.all()
rows=[]
for obj in qs:
row = {}
row['first_name'] = obj.registrant.first_name
row['last_name'] = obj.registrant.last_name
try:
accomodation_require = Accommodation.objects.filter(user__username=obj.registrant.username)[0]
row['sex'] = accomodation_require.sex
except:
row['sex'] = '-'
row['city'] = obj.city
row['organization'] = obj.organisation
row['occupation'] = obj.occupation
row['conference'] = obj.conference
row['sprint'] = obj.sprint
row['tutorial'] = obj.tutorial
try:
wifi_require = Wifi.objects.filter(user__username=obj.registrant.username)[0]
row['wifi'] = wifi_require.wifi
except:
row['wifi']='Wifi Unspecified'
rows.append(row)
return render_to_response(template_name, RequestContext(request, {
'rows': rows}))
else:
raise Http404
@login_required
def badge(request,scope):
from django.conf import settings
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=scipybadge.pdf'
# Create the PDF object, using the response object as its "file."
c = canvas.Canvas(response)
ref=5*cm
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
c.rect(ref,ref,9*cm,6*cm)
img_path = os.path.join(settings.STATIC_ROOT, 'img', 'scipyshiny_small.png')
im = reportlabImage(img_path, width=1.75*cm, height=1.75*cm)
im.drawOn(c,(ref+0.8*cm),(ref+4.3*cm))
c.setFont('Helvetica', 6)
c.drawString((ref+1.0*cm),(ref+4.2*cm),'scipy.in 2010')
c.drawString((ref+1.1*cm),(ref+4.0*cm),'Hyderabad')
c.setFont('Helvetica', 14)
print request.user.id
reg_obj=Registration.objects.get(registrant=request.user.id)
c.drawString((ref+3.4*cm),(ref+4.9*cm),str(reg_obj.slug))
c.setFont('Helvetica-Bold', 15)
c.drawString((ref+0.6*cm),(ref+3.4*cm),str(request.user.get_full_name()))
c.setFont('Helvetica', 11)
c.drawString((ref+2.8*cm),(ref+2.7*cm),reg_obj.organisation)
c.setFont('Helvetica', 11)
try:
c.drawString((ref+2.8*cm),(ref+2.2*cm),reg_obj.occupation.split(':')[1])
except IndexError:
c.drawString((ref+2.8*cm),(ref+2.3*cm),reg_obj.occupation)
c.setFont('Helvetica', 10)
c.drawString((ref+2.8*cm),(ref+1.7*cm),reg_obj.city)
c.setFont('Helvetica', 10)
c.drawString((ref+2.8*cm),(ref+1*cm),'Participant')
try:
wifi_obj=Wifi.objects.get(user=request.user.id)
c.setFont('Helvetica', 10)
c.drawString((ref+5.6*cm),(ref+0.5*cm),wifi_obj.registration_id)
except :
pass
# Close the PDF object cleanly, and we're done.
c.showPage()
c.save()
return response
|