Ejemplo n.º 1
0
    def test_style_validity_and_name(self):
        # Check that including an SLD with a valid shapefile results in the SLD
        # getting picked up
        d = None
        try:
            d = tempfile.mkdtemp()
            for f in ("san_andres_y_providencia.sld",
                      "lac.sld",
                      "freshgwabs2.sld",
                      "raster.sld",
                      "line.sld",):
                path = os.path.join(d, f)
                f = open(path, "wb")
                f.write(SLDS[splitext(basename(path))[0]])
                f.close()

            # Test 'san_andres_y_providencia.sld'
            san_andres_y_providencia_sld_file = os.path.join(
                d, "san_andres_y_providencia.sld")
            san_andres_y_providencia_sld_xml = open(
                san_andres_y_providencia_sld_file).read()
            san_andres_y_providencia_sld_name = extract_name_from_sld(
                None, san_andres_y_providencia_sld_xml)
            self.assertEquals(
                san_andres_y_providencia_sld_name,
                'san_andres_y_providencia_administrative')

            # Test 'lac.sld'
            lac_sld_file = os.path.join(d, "lac.sld")
            lac_sld_xml = open(lac_sld_file).read()
            lac_sld_name = extract_name_from_sld(
                None, lac_sld_xml, sld_file=lac_sld_file)
            self.assertEquals(lac_sld_name,
                              'LAC NonIndigenous Access to Sanitation')

            # Test 'freshgwabs2.sld'
            freshgwabs2_sld_file = os.path.join(d, "freshgwabs2.sld")
            freshgwabs2_sld_xml = open(freshgwabs2_sld_file).read()
            freshgwabs2_sld_name = extract_name_from_sld(
                None, freshgwabs2_sld_xml, sld_file=freshgwabs2_sld_file)
            self.assertEquals(freshgwabs2_sld_name, 'freshgwabs2')

            # Test 'raster.sld'
            raster_sld_file = os.path.join(d, "raster.sld")
            raster_sld_xml = open(raster_sld_file).read()
            raster_sld_name = extract_name_from_sld(
                None, raster_sld_xml, sld_file=raster_sld_file)
            self.assertEquals(
                raster_sld_name,
                'geonode-geonode_gwpollriskafriotest')

            # Test 'line.sld'
            line_sld_file = os.path.join(d, "line.sld")
            line_sld_xml = open(line_sld_file).read()
            line_sld_name = extract_name_from_sld(
                None, line_sld_xml, sld_file=line_sld_file)
            self.assertEquals(line_sld_name, 'line 3')
        finally:
            if d is not None:
                shutil.rmtree(d)
