-
Notifications
You must be signed in to change notification settings - Fork 49
/
utils.py
587 lines (503 loc) · 21.3 KB
/
utils.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
# -*- coding: utf-8 -*-
import json
import os
import uuid
from datetime import datetime
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.gis.geos import Point, Polygon
from django.contrib.gis.measure import D
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Count, Max, Min
from django.template import Context, Template
from core.utilities import extract_time
from localities.models import (
Attribute, Changeset, Country, Domain, Locality, LocalityArchive,
Specification, SynonymLocalities, UnconfirmedSynonym, Value, ValueArchive
)
from localities.tasks import regenerate_cache, regenerate_cache_cluster
from social_users.utils import get_profile
def extract_updates(updates):
# updates
last_updates = []
histories = updates
for update in histories:
update['locality_uuid'] = ''
update['locality'] = ''
if update['edit_count'] == 1:
# get the locality to show in web
try:
locality = Locality.objects.get(pk=update['locality_id'])
update['locality_uuid'] = locality.uuid
update['locality'] = locality.name
except Locality.DoesNotExist:
update['locality_uuid'] = 'unknown'
update['locality'] = 'unknown'
if 'version' in update:
if update['version'] == 1:
update['mode'] = 1
else:
update['mode'] = 2
else:
update['mode'] = 1
last_updates.append({'author': update['changeset__social_user__username'],
'author_nickname': update['nickname'],
'date_applied': update['changeset__created'],
'mode': update['mode'],
'locality': update['locality'],
'locality_uuid': update['locality_uuid'],
'data_count': update['edit_count']})
return last_updates
def get_country_statistic(query):
output = ''
try:
if query != '':
# getting country's polygon
country = Country.objects.get(
name__iexact=query)
polygons = country.polygon_geometry
# get cache
filename = os.path.join(
settings.CLUSTER_CACHE_DIR,
country.name + '_statistic'
)
try:
file = open(filename, 'r')
data = file.read()
output = json.loads(data)
except IOError:
try:
# query for each of ATTRIBUTE
healthsites = get_heathsites_master().in_polygon(
polygons)
output = get_statistic(healthsites)
output = json.dumps(output, cls=DjangoJSONEncoder)
file = open(filename, 'w')
file.write(output) # python will convert \n to os.linesep
file.close() # you can omit in most cases as the destructor will call it
output = json.loads(output)
except Exception:
pass
else:
# get cache
filename = os.path.join(
settings.CLUSTER_CACHE_DIR,
'world_statistic'
)
try:
file = open(filename, 'r')
data = file.read()
output = json.loads(data)
except IOError:
try:
if not os.path.exists(settings.CLUSTER_CACHE_DIR):
os.makedirs(settings.CLUSTER_CACHE_DIR)
# query for each of attribute
healthsites = get_heathsites_master()
output = get_statistic(healthsites)
output = json.dumps(output, cls=DjangoJSONEncoder)
file = open(filename, 'w')
file.write(output) # python will convert \n to os.linesep
file.close() # you can omit in most cases as the destructor will call it
output = json.loads(output)
except Exception as e:
print str(e)
except Country.DoesNotExist:
output = ''
return output
def get_heathsites_master():
return Locality.objects.filter(is_master=True).order_by('name')
def get_json_from_request(request):
# special request:
special_request = ['name', 'source', 'long', 'lat', 'csrfmiddlewaretoken', 'uuid']
mstring = []
json = {}
for key in request.POST.iterkeys(): # 'for key in request.GET' works too.
# Add filtering logic here.
valuelist = request.POST.getlist(key)
mstring.extend(['%s=%s' % (key, val) for val in valuelist])
for str in mstring:
req = str.split('=', 1)
json[req[0].lower()] = req[1]
try:
Attribute.objects.get(key=req[0].lower())
except Attribute.DoesNotExist:
if req[0] not in special_request:
tmp_changeset = Changeset.objects.create(
social_user=request.user
)
attribute = Attribute()
attribute.key = req[0]
attribute.changeset = tmp_changeset
attribute.save()
domain = Domain.objects.get(name='Health')
specification = Specification()
specification.domain = domain
specification.attribute = attribute
specification.changeset = tmp_changeset
specification.save()
# check mandatory
is_valid = True
if not json['name'] or json['name'] == '':
is_valid = False
json['invalid_key'] = 'name'
if not json['lat'] or json['lat'] == '':
is_valid = False
json['invalid_key'] = 'latitude'
if not json['long'] or json['long'] == '':
is_valid = False
json['invalid_key'] = 'longitude'
if is_valid:
domain = Domain.objects.get(name='Health')
attributes = Specification.objects.filter(domain=domain).filter(required=True)
for attribute in attributes:
try:
if len(json[attribute.attribute.key]) == 0:
is_valid = False
json['invalid_key'] = attribute.attribute.key
break
except Exception:
pass
json['is_valid'] = is_valid
return json
def get_locality_detail(locality, changes):
# count completeness based attributes
obj_repr = locality.repr_dict()
# get master
masters = []
for val in SynonymLocalities.objects.filter(synonym_id=locality.id):
master_uuid = val.locality.uuid
master_name = val.locality.name
masters.append({'name': master_name, 'uuid': master_uuid})
# get synonyms
synonyms = []
for val in SynonymLocalities.objects.filter(locality_id=locality.id):
synonym_uuid = val.synonym.uuid
synonym_name = val.synonym.name
synonyms.append({'name': synonym_name, 'uuid': synonym_uuid})
# get potential master
potential_masters = []
for val in UnconfirmedSynonym.objects.filter(synonym_id=locality.id):
master_uuid = val.locality.uuid
master_name = val.locality.name
potential_masters.append({'name': master_name, 'uuid': master_uuid})
# get unconfirmed synonyms
unconfirmed_synonyms = []
for val in UnconfirmedSynonym.objects.filter(locality_id=locality.id):
synonym_uuid = val.synonym.uuid
synonym_name = val.synonym.name
unconfirmed_synonyms.append({'name': synonym_name, 'uuid': synonym_uuid})
obj_repr[u'masters'] = masters
obj_repr[u'synonyms'] = synonyms
obj_repr[u'potential_masters'] = potential_masters
obj_repr[u'unconfirmed_synonyms'] = unconfirmed_synonyms
# get latest update
try:
updates = []
last_updates = locality_updates(locality.id, datetime.now())
for last_update in last_updates:
updates.append({'last_update': last_update['changeset__created'],
'uploader': last_update['changeset__social_user__username'],
'nickname': last_update['nickname'],
'changeset_id': last_update['changeset']})
obj_repr.update({'updates': updates})
except Exception:
pass
# FOR HISTORY
obj_repr['history'] = False
if changes:
# get updates
changeset = Changeset.objects.get(id=changes)
obj_repr['updates'][0]['last_update'] = changeset.created
obj_repr['updates'][0]['uploader'] = changeset.social_user.username
obj_repr['updates'][0]['changeset_id'] = changes
# get profile name
profile = get_profile(User.objects.get(username=changeset.social_user.username))
obj_repr['updates'][0]['nickname'] = profile.screen_name
# check archive
try:
localityArchives = (
LocalityArchive.objects.filter(changeset=changes).filter(uuid=obj_repr['uuid'])
)
for archive in localityArchives:
obj_repr['geom'] = (archive.geom.x, archive.geom.y)
obj_repr['history'] = True
except LocalityArchive.DoesNotExist:
pass
try:
localityArchives = (
ValueArchive.objects.filter(changeset=changes).filter(locality_id=locality.id)
)
for archive in localityArchives:
try:
specification = Specification.objects.get(id=archive.specification_id)
obj_repr['values'][specification.attribute.key] = archive.data
obj_repr['history'] = True
except Specification.DoesNotExist:
pass
except LocalityArchive.DoesNotExist:
pass
return obj_repr
def locality_create(request):
if request.method == 'POST':
if request.user.is_authenticated():
json_request = get_json_from_request(request)
# checking mandatory
if json_request['is_valid'] is True:
tmp_changeset = Changeset.objects.create(
social_user=request.user
)
# generate new uuid
tmp_uuid = uuid.uuid4().hex
loc = Locality()
loc.changeset = tmp_changeset
loc.domain = Domain.objects.get(name='Health')
loc.uuid = tmp_uuid
# generate unique upstream_id
loc.upstream_id = u'web¶{}'.format(tmp_uuid)
loc.geom = Point(
float(json_request['long']), float(json_request['lat'])
)
loc.name = json_request['name']
loc.save()
# ------------------------------------------------------
loc.set_values(json_request, request.user, tmp_changeset)
regenerate_cache.delay(tmp_changeset.pk, loc.pk)
regenerate_cache_cluster.delay()
return {'success': json_request['is_valid'], 'uuid': tmp_uuid, 'reason': ''}
else:
return {'success': json_request['is_valid'],
'reason': json_request['invalid_key'] + ' can not be empty'}
else:
return {'success': False, 'reason': 'Not Login Yet'}
return {'success': False, 'reason': 'There is error occured'}
def locality_edit(request):
if request.method == 'POST':
if request.user.is_authenticated():
json_request = get_json_from_request(request)
# checking mandatory
if json_request['is_valid'] is True:
locality = Locality.objects.get(uuid=json_request['uuid'])
old_geom = [locality.geom.x, locality.geom.y]
locality.set_geom(float(json_request['long']), float(json_request['lat']))
locality.name = json_request['name']
# there are some changes so create a new changeset
tmp_changeset = Changeset.objects.create(
social_user=request.user
)
locality.changeset = tmp_changeset
locality.save()
locality.set_values(json_request, request.user, tmp_changeset)
# if location is changed
new_geom = [locality.geom.x, locality.geom.y]
if new_geom != old_geom:
regenerate_cache_cluster.delay()
regenerate_cache.delay(tmp_changeset.id, locality.pk)
return {
'success': json_request['is_valid'], 'uuid': json_request['uuid'],
'reason': ''
}
else:
return {'success': json_request['is_valid'],
'reason': json_request['invalid_key'] + ' can not be empty'}
else:
return {'success': False, 'reason': 'Not Login Yet'}
return {'success': False, 'reason': 'There is error occured'}
def get_statistic(healthsites):
# locality which in polygon
# data for frontend
healthsites_number = healthsites.count()
values = Value.objects.filter(locality__in=healthsites)
hospital_number = values.filter(
specification__attribute__key='type').filter(
data__icontains='hospital').count()
medical_clinic_number = values.filter(
specification__attribute__key='type').filter(
data__icontains='clinic').count()
orthopaedic_clinic_number = values.filter(
specification__attribute__key='type').filter(
data__icontains='orthopaedic').count()
# count completeness based attributes
# this make long waiting, need to more good query
complete = healthsites.filter(completeness=100).count()
partial = healthsites.filter(completeness__gt=30).filter(completeness__lt=100).count()
basic = healthsites.filter(completeness__lte=30).count()
output = {
'numbers': {
'hospital': hospital_number,
'medical_clinic': medical_clinic_number,
'orthopaedic_clinic': orthopaedic_clinic_number
},
'completeness': {'complete': complete, 'partial': partial, 'basic': basic},
'localities': healthsites_number
}
# updates
histories = localities_updates(healthsites)
output['last_update'] = extract_updates(histories)
return output
def localities_updates(locality_ids):
updates = []
# from locality archive
ids = LocalityArchive.objects.filter(object_id__in=locality_ids).order_by(
'-changeset__created').values(
'changeset', 'object_id').annotate(
id=Min('id')).values('id')
updates_temp = (
LocalityArchive.objects
.filter(object_id__in=locality_ids)
.filter(id__in=ids)
.order_by('-changeset__created')
.values('changeset', 'changeset__created', 'changeset__social_user__username', 'version')
.annotate(edit_count=Count('changeset'), locality_id=Max('object_id'))
)
changesets = []
for update in updates_temp:
changesets.append(update['changeset'])
updates.append(get_update_detail(update))
# get from locality if not in Locality Archive yet
updates_temp = (
locality_ids
.exclude(changeset__in=changesets)
.order_by('-changeset__created')
.values('changeset', 'changeset__created', 'changeset__social_user__username', 'version')
.annotate(edit_count=Count('changeset'), locality_id=Max('id'))[:15]
)
for update in updates_temp:
updates.append(get_update_detail(update))
updates.sort(key=extract_time, reverse=True)
return updates[:10]
def locality_updates(locality_id, date):
updates = []
updatestemp = LocalityArchive.objects.filter(object_id=locality_id).filter(
changeset__created__lt=date).order_by(
'-changeset__created').values(
'changeset', 'changeset__created', 'changeset__social_user__username').annotate(
edit_count=Count('changeset'))[:10]
changesets = []
for update in updatestemp:
changesets.append(update['changeset'])
updates.append(get_update_detail(update))
# get from locality if not in Locality Archive yet
updatestemp = Locality.objects.filter(id=locality_id).values(
'changeset', 'changeset__created', 'changeset__social_user__username').annotate(
edit_count=Count('changeset'))
for update in updatestemp:
if not update['changeset'] in changesets:
changesets.append(update['changeset'])
updates.append(get_update_detail(update))
updates.sort(key=extract_time, reverse=True)
return updates[:10]
def get_locality_by_spec_data(spec, data, uuid):
try:
locality = Locality.objects.get(uuid=uuid)
localities = get_heathsites_master().filter(
geom__distance_lte=(locality.geom, D(mi=100))
).exclude(uuid=uuid).distance(locality.geom).order_by('-distance')
except Locality.DoesNotExist:
localities = get_heathsites_master()
try:
if spec == 'attribute':
if uuid:
localities = Value.objects.filter(
data__icontains=data).filter(locality__in=localities).values('locality')[:5]
else:
localities = Value.objects.filter(
data__icontains=data).values('locality')
else:
if uuid:
localities = Value.objects.filter(
specification__attribute__key=spec).filter(
data__icontains=data).filter(locality__in=localities).values('locality')[:5]
else:
localities = Value.objects.filter(
specification__attribute__key=spec).filter(
data__icontains=data).values('locality')
except Value.DoesNotExist:
pass
return Locality.objects.filter(id__in=localities)
def search_locality_by_spec_data(spec, data, uuid):
localities = get_locality_by_spec_data(spec, data, uuid)
output = get_statistic(localities)
output['locality_name'] = ''
output['location'] = 0.0
if uuid:
try:
locality = Locality.objects.get(uuid=uuid)
locality_name = locality.name
output['locality_name'] = locality_name
output['location'] = {'x': '%f' % locality.geom.x, 'y': '%f' % locality.geom.y}
except Locality.DoesNotExist:
pass
output = json.dumps(output, cls=DjangoJSONEncoder)
output = json.loads(output)
return output
def search_locality_by_tag(query):
try:
localities = (
Value.objects
.filter(specification__attribute__key='tags')
.filter(data__icontains='|' + query + '|').values('locality')
)
localities = get_heathsites_master().filter(id__in=localities)
output = json.dumps(get_statistic(localities), cls=DjangoJSONEncoder)
output = json.loads(output)
return output
except Value.DoesNotExist:
return []
def render_fragment(template, context):
"""
Render a template fragment using provided context
"""
t = Template(template)
c = Context(context)
return t.render(c)
def parse_bbox(bbox):
"""
Convert a textual bbox to a GEOS polygon object
This function assumes that any raised exceptions will be handled upstream
"""
tmp_bbox = map(float, bbox.split(','))
if tmp_bbox[0] > tmp_bbox[2] or tmp_bbox[1] > tmp_bbox[3]:
# bbox is not properly formatted minLng, minLat, maxLng, maxLat
raise ValueError
# create polygon from bbox
return Polygon.from_bbox(tmp_bbox)
def user_updates(user, date):
updates = []
# from locality archive
ids = (
LocalityArchive.objects
.filter(changeset__social_user=user).filter(changeset__created__lt=date)
.order_by('-changeset__created')
.values('changeset', 'object_id')
.annotate(id=Min('id')).values('id')
)
updates_temp = (
LocalityArchive.objects
.filter(changeset__social_user=user).filter(changeset__created__lt=date)
.filter(id__in=ids)
.order_by('-changeset__created')
.values('changeset', 'changeset__created', 'changeset__social_user__username', 'version')
.annotate(edit_count=Count('changeset'), locality_id=Max('object_id'))[:10]
)
changesets = []
for update in updates_temp:
changesets.append(update['changeset'])
updates.append(get_update_detail(update))
# get from locality if not in Locality Archive yet
updates_temp = (
Locality.objects
.filter(changeset__social_user=user).exclude(changeset__in=changesets)
.order_by('-changeset__created')
.values('changeset', 'changeset__created', 'changeset__social_user__username', 'version')
.annotate(edit_count=Count('changeset'), locality_id=Max('id'))[:10]
)
for update in updates_temp:
updates.append(get_update_detail(update))
updates.sort(key=extract_time, reverse=True)
return updates[:10]
def get_update_detail(update):
profile = get_profile(User.objects.get(username=update['changeset__social_user__username']))
update['nickname'] = profile.screen_name
update['changeset__created'] = update['changeset__created']
return update