Esempio n. 1
0
def handle_ObjectStateModified(site, uid):
    """ Handle when a CaseStudy is published / unpublished

    If published:
        * if this is a working copy, abort
        * check if object already exists in ArcGIS
            * if doesn't exist, add one
            * if exists, update the existing one
    If unpublished:
        * if this is a working copy, abort
        * check if object exists in ArcGIS
            * if exists, remove it
    """
    obj = _get_obj_by_measure_id(site, uid)
    if IWorkingCopy.providedBy(obj):
        logger.debug("Skipping CaseStudy status change processing")
        return

    state = get_state(obj)

    token = get_auth_token()
    fid = _get_obj_FID(obj=obj, token=token)

    if (state != 'published') and fid:
        # it's unpublished, we'll remove the object
        logger.info("ArcGIS: Deleting CaseStudy with FID %s", fid)

        res = apply_edits(fid, op='deletes', token=token)

        assert res['deleteResults']
        assert res['deleteResults'][0]['objectId'] == fid
        return

    if state == "published":
        repr = obj._repr_for_arcgis()

        # new case study, add it to ArcGIS
        if fid is None:
            logger.info("ArcGIS: Adding CaseStudy with measure id %s", uid)
            entry = json.dumps([repr])
            res = apply_edits(entry, op='adds', token=token)
            assert len(res.get('addResults', [])) == 1
            assert res['addResults'][0]['success'] == True

        # existing case study, sync its info
        else:
            logger.info("ArcGIS: Updating CaseStudy with FID %s", fid)

            repr['attributes']['FID'] = fid
            entry = json.dumps([repr])
            res = apply_edits(entry, op='updates', token=token)

            assert res['updateResults']
            assert res['updateResults'][0]['objectId'] == fid
Esempio n. 2
0
def handle_ObjectRemovedEvent(site, uid):
    token = get_auth_token()

    fid = _get_obj_OBJECTID(obj=None, uid=uid, token=token)

    logger.info("ArcGIS: Deleting CaseStudy with OBJECTID %s", fid)

    res = apply_edits(fid, op='deletes', token=token)

    assert res['deleteResults']
    assert res['deleteResults'][0]['objectId'] == fid
Esempio n. 3
0
def handle_ObjectRemovedEvent(site, uid):
    token = get_auth_token()

    fid = _get_obj_FID(obj=None, uid=uid, token=token)

    logger.info("ArcGIS: Deleting CaseStudy with FID %s", fid)

    res = apply_edits(fid, op='deletes', token=token)

    assert res['deleteResults']
    assert res['deleteResults'][0]['objectId'] == fid
def add_all_casestudies(path, token):
    """ Used from command line
    """
    from lxml.html import fromstring, tostring
    entries = parse_dump(path)

    # need to escape html
    for entry in entries:
        desc = entry['attributes']['desc_']
        html = fromstring(desc)
        desc = tostring(html, encoding='utf-8', method='text').strip()
        entry['attributes']['desc_'] = desc

    res = apply_edits(json.dumps(entries), op='adds', token=token)
    return res
Esempio n. 5
0
def handle_ObjectModifiedEvent(site, uid):
    obj = _get_obj_by_measure_id(site, uid)
    repr = obj._repr_for_arcgis()

    token = get_auth_token()

    fid = _get_obj_OBJECTID(obj, token=token)

    if fid is None:
        logger.info("ArcGIS: Adding CaseStudy with measure id %s", uid)
        entry = json.dumps([repr])
        res = apply_edits(entry, op='adds', token=token)
        assert len(res.get('addResults', [])) == 1
        assert res['addResults'][0]['success'] is True
    else:
        repr['attributes']['OBJECTID'] = fid

        logger.info("ArcGIS: Updating CaseStudy with OBJECTID %s", fid)

        entry = json.dumps([repr])
        res = apply_edits(entry, op='updates', token=token)

        assert res['updateResults']
        assert res['updateResults'][0]['objectId'] == fid
Esempio n. 6
0
def handle_ObjectModifiedEvent(site, uid):
    obj = _get_obj_by_measure_id(site, uid)
    repr = obj._repr_for_arcgis()

    token = get_auth_token()

    fid = _get_obj_FID(obj, token=token)

    if fid is None:
        logger.info("ArcGIS: Adding CaseStudy with measure id %s", uid)
        entry = json.dumps([repr])
        res = apply_edits(entry, op='adds', token=token)
        assert len(res.get('addResults', [])) == 1
        assert res['addResults'][0]['success'] == True
    else:
        repr['attributes']['FID'] = fid

        logger.info("ArcGIS: Updating CaseStudy with FID %s", fid)

        entry = json.dumps([repr])
        res = apply_edits(entry, op='updates', token=token)

        assert res['updateResults']
        assert res['updateResults'][0]['objectId'] == fid