Ejemplo n.º 2
0
def layer_upload(request, template='upload/layer_upload.html'):
    if request.method == 'GET':
        mosaics = Layer.objects.filter(is_mosaic=True).order_by('name')
        ctx = {
            'mosaics': mosaics,
            'charsets': CHARSETS,
            'is_layer': True,
        }
        return render_to_response(template, RequestContext(request, ctx))
    elif request.method == 'POST':
        form = NewLayerUploadForm(request.POST, request.FILES)
        tempdir = None
        errormsgs = []
        out = {'success': False}
        if form.is_valid():
            title = form.cleaned_data["layer_title"]

            # Replace dots in filename - GeoServer REST API upload bug
            # and avoid any other invalid characters.
            # Use the title if possible, otherwise default to the filename
            if title is not None and len(title) > 0:
                name_base = title
            else:
                name_base, __ = os.path.splitext(
                    form.cleaned_data["base_file"].name)
                title = slugify(name_base.replace(".", "_"))
            name = slugify(name_base.replace(".", "_"))

            if form.cleaned_data["abstract"] is not None and len(form.cleaned_data["abstract"]) > 0:
                abstract = form.cleaned_data["abstract"]
            else:
                abstract = "No abstract provided."

            try:
                # Moved this inside the try/except block because it can raise
                # exceptions when unicode characters are present.
                # This should be followed up in upstream Django.
                tempdir, base_file = form.write_files()
                if not form.cleaned_data["style_upload_form"]:
                    saved_layer = file_upload(
                        base_file,
                        name=name,
                        user=request.user,
                        overwrite=False,
                        charset=form.cleaned_data["charset"],
                        abstract=abstract,
                        title=title,
                        metadata_uploaded_preserve=form.cleaned_data[
                            "metadata_uploaded_preserve"],
                        metadata_upload_form=form.cleaned_data["metadata_upload_form"])
                else:
                    saved_layer = Layer.objects.get(alternate=title)
                    if not saved_layer:
                        msg = 'Failed to process.  Could not find matching layer.'
                        raise Exception(msg)
                    sld = open(base_file).read()
                    # Check SLD is valid
                    extract_name_from_sld(gs_catalog, sld, sld_file=base_file)

                    match = None
                    styles = list(saved_layer.styles.all()) + [
                        saved_layer.default_style]
                    for style in styles:
                        if style and style.name == saved_layer.name:
                            match = style
                            break
                    cat = gs_catalog
                    layer = cat.get_layer(title)
                    if match is None:
                        try:
                            cat.create_style(saved_layer.name, sld, raw=True)
                            style = cat.get_style(saved_layer.name)
                            if layer and style:
                                layer.default_style = style
                                cat.save(layer)
                                saved_layer.default_style = save_style(style)
                        except Exception as e:
                            logger.exception(e)
                    else:
                        style = cat.get_style(saved_layer.name)
                        # style.update_body(sld)
                        try:
                            cat.create_style(saved_layer.name, sld, overwrite=True, raw=True)
                            style = cat.get_style(saved_layer.name)
                            if layer and style:
                                layer.default_style = style
                                cat.save(layer)
                                saved_layer.default_style = save_style(style)
                        except Exception as e:
                            logger.exception(e)

                    # Invalidate GeoWebCache for the updated resource
                    _invalidate_geowebcache_layer(saved_layer.alternate)

            except Exception as e:
                exception_type, error, tb = sys.exc_info()
                logger.exception(e)
                out['success'] = False
                out['errors'] = str(error)
                # Assign the error message to the latest UploadSession from
                # that user.
                latest_uploads = UploadSession.objects.filter(
                    user=request.user).order_by('-date')
                if latest_uploads.count() > 0:
                    upload_session = latest_uploads[0]
                    upload_session.error = str(error)
                    upload_session.traceback = traceback.format_exc(tb)
                    upload_session.context = log_snippet(CONTEXT_LOG_FILE)
                    upload_session.save()
                    out['traceback'] = upload_session.traceback
                    out['context'] = upload_session.context
                    out['upload_session'] = upload_session.id
            else:
                out['success'] = True
                if hasattr(saved_layer, 'info'):
                    out['info'] = saved_layer.info
                out['url'] = reverse(
                    'layer_detail', args=[
                        saved_layer.service_typename])
                if hasattr(saved_layer, 'bbox_string'):
                    out['bbox'] = saved_layer.bbox_string
                if hasattr(saved_layer, 'srid'):
                    out['crs'] = {
                        'type': 'name',
                        'properties': saved_layer.srid
                    }
                out['ogc_backend'] = settings.OGC_SERVER['default']['BACKEND']
                upload_session = saved_layer.upload_session
                if upload_session:
                    upload_session.processed = True
                    upload_session.save()
                permissions = form.cleaned_data["permissions"]
                if permissions is not None and len(permissions.keys()) > 0:
                    saved_layer.set_permissions(permissions)
                saved_layer.handle_moderated_uploads()
            finally:
                if tempdir is not None:
                    shutil.rmtree(tempdir)
        else:
            for e in form.errors.values():
                errormsgs.extend([escape(v) for v in e])
            out['errors'] = form.errors
            out['errormsgs'] = errormsgs
        if out['success']:
            status_code = 200
        else:
            status_code = 400
        if settings.MONITORING_ENABLED:
            request.add_resource('layer', saved_layer.alternate if saved_layer else name)
        return HttpResponse(
            json.dumps(out),
            content_type='application/json',
            status=status_code)
