Example #1
0
def view_ajax_dv_notify_of_map(request, worldmap_layer_info):
    """
    Given a WorldMap layer information, send it to Dataverse
    worldmap_info may be:
        - WorldMapJoinLayerInfo
        - WorldMapLatLngInfo
        - WorldMapShapefileLayerInfo
    """
    if worldmap_layer_info is None:
        err_msg = 'WorldMapLayerInfo not found'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=404)

    # ------------------------------
    # Send Dataverse Notification
    # ------------------------------
    success, resp_dict = MetadataUpdater.update_dataverse_with_metadata(
        worldmap_layer_info)
    if success:
        json_msg = MessageHelperJSON.get_json_success_msg('Data sent')
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)
    else:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                'Failed to send data to dataverse. %s' % resp_dict['message'])
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=500)
Example #2
0
def ajax_get_join_targets(request, selected_geo_type):
    """
    Ajax - Retrieve JoinTarget information that matches
            a selected geospatial identification type

    Returns a list for use in loading a dropdown box
    """
    jt = get_latest_jointarget_information()

    join_target_info = jt.get_available_layers_list_by_type(selected_geo_type)
    if join_target_info is None:
        err_msg = ("Sorry! No Join Targets found"
                   " for Geospatial type: %s") % selected_geo_type

        json_msg = MessageHelperJSON.get_json_msg(success=False,
                                                  msg=err_msg)
        return HttpResponse(status=400,
                            content=json_msg,
                            content_type="application/json")


    json_msg = MessageHelperJSON.get_json_msg(success=True,
                                              msg="success",
                                              data_dict=join_target_info)

    return HttpResponse(status=200, content=json_msg, content_type="application/json")
Example #3
0
def view_unmatched_join_rows_json(request, tab_md5):
    """
    View the unmatched rows resulting from a Table Join
    """
    # ----------------------------------
    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        worldmap_info = WorldMapJoinLayerInfo.objects.get(md5=tab_md5)
    except WorldMapJoinLayerInfo.DoesNotExist:
        raise Http404('No WorldMapJoinLayerInfo for md5: %s' % tab_md5)

    if worldmap_info.core_data and\
        'unmatched_records_list' in worldmap_info.core_data:
        # Unmatched records exist
        unmatched_row_dict = dict(ummatched_rows=worldmap_info.core_data.get(
            'unmatched_records_list', None),
                                  column_names=worldmap_info.attribute_data)

        unmatched_rows_html = render_to_string(
            'metadata/unmatched_records.html', unmatched_row_dict, request)

        return HttpResponse(unmatched_rows_html)
        json_msg = MessageHelperJSON.get_json_msg(success=True,\
                        msg="Records found",\
                        data_dict=worldmap_info.core_data['unmatched_rows'])
    else:
        # No unmatched records exist
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                        msg="No unmatched records found.")

    return HttpResponse(json_msg, content_type="application/json")
Example #4
0
def ajax_join_targets_with_descriptions(request, selected_geo_type=None):
    """For use when choosing a join target through the UI
    Return JSON list with:
       join target id
       name
       description
    """
    jt = get_latest_jointarget_information()

    join_target_info = jt.get_available_layers_list_by_type(selected_geo_type,
                                                            for_json=True)

    if join_target_info is None:
        err_msg = "Sorry! No Join Targets found for Geospatial type: {0}".format(
            selected_geo_type)
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                                msg=err_msg)
        return HttpResponse(status=400,
                            content=json_msg,
                            content_type="application/json")

    else:
        json_msg = MessageHelperJSON.get_json_msg(success=True,\
                                msg="success",\
                                data_dict=join_target_info)

        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")
Example #5
0
def view_unmatched_lat_lng_rows_json(request, tab_md5):
    """
    View the unmatched rows resulting from trying to map lat/lng columns
    """

    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        worldmap_info = WorldMapLatLngInfo.objects.get(md5=tab_md5)
    except WorldMapLatLngInfo.DoesNotExist:
        raise Http404('No WorldMapLatLngInfo for md5: %s' % tab_md5)

    if worldmap_info.core_data and\
        'unmapped_records_list' in worldmap_info.core_data:
        # Unmatched records exist

        template_dict = dict(
            ummatched_rows=worldmap_info.core_data['unmapped_records_list'],
            column_names=worldmap_info.attribute_data)

        unmatched_rows_html = render_to_string(
            'metadata/unmatched_tabular_rows.html', template_dict, request)

        return HttpResponse(unmatched_rows_html)
        json_msg = MessageHelperJSON.get_json_msg(success=True,\
                        msg="Records found",\
                        data_dict=worldmap_info.core_data['unmapped_records_list'])
    else:
        # No unmatched records exist
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                        msg="No unmatched records found.")

    #from django.template.loader import render_to_string

    return HttpResponse(json_msg, content_type="application/json")
Example #6
0
def ajax_get_join_targets(request, selected_geo_type):
    """
    Ajax - Retrieve JoinTarget information that matches
            a selected geospatial identification type

    Returns a list for use in loading a dropdown box
    """
    jt = get_latest_jointarget_information()

    join_target_info = jt.get_available_layers_list_by_type(selected_geo_type)
    if join_target_info is None:
        err_msg = "Sorry! No Join Targets found for Geospatial type: {0}".format(
            selected_geo_type)
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                                msg=err_msg)
        return HttpResponse(status=400,
                            content=json_msg,
                            content_type="application/json")


    json_msg = MessageHelperJSON.get_json_msg(success=True,\
                                msg="success",\
                                data_dict=join_target_info)

    return HttpResponse(status=200,
                        content=json_msg,
                        content_type="application/json")
Example #7
0
def view_ajax_dv_notify_of_map(request, worldmap_layer_info):
    """
    Given a WorldMap layer information, send it to Dataverse
    worldmap_info may be:
        - WorldMapJoinLayerInfo
        - WorldMapLatLngInfo
        - WorldMapShapefileLayerInfo
    """
    if worldmap_layer_info is None:
        err_msg = 'WorldMapLayerInfo not found'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg, content_type="application/json", status=404)


    # ------------------------------
    # Send Dataverse Notification
    # ------------------------------
    success, resp_dict = MetadataUpdater.update_dataverse_with_metadata(worldmap_layer_info)
    if success:
        json_msg = MessageHelperJSON.get_json_success_msg('Data sent')
        return HttpResponse(json_msg, content_type="application/json", status=200)
    else:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                'Failed to send data to dataverse. %s' % resp_dict['message'])
        return HttpResponse(json_msg, content_type="application/json", status=500)