def edit_casestudy(fid, path, token):
    """ Used from command line
    """
    from lxml.html import fromstring, tostring

    entries = parse_dump(path)
    entry = None
    for e in entries:
        if e['attributes']['FID'] == fid:
            entry = e
            break

    assert entry
    desc = entry['attributes']['desc_']
    html = fromstring(desc)
    desc = tostring(html, encoding='utf-8', method='text').strip()
    entry['attributes']['desc_'] = desc
    res = apply_edits(json.dumps([entry]), op='updates', token=token)

    assert 'error' not in res

    return res
def add_all_casestudies(path, token):
    """ Used from command line
    """
    entries = parse_dump(path)

    # need to escape html

    for entry in entries:
        entry['geometry']["spatialReference"] = {"wkid": 3857}

        desc = entry['attributes']['desc_']
        try:
            html = fromstring(desc)
            desc = tostring(html, encoding='utf-8', method='text').strip()
        except:
            # probably already plain text
            logger.error("Could not convert %s", desc)

        entry['attributes']['desc_'] = desc

    res = apply_edits(json.dumps(entries), op='adds', token=token)

    return res
def edit_casestudy(fid, path, token):
    """ Used from command line
    """

    entries = parse_dump(path)
    entry = None

    for e in entries:
        if e['attributes']['OBJECTID'] == fid:
            entry = e

            break

    assert entry
    desc = entry['attributes']['desc_']
    html = fromstring(desc)
    desc = tostring(html, encoding='utf-8', method='text').strip()
    entry['attributes']['desc_'] = desc
    res = apply_edits(json.dumps([entry]), op='updates', token=token)

    assert 'error' not in res

    return res
def delete_all_casestudies(token):
    res = query_layer(token)
    all_ids = [x['attributes']['FID'] for x in res['features']]
    return apply_edits(all_ids, op='deletes', token=token)
def delete_casestudy(fid, token):
    """ Delete a casestudy. To be used from command line
    """
    return apply_edits(fid, op='deletes', token=token)
def delete_all_casestudies(token):
    res = query_layer(token)
    all_ids = [x['attributes']['OBJECTID'] for x in res['features']]

    return apply_edits(all_ids, op='deletes', token=token)
def delete_casestudy(fid, token):
    """ Delete a casestudy. To be used from command line
    """

    return apply_edits(fid, op='deletes', token=token)
Esempio n. 14
0
def handle_ObjectStateModified(site, uid):
    """ Handle when a CaseStudy is published / unpublished

    If published:
        * if this is a working copy, abort
        * check if object already exists in ArcGIS
            * if doesn't exist, add one
            * if exists, update the existing one
    If unpublished:
        * if this is a working copy, abort
        * check if object exists in ArcGIS
            * if exists, remove it
    """
    obj = _get_obj_by_measure_id(site, uid)

    if IWorkingCopy.providedBy(obj):
        logger.debug("Skipping CaseStudy status change processing")

        return

    state = get_state(obj)

    try:
        token = get_auth_token()
    except KeyError:
        logger.exception("Could not get an Arcgis auth token")

        return

    fid = _get_obj_OBJECTID(obj=obj, token=token)

    if (state != 'published') and fid:
        # it's unpublished, we'll remove the object
        logger.info("ArcGIS: Deleting CaseStudy with OBJECTID %s", fid)

        res = apply_edits(fid, op='deletes', token=token)

        assert res['deleteResults']
        assert res['deleteResults'][0]['objectId'] == fid

        return

    if state == "published":
        repr = obj._repr_for_arcgis()

        # new case study, add it to ArcGIS

        if fid is None:
            logger.info("ArcGIS: Adding CaseStudy with measure id %s", uid)
            entry = json.dumps([repr])
            res = apply_edits(entry, op='adds', token=token)
            assert len(res.get('addResults', [])) == 1
            assert res['addResults'][0]['success'] is True

        # existing case study, sync its info
        else:
            logger.info("ArcGIS: Updating CaseStudy with OBJECTID %s", fid)

            repr['attributes']['OBJECTID'] = fid
            entry = json.dumps([repr])
            res = apply_edits(entry, op='updates', token=token)

            assert res['updateResults']
            assert res['updateResults'][0]['objectId'] == fid