Ejemplo n.º 3
0
    def test_style_validity_and_name(self):
        # Check that including an SLD with a valid shapefile results in the SLD
        # getting picked up
        d = None
        try:
            d = tempfile.mkdtemp()
            for f in (
                    "san_andres_y_providencia.sld",
                    "lac.sld",
                    "freshgwabs2.sld",
                    "raster.sld",
                    "line.sld",
            ):
                path = os.path.join(d, f)
                f = open(path, "wb")
                f.write(SLDS[splitext(basename(path))[0]])
                f.close()

            # Test 'san_andres_y_providencia.sld'
            san_andres_y_providencia_sld_file = os.path.join(
                d, "san_andres_y_providencia.sld")
            san_andres_y_providencia_sld_xml = open(
                san_andres_y_providencia_sld_file).read()
            san_andres_y_providencia_sld_name = extract_name_from_sld(
                None, san_andres_y_providencia_sld_xml)
            self.assertEquals(san_andres_y_providencia_sld_name,
                              'san_andres_y_providencia_administrative')

            # Test 'lac.sld'
            lac_sld_file = os.path.join(d, "lac.sld")
            lac_sld_xml = open(lac_sld_file).read()
            lac_sld_name = extract_name_from_sld(None,
                                                 lac_sld_xml,
                                                 sld_file=lac_sld_file)
            self.assertEquals(lac_sld_name,
                              'LAC NonIndigenous Access to Sanitation')

            # Test 'freshgwabs2.sld'
            freshgwabs2_sld_file = os.path.join(d, "freshgwabs2.sld")
            freshgwabs2_sld_xml = open(freshgwabs2_sld_file).read()
            freshgwabs2_sld_name = extract_name_from_sld(
                None, freshgwabs2_sld_xml, sld_file=freshgwabs2_sld_file)
            self.assertEquals(freshgwabs2_sld_name, 'freshgwabs2')

            # Test 'raster.sld'
            raster_sld_file = os.path.join(d, "raster.sld")
            raster_sld_xml = open(raster_sld_file).read()
            raster_sld_name = extract_name_from_sld(None,
                                                    raster_sld_xml,
                                                    sld_file=raster_sld_file)
            self.assertEquals(raster_sld_name,
                              'geonode-geonode_gwpollriskafriotest')

            # Test 'line.sld'
            line_sld_file = os.path.join(d, "line.sld")
            line_sld_xml = open(line_sld_file).read()
            line_sld_name = extract_name_from_sld(None,
                                                  line_sld_xml,
                                                  sld_file=line_sld_file)
            self.assertEquals(line_sld_name, 'line 3')
        finally:
            if d is not None:
                shutil.rmtree(d)