Example #8
0
    def get_result_msg(self, success=False, msg='', data_dict=None):

        if isinstance(data_dict, dict):
            d = MessageHelperJSON.get_dict_msg(success=success, msg=msg, data_dict=data_dict)
        else:
            d = MessageHelperJSON.get_dict_msg(success=success, msg=msg)

        if not self.return_type_json:
            return d

        return MessageHelperJSON.get_json_msg_from_dict(d)
Example #9
0
    def get_result_msg(self, success=False, msg='', data_dict=None):

        if isinstance(data_dict, dict):
            d = MessageHelperJSON.get_dict_msg(success=success, msg=msg, data_dict=data_dict)
        else:
            d = MessageHelperJSON.get_dict_msg(success=success, msg=msg)

        if not self.return_type_json:
            return d

        return MessageHelperJSON.get_json_msg_from_dict(d)
def get_join_targets_as_json():
    """
    Retrieve JoinTarget information from WorldMap and
    return it in JSON format
    """

    (success, data_dict_or_error_msg) = get_join_targets()
    if success is True:
        return MessageHelperJSON.get_dict_msg(success=True,\
                    msg="Join Targets retrieved",\
                    data_dict=data_dict_or_error_msg)

    else:
        return MessageHelperJSON.get_dict_msg(success=False,\
                    msg=data_dict_or_error_msg)
def get_join_targets_as_json():
    """
    Retrieve JoinTarget information from WorldMap and
    return it in JSON format
    """

    (success, data_dict_or_error_msg) = get_join_targets()
    if success is True:
        return MessageHelperJSON.get_dict_msg(success=True,\
                    msg="Join Targets retrieved",\
                    data_dict=data_dict_or_error_msg)

    else:
        return MessageHelperJSON.get_dict_msg(success=False,\
                    msg=data_dict_or_error_msg)
Example #12
0
    def get_result_msg(self, success=False, user_msg='', data_dict=None):
        """
        Return a message in JSON format
        """
        if type(data_dict) is dict:
            msg_dict = MessageHelperJSON.get_dict_msg(\
                        success=success,
                        msg=user_msg,
                        data_dict=data_dict)
        else:
            msg_dict = MessageHelperJSON.get_dict_msg(\
                        success=success,
                        msg=user_msg)

        if not self.return_type_json:
            return msg_dict

        return MessageHelperJSON.get_json_msg_from_dict(msg_dict)
Example #13
0
def ajax_dv_notify_tabular_join_map(request, worldmapinfo_md5):
    """
    Retrieve a WorldMapJoinLayerInfo object and send it to Dataverse
    """
    worldmap_layer_info = get_worldmap_info_object(TYPE_JOIN_LAYER, worldmapinfo_md5)
    if worldmap_layer_info is None:
        err_msg = 'WorldMapJoinLayerInfo not found'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg, content_type="application/json", status=404)

    return view_ajax_dv_notify_of_map(request, worldmap_layer_info)
Example #14
0
def ajax_dv_notify_tabular_join_map(request, worldmapinfo_md5):
    """
    Retrieve a WorldMapJoinLayerInfo object and send it to Dataverse
    """
    worldmap_layer_info = get_worldmap_info_object(TYPE_JOIN_LAYER,
                                                   worldmapinfo_md5)
    if worldmap_layer_info is None:
        err_msg = 'WorldMapJoinLayerInfo not found'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=404)

    return view_ajax_dv_notify_of_map(request, worldmap_layer_info)
Example #15
0
def ajax_join_targets_with_descriptions(request, selected_geo_type=None):
    """For use when choosing a join target through the UI
    Return JSON list with:
       join target id
       name
       description
    """
    jt = get_latest_jointarget_information()

    join_target_info = jt.get_available_layers_list_by_type(selected_geo_type, for_json=True)

    if join_target_info is None:
        err_msg = "Sorry! No Join Targets found for Geospatial type: {0}".format(selected_geo_type)
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                                msg=err_msg)
        return HttpResponse(status=400, content=json_msg, content_type="application/json")

    else:
        json_msg = MessageHelperJSON.get_json_msg(success=True,\
                                msg="success",\
                                data_dict=join_target_info)

        return HttpResponse(status=200, content=json_msg, content_type="application/json")
Example #16
0
def view_unmatched_lat_lng_rows_json(request, tab_md5):
    """
    View the unmatched rows resulting from trying to map lat/lng columns
    """

    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        worldmap_info = WorldMapLatLngInfo.objects.get(md5=tab_md5)
    except WorldMapLatLngInfo.DoesNotExist:
        raise Http404('No WorldMapLatLngInfo for md5: %s' % tab_md5)

    if worldmap_info.core_data and\
        'unmapped_records_list' in worldmap_info.core_data:
        # Unmatched records exist

        template_dict = dict(ummatched_rows=worldmap_info.core_data['unmapped_records_list'],
                        column_names=worldmap_info.attribute_data)

        unmatched_rows_html = render_to_string('metadata/unmatched_tabular_rows.html',
                                template_dict,
                                request)

        return HttpResponse(unmatched_rows_html)
        json_msg = MessageHelperJSON.get_json_msg(success=True,\
                        msg="Records found",\
                        data_dict=worldmap_info.core_data['unmapped_records_list'])
    else:
        # No unmatched records exist
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                        msg="No unmatched records found.")


    #from django.template.loader import render_to_string

    return HttpResponse(json_msg, content_type="application/json")
Example #17
0
def view_unmatched_join_rows_json(request, tab_md5):
    """
    View the unmatched rows resulting from a Table Join
    """
    # ----------------------------------
    # Retrieve the Tabular file information
    # ----------------------------------
    try:
        worldmap_info = WorldMapJoinLayerInfo.objects.get(md5=tab_md5)
    except WorldMapJoinLayerInfo.DoesNotExist:
        raise Http404('No WorldMapJoinLayerInfo for md5: %s' % tab_md5)

    if worldmap_info.core_data and\
        'unmatched_records_list' in worldmap_info.core_data:
        # Unmatched records exist
        unmatched_row_dict = dict(
            ummatched_rows=worldmap_info.core_data.get('unmatched_records_list', None),
            column_names=worldmap_info.attribute_data)

        unmatched_rows_html = render_to_string('metadata/unmatched_records.html',
            unmatched_row_dict,
            request)

        return HttpResponse(unmatched_rows_html)

        json_msg = MessageHelperJSON.get_json_msg(\
                        success=True,
                        msg="Records found",
                        data_dict=worldmap_info.core_data['unmatched_rows'])
    else:
        # No unmatched records exist
        json_msg = MessageHelperJSON.get_json_msg(\
                        success=False,
                        msg="No unmatched records found.")

    return HttpResponse(json_msg, content_type="application/json")
