313
313
def location_duplicates():
315
""" Handle De-duplication of Locations """
315
Handle De-duplication of Locations
317
318
if deployment_settings.get_security_map() and not shn_has_role("MapAdmin"):
320
321
def delete_location(old, new):
321
322
# Find all tables which link to the Locations table
324
# via another field (such as 'parent')
326
for table in db.tables:
327
if "location_id" in db[table]:
330
# # Danger: 'else' doesn't catch secondary references
331
# # This would need to be the only routine.
333
# for field in db[table].fields:
334
# if db[table][field].type == "reference gis_location":
336
# tables2[table] = {}
337
# tables2[table][count] = field
323
# @ToDo Replace with db.gis_location._referenced_by
324
tables = shn_table_links("gis_location")
340
# Update all pointers to the record to be removed
341
326
for table in tables:
342
query = db[table].location_id == old
343
db(query).update(location_id=new)
345
#for table in tables2:
346
# for count in range(len(tables2[table])):
347
# field = tables2[str(db[table])][count]
348
# query = db[table][field] == old
349
# # How to use a variable as the Keyword here?
350
# db(query).update(eval(field)=new)
352
# Manually update others (until we can do it properly)
353
query = db.gis_location.parent == old
354
db(query).update(parent=new)
355
query = db.pr_presence.orig_id == old
356
db(query).update(orig_id=new)
357
query = db.pr_presence.dest_id == old
358
db(query).update(dest_id=new)
327
for count in range(len(tables[table])):
328
field = tables[str(db[table])][count]
329
query = db[table][field] == old
330
db(query).update(**{field:XXX})
360
332
# Remove the record
361
333
db(db.gis_location.id == old).update(deleted=True)
366
return A(T("Load Details"), _id=field, _href=URL(r=request, f="location"), _class="action-btn", _target="_blank")
338
return A(T("Load Details"), _id=id, _href=URL(r=request, f="location"), _class="action-btn", _target="_blank")
341
return A(T("Linked Records"), _id=id, _href=URL(r=request, f="location_links"), _class="action-btn", _target="_blank")
343
# Unused: we do all filtering client-side using AJAX
344
filter = request.vars.get("filter", None)
346
#repr_select = lambda l: len(l.name) > 48 and "%s..." % l.name[:44] or l.name
347
repr_select = lambda l: l.level and "%s (%s)" % (l.name, response.s3.gis.location_hierarchy[l.level]) or l.name
349
requires = IS_ONE_OF(db, "gis_location.id", repr_select, filterby="level", filter_opts=(filter,), orderby="gis_location.name", sort=True)
351
requires = IS_ONE_OF(db, "gis_location.id", repr_select, orderby="gis_location.name", sort=True, zero=T("Select a location"))
368
352
table = db.gis_location
369
353
form = SQLFORM.factory(
370
Field("old", table, requires=IS_ONE_OF(db, "gis_location.id", "%(name)s"), label = SPAN(B(T("Old")), " (" + Tstr("To delete") + ")"), comment=open_btn("btn_old")),
371
Field("new", table, requires=IS_ONE_OF(db, "gis_location.id", "%(name)s"), label = B(T("New")), comment=open_btn("btn_new")),
354
Field("old", table, requires=requires, label = SPAN(B(T("Old")), " (" + Tstr("To delete") + ")"), comment=DIV(links_btn("linkbtn_old"), open_btn("btn_old"))),
355
Field("new", table, requires=requires, label = B(T("New")), comment=DIV(links_btn("linkbtn_new"), open_btn("btn_new"))),
372
356
formstyle = s3_formstyle
389
373
response.error = T("Need to select 2 Locations")
391
375
return dict(form=form)
377
def location_links():
379
@arg id - the location record id
380
Returns a JSON array of records which link to the specified location
384
record_id = request.args[0]
386
item = s3xrc.xml.json_message(False, 400, "Need to specify a record ID!")
387
raise HTTP(400, body=item)
390
deleted = (db.gis_location.deleted == False)
391
query = (db.gis_location.id == record_id)
392
query = deleted & query
393
record = db(query).select(db.gis_location.id, limitby=(0, 1)).first().id
395
item = s3xrc.xml.json_message(False, 404, "Record not found!")
396
raise HTTP(404, body=item)
398
import gluon.contrib.simplejson as json
400
# Find all tables which link to the Locations table
401
# @ToDo Replace with db.gis_location._referenced_by
402
tables = shn_table_links("gis_location")
406
for count in range(len(tables[table])):
407
field = tables[str(db[table])][count]
408
query = db[table][field] == record_id
409
_results = db(query).select()
410
module, resource = table.split("_", 1)
411
for result in _results:
413
# We currently have no easy way to get the default represent for a table!
415
# Locations & Persons
416
represent = eval("shn_%s_represent(id)" % table)
420
represent = eval("shn_%s_represent(id)" % resource)
423
# Many tables have a Name field
424
represent = (id and [db[table][id].name] or ["None"])[0]
430
"resource" : resource,
432
"represent" : represent
435
output = json.dumps(results)
393
438
# -----------------------------------------------------------------------------
394
439
def map_service_catalogue():