This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
requests.py
595 lines (535 loc) · 25 KB
/
requests.py
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseForbidden, HttpResponseNotFound,
HttpResponseServerError)
from ffdemo.markup.models import Mark
from ffdemo.markup import common
from django.utils import simplejson
from django.core import serializers
from django.views.decorators.http import require_GET, require_POST
from datetime import datetime, date, timedelta
from django.db.models import Q
from django.utils.translation import gettext as _
import random
def get_translated_marks(request):
marks_to_be_dumped = None
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': True}
marks_to_be_dumped = Mark.objects.exclude(contributor_locale__isnull=True).order_by('id')
if marks_to_be_dumped:
all_marks = []
for m in marks_to_be_dumped:
# Append to all marks
all_marks.append({'date_drawn': m.date_drawn.strftime("%a, %d %b %Y %I:%M:%S"),
'reference': m.reference,
'id': m.id,
'points_obj_simplified': m.points_obj_simplified,
'country_code': m.country_code,
'is_approved': m.is_approved,
'contributor_locale': m.contributor_locale})
response['marks'] = all_marks
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
@require_POST
def flag_mark(request):
response = {'success': False}
if 'reference' in request.POST and len(request.POST['reference']) > 0:
try:
mark = Mark.objects.get(reference=request.POST['reference'])
if mark.is_approved:
pass
else:
mark.flaggings += 1
mark.save()
response['success'] = True
except Mark.DoesNotExist:
response['error'] = _("Mark does not exist")
except Mark.MultipleObjectsReturned:
# should never [ever] happen, purely CYA
response['error'] = _("Multiple marks returned")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
else:
response['error'] = _("No mark specified")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
def init_viz_data(request):
# grab the last mark
# grab the first mark
response = {}
response['country_total_marks'] = ''
response['country_first_mark'] = ''
response['country_last_mark'] = ''
response['country_first_mark_at'] = ''
# for contributed marks
response['contributor_marks'] = []
contributor_marks = common.pack_mark_objects(Mark.objects.exclude(contributor__isnull=True).order_by('id'))
response['contributor_marks'] = contributor_marks
if 'country_code' in request.GET and len(request.GET['country_code']) > 0:
country_marks = Mark.objects.exclude(flaggings__gte=1).filter(
contributor_locale__isnull=True, country_code=request.GET['country_code']).order_by('id')
if len(country_marks) > 0:
response['country_total_marks'] = country_marks.count()
response['country_first_mark'] = country_marks[0].reference
response['country_last_mark'] = country_marks[response['country_total_marks'] - 1].reference
response['country_first_mark_at'] = country_marks[0].date_drawn.strftime("%a, %d %b %Y %I:%M:%S")
else:
pass
else:
pass
# Grab first and last mark
all_marks = Mark.objects.exclude(flaggings__gte=1).filter(contributor_locale__isnull=True)
last_mark = all_marks.order_by('-id')[0]
first_mark = all_marks.order_by('id')[0]
response['max_id'] = last_mark.id
response['last_mark'] = last_mark.reference
response['first_mark'] = first_mark.reference
response['first_mark_at'] = first_mark.date_drawn.strftime("%a, %d %b %Y %I:%M:%S")
response['total_countries'] = Mark.objects.values('country_code').distinct().count()
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
@require_POST
def save_mark(request):
# Default response
response = {'success': False}
# Check for mandatory POST data
if 'points_obj_simplified' in request.POST:
# Cosntruct mark data
mark_data = {'points_obj_simplified': request.POST['points_obj_simplified']}
if 'points_obj' in request.POST:
mark_data['points_obj'] = request.POST['points_obj']
if 'country_code' in request.POST:
mark_data['country_code'] = request.POST['country_code']
if 'invite' in request.POST:
mark_data['invite'] = request.POST['invite']
if 'contributor_locale' in request.POST:
mark_data['contributor_locale'] = request.POST['contributor_locale']
if 'contributor' in request.POST:
mark_data['contributor'] = request.POST['contributor']
try:
# Confirm that what we're getting is in fact JSON
if 'points_obj' in request.POST:
json = simplejson.loads(request.POST['points_obj'])
validate_json(json, False)
json_simplified = simplejson.loads(request.POST['points_obj_simplified'])
validate_json(json_simplified, True)
# Save new mark, handled by common.py
new_mark_reference = common.save_new_mark_with_data(mark_data, request.META['REMOTE_ADDR'])
# Successful response, returning new mark reference
response['mark_reference'] = new_mark_reference
except ValueError:
response['error'] = _('mark was too large. Please try drawing a smaller mark. Thanks!')
return HttpResponseBadRequest(simplejson.dumps(response), 'application/json')
except KeyError:
response['error'] = _('invalid JSON in the POST request')
return HttpResponseBadRequest(simplejson.dumps(response), 'application/json')
else:
# Error response
response['error'] = _('missing data in POST request')
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
# Return response as json
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
def validate_json(json, simplified):
# Confirms size below 30, 150k
if simplified:
if len(json) > 50000:
raise ValueError
else:
if len(json) > 250000:
raise ValueError
# Confirms existence of stroke and chooses random stroke to confirm structure
ran = json['strokes'][0][random.randrange(0, len(json['strokes'][0]))]
ran['angle']; ran['significance']; ran['time']; ran['y']; ran['x']; ran['z']; ran['speed']
# Checks x,y,z values to be within some maxium
for stroke in json['strokes'][0]:
if stroke['x'] > 2500 or stroke['y'] > 1600 or stroke['z'] != 0 or stroke['significance'] > 5:
raise ValueError
if stroke['x'] < 0 or stroke['y'] < 0 or stroke['significance'] < 0:
raise ValueError
def delete_mark(request):
# Completely remove the mark
response = {'success': False}
if not request.user.is_authenticated():
response['error'] = _('Authentication required')
json_response = simplejson.dumps(response)
return HttpResponseForbidden(json_response, 'application/json')
else:
if 'reference' in request.POST and len(request.POST['reference']) > 0:
try:
m = Mark.objects.get(reference=request.POST['reference'])
m.delete()
response['success'] = True
except Mark.DoesNotExist:
response['error'] = _('Mark does not exist')
json_response = simplejson.dumps(response)
return HttpResponseNotFound(json_response, 'application/json')
else:
response['error'] = _("No mark specified")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
def delete_all_based_on_ip(request):
# Completely remove all marks based on IP
response = {'success': False}
if not request.user.is_authenticated():
response['error'] = _('Authentication required')
json_response = simplejson.dumps(response)
return HttpResponseForbidden(json_response, 'application/json')
else:
if 'ip' in request.POST and len(request.POST['ip']) > 0:
try:
Mark.objects.filter(ip_address=request.POST['ip']).delete()
response['success'] = True
except Mark.DoesNotExist:
response['error'] = _('Marks from IP address do not exist')
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
else:
response['error'] = _("No IP address specified")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
@require_POST
def approve_mark(request):
# Approve the mark // CHECK
response = {'success': False}
if not request.user.is_authenticated():
response['error'] = _('Authentication required')
json_response = simplejson.dumps(response)
return HttpResponseForbidden(json_response, 'application/json')
else:
if 'reference' in request.POST and len(request.POST['reference']) > 0:
try:
m = Mark.objects.get(reference=request.POST['reference'])
should_approve = False
if request.POST['should_approve'] == "true":
should_approve = True
m.is_approved = should_approve
m.save()
response['success'] = True
except Mark.DoesNotExist:
response['error'] = _('Mark does not exist')
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
else:
response['error'] = _("No mark specified")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
json_response = simplejson.dumps(response)
return HttpResponse(json_response, 'application/json')
def get_mark(request):
# Get mark by ID
mark = None
try:
mark = Mark.objects.get(reference=request.GET['mark_id'])
except Mark.DoesNotExist:
pass
except Mark.MultipleObjectsReturned:
pass
previous_marks = ""
next_marks = ""
return HttpResponse(mark.points_obj_simplified, 'application/json')
def marks_by_offset(request):
# Parameters:
# offset: Integer -
# max: Integer - (defaults 15)
# country_code: String - filter by country-code
#
# returns json object including relevant marks with their attributes: id, reference string, points_obj, points_obj_simplified
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
marks_to_be_dumped = None
did_fail_get_marks = False
max_returned = 50
# We've got an offset to play with
if 'offset' in request.GET:
# An offset requires a max value to be returned from this offset
if 'max' in request.GET:
offset = request.GET['offset']
max = request.GET['max']
if max > max_returned:
max = max_returned
# We can also filter by country code if need be
if 'country_code' in request.GET:
marks_to_be_dumped = Mark.objects.exclude.exclude(contributor_locale__isnull=False).filter(country_code=request.GET['country_code'])[offset:max]
else:
marks_to_be_dumped = Mark.objects.all()[offset:max]
else:
response['success'] = False
response['error'] = _("Querying by offset also requires a 'max' POST var")
did_fail_get_marks = True
else:
# No special query parameters, query for all marks
marks_to_be_dumped = Mark.objects.exclude(contributor_locale__isnull=False)
# Check that we've got marks to dump
if not did_fail_get_marks:
if marks_to_be_dumped:
# Dump out
all_marks = []
for m in marks_to_be_dumped:
# Append to all marks
all_marks.append({'date_drawn': m.date_drawn.strftime(
"%a, %d %b %Y %I:%M:%S"),
'reference': m.reference,
'points_obj_simplified': m.points_obj_simplified,
'contributor': m.contributor, 'country_code': m.country_code})
response['success'] = True
response['marks'] = all_marks
else:
# No marks to dump
response['success'] = False
response['error'] = _("No marks to be parsed")
json_response = simplejson.dumps(response)
return HttpResponseServerError(json_response, 'application/json')
# Dump and return
json_response = simplejson.dumps(response, default=dthandler)
return HttpResponse(json_response, 'application/json')
def marks_by_locale(request):
# Parameters:
# country_code: String - filter by country-code
# max: Integer - (defaults 15)
#
# returns json object including relevant marks with their attributes: id, reference string, points_obj, points_obj_simplified
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
max_returned = 15
marks_to_be_dumped = None
did_fail_get_marks = False
def marks_by_reference(request):
# Parameters:
# reference_mark: String - slug of reference mark
# include_back: Integer - number of returned marks before the reference mark (defaults to 0)
# include_forward: Integer - number of returned marks after the reference mark (defaults to 15)
# include_mark: Boolean - include the reference mark (defaults true)
# country_code: String - filter by country-code
# returns json object including relevant marks with their attributes: id, reference string, points_obj, points_obj_simplified
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
reference_mark = None
include_back = 0
include_forward = 15
max_before_after = 20
include_mark = True
country_code = None
marks_to_be_dumped = None
did_fail_get_marks = False
m_offset = None
# default limit returns 19 marks
m_limit = include_back + 1 + include_forward
all_marks = None
offset_index = 0
total_marks = 0
if 'reference' in request.GET:
reference_mark = request.GET['reference']
try:
m_offset = Mark.objects.get(reference=reference_mark)
except Mark.DoesNotExist:
response['success'] = False
response['error'] = _("Reference mark doesn't exist")
did_fail_get_marks = True
except Mark.MultipleObjectsReturned:
response['success'] = False
response['error'] = _("Multiple marks found for reference")
did_fail_get_marks = True
if 'include_mark' in request.GET:
try:
if int(request.GET['include_mark']) == 0:
include_mark = False
except ValueError:
return HttpResponseBadRequest()
if 'include_forward' in request.GET:
try:
include_forward = int(request.GET['include_forward'])
if include_forward > max_before_after:
include_forward = max_before_after
except ValueError:
return HttpResponseBadRequest()
if 'include_back' in request.GET:
try:
include_back = int(request.GET['include_back'])
if include_back > max_before_after:
include_back = max_before_after
except ValueError:
return HttpResponseBadRequest()
if 'country_code' in request.GET:
kountry_code = request.GET['country_code']
all_marks = Mark.objects.exclude(flaggings__gte=1).filter(country_code=kountry_code, contributor_locale__isnull=True).order_by('id')
else:
all_marks = Mark.objects.exclude(flaggings__gte=1).filter(contributor_locale__isnull=True).order_by('id')
# Find mark to start with backwards, if requested.
if include_back and not did_fail_get_marks:
# m_offset is our reference mark
back_marks = list(all_marks.order_by('-id').filter(id__lt=m_offset.id)[:include_back])
# These marks needed to be queried in reverse. Undo that now.
back_marks.reverse()
else:
back_marks = []
if not did_fail_get_marks:
# Find last, forward mark
forward_marks = list(all_marks.filter(id__gt=m_offset.id)[:include_forward])
# Combine backwards and forward marks with reference.
marks_to_be_dumped = back_marks + [m_offset] + forward_marks
if not marks_to_be_dumped:
response['success'] = False
response['error'] = _("No marks to be dumped")
did_fail_get_marks = True
else:
# required param
response['success'] = False
response['error'] = _("Querying by reference requires a reference string")
did_fail_get_marks = True
# Check that we've got marks to dump
if not did_fail_get_marks:
if marks_to_be_dumped:
# Dump out
all_marks = []
for m in marks_to_be_dumped:
is_reference_mark = False
if m.reference == reference_mark:
is_reference_mark = True
if include_mark == False and is_reference_mark:
pass
else:
# Append to all marks
all_marks.append({'is_reference_mark': is_reference_mark,
'date_drawn': m.date_drawn.strftime("%a, %d %b %Y %I:%M:%S"),
'reference': m.reference,
'id': m.id,
'points_obj_simplified': m.points_obj_simplified,
'contributor': m.contributor,
'country_code': m.country_code})
response['success'] = True
response['marks'] = all_marks
else:
# No marks to dump
response['success'] = False
response['error'] = _("No marks to be parsed")
json_response = simplejson.dumps(response)
return HttpResponseNotFound(json_response, 'application/json')
# Dump and return
json_response = simplejson.dumps(response, default=dthandler)
return HttpResponse(json_response, 'application/json')
def all_marks(request):
# Get all marks, all data per mark excluding full points object
# This method can be queried via POST depending what the frontend requires
# Handler for dumping datetime field as JSON
#
# offset: Integer -
# max: Integer - (defaults 15)
# country_code: String - filter by country-code
#
# returns json object including relevant marks with their attributes: id, reference string, points_obj, points_obj_simplified
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
include_back = 3
include_forward = 15
include_mark = True
max_returned = 50
marks_to_be_dumped = None
did_fail_get_marks = False
# We've got an offset to play with
if 'offset' in request.GET:
offset = request.GET['offset']
# An offset must be a positive number
if int(offset) < 0:
response['success'] = False
response['error'] = _("Offset must be greater than zero")
return HttpResponseBadRequest(simplejson.dumps(response, default=dthandler))
# An offset requires a max value to be returned from this offset
if 'max' in request.GET:
max = request.GET['max']
if max > max_returned:
max = max_returned
# We can also filter by country code if need be
if 'country_code' in request.GET:
marks_to_be_dumped = Mark.objects.exclude(
flaggings__gte=1).filter(
contributor_locale__isnull=True).order_by(
'id').filter(country_code=request.GET['country_code'])[offset:max]
else:
marks_to_be_dumped = Mark.objects.exclude(
flaggings__gte=1).filter(
contributor_locale__isnull=True).order_by('id')[offset:max]
else:
response['success'] = False
response['error'] = _("Querying by offset also requires a 'max' POST var")
return HttpResponseBadRequest(simplejson.dumps(response, default=dthandler))
else:
did_fail_get_marks = True
if not did_fail_get_marks:
if marks_to_be_dumped:
# Dump out
all_marks = []
for m in marks_to_be_dumped:
# Append to all marks
all_marks.append({'date_drawn': m.date_drawn.strftime("%a, %d %b %Y %I:%M:%S"), 'reference': m.reference, 'id': m.id, 'points_obj_simplified': m.points_obj_simplified, 'contributor': m.contributor, 'country_code': m.country_code, 'flaggings': m.flaggings})
response['success'] = True
response['marks'] = all_marks
else:
# No marks to dump
response['success'] = False
response['error'] = _("No marks available")
# Dump and return
json_response = simplejson.dumps(response, default=dthandler)
return HttpResponse(json_response, 'application/json')
def recent_marks(request):
# Get all recent marks (previous day)
# This method can be queried via POST depending what the frontend requires
# Handler for dumping datetime field as JSON
#
# returns json object including relevant marks with their attributes: id, reference string, points_obj, points_obj_simplified
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
include_back = 3
include_forward = 15
include_mark = True
max_returned = 15
marks_to_be_dumped = None
did_fail_get_marks = False
yesterday = date.today()-timedelta(days=1)
marks_to_be_dumped = Mark.objects.filter(date_drawn__gte=yesterday)
all_marks = []
for m in marks_to_be_dumped:
# Append to all marks
all_marks.append({'date_drawn': m.date_drawn.strftime("%a, %d %b %Y %I:%M:%S"), 'reference': m.reference, 'id': m.id, 'points_obj_simplified': m.points_obj_simplified, 'contributor': m.contributor, 'country_code': m.country_code, 'flaggings': m.flaggings})
response['success'] = True
response['marks'] = all_marks
# Dump and return
json_response = simplejson.dumps(response, default=dthandler)
return HttpResponse(json_response, 'application/json')
def marks_by_flagged(request):
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
response = {'success': False}
include_back = 3
include_forward = 15
include_mark = True
max_returned = 15
marks_to_be_dumped = Mark.objects.filter(flaggings__gte=1).exclude(is_approved=1).order_by('id') # CHECK offset?
# Check that we've got marks to dump
if marks_to_be_dumped:
# Dump out
all_marks = []
for m in marks_to_be_dumped:
# Append to all marks
all_marks.append({'date_drawn': m.date_drawn, 'reference': m.reference, 'id': m.id, 'points_obj_simplified': m.points_obj_simplified, 'contributor': m.contributor, 'country_code': m.country_code, 'is_approved': m.is_approved, 'ip_address': m.ip_address})
response['success'] = True
response['marks'] = all_marks
else:
# No marks to dump
response['success'] = False
response['error'] = _("No marks to be parsed")
# Dump and return
json_response = simplejson.dumps(response, default=dthandler)
return HttpResponse(json_response, 'application/json')
def update_language(request):
response = {}
if 'language_code' in request.GET:
set_language(request)
else:
pass
return response