Example #18
0
def view_delete_tabular_map_no_ui(request, dataverse_token):
    """
    Attempt to delete a dataverse-created WorldMap layer via a direct API call, w/out any UI involved.
    Operates on the same token-callback url principle as other geoconnect methods.
    (1) Check incoming url for a callback key 'cb'
        and use the callback url to retrieve the DataverseInfo via a POST
        (this is copied directly from the workflow of the "map-it" method)
        OK, the whole process has been royally simplified! We are essentially
        dropping the previously performed steps 2-4. It is in fact possible to
        make a WorldMap API delete layer call *by the datafile_id and the
        dataverse installation name*. And we already have these in the dict
        that we received from following the callback URL.
    (2) Try deleting the map layer and associated data on the WorldMap side
        (a new call has been added to the layer_services, that takes a dict, containing
        the datafile_id and dataverse_installation_name
    (3) And if that worked too, delete it locally, in GeoConnect.
    (4) Return 200 and a success message.
    """
    # ----------------------------------------------------
    # (1) Process the incoming url for a callback key 'cb'
    # and use the callback url to retrieve the DataverseInfo via a POST
    # (this is copied directly from the workflow of the "map-it" method)
    # ----------------------------------------------------
    request_helper = InitialRequestHelper(request, dataverse_token)
    if request_helper.has_err:
        user_err_msg = ('Failed to obtain datafile metadata'
                        ' from the Dataverse.'
                        ' Error: %s') % request_helper.err_msg
        return HttpResponse(\
                    MessageHelperJSON.get_json_fail_msg(user_err_msg),
                    status=412)

    dv_data_dict = request_helper.dv_data_dict

    # ----------------------------------------------------
    # (2) Try deleting the map layer and associated data on the WorldMap side
    # Note:
    # We dropped the "dv_data_dict['datafile_is_restricted'] is True" requirement
    # ----------------------------------------------------
    (success, err_msg) = delete_map_layer_by_cb_dict(request_helper.dv_data_dict)
    if success is False:
        LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg)
        return HttpResponse(\
                    MessageHelperJSON.get_json_fail_msg(\
                        "Failed to delete WorldMap layer: "+err_msg),
                    status=503)

    # ----------------------------------------------------
    # (3) Retrieve local GISDataFile objects and delete them.
    # This essentially clears all info about the layer and underlying datafile_id
    # e.g. GISDataFile is the superclass to ShapefileInfo, TabularInfo, etc
    #
    # Note: datafile_id and dataverse_installation_name have to be there--blow
    #       up if there's a key error
    # ----------------------------------------------------
    gis_data_kwargs = dict(\
        datafile_id=dv_data_dict['datafile_id'],
        dataverse_installation_name=dv_data_dict['dataverse_installation_name'])

    gis_data_objects = GISDataFile.objects.filter(**gis_data_kwargs)
    LOGGER.debug('gis_data_objects found: %s', gis_data_objects)

    if gis_data_objects.count() > 0:
        gis_data_objects.delete()

    # ----------------------------------------------------
    # (4) Success!
    # ----------------------------------------------------
    json_msg = MessageHelperJSON.get_json_success_msg(\
                    msg=('Successfully deleted WorldMap layer for the %s'
                         ' file, (datafile_id: %s)') %\
                            (request_helper.mapping_type,
                             dv_data_dict['datafile_id']))

    return HttpResponse(json_msg, content_type="application/json", status=200)
