コード例 #1
0
def layer_upload(request, template='upload/layer_upload.html'):
    if request.method == 'GET':
        ctx = {
            '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():
            tempdir, base_file = form.write_files()
            out.update(
                _save_layer(form.cleaned_data, base_file, request.user,
                            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
        return HttpResponse(json.dumps(out),
                            mimetype='application/json',
                            status=status_code)
コード例 #2
0
ファイル: views.py プロジェクト: rbs-pli/geonode
def layer_upload(request, template='upload/layer_upload.html'):
    if request.method == 'GET':
        ctx = {'charsets': CHARSETS}
        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)

            name = slugify(name_base.replace(".", "_"))

            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()
                saved_layer = file_upload(
                    base_file,
                    name=name,
                    user=request.user,
                    overwrite=False,
                    charset=form.cleaned_data["charset"],
                    abstract=form.cleaned_data["abstract"],
                    title=form.cleaned_data["layer_title"],
                )

            except Exception, e:
                logger.exception(e)
                out['success'] = False
                out['errors'] = str(e)
            else:
                out['success'] = True
                out['url'] = reverse('layer_detail',
                                     args=[saved_layer.service_typename])

                permissions = form.cleaned_data["permissions"]
                if permissions is not None and len(permissions.keys()) > 0:
                    saved_layer.set_permissions(permissions)

            finally:
コード例 #3
0
def layer_upload(request, template='layers/layer_upload.html'):
    if request.method == 'GET':
        return render_to_response(template, RequestContext(request, {}))
    elif request.method == 'POST':
        form = NewLayerUploadForm(request.POST, request.FILES)
        tempdir = None
        if form.is_valid():
            try:
                tempdir, base_file = form.write_files()
                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)

                name = slugify(name_base.replace(".", "_"))

                saved_layer = save(
                    name,
                    base_file,
                    request.user,
                    overwrite=False,
                    abstract=form.cleaned_data["abstract"],
                    title=form.cleaned_data["layer_title"],
                    permissions=form.cleaned_data["permissions"])
                return HttpResponse(
                    json.dumps({
                        "success":
                        True,
                        "redirect_to":
                        reverse('layer_metadata', args=[saved_layer.typename])
                    }))
            except Exception, e:
                logger.exception("Unexpected error during upload.")
                return HttpResponse(
                    json.dumps({
                        "success":
                        False,
                        "errormsgs":
                        ["Unexpected error during upload: " + escape(str(e))]
                    }))
            finally:
コード例 #4
0
def dataset_style_upload(request):
    form = NewLayerUploadForm(request.POST, request.FILES)
    body = {}
    if not form.is_valid():
        body['success'] = False
        body['errors'] = form.errors
        return HttpResponse(
            json.dumps(body),
            content_type='application/json',
            status=500)

    status_code = 200
    try:
        data = form.cleaned_data
        body = {
            'success': True,
            'style': data.get('dataset_title'),
        }

        layer = _resolve_dataset(
            request,
            data.get('dataset_title'),
            'base.change_resourcebase',
            _PERMISSION_MSG_MODIFY)

        sld = request.FILES['sld_file'].read()

        set_dataset_style(layer, data.get('dataset_title'), sld)
        body['url'] = layer.get_absolute_url()
        body['bbox'] = layer.bbox_string
        body['crs'] = {
            'type': 'name',
            'properties': layer.srid
        }
        body['ogc_backend'] = settings.OGC_SERVER['default']['BACKEND']
        body['status'] = ['finished']
    except Exception as e:
        status_code = 500
        body['success'] = False
        body['errors'] = str(e.args[0])

    return HttpResponse(
        json.dumps(body),
        content_type='application/json',
        status=status_code)
コード例 #5
0
ファイル: views.py プロジェクト: fedesanchez/geonode
def layer_upload(request, template='upload/layer_upload.html'):
    if request.method == 'GET':
        return render_to_response(template, RequestContext(request, {}))
    elif request.method == 'POST':
        form = NewLayerUploadForm(request.POST, request.FILES)
        tempdir = None
        errormsgs = []
        out = {'success': False}

        if form.is_valid():
            tempdir, base_file = form.write_files()
            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)

            name = slugify(name_base.replace(".", "_"))

            try:
                saved_layer = save(
                    name,
                    base_file,
                    request.user,
                    overwrite=False,
                    abstract=form.cleaned_data["abstract"],
                    title=form.cleaned_data["layer_title"],
                    permissions=form.cleaned_data["permissions"])
            except Exception, e:
                out['success'] = False
                out['errors'] = str(e)
            else:
                out['success'] = True
                out['url'] = reverse('layer_detail',
                                     args=[saved_layer.typename])
            finally:
