~robbyoconnor/sahana-eden/surveytool

« back to all changes in this revision

Viewing changes to controllers/gis.py

  • Committer: Robert O'Connor
  • Date: 2010-09-15 14:29:32 UTC
  • mfrom: (840.1.415 eden)
  • Revision ID: robby.oconnor@gmail.com-20100915142932-7uw2dm1yqdq09thb
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
311
311
    return output
312
312
 
313
313
def location_duplicates():
314
 
 
315
 
    """ Handle De-duplication of Locations """
 
314
    """
 
315
        Handle De-duplication of Locations
 
316
    """
316
317
 
317
318
    if deployment_settings.get_security_map() and not shn_has_role("MapAdmin"):
318
319
        unauthorised()
319
320
 
320
321
    def delete_location(old, new):
321
322
        # Find all tables which link to the Locations table
322
 
        # via 'location_id'
323
 
        tables = []
324
 
        # via another field (such as 'parent')
325
 
        #tables2 = {}
326
 
        for table in db.tables:
327
 
            if "location_id" in db[table]:
328
 
                tables.append(table)
329
 
            #else:
330
 
            #    # Danger: 'else' doesn't catch secondary references
331
 
            #    # This would need to be the only routine.
332
 
            #    count = 0
333
 
            #    for field in db[table].fields:
334
 
            #        if db[table][field].type == "reference gis_location":
335
 
            #            if count == 0:
336
 
            #                tables2[table] = {}
337
 
            #            tables2[table][count] = field
338
 
            #            count += 1
 
323
        # @ToDo Replace with db.gis_location._referenced_by
 
324
        tables = shn_table_links("gis_location")
339
325
 
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)
344
 
        
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)
351
 
        
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)
359
 
        
 
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})
 
331
 
360
332
        # Remove the record
361
333
        db(db.gis_location.id == old).update(deleted=True)
362
334
        
363
335
        return
364
336
 
365
 
    def open_btn(field):
366
 
        return A(T("Load Details"), _id=field, _href=URL(r=request, f="location"), _class="action-btn", _target="_blank")
367
 
    
 
337
    def open_btn(id):
 
338
        return A(T("Load Details"), _id=id, _href=URL(r=request, f="location"), _class="action-btn", _target="_blank")
 
339
    
 
340
    def links_btn(id):
 
341
        return A(T("Linked Records"), _id=id, _href=URL(r=request, f="location_links"), _class="action-btn", _target="_blank")
 
342
    
 
343
    # Unused: we do all filtering client-side using AJAX
 
344
    filter = request.vars.get("filter", None)
 
345
    
 
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
 
348
    if filter:
 
349
        requires = IS_ONE_OF(db, "gis_location.id", repr_select, filterby="level", filter_opts=(filter,), orderby="gis_location.name", sort=True)
 
350
    else:
 
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
373
357
                          )
374
358
    
389
373
        response.error = T("Need to select 2 Locations")
390
374
 
391
375
    return dict(form=form)
 
376
 
 
377
def location_links():
 
378
    """
 
379
        @arg id - the location record id
 
380
        Returns a JSON array of records which link to the specified location
 
381
    """
 
382
 
 
383
    try:
 
384
        record_id = request.args[0]
 
385
    except:
 
386
        item = s3xrc.xml.json_message(False, 400, "Need to specify a record ID!")
 
387
        raise HTTP(400, body=item)
 
388
 
 
389
    try:
 
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
 
394
    except:
 
395
        item = s3xrc.xml.json_message(False, 404, "Record not found!")
 
396
        raise HTTP(404, body=item)
 
397
 
 
398
    import gluon.contrib.simplejson as json
 
399
 
 
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")
392
403
    
 
404
    results = []
 
405
    for table in tables:
 
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:
 
412
                id = result.id
 
413
                # We currently have no easy way to get the default represent for a table!
 
414
                try:
 
415
                    # Locations & Persons
 
416
                    represent = eval("shn_%s_represent(id)" % table)
 
417
                except:
 
418
                    try:
 
419
                        # Organisations
 
420
                        represent = eval("shn_%s_represent(id)" % resource)
 
421
                    except:
 
422
                        try:
 
423
                            # Many tables have a Name field
 
424
                            represent = (id and [db[table][id].name] or ["None"])[0]
 
425
                        except:
 
426
                            # Fallback
 
427
                            represent = id
 
428
                results.append({
 
429
                    "module" : module,
 
430
                    "resource" : resource,
 
431
                    "id" : id,
 
432
                    "represent" : represent
 
433
                    })
 
434
 
 
435
    output = json.dumps(results)
 
436
    return output
 
437
 
393
438
# -----------------------------------------------------------------------------
394
439
def map_service_catalogue():
395
440
    """