Example #19
0
def view_classify_layer_form(request, import_success_md5):
    """
    (note: This should be refactored into a class...)
    Given a ClassifyLayerForm submission, return a JSON response

    :param import_success_md5: str, md5 identifying a WorldMapLayerInfo object
    :returns JSON response: JSON generated with the MessageHelperJSON.get_json_msg function
    """
    # --------------------------------------------------------------
    # Is it a POST?
    # --------------------------------------------------------------
    if not request.POST:
        err_note = 'A POST request must be used to classify the layer.'

        div_content = format_major_error_message(err_note,\
            import_success_md5=import_success_md5)
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                        msg=err_note,\
                        data_dict=dict(div_content=div_content))
        # technically a 405 error, but we want the JSON message to appear
        return HttpResponse(status=200,\
                        content=json_msg,\
                        content_type="application/json")

    # --------------------------------------------------------------
    # Is the WorldMapLayerInfo object md5 available?
    # --------------------------------------------------------------
    if not import_success_md5:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available.')
        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        # technically a 404 error, but we want the JSON message to appear
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    # --------------------------------------------------------------
    # Does the WorldMapLayerInfo object exist?
    # --------------------------------------------------------------
    if not 'data_source_type' in request.POST:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available. (id:ds2)')
        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    # Retrieve an object containing the WorldMap data
    #
    worldmap_layerinfo = get_worldmap_info_object(request.POST['data_source_type'],\
                                import_success_md5)

    if worldmap_layerinfo is None:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available.'
                    ' (id:ds3) %s' % request.POST['data_source_type'])

        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        # technically a 410 error, but we want the JSON message to appear
        return HttpResponse(status=200, content=json_msg, content_type="application/json")


    d = {'ATTRIBUTE_VALUE_DELIMITER' : ATTRIBUTE_VALUE_DELIMITER}

    # --------------------------------------------------------------
    # Is the form valid?
    # --------------------------------------------------------------

    # Load up classification form parameters and validate this request
    #   e.g. Is the classification attribute in the AJAX call actually
    #       part of this layer? etc.
    #
    classify_form = ClassifyLayerForm(\
                        request.POST,
                        **worldmap_layerinfo.get_dict_for_classify_form())

    # --------------------------------------------------------------
    # Invalid forms are status=200 so caught by ajax
    # --------------------------------------------------------------
    if not classify_form.is_valid():    # Oops, not valid (shouldn't happen)
        user_message = ('There was an error with the styling form.'
                        ' Please check the errors below.')
        additional_info = dict(classify_form=classify_form,
                               worldmap_layerinfo=worldmap_layerinfo,
                               error_msg=user_message)
        d.update(additional_info)

        form_content = render_to_string(\
                            'classification/view_classify_form.html',
                            d,
                            request)
        json_msg = MessageHelperJSON.get_json_msg(\
                                success=False,
                                msg=user_message,
                                data_dict={'div_content':form_content})
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    initial_classify_params = classify_form.get_params_dict_for_classification()
    if initial_classify_params is None:
        LOGGER.error('Failed with *valid* form: classify_form.get_params_dict_for_classification()')
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
            msg='The layer styling form contains errors (code: 2)')
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    #----------------------------------------------------------
    # Prepare params for classify request against WorldMap API
    #
    #----------------------------------------------------------
    initial_classify_params.update(\
            worldmap_layerinfo.get_params_to_check_for_existing_layer_metadata())

    api_form = ClassifyRequestDataForm(initial_classify_params)
    if not api_form.is_valid():
        err_msg = ('Validation failed with ClassifyRequestDataForm.'
                   'Errors: %s') % api_form.errors
        LOGGER.error(err_msg)
        json_msg = MessageHelperJSON.get_json_msg(\
                        success=False,
                        msg='The layer styling form contains errors (code: 3)')
        return HttpResponse(status=200, content=json_msg, content_type="application/json")


    classify_params = api_form.cleaned_data

    classify_url = classify_form.get_worldmap_classify_api_url()

    LOGGER.debug('classify_params', classify_params)

    resp = None
    try:
        resp = requests.post(classify_url,\
                    data=classify_params,\
                    auth=settings.WORLDMAP_ACCOUNT_AUTH,\
                    timeout=settings.WORLDMAP_DEFAULT_TIMEOUT)

    except requests.exceptions.ConnectionError as exception_obj:
        err_msg = ('<b>Details for administrator:</b>'
                   'Could not contact the Dataverse server: %s')\
                   % (classify_url)

        log_connect_error_message(err_msg, LOGGER, exception_obj)

        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg='Sorry!  The classification failed.<br /><p>%s</p>' % err_msg)

        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    if not resp.status_code == 200:
        try:
            json_resp = resp.json()
        except ValueError:
            error_msg = ('Worldmap classification failed. Status code:'
                         ' %s\nText;%s')\
                         % (resp.status_code, resp.text.encode('utf-8'))

            LOGGER.error(error_msg)

            json_msg = MessageHelperJSON.get_json_msg(\
                            success=False,
                            msg='Sorry!  The classification failed.')
            return HttpResponse(status=200, content=json_msg, content_type="application/json")

        wm_err_msg = json_resp.get('message', None)
        if wm_err_msg is None:
            wm_err_msg = 'No message given.'

        err_msg = 'Sorry!  The classification failed.<p>%s</p>' % wm_err_msg

        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=err_msg)

        return HttpResponse(status=200, content=json_msg, content_type="application/json")


    # RESPONSE CODE IS 200

    # --------------------------------------------------------------
    # convert response to JSON
    # --------------------------------------------------------------
    try:
        json_resp = resp.json()
    except ValueError:
        LOGGER.error('Worldmap response was not valid json: %s',\
                resp.text.encode('utf-8'))
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
            msg='Sorry!  The classification failed.')
        return HttpResponse(status=200,\
            content=json_msg,\
            content_type="application/json")

    # --------------------------------------------------------------
    #   Classification Failed
    # --------------------------------------------------------------
    if not json_resp.get("success") is True:
        LOGGER.info('Worldmap response did not have success==true: %s', resp.text)
        user_msg = 'Sorry!  The classification failed.<br /><br />%s' %\
                    json_resp.get('message', 'nada')
        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=user_msg)
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    # --------------------------------------------------------------
    #   Validate the response
    # --------------------------------------------------------------
    f_val = WorldMapToGeoconnectMapLayerMetadataValidationForm(json_resp.get('data', None))
    if not f_val.is_valid():
        LOGGER.error('Classify return data failed validation: %s', f_val.errors)
        user_msg = 'Sorry!  The classification failed.<br /><br />%s' \
                        % json_resp.get('message', f_val.errors)
        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=user_msg)
        return HttpResponse(status=200, content=json_msg, content_type="application/json")

    # --------------------------------------------------------------
    #   Looks good, update the WorldMapLayerInfo's attribute info
    # --------------------------------------------------------------
    if hasattr(worldmap_layerinfo, 'add_attribute_info_as_json_string'):
        # handle shapefiles

        worldmap_layerinfo.add_attribute_info_as_json_string(f_val.cleaned_data['attribute_info'])

        worldmap_layerinfo.save()

    elif hasattr(worldmap_layerinfo, 'attribute_data'):
        # handle tabular files

        worldmap_layerinfo.attribute_info =\
            JSONHelper.to_python_or_none(f_val.cleaned_data['attribute_info'])

        worldmap_layerinfo.download_links =\
            JSONHelper.to_python_or_none(f_val.cleaned_data['download_links'])

        worldmap_layerinfo.save()
    else:
        LOGGER.error(dir(worldmap_layerinfo))
        LOGGER.error(type(worldmap_layerinfo))
        LOGGER.error('nada?')

    # --------------------------------------------------------------
    # Refresh the classify form with the latest WorldMap parameter information
    # --------------------------------------------------------------
    classify_form = ClassifyLayerForm(\
                        request.POST,
                        **worldmap_layerinfo.get_dict_for_classify_form())

    # --------------------------------------------------------------
    # Update the Dataverse metadata -- so that the new icon will be refreshed
    # --------------------------------------------------------------

    # Is all this needed, or should there be a
    # Dataverse API call to only update the image?
    MetadataUpdater.run_update_via_popen(worldmap_layerinfo)

    msg_params = classify_form.get_params_for_display()

    success_msg = render_to_string('classification/classify_success_msg.html', msg_params)

    d.update(dict(classify_form=classify_form,
                  success_msg=success_msg,
                  SELECT_LABEL=SELECT_LABEL))

    form_content = render_to_string('classification/view_classify_form.html',
                                    d,
                                    request)


    json_msg = MessageHelperJSON.get_json_msg(\
                    success=True,
                    msg=success_msg, #'Success!'
                    data_dict={'div_content':form_content})

    return HttpResponse(status=200, content=json_msg, content_type="application/json")
