@staticmethod
    def cap_area_tag_onaccept(form):

        form_vars = form.vars

        area_id = form_vars.get("area_id", None)
        if not area_id:
            # comes from assign method
            return

        db = current.db
        atable = db.cap_area
        same_code = current.deployment_settings.get_cap_same_code()
        tag = form_vars.get("tag")

        arow = db(atable.id == area_id).select(atable.alert_id,
                                               limitby=(0, 1)).first()
        alert_id = arow.alert_id

        if tag and same_code:
            if tag == "SAME":
                # SAME tag referes to some location_id in CAP
                ttable = current.s3db.gis_location_tag

                tquery = (ttable.tag == same_code) & \
                         (ttable.value == form_vars.get("value")) & \
                         (ttable.deleted != True)
                trow = db(tquery).select(ttable.location_id,
                                         limitby=(0, 1)).first()
                if trow:
                    # Match
                    ltable = db.cap_area_location
                    ldata = {"area_id": area_id,
                             "alert_id": alert_id or None,
                             "location_id": trow.location_id,
                             }
                    lid = ltable.insert(**ldata)
                    current.auth.s3_set_record_owner(ltable, lid)
                    # Uncomment this when required
                    #ldata["id"] = lid
                    #s3db.onaccept(ltable, ldata)

        if alert_id:
            # This is not template area
            db(db.cap_area_tag.id == form_vars.id).update(alert_id = alert_id)

    # -------------------------------------------------------------------------
Link location if area_tag has SAME code
dataset/ETHPy150Open sahana/eden/modules/s3db/cap.py/S3CAPModel.cap_area_tag_onaccept