本文整理汇总了Python中geonode.layers.forms.NewLayerUploadForm类的典型用法代码示例。如果您正苦于以下问题:Python NewLayerUploadForm类的具体用法?Python NewLayerUploadForm怎么用?Python NewLayerUploadForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NewLayerUploadForm类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: layer_upload
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:
示例2: layer_upload
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(".","_"))
if form.cleaned_data["templetize"] == True:
if request.user.is_superuser:
saved_template = save_template(name, base_file, request.user)
return HttpResponse(json.dumps({
"success": True,
"redirect_to": reverse('layer_simpli_upload')}))
##Verify NAME field of the shapefile
infile = ogr.Open(str(base_file), 1)
inlyr = infile.GetLayerByIndex(0)
layer_defn = inlyr.GetLayerDefn()
field_names = [layer_defn.GetFieldDefn(i).GetName() for i in range(layer_defn.GetFieldCount())]
if not 'NAME' in field_names:
fieldDefn = ogr.FieldDefn('NAME', ogr.OFTString)
fieldDefn.SetWidth(14)
inlyr.CreateField(fieldDefn)
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:
示例3: layer_upload
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:
logger.exception(e)
out['success'] = False
out['errors'] = str(e)
else:
out['success'] = True
out['url'] = reverse('layer_detail', args=[saved_layer.typename])
finally:
示例4: layer_upload
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:
示例5: layer_upload
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():
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"]
)
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)
示例6: layer_upload
def layer_upload(request, template="upload/layer_upload.html"):
if request.method == "GET":
ctx = {"charsets": CHARSETS, "is_layer": True}
category_form = CategoryForm(prefix="category_choice_field", initial=None)
return render_to_response(template, {"category_form": category_form}, 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()
topic_id = request.POST["category"]
topic_category = TopicCategory.objects.get(id=topic_id)
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"],
category=topic_category.identifier,
)
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), mimetype="application/json", status=status_code)
示例7: layer_upload
def layer_upload(request, template='upload/layer_upload.html'):
if request.method == 'GET':
creator_candidates = [request.user]
owner_candidates = [request.user]
for app in request.user.own_apps_list_all():
creator_candidates.append(app.get_alter_ego())
owner_candidates.extend(app.get_members())
ctx = {
'charsets': CHARSETS,
'layer_types': LayerType.objects.all(),
'owner_candidates': owner_candidates,
'creator_candidates': creator_candidates
}
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,
creator=request.user,
overwrite=False,
charset=form.cleaned_data["charset"],
abstract=form.cleaned_data["abstract"],
title=form.cleaned_data["layer_title"],
layer_type=form.cleaned_data["layer_type"],
owner=form.cleaned_data["owner"]
)
if not saved_layer.layer_type.is_default:
saved_layer.keywords.add(
*[saved_layer.layer_type.name]
)
except Exception as 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)
# XXX:
# se guarda de nuevo porque se borran los permisos
# y se deben dar permisos a las aplicaciones de nuevo
saved_layer.save()
# else:
# saved_layer.remove_all_permissions()
# for perm in ADMIN_PERMISSIONS:
# assign_perm(perm, saved_layer.owner, saved_layer.get_self_resource())
# if saved_layer.owner != saved_layer.creator:
# assign_perm(perm, saved_layer.creator, saved_layer.get_self_resource())
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
# out['layer_type'] = form.cleaned_data["layer_type"]
out['fill_metadata'] = saved_layer.layer_type.fill_metadata
out['layer_id'] = saved_layer.id
else:
status_code = 500
#.........这里部分代码省略.........
示例8: layer_upload
def layer_upload(request, template='upload/layer_upload.html'):
if request.method == 'GET':
##icraf_dr_categories = Category.objects.all() #^^
icraf_dr_categories = Category.objects.order_by('cat_num') #^^
##icraf_dr_coverages = Coverage.objects.all() #^^
icraf_dr_coverages = Coverage.objects.order_by('cov_num') #^^
##icraf_dr_sources = Source.objects.all() #^^
icraf_dr_sources = Source.objects.order_by('src_num') #^^
##icraf_dr_years = Year.objects.all() #^^
icraf_dr_years = Year.objects.order_by('year_num') #^^
layer_form = LayerForm(prefix="resource") #^^
category_form = CategoryForm(prefix="category_choice_field") #^^
ctx = {
'icraf_dr_categories': icraf_dr_categories, #^^
'icraf_dr_coverages': icraf_dr_coverages, #^^
'icraf_dr_sources': icraf_dr_sources, #^^
'icraf_dr_years': icraf_dr_years, #^^
"layer_form": layer_form, #^^
"category_form": category_form, #^^
'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.
icraf_dr_category =Category.objects.get(pk=request.POST['icraf_dr_category']) #^^
icraf_dr_coverage =Coverage.objects.get(pk=request.POST['icraf_dr_coverage']) #^^
icraf_dr_source =Source.objects.get(pk=request.POST['icraf_dr_source']) #^^
icraf_dr_year =Year.objects.get(pk=request.POST['icraf_dr_year']) #^^
icraf_dr_date_created = request.POST['icraf_dr_date_created'] #^^
icraf_dr_date_published = request.POST['icraf_dr_date_published'] #^^
icraf_dr_date_revised = request.POST['icraf_dr_date_revised'] #^^
#^^ validate date format
if (len(icraf_dr_date_created)): #^^
try: #^^
parse(icraf_dr_date_created) #^^
except ValueError: #^^
icraf_dr_date_created = None #^^
else: #^^
icraf_dr_date_created = None #^^
if (len(icraf_dr_date_published)): #^^
try: #^^
parse(icraf_dr_date_published) #^^
except ValueError: #^^
icraf_dr_date_published = None #^^
else: #^^
icraf_dr_date_published = None #^^
if (len(icraf_dr_date_revised)): #^^
try: #^^
parse(icraf_dr_date_revised) #^^
except ValueError: #^^
icraf_dr_date_revised = None #^^
else: #^^
icraf_dr_date_revised = None #^^
main = Main( #^^
category=icraf_dr_category, #^^
coverage=icraf_dr_coverage, #^^
source=icraf_dr_source, #^^
year=icraf_dr_year, #^^
basename=name_base, #^^
topic_category = TopicCategory(id=request.POST['category_choice_field']), #^^
regions = request.POST['regions'], #^^
date_created=icraf_dr_date_created, #^^
date_published=icraf_dr_date_published, #^^
date_revised=icraf_dr_date_revised #^^
) #^^
#^^ save icraf_dr_main and pass id to file_upload below
main.save() #^^
#.........这里部分代码省略.........
示例9: layer_upload
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
#.........这里部分代码省略.........
示例10: layer_upload
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
#.........这里部分代码省略.........
示例11: dispatch
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
#.........这里部分代码省略.........