Example #20
0
    def get(self, request, shp_md5):
        """Use the SendShapefileService to create a map from a shapefile.

        - SendShapefileService takes care of details starting with retrieving
        the ShapefileInfo object
        """

        # OK if shp_md5 is None, SendShapefileService creates error message
        #
        send_shp_service = SendShapefileService(**dict(shp_md5=shp_md5))

        # Send the shapefile to WorldMap
        #
        success = send_shp_service.send_shapefile_to_worldmap()

        # -----------------------------------
        # Did it work? NOPE!  Failed along the way!
        # -----------------------------------
        if not success:
            err_note = "Sorry! The shapefile mapping did not work.<br /><span class='small'>%s</span>" % '<br />'.join(send_shp_service.err_msgs)
            LOGGER.error(err_note)
            err_note_html = render_ajax_basic_err_msg(err_note,\
                                            send_shp_service.shapefile_info)

            json_msg = MessageHelperJSON.get_json_fail_msg(err_note_html, dict(id_main_panel_content=err_note_html))

            return HttpResponse(json_msg, content_type="application/json", status=200)


        # -----------------------------------
        # Yes!  We have a new map layer
        # -----------------------------------
        worldmap_shapefile_layerinfo = send_shp_service.get_worldmap_layerinfo()
        shapefile_info = worldmap_shapefile_layerinfo.get_gis_data_info()

        assert worldmap_shapefile_layerinfo is not None,\
            "Failure in SendShapefileService!  Said success but not worldmap_layerinfo (WorldMapShapefileLayerInfo)"

        # -----------------------------------------
        # Build the Map HTML to replace the form
        # -----------------------------------------
        map_html, user_message_html = build_map_html(request, worldmap_shapefile_layerinfo)
        if map_html is None:    # Failed!  Send an error
            LOGGER.error("Failed to create map HTML using WorldMapShapefileLayerInfo: %s (%d)",\
                worldmap_shapefile_layerinfo, worldmap_shapefile_layerinfo.id)

            user_msg = 'Sorry! Failed to create map. Please try again. (code: s3)'
            json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
            return HttpResponse(json_msg, content_type="application/json", status=200)

        # -----------------------------------------
        # Looks good.  In the JSON response, send
        #   back the map HTML
        # -----------------------------------------
        data_dict = dict(map_html=map_html,
                    user_message_html=user_message_html,
                    id_main_panel_title=PANEL_TITLE_STYLE_MAP,
                    message='Success!  The shapefile was successfully mapped!')

        json_msg = MessageHelperJSON.get_json_success_msg("great job", data_dict=data_dict)

        return HttpResponse(json_msg, content_type="application/json", status=200)
Example #21
0
    def get(self, request, shp_md5):
        """Use the SendShapefileService to create a map from a shapefile.

        - SendShapefileService takes care of details starting with retrieving
        the ShapefileInfo object
        """

        # OK if shp_md5 is None, SendShapefileService creates error message
        #
        send_shp_service = SendShapefileService(**dict(shp_md5=shp_md5))

        # Send the shapefile to WorldMap
        #
        success = send_shp_service.send_shapefile_to_worldmap()


        # -----------------------------------
        # Did it work? NOPE!  Failed along the way!
        # -----------------------------------
        if not success:
            err_note = ('Sorry! The shapefile mapping did not work.'
                        '<br /><span class="small">{0}</span>').format(\
                            '<br />'.join(send_shp_service.err_msgs))
            LOGGER.error(err_note)

            err_note_html = render_ajax_basic_err_msg(err_note,\
                                            send_shp_service.shapefile_info)

            json_msg = MessageHelperJSON.get_json_fail_msg(err_note_html, dict(id_main_panel_content=err_note_html))

            return HttpResponse(json_msg, content_type="application/json", status=200)


        # -----------------------------------
        # Yes!  We have a new map layer
        # -----------------------------------
        worldmap_shapefile_layerinfo = send_shp_service.get_worldmap_layerinfo()
        shapefile_info = worldmap_shapefile_layerinfo.get_gis_data_info()

        assert worldmap_shapefile_layerinfo is not None,\
            "Failure in SendShapefileService!  Said success but not worldmap_layerinfo (WorldMapShapefileLayerInfo)"

        # -----------------------------------------
        # Build the Map HTML to replace the form
        # -----------------------------------------
        map_html, user_message_html = build_map_html(request, worldmap_shapefile_layerinfo)
        if map_html is None:    # Failed!  Send an error
            LOGGER.error("Failed to create map HTML using WorldMapShapefileLayerInfo: %s (%d)",\
                worldmap_shapefile_layerinfo, worldmap_shapefile_layerinfo.id)

            user_msg = 'Sorry! Failed to create map. Please try again. (code: s3)'
            json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
            return HttpResponse(json_msg, content_type="application/json", status=200)

        # -----------------------------------------
        # Looks good.  In the JSON response, send
        #   back the map HTML
        # -----------------------------------------
        data_dict = dict(\
                    map_html=map_html,
                    user_message_html=user_message_html,
                    id_main_panel_title=PANEL_TITLE_STYLE_MAP,
                    message='Success! The shapefile was successfully mapped!')

        json_msg = MessageHelperJSON.get_json_success_msg("great job", data_dict=data_dict)

        return HttpResponse(json_msg, content_type="application/json", status=200)
