-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_pep8_view.txt
470 lines (410 loc) · 20.8 KB
/
python_pep8_view.txt
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
Check results
=============
E501:7:80:line too long (103 > 79 characters)
E501:67:80:line too long (84 > 79 characters)
E501:73:80:line too long (88 > 79 characters)
E501:83:80:line too long (80 > 79 characters)
E501:125:80:line too long (82 > 79 characters)
E501:126:80:line too long (85 > 79 characters)
E501:127:80:line too long (83 > 79 characters)
E501:129:80:line too long (84 > 79 characters)
E501:130:80:line too long (87 > 79 characters)
E501:131:80:line too long (83 > 79 characters)
E501:133:80:line too long (81 > 79 characters)
E501:134:80:line too long (88 > 79 characters)
E501:135:80:line too long (83 > 79 characters)
E501:142:80:line too long (90 > 79 characters)
E501:143:80:line too long (93 > 79 characters)
E501:144:80:line too long (102 > 79 characters)
E501:170:80:line too long (91 > 79 characters)
E501:188:80:line too long (87 > 79 characters)
E501:193:80:line too long (83 > 79 characters)
E501:210:80:line too long (85 > 79 characters)
E501:216:80:line too long (89 > 79 characters)
E501:218:80:line too long (81 > 79 characters)
E501:241:80:line too long (81 > 79 characters)
E501:253:80:line too long (82 > 79 characters)
E501:273:80:line too long (95 > 79 characters)
E501:287:80:line too long (92 > 79 characters)
E501:293:80:line too long (81 > 79 characters)
E501:296:80:line too long (89 > 79 characters)
E501:316:80:line too long (83 > 79 characters)
E501:321:80:line too long (100 > 79 characters)
E501:336:80:line too long (83 > 79 characters)
E501:343:80:line too long (84 > 79 characters)
E501:347:80:line too long (85 > 79 characters)
E501:351:80:line too long (92 > 79 characters)
E501:353:80:line too long (95 > 79 characters)
E501:356:80:line too long (81 > 79 characters)
E501:359:80:line too long (80 > 79 characters)
E501:362:80:line too long (81 > 79 characters)
E501:365:80:line too long (114 > 79 characters)
E501:368:80:line too long (81 > 79 characters)
E501:370:80:line too long (88 > 79 characters)
E501:371:80:line too long (113 > 79 characters)
E501:375:80:line too long (81 > 79 characters)
E501:377:80:line too long (87 > 79 characters)
E501:378:80:line too long (112 > 79 characters)
E501:382:80:line too long (81 > 79 characters)
E501:411:80:line too long (95 > 79 characters)
Code
=============
from django.contrib.auth import get_user_model, authenticate, login, logout
from django.shortcuts import render, redirect
from itertools import chain
from rest_framework.renderers import JSONRenderer
from django.contrib.auth.decorators import login_required
from .models import Item, Bag, Climate, Landform, Environment, ItemSerializer, Feedback, Recommendation
User = get_user_model()
# Home Page (home.html)
def home(request):
return render(request, 'myappocalypse/home.html')
# Why A Bag Page (whyabag.html)
# => Page Aim : Description / justification of application
def whyabag(request):
return render(request, 'myappocalypse/whyabag.html')
# Login Page (login.html)
# => Page Aim :
# Allow registered users to login
# via standard form / social login (Facebook, Twitter, Google)
def signin(request):
context = {}
# Get username and password from user
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
# Check if username-password exists, and allow login if so
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
context['successMsg'] = 'Successful login'
return render(request, 'myappocalypse/login.html', context=context)
else:
context['errorMsg'] = 'Invalid login'
return render(request, 'myappocalypse/login.html', context=context)
else:
return render(request, 'myappocalypse/login.html')
# Sign Up Page (signup.html)
# => Page Aim :
# Allow not registered users to sign up via
# standard form / social login (Facebook, Twitter, Google)
def signup(request):
context = {}
if request.method == "POST":
password1 = request.POST['password1']
password2 = request.POST['password2']
# Check if passwords match
if password1 == password2:
username = request.POST['username']
email = request.POST['email']
# Check if username already exists
try:
user1 = User.objects.get(username=username)
context['errorMsg'] = 'Username is already in system'
return render(request, 'myappocalypse/signup.html', context=context)
except User.DoesNotExist:
# If username is unique, check if email already exists
try:
user2 = User.objects.get(email=email)
context['errorMsg'] = 'Email is already in system'
return render(request, 'myappocalypse/signup.html', context=context)
except User.DoesNotExist:
# If email and username are unique, create user
user = User.objects.create_user(username=username,
email=email,
password=password1)
context['successMsg'] = "Thank you for signing up!"
return redirect('home')
else:
context['errorMsg'] = 'Passwords must match'
return render(request, 'myappocalypse/signup.html', context=context)
else:
return render(request, 'myappocalypse/signup.html')
# Logout Page (logout.html)
# => Page Aim :
# Allow already logged in users to logout
@login_required
def signout(request):
if request.user.is_authenticated:
logout(request)
return redirect('login')
else:
return redirect('myappocalypse/logout.html')
# Pack A Bag Page (packmybag.html)
# => Page Aim :
# Allow logged in users to create a new bag
# in which one can pack items after
@login_required
def packmybag(request):
context = {}
if request.method == "POST":
# Get bag details from user form
user = request.user
name = request.POST['bagname']
userweight = request.POST['userweight']
bagweight = request.POST['bagweight']
have_child = request.POST.get('have_child', False)
have_elder = request.POST.get('have_elder', False)
have_pet = request.POST.get('have_pet', False)
climate = request.POST['climate']
landform = request.POST['landform']
environment = request.POST['environment']
human_infra = request.POST['human_infra']
drinking_water = request.POST['drinking_water']
comestible_food = request.POST['comestible_food']
# Check if climate Dry was paired with matching Desert environment
if (climate == 'Dry' and landform != 'Ocean') and environment != 'Desert':
context['errorMsg'] = 'For Dry climate, please choose Desert environment'
return render(request, 'myappocalypse/packmybag.html', context=context)
# Check if climate Polar was paired with matching Tundra environment
if (climate == 'Polar' and landform != 'Ocean') and environment != 'Tundra':
context['errorMsg'] = 'For Polar climate, please choose Tundra environment'
return render(request, 'myappocalypse/packmybag.html', context=context)
# Check if landform Ocean was paired with matching Marine environment
if landform == 'Ocean' and (environment != 'Marine' or climate == 'Dry'):
context['errorMsg'] = 'For Ocean landform, please choose Marine environment'
return render(request, 'myappocalypse/packmybag.html', context=context)
else:
# If all bag details are filled out appropriately, create bag,
bag = Bag.objects.create(user=user,
name=name,
weight_bag=bagweight,
weight_user=userweight,
climate=Climate.objects.filter(name=climate).first(),
landform=Landform.objects.filter(name=landform).first(),
environment=Environment.objects.filter(name=environment).first(),
with_child=have_child,
with_elder=have_elder,
with_pet=have_pet,
available_infrastructure=human_infra,
available_water=drinking_water,
available_food=comestible_food)
bag.save()
# Once bag is created, redirect user to page to add items to bag
return redirect('mybag_add_items', id=bag.id)
return render(request, 'myappocalypse/packmybag.html', context=context)
# Add Items Page (mybag_add_items/<bag ID>.html)
# => Page Aim :
# Allow logged in users to add items recommended
# based on bag details to newly created or existing bag
@login_required
def add_items(request, id):
context = {}
# Get newly created bag
bag = Bag.objects.filter(id=id).first()
context['bag'] = bag
# Pass existing items' list and actual bag weight for use in case of edit, not creation
if bag.weight_bag_actual is not None:
context['existing_items'] = bag.items.all()
context['actual_weight'] = round(bag.weight_bag_actual)
# Pass all items to Javascript via json
all_items = Item.objects.all()
serializer = ItemSerializer(all_items, many=True)
data = JSONRenderer().render(serializer.data)
context['items_json'] = data.decode()
# Set items recommended based on bag details
items_by_locale = Item.objects.filter(climate__in=[bag.climate],
landform__in=[bag.landform],
environment__in=[bag.environment])
items_all_company_condition = items_by_locale.filter(with_child=None,
with_elder=None,
with_pet=None,
available_infrastructure=None,
available_water=None,
available_food=None)
context['basic_items'] = items_all_company_condition
context['items_user_company'] = get_items_by_company(items_by_locale, bag)
context['items_user_condition'] = get_items_by_conditions(items_by_locale, bag)
context['choices'] = get_choices_array()
# Add selected items to bag
if request.method == "POST":
selected_items = request.POST.getlist('item_checkbox')
# Check if any item selected
if len(selected_items) > 0:
# Get items from database based on user input of selected items
# and add them to bag 'items' field as list
bagweight = request.POST['additems_new_bag_weight']
items_to_create = []
for i in all_items:
if i.name in selected_items:
items_to_create.append(i)
bag.items.set(items_to_create)
# Update actual weight of bag based on the total weight of selected items
bag.weight_bag_actual = bagweight
bag.save()
return redirect('mybag_details', id=bag.id)
else:
context['errorMsg'] = 'Please add at least 1 item to the bag'
return render(request, 'myappocalypse/mybag_add_items.html', context=context)
return render(request, 'myappocalypse/mybag_add_items.html', context=context)
# Detail of a Bag Page (mybag_details/<bag ID>.html)
# => Page Aim :
# Allow logged in users to checks details of an already created bag
@login_required
def mybag_details(request, id):
context = {}
# Get a given bag based on ID
bag = Bag.objects.filter(id=id).first()
context['bag'] = bag
# Pass items of bag to Javascript via json
serializer = ItemSerializer(bag.items, many=True)
data = JSONRenderer().render(serializer.data)
context['items_json'] = data.decode()
# Get an sort bag items to show them in sections / category on UI
items_all_company_condition = bag.items.filter(with_child=None,
with_elder=None,
with_pet=None,
available_infrastructure=None,
available_water=None,
available_food=None)
context['basic_items'] = items_all_company_condition
context['items_user_company'] = get_items_by_company(bag.items, bag)
context['items_user_condition'] = get_items_by_conditions(bag.items, bag)
context['choices'] = get_choices_array()
# Quick actions from page
if request.method == "POST":
# Delete a bag
if request.POST.get('thisbag-to-delete'):
bag = Bag.objects.filter(id=request.POST['thisbag-to-delete']).first()
bag.delete()
return redirect('profile')
return render(request, 'myappocalypse/mybag_details.html', context=context)
# Blog Page (blog.html)
# => Page Aim :
# Allow logged in users to leave star-based feedback & comment about the site,
# recommend an item and consult existing feedbacks
@login_required
def blog(request):
context = {}
# Pass all existing feedbacks to show them as list
context['feedbacks'] = Feedback.objects.all()
# Get star-based rating and/or text-based comment from user
# (Click on Send Feedback button)
if request.method == "POST" and request.POST.get('feedback') or request.POST.get('rating'):
rating = request.POST.get('rating').split('_')[0]
rating_desc = request.POST.get('rating').split('_')[1]
content = request.POST['feedback']
# Create feedback only if either star-based rating,
# either text-based comment, either both were provided
if content or (rating != '0'):
user = request.user
feedback = Feedback.objects.create(rating_point=rating,
rating_description=rating_desc,
content=content,
user=user)
feedback.save()
context['successMsg'] = 'Feedback #' + str(feedback.id) + ' created, thank you!'
return render(request, 'myappocalypse/blog.html', context=context)
else:
context['errorMsg'] = 'Please share your feedback or rating!'
return render(request, 'myappocalypse/blog.html', context=context)
# Allow feedback owner or admin user (superuser) to delete feedback from list
# (Click on Delete button of a feedback)
if request.method == "POST" and request.POST.get('feedback-to-delete'):
feedback = Feedback.objects.filter(id=request.POST['feedback-to-delete']).first()
feedback.delete()
context['successMsg'] = 'Feedback was deleted.'
return render(request, 'myappocalypse/blog.html', context=context)
# Get recommended item from user (Click on Recommend Item button)
if request.method == "POST" and request.POST.get('item_name'):
user = request.user
name = request.POST['item_name']
weight = request.POST['weight']
category = request.POST['category']
usefulness = request.POST['usefulness']
external = request.POST['external']
justification = request.POST['justification']
# Create recommendation for admin approval
recommendation = Recommendation.objects.create(user=user,
name=name,
weight=weight,
category=category,
justification=justification,
usefulness=usefulness,
external=external,
status='Pending')
recommendation.save()
context['successMsg'] = 'Recommendation #' + str(recommendation.id) + ' created, thank you!'
return render(request, 'myappocalypse/blog.html', context=context)
return render(request, 'myappocalypse/blog.html', context=context)
# Profile Page (profile.html)
# => Page Aim :
# Allow logged in users to consult their bags,
# feedbacks and recommendations in one place
@login_required
def profile(request):
context = {}
context['user'] = request.user
# Select bags created by current user (so deleting bags only allowed for owner)
context['bags'] = Bag.objects.filter(user=request.user)
if request.user.is_superuser:
# If user is admin (superuser), show all feedbacks
# and all recommendations with Pending status, regardless of owner
context['feedbacks'] = Feedback.objects.all()
context['recommendations'] = Recommendation.objects.filter(status='Pending')
else:
# If user is standard user, show own feedbacks and recommendations
context['feedbacks'] = Feedback.objects.filter(user=request.user)
context['recommendations'] = Recommendation.objects.filter(user=request.user)
# Delete object records
if request.method == "POST":
# Delete a feedback, allowed for feedback owner / request user and admin / superuser
if request.POST.get('myfeedback-to-delete'):
feedback = Feedback.objects.filter(id=request.POST['myfeedback-to-delete']).first()
feedback.delete()
context['successMsg'] = 'Feedback was deleted.'
return render(request, 'myappocalypse/profile.html', context=context)
# Delete a bag, allowed only for bag owner
if request.POST.get('mybag-to-delete'):
bag = Bag.objects.filter(id=request.POST['mybag-to-delete']).first()
bag.delete()
context['successMsg'] = 'Your Bag was deleted.'
return render(request, 'myappocalypse/profile.html', context=context)
# Delete a recommendation, allowed only for recommendation owner
if request.POST.get('myrecommendations-to-delete'):
recommendation = Recommendation.objects.filter(id=request.POST['myrecommendations-to-delete']).first()
recommendation.delete()
context['successMsg'] = 'Recommendation was deleted.'
return render(request, 'myappocalypse/profile.html', context=context)
# Approve recommendation, allowed only for admin / superuser
if request.POST.get('recommendations-to-approve') and request.user.is_superuser:
recommendation = Recommendation.objects.filter(id=request.POST['recommendations-to-approve']).first()
recommendation.status = 'Approved'
recommendation.save()
context['successMsg'] = 'You approved the recommendation.'
return render(request, 'myappocalypse/profile.html', context=context)
# Reject recommendation, allowed only for admin / superuser
if request.POST.get('recommendations-to-reject') and request.user.is_superuser:
recommendation = Recommendation.objects.filter(id=request.POST['recommendations-to-reject']).first()
recommendation.status = 'Rejected'
recommendation.save()
context['successMsg'] = 'You rejected the recommendation.'
return render(request, 'myappocalypse/profile.html', context=context)
else:
return render(request, 'myappocalypse/profile.html', context=context)
# Helper classes
# Get text value of Category choices field of Item object / model
def get_choices_array():
choices = Item._meta.get_field('category').choices
category_choices = []
for c in choices:
category_choices.append(c[0])
return category_choices
# Get all items specific to special company
def get_items_by_company(items, bag):
items_with_child = items.filter(with_child=bag.with_child)
items_with_elder = items.filter(with_elder=bag.with_elder)
items_with_pet = items.filter(with_pet=bag.with_pet)
items_user_company = list(chain(items_with_child,
items_with_elder,
items_with_pet))
return items_user_company
# Get items specific to special conditions precised for a bag
def get_items_by_conditions(items, bag):
items_user_humaninfra = items.filter(available_infrastructure=bag.available_infrastructure)
items_user_water = items.filter(available_water=bag.available_water)
items_user_food = items.filter(available_food=bag.available_food)
items_user_condition = list(chain(items_user_humaninfra,
items_user_water,
items_user_food))
return items_user_condition