コード例 #6
0
ファイル: views.py プロジェクト: ianedwardthomas/geonode
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)
            name = slugify(name_base.replace(".", "_"))
            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()
                saved_layer = file_upload(
                    base_file,
                    name=name,
                    user=request.user,
                    overwrite=False,
                    charset=form.cleaned_data["charset"],
                    abstract=form.cleaned_data["abstract"],
                    title=form.cleaned_data["layer_title"],
                    metadata_uploaded_preserve=form.cleaned_data["metadata_uploaded_preserve"],
                    metadata_upload_form=form.cleaned_data["metadata_upload_form"]
                )
            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])
                upload_session = saved_layer.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
        return HttpResponse(
            json.dumps(out),
            content_type='application/json',
            status=status_code)
コード例 #7
0
    def dispatch(self, request_type, request, **kwargs):
        if request.method == 'POST':
            username = request.GET.get('username') or request.POST.get(
                'username')
            password = request.GET.get('password') or request.POST.get(
                'password')
            out = {'success': False}
            try:
                user = Profile.objects.get(username=username)
            except Profile.DoesNotExist:
                out['errors'] = 'The username and/or password you specified are not correct.'
                return HttpResponse(json.dumps(out),
                                    content_type='application/json',
                                    status=404)

            if user.check_password(password):
                request.user = user
            else:
                out['errors'] = 'The username and/or password you specified are not correct.'
                return HttpResponse(json.dumps(out),
                                    content_type='application/json',
                                    status=404)
            form = NewLayerUploadForm(request.POST, request.FILES)
            tempdir = None
            errormsgs = []
            if form.is_valid():
                title = form.cleaned_data["layer_title"]
                category = form.cleaned_data["category"]
                organization_id = form.cleaned_data["organization"]
                try:
                    group = GroupProfile.objects.get(id=organization_id)
                except GroupProfile.DoesNotExist:
                    out['errors'] = 'Organization does not exists'
                    return HttpResponse(json.dumps(out),
                                        content_type='application/json',
                                        status=404)
                else:
                    if not group in group.groups_for_user(request.user):
                        out['errors'] = 'Organization access denied'
                        return HttpResponse(json.dumps(out),
                                            content_type='application/json',
                                            status=404)
                # 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
                    keywords = title.split()
                else:
                    name_base, __ = os.path.splitext(
                        form.cleaned_data["base_file"].name)
                name = slugify(name_base.replace(".", "_"))
                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()
                    saved_layer = file_upload(
                        base_file,
                        name=name,
                        user=request.user,
                        category=category,
                        group=group,
                        keywords=keywords,
                        status='ACTIVE',
                        overwrite=False,
                        charset=form.cleaned_data["charset"],
                        abstract=form.cleaned_data["abstract"],
                        title=form.cleaned_data["layer_title"],
                        metadata_uploaded_preserve=form.
                        cleaned_data["metadata_uploaded_preserve"])
                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])
                    upload_session = saved_layer.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)
                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
            return HttpResponse(json.dumps(out),
                                content_type='application/json',
                                status=status_code)
コード例 #8
0
ファイル: views.py プロジェクト: akarmas/geonode
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)
            name = slugify(name_base.replace(".", "_"))
            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=form.cleaned_data["abstract"],
                        title=form.cleaned_data["layer_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()

                    try:
                        dom = etree.XML(sld)
                    except Exception:
                        raise Exception(
                            "The uploaded SLD file is not valid XML")

                    el = dom.findall(
                        "{http://www.opengis.net/sld}NamedLayer/{http://www.opengis.net/sld}Name"
                    )
                    if len(el) == 0:
                        raise Exception(
                            "Please provide a name, unable to extract one from the SLD."
                        )

                    match = None
                    styles = list(saved_layer.styles.all()) + [
                        saved_layer.default_style
                    ]
                    for style in styles:
                        if style.name == saved_layer.name:
                            match = style
                            break
                    if match is None:
                        cat = gs_catalog
                        try:
                            cat.create_style(saved_layer.name, sld)
                        except Exception as e:
                            logger.exception(e)
                        style = cat.get_style(saved_layer.name)
                        layer = cat.get_layer(title)
                        if layer and style:
                            layer.default_style = style
                            cat.save(layer)
                            saved_layer.default_style = save_style(style)
                    else:
                        cat = gs_catalog
                        style = cat.get_style(saved_layer.name)
                        style.update_body(sld)
            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
                    }
                upload_session = saved_layer.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
        return HttpResponse(json.dumps(out),
                            content_type='application/json',
                            status=status_code)