Example #22
0
def view_classify_layer_form(request, import_success_md5):
    """
    (note: This should be refactored into a class...)
    Given a ClassifyLayerForm submission, return a JSON response

    :param import_success_md5: str, md5 identifying a WorldMapLayerInfo object
    :returns JSON response: JSON generated with the MessageHelperJSON.get_json_msg function
    """
    # --------------------------------------------------------------
    # Is it a POST?
    # --------------------------------------------------------------
    if not request.POST:
        err_note = 'A POST request must be used to classify the layer.'

        div_content = format_major_error_message(err_note,\
            import_success_md5=import_success_md5)
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
                        msg=err_note,\
                        data_dict=dict(div_content=div_content))
        # technically a 405 error, but we want the JSON message to appear
        return HttpResponse(status=200,\
                        content=json_msg,\
                        content_type="application/json")

    # --------------------------------------------------------------
    # Is the WorldMapLayerInfo object md5 available?
    # --------------------------------------------------------------
    if not import_success_md5:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available.')
        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        # technically a 404 error, but we want the JSON message to appear
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    # --------------------------------------------------------------
    # Does the WorldMapLayerInfo object exist?
    # --------------------------------------------------------------
    if not 'data_source_type' in request.POST:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available. (id:ds2)')
        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    # Retrieve an object containing the WorldMap data
    #
    worldmap_layerinfo = get_worldmap_info_object(request.POST['data_source_type'],\
                                import_success_md5)

    if worldmap_layerinfo is None:
        err_note = ('Sorry! The layer could not be identified.'
                    '\n\nThe Styling option is not available.'
                    ' (id:ds3) %s' % request.POST['data_source_type'])

        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg=err_note,
                    data_dict=dict(div_content=format_major_error_message(err_note)))
        # technically a 410 error, but we want the JSON message to appear
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    d = {'ATTRIBUTE_VALUE_DELIMITER': ATTRIBUTE_VALUE_DELIMITER}

    # --------------------------------------------------------------
    # Is the form valid?
    # --------------------------------------------------------------

    # Load up classification form parameters and validate this request
    #   e.g. Is the classification attribute in the AJAX call actually
    #       part of this layer? etc.
    #
    classify_form = ClassifyLayerForm(\
                        request.POST,
                        **worldmap_layerinfo.get_dict_for_classify_form())

    # --------------------------------------------------------------
    # Invalid forms are status=200 so caught by ajax
    # --------------------------------------------------------------
    if not classify_form.is_valid():  # Oops, not valid (shouldn't happen)
        user_message = ('There was an error with the styling form.'
                        ' Please check the errors below.')
        additional_info = dict(classify_form=classify_form,
                               worldmap_layerinfo=worldmap_layerinfo,
                               error_msg=user_message)
        d.update(additional_info)

        form_content = render_to_string(\
                            'classification/view_classify_form.html',
                            d,
                            request)
        json_msg = MessageHelperJSON.get_json_msg(\
                                success=False,
                                msg=user_message,
                                data_dict={'div_content':form_content})
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    initial_classify_params = classify_form.get_params_dict_for_classification(
    )
    if initial_classify_params is None:
        LOGGER.error(
            'Failed with *valid* form: classify_form.get_params_dict_for_classification()'
        )
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
            msg='The layer styling form contains errors (code: 2)')
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    #----------------------------------------------------------
    # Prepare params for classify request against WorldMap API
    #
    #----------------------------------------------------------
    initial_classify_params.update(\
            worldmap_layerinfo.get_params_to_check_for_existing_layer_metadata())

    api_form = ClassifyRequestDataForm(initial_classify_params)
    if not api_form.is_valid():
        err_msg = ('Validation failed with ClassifyRequestDataForm.'
                   'Errors: %s') % api_form.errors
        LOGGER.error(err_msg)
        json_msg = MessageHelperJSON.get_json_msg(\
                        success=False,
                        msg='The layer styling form contains errors (code: 3)')
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    classify_params = api_form.cleaned_data

    classify_url = classify_form.get_worldmap_classify_api_url()

    LOGGER.debug('classify_params', classify_params)

    resp = None
    try:
        resp = requests.post(classify_url,\
                    data=classify_params,\
                    auth=settings.WORLDMAP_ACCOUNT_AUTH,\
                    timeout=settings.WORLDMAP_DEFAULT_TIMEOUT)

    except requests.exceptions.ConnectionError as exception_obj:
        err_msg = ('<b>Details for administrator:</b>'
                   'Could not contact the Dataverse server: %s')\
                   % (classify_url)

        log_connect_error_message(err_msg, LOGGER, exception_obj)

        json_msg = MessageHelperJSON.get_json_msg(\
                    success=False,
                    msg='Sorry!  The classification failed.<br /><p>%s</p>' % err_msg)

        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    if not resp.status_code == 200:
        try:
            json_resp = resp.json()
        except ValueError:
            error_msg = ('Worldmap classification failed. Status code:'
                         ' %s\nText;%s')\
                         % (resp.status_code, resp.text.encode('utf-8'))

            LOGGER.error(error_msg)

            json_msg = MessageHelperJSON.get_json_msg(\
                            success=False,
                            msg='Sorry!  The classification failed.')
            return HttpResponse(status=200,
                                content=json_msg,
                                content_type="application/json")

        wm_err_msg = json_resp.get('message', None)
        if wm_err_msg is None:
            wm_err_msg = 'No message given.'

        err_msg = 'Sorry!  The classification failed.<p>%s</p>' % wm_err_msg

        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=err_msg)

        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    # RESPONSE CODE IS 200

    # --------------------------------------------------------------
    # convert response to JSON
    # --------------------------------------------------------------
    try:
        json_resp = resp.json()
    except ValueError:
        LOGGER.error('Worldmap response was not valid json: %s',\
                resp.text.encode('utf-8'))
        json_msg = MessageHelperJSON.get_json_msg(success=False,\
            msg='Sorry!  The classification failed.')
        return HttpResponse(status=200,\
            content=json_msg,\
            content_type="application/json")

    # --------------------------------------------------------------
    #   Classification Failed
    # --------------------------------------------------------------
    if not json_resp.get("success") is True:
        LOGGER.info('Worldmap response did not have success==true: %s',
                    resp.text)
        user_msg = 'Sorry!  The classification failed.<br /><br />%s' %\
                    json_resp.get('message', 'nada')
        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=user_msg)
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    # --------------------------------------------------------------
    #   Validate the response
    # --------------------------------------------------------------
    f_val = WorldMapToGeoconnectMapLayerMetadataValidationForm(
        json_resp.get('data', None))
    if not f_val.is_valid():
        LOGGER.error('Classify return data failed validation: %s',
                     f_val.errors)
        user_msg = 'Sorry!  The classification failed.<br /><br />%s' \
                        % json_resp.get('message', f_val.errors)
        json_msg = MessageHelperJSON.get_json_msg(success=False, msg=user_msg)
        return HttpResponse(status=200,
                            content=json_msg,
                            content_type="application/json")

    # --------------------------------------------------------------
    #   Looks good, update the WorldMapLayerInfo's attribute info
    # --------------------------------------------------------------
    if hasattr(worldmap_layerinfo, 'add_attribute_info_as_json_string'):
        # handle shapefiles

        worldmap_layerinfo.add_attribute_info_as_json_string(
            f_val.cleaned_data['attribute_info'])

        worldmap_layerinfo.save()

    elif hasattr(worldmap_layerinfo, 'attribute_data'):
        # handle tabular files

        worldmap_layerinfo.attribute_info =\
            JSONHelper.to_python_or_none(f_val.cleaned_data['attribute_info'])

        worldmap_layerinfo.download_links =\
            JSONHelper.to_python_or_none(f_val.cleaned_data['download_links'])

        worldmap_layerinfo.save()
    else:
        LOGGER.error(dir(worldmap_layerinfo))
        LOGGER.error(type(worldmap_layerinfo))
        LOGGER.error('nada?')

    # --------------------------------------------------------------
    # Refresh the classify form with the latest WorldMap parameter information
    # --------------------------------------------------------------
    classify_form = ClassifyLayerForm(\
                        request.POST,
                        **worldmap_layerinfo.get_dict_for_classify_form())

    # --------------------------------------------------------------
    # Update the Dataverse metadata -- so that the new icon will be refreshed
    # --------------------------------------------------------------

    # Is all this needed, or should there be a
    # Dataverse API call to only update the image?
    MetadataUpdater.run_update_via_popen(worldmap_layerinfo)

    msg_params = classify_form.get_params_for_display()

    success_msg = render_to_string('classification/classify_success_msg.html',
                                   msg_params)

    d.update(
        dict(classify_form=classify_form,
             success_msg=success_msg,
             SELECT_LABEL=SELECT_LABEL))

    form_content = render_to_string('classification/view_classify_form.html',
                                    d, request)


    json_msg = MessageHelperJSON.get_json_msg(\
                    success=True,
                    msg=success_msg, #'Success!'
                    data_dict={'div_content':form_content})

    return HttpResponse(status=200,
                        content=json_msg,
                        content_type="application/json")