Ejemplo n.º 4
0
def layer_upload(request, template='upload/layer_upload.html'):
    if request.method == 'GET':
        mosaics = Layer.objects.filter(is_mosaic=True).order_by('name')
        ctx = {
            'mosaics': mosaics,
            'charsets': CHARSETS,
            'is_layer': True,
        }
        return render_to_response(template, RequestContext(request, ctx))
    elif request.method == 'POST':
        form = NewLayerUploadForm(request.POST, request.FILES)
        tempdir = None
        errormsgs = []
        out = {'success': False}
        if form.is_valid():
            title = form.cleaned_data["layer_title"]

            # Replace dots in filename - GeoServer REST API upload bug
            # and avoid any other invalid characters.
            # Use the title if possible, otherwise default to the filename
            if title is not None and len(title) > 0:
                name_base = title
            else:
                name_base, __ = os.path.splitext(
                    form.cleaned_data["base_file"].name)
                title = slugify(name_base.replace(".", "_"))
            name = slugify(name_base.replace(".", "_"))

            if form.cleaned_data["abstract"] is not None and len(
                    form.cleaned_data["abstract"]) > 0:
                abstract = form.cleaned_data["abstract"]
            else:
                abstract = "No abstract provided."

            try:
                # Moved this inside the try/except block because it can raise
                # exceptions when unicode characters are present.
                # This should be followed up in upstream Django.
                tempdir, base_file = form.write_files()
                if not form.cleaned_data["style_upload_form"]:
                    saved_layer = file_upload(
                        base_file,
                        name=name,
                        user=request.user,
                        overwrite=False,
                        charset=form.cleaned_data["charset"],
                        abstract=abstract,
                        title=title,
                        metadata_uploaded_preserve=form.
                        cleaned_data["metadata_uploaded_preserve"],
                        metadata_upload_form=form.
                        cleaned_data["metadata_upload_form"])
                else:
                    saved_layer = Layer.objects.get(alternate=title)
                    if not saved_layer:
                        msg = 'Failed to process.  Could not find matching layer.'
                        raise Exception(msg)
                    sld = open(base_file).read()
                    # Check SLD is valid
                    extract_name_from_sld(gs_catalog, sld, sld_file=base_file)

                    match = None
                    styles = list(saved_layer.styles.all()) + [
                        saved_layer.default_style
                    ]
                    for style in styles:
                        if style and style.name == saved_layer.name:
                            match = style
                            break
                    cat = gs_catalog
                    layer = cat.get_layer(title)
                    if match is None:
                        try:
                            cat.create_style(saved_layer.name, sld, raw=True)
                            style = cat.get_style(saved_layer.name)
                            if layer and style:
                                layer.default_style = style
                                cat.save(layer)
                                saved_layer.default_style = save_style(style)
                        except Exception as e:
                            logger.exception(e)
                    else:
                        style = cat.get_style(saved_layer.name)
                        # style.update_body(sld)
                        try:
                            cat.create_style(saved_layer.name,
                                             sld,
                                             overwrite=True,
                                             raw=True)
                            style = cat.get_style(saved_layer.name)
                            if layer and style:
                                layer.default_style = style
                                cat.save(layer)
                                saved_layer.default_style = save_style(style)
                        except Exception as e:
                            logger.exception(e)

                    # Invalidate GeoWebCache for the updated resource
                    _invalidate_geowebcache_layer(saved_layer.alternate)

            except Exception as e:
                exception_type, error, tb = sys.exc_info()
                logger.exception(e)
                out['success'] = False
                out['errors'] = str(error)
                # Assign the error message to the latest UploadSession from
                # that user.
                latest_uploads = UploadSession.objects.filter(
                    user=request.user).order_by('-date')
                if latest_uploads.count() > 0:
                    upload_session = latest_uploads[0]
                    upload_session.error = str(error)
                    upload_session.traceback = traceback.format_exc(tb)
                    upload_session.context = log_snippet(CONTEXT_LOG_FILE)
                    upload_session.save()
                    out['traceback'] = upload_session.traceback
                    out['context'] = upload_session.context
                    out['upload_session'] = upload_session.id
            else:
                out['success'] = True
                if hasattr(saved_layer, 'info'):
                    out['info'] = saved_layer.info
                out['url'] = reverse('layer_detail',
                                     args=[saved_layer.service_typename])
                if hasattr(saved_layer, 'bbox_string'):
                    out['bbox'] = saved_layer.bbox_string
                if hasattr(saved_layer, 'srid'):
                    out['crs'] = {
                        'type': 'name',
                        'properties': saved_layer.srid
                    }
                out['ogc_backend'] = settings.OGC_SERVER['default']['BACKEND']
                upload_session = saved_layer.upload_session
                if upload_session:
                    upload_session.processed = True
                    upload_session.save()
                permissions = form.cleaned_data["permissions"]
                if permissions is not None and len(permissions.keys()) > 0:
                    saved_layer.set_permissions(permissions)
                saved_layer.handle_moderated_uploads()
            finally:
                if tempdir is not None:
                    shutil.rmtree(tempdir)
        else:
            for e in form.errors.values():
                errormsgs.extend([escape(v) for v in e])
            out['errors'] = form.errors
            out['errormsgs'] = errormsgs
        if out['success']:
            status_code = 200
        else:
            status_code = 400
        if settings.MONITORING_ENABLED:
            request.add_resource(
                'layer', saved_layer.alternate if saved_layer else name)
        return HttpResponse(json.dumps(out),
                            content_type='application/json',
                            status=status_code)
