本文整理汇总了Python中geonode.layers.forms.LayerUploadForm类的典型用法代码示例。如果您正苦于以下问题:Python LayerUploadForm类的具体用法?Python LayerUploadForm怎么用?Python LayerUploadForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LayerUploadForm类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(request, layername, 'layers.change_layer',_PERMISSION_MSG_MODIFY)
if request.method == 'GET':
cat = Layer.objects.gs_catalog
info = cat.get_resource(layer.name)
is_featuretype = info.resource_type == FeatureType.resource_type
return render_to_response(template,
RequestContext(request, {'layer': layer,
'is_featuretype': is_featuretype}))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
if form.is_valid():
try:
tempdir, base_file = form.write_files()
saved_layer = save(layer, base_file, request.user, overwrite=True,
permissions=layer.get_all_level_info())
return HttpResponse(json.dumps({
"success": True,
"redirect_to": reverse('layer_metadata', args=[saved_layer.typename])}))
except Exception, e:
logger.info("Unexpected error during upload.")
return HttpResponse(json.dumps({
"success": False,
"errors": ["Unexpected error during upload: " + escape(str(e))]}))
finally:
示例2: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(request, layername, 'layers.change_layer',_PERMISSION_MSG_MODIFY)
if request.method == 'GET':
cat = Layer.objects.gs_catalog
info = cat.get_resource(layer.name)
is_featuretype = info.resource_type == FeatureType.resource_type
return render_to_response(template,
RequestContext(request, {'layer': layer,
'is_featuretype': is_featuretype}))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
saved_layer = save(layer, base_file, request.user, overwrite=True,
permissions=layer.get_all_level_info())
except Exception, e:
out['success'] = False
out['errors'] = str(e)
else:
out['success'] = True
out['url'] = reverse('layer_detail', args=[saved_layer.typename])
finally:
示例3: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(request, layername, 'base.change_resourcebase',_PERMISSION_MSG_MODIFY)
if request.method == 'GET':
return render_to_response(template,
RequestContext(request, {'layer': layer,
'is_featuretype': layer.is_vector()}))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
saved_layer = file_upload(base_file, name=layer.name,
user=request.user, overwrite=True)
except Exception, e:
out['success'] = False
out['errors'] = str(e)
else:
out['success'] = True
out['url'] = reverse('layer_detail', args=[saved_layer.service_typename])
finally:
示例4: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(
request,
layername,
'base.change_resourcebase',
_PERMISSION_MSG_MODIFY)
if request.method == 'GET':
ctx = {
'charsets': CHARSETS,
'layer': layer,
'is_featuretype': layer.is_vector(),
'is_layer': True,
}
return render_to_response(template,
RequestContext(request, ctx))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
saved_layer = file_upload(
base_file,
name=layer.name,
user=request.user,
overwrite=True,
charset=form.cleaned_data["charset"],
)
except Exception as e:
out['success'] = False
out['errors'] = str(e)
else:
out['success'] = True
out['url'] = reverse(
'layer_detail', args=[
saved_layer.service_typename])
finally:
if tempdir is not None:
shutil.rmtree(tempdir)
else:
errormsgs = []
for e in form.errors.values():
errormsgs.append([escape(v) for v in e])
out['errors'] = form.errors
out['errormsgs'] = errormsgs
if out['success']:
status_code = 200
else:
status_code = 500
return HttpResponse(
json.dumps(out),
mimetype='application/json',
status=status_code)
示例5: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(
request,
layername,
'base.change_resourcebase_permissions',
_PERMISSION_MSG_MODIFY)
try:
if "permission_denied" in layer:
return HttpResponse('You are not allowed to replace this layer',
mimetype="text/plain",
status=401)
except:
pass
if request.method == 'GET':
return render_to_response(
template, RequestContext(
request, {
'layer': layer, 'is_featuretype': layer.is_vector()}))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
saved_layer = file_upload(base_file, name=layer.name,
user=request.user, overwrite=True)
except Exception as e:
out['success'] = False
out['errors'] = str(e)
else:
out['success'] = True
out['url'] = reverse(
'layer_detail', args=[
saved_layer.service_typename])
finally:
if tempdir is not None:
shutil.rmtree(tempdir)
else:
errormsgs = []
for e in form.errors.values():
errormsgs.append([escape(v) for v in e])
out['errors'] = form.errors
out['errormsgs'] = errormsgs
if out['success']:
status_code = 200
else:
status_code = 500
return HttpResponse(
json.dumps(out),
mimetype='application/json',
status=status_code)
示例6: layer_replace
def layer_replace(request, layername, template="layers/layer_replace.html"):
layer = _resolve_layer(request, layername, "base.change_resourcebase", _PERMISSION_MSG_MODIFY)
if request.method == "GET":
ctx = {"charsets": CHARSETS, "layer": layer, "is_featuretype": layer.is_vector(), "is_layer": True}
return render_to_response(template, RequestContext(request, ctx))
elif request.method == "POST":
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
if layer.is_vector() and is_raster(base_file):
out["success"] = False
out["errors"] = _("You are attempting to replace a vector layer with a raster.")
elif (not layer.is_vector()) and is_vector(base_file):
out["success"] = False
out["errors"] = _("You are attempting to replace a raster layer with a vector.")
else:
# delete geoserver's store before upload
cat = gs_catalog
cascading_delete(cat, layer.typename)
saved_layer = file_upload(
base_file,
name=layer.name,
user=request.user,
overwrite=True,
charset=form.cleaned_data["charset"],
)
out["success"] = True
out["url"] = reverse("layer_detail", args=[saved_layer.service_typename])
except Exception as e:
out["success"] = False
out["errors"] = str(e)
finally:
if tempdir is not None:
shutil.rmtree(tempdir)
else:
errormsgs = []
for e in form.errors.values():
errormsgs.append([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)
示例7: testWriteFiles
def testWriteFiles(self):
files = dict(
base_file=SimpleUploadedFile('foo.shp', ' '),
shx_file=SimpleUploadedFile('foo.shx', ' '),
dbf_file=SimpleUploadedFile('foo.dbf', ' '),
prj_file=SimpleUploadedFile('foo.prj', ' '))
form = LayerUploadForm(dict(), files)
self.assertTrue(form.is_valid())
tempdir = form.write_files()[0]
self.assertEquals(set(os.listdir(tempdir)),
set(['foo.shp', 'foo.shx', 'foo.dbf', 'foo.prj']))
示例8: testWriteFiles
def testWriteFiles(self):
files = dict(
base_file=SimpleUploadedFile('foo.shp', ' '),
shx_file=SimpleUploadedFile('foo.shx', ' '),
dbf_file=SimpleUploadedFile('foo.dbf', ' '),
prj_file=SimpleUploadedFile('foo.prj', ' '))
form = LayerUploadForm(dict(), files)
self.assertTrue(form.is_valid())
tempdir = form.write_files()[0]
self.assertEquals(set(os.listdir(tempdir)),
set(['foo.shp', 'foo.shx', 'foo.dbf', 'foo.prj']))
the_zip = zipfile.ZipFile('test_upload.zip', 'w')
in_memory_file = StringIO.StringIO()
in_memory_file.write('test')
the_zip.writestr('foo.shp', in_memory_file.getvalue())
the_zip.writestr('foo.dbf', in_memory_file.getvalue())
the_zip.writestr('foo.shx', in_memory_file.getvalue())
the_zip.writestr('foo.prj', in_memory_file.getvalue())
the_zip.close()
files = dict(base_file=SimpleUploadedFile('test_upload.zip', open('test_upload.zip').read()))
form = LayerUploadForm(dict(), files)
self.assertTrue(form.is_valid())
tempdir = form.write_files()[0]
self.assertEquals(set(os.listdir(tempdir)),
set(['foo.shp', 'foo.shx', 'foo.dbf', 'foo.prj']))
os.remove('test_upload.zip')
示例9: testWriteFiles
def testWriteFiles(self):
files = dict(
base_file=SimpleUploadedFile("foo.shp", " "),
shx_file=SimpleUploadedFile("foo.shx", " "),
dbf_file=SimpleUploadedFile("foo.dbf", " "),
prj_file=SimpleUploadedFile("foo.prj", " "),
)
form = LayerUploadForm(dict(), files)
self.assertTrue(form.is_valid())
tempdir = form.write_files()[0]
self.assertEquals(set(os.listdir(tempdir)), set(["foo.shp", "foo.shx", "foo.dbf", "foo.prj"]))
the_zip = zipfile.ZipFile("test_upload.zip", "w")
in_memory_file = StringIO.StringIO()
in_memory_file.write("test")
the_zip.writestr("foo.shp", in_memory_file.getvalue())
the_zip.writestr("foo.dbf", in_memory_file.getvalue())
the_zip.writestr("foo.shx", in_memory_file.getvalue())
the_zip.writestr("foo.prj", in_memory_file.getvalue())
the_zip.close()
files = dict(base_file=SimpleUploadedFile("test_upload.zip", open("test_upload.zip").read()))
form = LayerUploadForm(dict(), files)
self.assertTrue(form.is_valid())
tempdir = form.write_files()[0]
self.assertEquals(set(os.listdir(tempdir)), set(["foo.shp", "foo.shx", "foo.dbf", "foo.prj"]))
os.remove("test_upload.zip")
示例10: layer_replace
def layer_replace(request, layername, template='layers/layer_replace.html'):
layer = _resolve_layer(
request,
layername,
'base.change_resourcebase',
_PERMISSION_MSG_MODIFY)
if request.method == 'GET':
ctx = {
'charsets': CHARSETS,
'layer': layer,
'is_featuretype': layer.is_vector(),
'is_layer': True,
}
return render_to_response(template,
RequestContext(request, ctx))
elif request.method == 'POST':
form = LayerUploadForm(request.POST, request.FILES)
tempdir = None
out = {}
if form.is_valid():
try:
tempdir, base_file = form.write_files()
if layer.is_vector() and is_raster(base_file):
out['success'] = False
out['errors'] = _("You are attempting to replace a vector layer with a raster.")
elif (not layer.is_vector()) and is_vector(base_file):
out['success'] = False
out['errors'] = _("You are attempting to replace a raster layer with a vector.")
else:
try: #^^
main = Main.objects.get(layer=layer) #^^
main_id = main.id #^^
except: #^^
main_id = None #^^
# delete geoserver's store before upload
cat = gs_catalog
cascading_delete(cat, layer.typename)
saved_layer = file_upload(
base_file,
name=layer.name,
user=request.user,
overwrite=True,
charset=form.cleaned_data["charset"],
main_id=main_id, #^^
)
print 'debug file_upload success'
out['success'] = True
out['url'] = reverse(
'layer_detail', args=[
saved_layer.service_typename])
except Exception as e:
out['success'] = False
out['errors'] = str(e)
finally:
if tempdir is not None:
shutil.rmtree(tempdir)
else:
errormsgs = []
for e in form.errors.values():
errormsgs.append([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)