Example #23
0
def view_map_tabular_file_form(request):
    """
    AJAX call: Join your tabular file to an existing WorldMap layer
    using the column selected in this form
    """
    #for k, v in request.POST.items():
    #        print k, v

    # -----------------------------------------
    # Retrieve the id of the Tabular info object
    # -----------------------------------------
    tabular_file_info_id = request.POST.get('tabular_file_info_id', -1)

    try:
        tabular_info = TabularFileInfo.objects.get(pk=tabular_file_info_id)
    except TabularFileInfo.DoesNotExist:
        err_msg = 'Sorry! The Tabular File was not found. (tabular_file_info_id)'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)
        #raise Http404('No TabularFileInfo for id: %s' % tabular_file_info_id)

    # -----------------------------------------
    # Retrieve available Geocode types and join Layers
    #   note: geocode_types_from_worldmap not needed here
    # -----------------------------------------
    (geocode_types_from_worldmap,
     available_layers_list) = get_geocode_types_and_join_layers()

    # -----------------------------------------
    # Create form with initial + POST data
    # -----------------------------------------
    form_single_column = ChooseSingleColumnForm(tabular_info.id,\
                    available_layers_list,\
                    tabular_info.column_names,\
                    request.POST)

    # -----------------------------------------
    # Check the form's validity
    # -----------------------------------------
    if not form_single_column.is_valid():
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                        format_errors_as_text(form_single_column, for_web=True))
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    print 'cleaned_data', form_single_column.cleaned_data

    # -----------------------------------------
    # Get Dataverse info dict
    # -----------------------------------------
    dataverse_metadata_dict = get_dataverse_info_dict(tabular_info)

    # -----------------------------------------
    # Use the WorldMap API and
    # try to create a layer
    # -----------------------------------------
    tj_map_maker = TableJoinMapMaker(
        tabular_info,
        dataverse_metadata_dict,
        form_single_column.cleaned_data.get('chosen_column'),
        form_single_column.cleaned_data.get('chosen_layer'),
    )
    success = tj_map_maker.run_map_create()
    msg('success: %s' % success)
    if not success:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                    'Sorry! ' + tj_map_maker.get_error_msg())
        msg('error msg: %s' % json_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Succeeded!  Create a WorldMapTabularLayerInfo object
    # -----------------------------------------
    worldmap_tabular_info = WorldMapTabularLayerInfo.build_from_worldmap_json(tabular_info,\
                                tj_map_maker.get_map_info())

    if worldmap_tabular_info is None:
        LOGGER.error("Failed to create WorldMapTabularLayerInfo using %s",\
            tj_map_maker.get_map_info())
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s1)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Notify Dataverse of the new map
    # -----------------------------------------
    MetadataUpdater.update_dataverse_with_metadata(worldmap_tabular_info)

    # -----------------------------------------
    # Build the Map HTML chunk to replace the form
    # -----------------------------------------
    map_html, user_message_html = build_map_html(request,
                                                 worldmap_tabular_info)
    if map_html is None:  # Failed!  Send an error
        LOGGER.error("Failed to create map HTML using WorldMapTabularLayerInfo: %s (%d)",\
            worldmap_tabular_info, worldmap_tabular_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s2)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Looks good.  In the JSON response, send
    #   back the map HTML
    # -----------------------------------------
    data_dict = dict(map_html=map_html,
                     user_message_html=user_message_html,
                     id_main_panel_title=PANEL_TITLE_STYLE_MAP)

    json_msg = MessageHelperJSON.get_json_success_msg("great job",
                                                      data_dict=data_dict)

    return HttpResponse(json_msg, content_type="application/json", status=200)