コード例 #9
0
ファイル: views.py プロジェクト: jahangir091/geodash
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,
            'allowed_file_types': ['.cst', '.dbf', '.prj', '.shp', '.shx'],
            'categories': TopicCategory.objects.all(),
            'organizations': GroupProfile.objects.filter(groupmember__user=request.user),
        }
        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"]
            category = form.cleaned_data["category"]
            organization_id = form.cleaned_data["organization"]
            admin_upload = form.cleaned_data["admin_upload"]
            group = GroupProfile.objects.get(id=organization_id)
            # 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
                keywords = title.split()
            else:
                name_base, __ = os.path.splitext(
                    form.cleaned_data["base_file"].name)
                keywords = name_base.split()
            ignore_keys = KeywordIgnoreListModel.objects.values_list('key', flat=True)
            keyword_list = []
            for key in keywords:
                if key not in ignore_keys and not key.isdigit() and any(c.isalpha() for c in key) and len(key) > 2:
                    keyword_list.append(key)

            keywords = keyword_list

            name = slugify(name_base.replace(".", "_"))
            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()
                saved_layer = file_upload(
                    base_file,
                    name=name,
                    user=request.user,
                    category=category,
                    group=group,
                    keywords=keywords,
                    overwrite=False,
                    charset=form.cleaned_data["charset"],
                    abstract=form.cleaned_data["abstract"],
                    title=form.cleaned_data["layer_title"],
                    metadata_uploaded_preserve=form.cleaned_data["metadata_uploaded_preserve"]
                )
                if admin_upload:
                    saved_layer.status = 'ACTIVE'
                    saved_layer.save()

            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])
                upload_session = saved_layer.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)
            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
        return HttpResponse(
            json.dumps(out),
            content_type='application/json',
            status=status_code)
コード例 #10
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
        saved_layer = 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,
                                workspace=settings.DEFAULT_WORKSPACE)
                            style = cat.get_style(saved_layer.name, workspace=settings.DEFAULT_WORKSPACE) or \
                                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, workspace=settings.DEFAULT_WORKSPACE) or \
                            cat.get_style(saved_layer.name)
                        # style.update_body(sld)
                        try:
                            cat.create_style(
                                saved_layer.name,
                                sld,
                                overwrite=True,
                                raw=True,
                                workspace=settings.DEFAULT_WORKSPACE)
                            style = cat.get_style(saved_layer.name, workspace=settings.DEFAULT_WORKSPACE) or \
                                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)
コード例 #11
0
def dataset_upload_metadata(request):
    out = {}
    errormsgs = []

    form = NewLayerUploadForm(request.POST, request.FILES)

    if form.is_valid():

        tempdir = tempfile.mkdtemp(dir=settings.STATIC_ROOT)

        relevant_files = select_relevant_files(['xml'],
                                               iter(request.FILES.values()))

        logger.debug(f"relevant_files: {relevant_files}")

        write_uploaded_files_to_disk(tempdir, relevant_files)

        base_file = os.path.join(tempdir, form.cleaned_data["base_file"].name)

        name = form.cleaned_data['dataset_title']
        layer = Dataset.objects.filter(typename=name)
        if layer.exists():
            dataset_uuid, vals, regions, keywords, _ = parse_metadata(
                open(base_file).read())
            if dataset_uuid and layer.first().uuid != dataset_uuid:
                out['success'] = False
                out['errors'] = "The UUID identifier from the XML Metadata, is different from the one saved"
                return HttpResponse(json.dumps(out),
                                    content_type='application/json',
                                    status=404)
            updated_dataset = update_resource(layer.first(), base_file,
                                              regions, keywords, vals)
            updated_dataset.save()
            out['status'] = ['finished']
            out['url'] = updated_dataset.get_absolute_url()
            out['bbox'] = updated_dataset.bbox_string
            out['crs'] = {'type': 'name', 'properties': updated_dataset.srid}
            out['ogc_backend'] = settings.OGC_SERVER['default']['BACKEND']
            if hasattr(updated_dataset, 'upload_session'):
                upload_session = updated_dataset.upload_session
                upload_session.processed = True
                upload_session.save()
            status_code = 200
            out['success'] = True
            return HttpResponse(json.dumps(out),
                                content_type='application/json',
                                status=status_code)
        else:
            out['success'] = False
            out['errors'] = "Dataset selected does not exists"
            status_code = 404
        return HttpResponse(json.dumps(out),
                            content_type='application/json',
                            status=status_code)
    else:
        for e in form.errors.values():
            errormsgs.extend([escape(v) for v in e])
        out['errors'] = form.errors
        out['errormsgs'] = errormsgs

    return HttpResponse(json.dumps(out),
                        content_type='application/json',
                        status=500)