Ejemplo n.º 5
0
    def test_style_validity_and_name(self):
        # Check that including an SLD with a valid shapefile results in the SLD
        # getting picked up
        d = None
        try:
            d = tempfile.mkdtemp()
            files = (
                "san_andres_y_providencia.sld",
                "lac.sld",
                "freshgwabs2.sld",
                "raster.sld",
                "line.sld",
            )

            for f in files:
                path = os.path.join(d, f)
                with open(path, 'w') as f:
                    name, ext = splitext(basename(path))
                    f.write(SLDS[name])

            # Test 'san_andres_y_providencia.sld'
            san_andres_y_providencia_sld_file = os.path.join(
                d, "san_andres_y_providencia.sld")
            with open(san_andres_y_providencia_sld_file
                      ) as san_andres_y_providencia_sld_xml_file:
                san_andres_y_providencia_sld_xml = san_andres_y_providencia_sld_xml_file.read(
                )
            san_andres_y_providencia_sld_name = extract_name_from_sld(
                None, san_andres_y_providencia_sld_xml)
            self.assertEqual(san_andres_y_providencia_sld_name,
                             'san_andres_y_providencia_administrative')

            # Test 'lac.sld'
            lac_sld_file = os.path.join(d, "lac.sld")
            with open(lac_sld_file) as lac_sld_xml_file:
                lac_sld_xml = lac_sld_xml_file.read()
            lac_sld_name = extract_name_from_sld(None,
                                                 lac_sld_xml,
                                                 sld_file=lac_sld_file)
            self.assertEqual(lac_sld_name,
                             'LAC NonIndigenous Access to Sanitation')

            # Test 'freshgwabs2.sld'
            freshgwabs2_sld_file = os.path.join(d, "freshgwabs2.sld")
            with open(freshgwabs2_sld_file) as freshgwabs2_sld_xml_file:
                freshgwabs2_sld_xml = freshgwabs2_sld_xml_file.read()
            freshgwabs2_sld_name = extract_name_from_sld(
                None, freshgwabs2_sld_xml, sld_file=freshgwabs2_sld_file)
            self.assertEqual(freshgwabs2_sld_name, 'freshgwabs2')

            # Test 'raster.sld'
            raster_sld_file = os.path.join(d, "raster.sld")
            with open(raster_sld_file) as raster_sld_xml_file:
                raster_sld_xml = raster_sld_xml_file.read()
            raster_sld_name = extract_name_from_sld(None,
                                                    raster_sld_xml,
                                                    sld_file=raster_sld_file)
            self.assertEqual(raster_sld_name,
                             'geonode-geonode_gwpollriskafriotest')

            # Test 'line.sld'
            line_sld_file = os.path.join(d, "line.sld")
            with open(line_sld_file) as line_sld_xml_file:
                line_sld_xml = line_sld_xml_file.read()
            line_sld_name = extract_name_from_sld(None,
                                                  line_sld_xml,
                                                  sld_file=line_sld_file)
            self.assertEqual(line_sld_name, 'line 3')
        finally:
            if d is not None:
                shutil.rmtree(d, ignore_errors=True)