Example #24
0
def view_delete_tabular_map_no_ui(request, dataverse_token):
    """
    Attempt to delete a dataverse-created WorldMap layer via a direct API call, w/out any UI involved.
    Operates on the same token-callback url principle as other geoconnect methods.
    (1) Check incoming url for a callback key 'cb'
        and use the callback url to retrieve the DataverseInfo via a POST
        (this is copied directly from the workflow of the "map-it" method)
        OK, the whole process has been royally simplified! We are essentially
        dropping the previously performed steps 2-4. It is in fact possible to
        make a WorldMap API delete layer call *by the datafile_id and the
        dataverse installation name*. And we already have these in the dict
        that we received from following the callback URL.
    (2) Try deleting the map layer and associated data on the WorldMap side
        (a new call has been added to the layer_services, that takes a dict, containing
        the datafile_id and dataverse_installation_name
    (3) And if that worked too, delete it locally, in GeoConnect.
    (4) Return 200 and a success message.
    """
    # ----------------------------------------------------
    # (1) Process the incoming url for a callback key 'cb'
    # and use the callback url to retrieve the DataverseInfo via a POST
    # (this is copied directly from the workflow of the "map-it" method)
    # ----------------------------------------------------
    request_helper = InitialRequestHelper(request, dataverse_token)
    if request_helper.has_err:
        user_err_msg = ('Failed to obtain datafile metadata'
                        ' from the Dataverse.'
                        ' Error: %s') % request_helper.err_msg
        return HttpResponse(\
                    MessageHelperJSON.get_json_fail_msg(user_err_msg),
                    status=412)

    dv_data_dict = request_helper.dv_data_dict

    # ----------------------------------------------------
    # (2) Try deleting the map layer and associated data on the WorldMap side
    # Note:
    # We dropped the "dv_data_dict['datafile_is_restricted'] is True" requirement
    # ----------------------------------------------------
    (success,
     err_msg) = delete_map_layer_by_cb_dict(request_helper.dv_data_dict)
    if success is False:
        LOGGER.error("Failed to delete WORLDMAP layer: %s", err_msg)
        return HttpResponse(\
                    MessageHelperJSON.get_json_fail_msg(\
                        "Failed to delete WorldMap layer: "+err_msg),
                    status=503)

    # ----------------------------------------------------
    # (3) Retrieve local GISDataFile objects and delete them.
    # This essentially clears all info about the layer and underlying datafile_id
    # e.g. GISDataFile is the superclass to ShapefileInfo, TabularInfo, etc
    #
    # Note: datafile_id and dataverse_installation_name have to be there--blow
    #       up if there's a key error
    # ----------------------------------------------------
    gis_data_kwargs = dict(\
        datafile_id=dv_data_dict['datafile_id'],
        dataverse_installation_name=dv_data_dict['dataverse_installation_name'])

    gis_data_objects = GISDataFile.objects.filter(**gis_data_kwargs)
    LOGGER.debug('gis_data_objects found: %s', gis_data_objects)

    if gis_data_objects.count() > 0:
        gis_data_objects.delete()

    # ----------------------------------------------------
    # (4) Success!
    # ----------------------------------------------------
    json_msg = MessageHelperJSON.get_json_success_msg(\
                    msg=('Successfully deleted WorldMap layer for the %s'
                         ' file, (datafile_id: %s)') %\
                            (request_helper.mapping_type,
                             dv_data_dict['datafile_id']))

    return HttpResponse(json_msg, content_type="application/json", status=200)
Example #25
0
def view_process_lat_lng_form(request):
    """
    Create a WorldMap layer from your tabular file
    using the latitude and longitude columns selected in this form
    """

    tabular_file_info_id = request.POST.get('tabular_file_info_id', -1)

    try:
        tabular_info = TabularFileInfo.objects.get(pk=tabular_file_info_id)
    except TabularFileInfo.DoesNotExist:
        err_msg = 'Sorry! The Tabular File was not found. (tabular_file_info_id)'
        json_msg = MessageHelperJSON.get_json_fail_msg(err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)
        #raise Http404('No TabularFileInfo for id: %s' % tabular_file_info_id)

    form_lat_lng = LatLngColumnsForm(tabular_info.id,\
                        tabular_info.column_names,\
                        request.POST)
    if not form_lat_lng.is_valid():
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                    format_errors_as_text(form_lat_lng,\
                                        for_web=True)\
                                    )
        #json_msg = MessageHelperJSON.get_json_fail_msg(f.err_msg_for_web)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)


    (success, worldmap_data_or_err_msg) = create_map_from_datatable_lat_lng(\
                        tabular_info,\
                        form_lat_lng.get_latitude_colname(),\
                        form_lat_lng.get_longitude_colname(),\
                        )

    # -----------------------------------------
    # Failed! Send error message
    # -----------------------------------------
    if not success:
        json_msg = MessageHelperJSON.get_json_fail_msg(\
                'Sorry! ' + worldmap_data_or_err_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Succeeded!  Create a WorldMapTabularLayerInfo object
    # -----------------------------------------
    user_msg, response_data = worldmap_data_or_err_msg
    #json_msg = MessageHelperJSON.get_json_success_msg(user_msg, data_dict=response_data)
    #return HttpResponse(json_msg, content_type="application/json", status=200)

    worldmap_latlng_info = WorldMapTabularLayerInfo.build_from_worldmap_json(tabular_info,\
                            response_data)

    if worldmap_latlng_info is None:
        LOGGER.error("Failed to create WorldMapLatLngInfo using data: %s",\
                    response_data)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s4)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Notify Dataverse of the new map
    # -----------------------------------------
    MetadataUpdater.update_dataverse_with_metadata(worldmap_latlng_info)

    # -----------------------------------------
    # Possible that this failed?
    # Make sure at least 1 row mapped
    # -----------------------------------------
    # Skip for now!  Error in row counts for Lat/Lng!
    """
    if worldmap_latlng_info.did_any_rows_map() is False:
        # Delete the worldmap_latlng_info object
        worldmap_latlng_info.delete()

        # Send back a user error message
        user_msg = "Sorry!  We couldn't map any of those latitude and longitude values."
        return HttpResponse(MessageHelperJSON.get_json_fail_msg(user_msg),\
                content_type="application/json",\
                status=200)
    """
    # -----------------------------------------
    # Build the Map HTML chunk to replace the form
    # -----------------------------------------
    map_html, user_message_html = build_map_html(request, worldmap_latlng_info)
    if map_html is None:  # Failed!  Send an error
        LOGGER.error("Failed to create map HTML using WorldMapLatLngInfo: %s (%d)",\
            worldmap_latlng_info, worldmap_latlng_info.id)
        user_msg = 'Sorry! Failed to create map. Please try again. (code: s5)'
        json_msg = MessageHelperJSON.get_json_fail_msg(user_msg)
        return HttpResponse(json_msg,
                            content_type="application/json",
                            status=200)

    # -----------------------------------------
    # Looks good.  In the JSON response, send
    #   back the map HTML
    # -----------------------------------------
    data_dict = dict(map_html=map_html,
                     user_message_html=user_message_html,
                     id_main_panel_title=PANEL_TITLE_STYLE_MAP)

    json_msg = MessageHelperJSON.get_json_success_msg("great job",
                                                      data_dict=data_dict)

    return HttpResponse(json_msg, content_type="application/json", status=200)