def POST(self, map_name, format): data = get_data(name="layer", mandatory=["name", "resource"], authorized=["name", "title", "abstract", "resource", "enabled"]) l_name = data.pop("name") l_enabled = data.pop("enabled", True) # This means we can have one mapfile for each workspace # and if eveything uses urls it should work *almost* as is. url = urlparse.urlparse(data["resource"]["href"]) if url.path.startswith(web.ctx.homepath): path = url.path[len(web.ctx.homepath):] else: raise webapp.BadRequest(message="Resource href is not handled by MRA.") try: _, map_name, _, ws_name, st_type, st_name, r_type, r_name = path.rsplit("/", 7) except ValueError: raise webapp.NotFound(message="ressource '%s' was not found." % path) r_name = r_name.rsplit(".", 1)[0] mf, ws = get_mapfile_workspace(map_name, ws_name) with webapp.mightNotFound(r_type, workspace=ws_name): try: model = ws.get_model(r_name, r_type[:-1], st_name) except ValueError: webapp.NotFound("Invalid layer model '%s'" % r_type[:-1]) with webapp.mightConflict("layer", mapfile=map_name): mf.create_layer(ws, model, l_name, l_enabled) mf.save() webapp.Created("%s/maps/%s/layers/%s%s" % (web.ctx.home, map_name, l_name, (".%s" % format) if format else ""))
def POST(self, ws_name, cs_name, format): """Create a new coverage.""" ws = get_workspace(ws_name) data = get_data(name="coverage", mandatory=["name"], authorized=["name", "title", "abstract"]) with webapp.mightConflict("coverage", coveragestore=cs_name): with webapp.mightNotFound("coverage", coveragestore=cs_name): ws.create_coveragemodel(data["name"], cs_name, data) ws.save() # Then creates the associated layer by default: model = ws.get_coveragemodel(cs_name, data["name"]) mf = mra.get_available() with webapp.mightConflict(): mf.create_layer(model, data["name"], True) mf.save() webapp.Created("%s/workspaces/%s/coveragestores/%s/coverages/%s.%s" % ( web.ctx.home, ws.name, cs_name, data["name"], format))
def POST(self, format): """Create a new workspace.""" data = get_data(name="workspace", mandatory=["name"], authorized=["name"]) ws_name = data.pop("name") with webapp.mightConflict(): mra.create_workspace(ws_name).save() webapp.Created("%s/workspaces/%s.%s" % (web.ctx.home, ws_name, format))
def POST(self, format): data = get_data(name="mapfile", mandatory=["name"], authorized=["name", "title", "abstract"]) map_name = data.pop("name") path = tools.mk_mapfile_path(map_name) with webapp.mightConflict(message="Mapfile '{exception}' already exists."): mapfile.create_mapfile(path, map_name, data) webapp.Created("%s/maps/%s%s" % (web.ctx.home, map_name, (".%s" % format) if format else ""))
def POST(self, map_name, ws_name, cs_name, format): mf, ws = get_mapfile_workspace(map_name, ws_name) data = get_data(name="coverage", mandatory=["name"], authorized=["name", "title", "abstract"]) with webapp.mightConflict("coverage", coveragestore=cs_name): ws.create_coveragemodel(data["name"], cs_name, data) ws.save() webapp.Created("%s/maps/%s/workspaces/%s/coveragestores/%s/coverages/%s%s" % ( web.ctx.home, map_name, ws.name, cs_name, data["name"], (".%s" % format) if format else ""))
def POST(self, map_name, ws_name, ds_name, format): mf, ws = get_mapfile_workspace(map_name, ws_name) data = get_data(name="featureType", mandatory=["name"], authorized=["name", "title", "abstract"]) with webapp.mightConflict("featureType", datastore=ds_name): with webapp.mightNotFound("featureType", datastore=ds_name): ws.create_featuretypemodel(data["name"], ds_name, data) ws.save() webapp.Created("%s/maps/%s/workspaces/%s/datastores/%s/featuretypes/%s%s" % ( web.ctx.home, map_name, ws.name, ds_name, data["name"], (".%s" % format) if format else ""))
def POST(self, map_name, ws_name, format): mf, ws = get_mapfile_workspace(map_name, ws_name) data = get_data(name="coverageStore", mandatory=["name"], authorized=["name", "title", "abstract", "connectionParameters"]) cs_name = data.pop("name") with webapp.mightConflict("coverageStore", workspace=ws_name): ws.create_coveragestore(cs_name, data) ws.save() webapp.Created("%s/maps/%s/workspaces/%s/coveragestores/%s%s" % ( web.ctx.home, map_name, ws_name, cs_name, (".%s" % format) if format else ""))
def POST(self, map_name, format): mf = get_mapfile(map_name) data = get_data(name="layerGroup", mandatory=["name"], authorized=["name", "title", "abstract", "layers"]) lg_name = data.pop("name") layers = [mf.get_layer(l_name) for l_name in data.pop("layers", [])] with webapp.mightConflict("layerGroup", mapfile=map_name): lg = mf.create_layergroup(lg_name, data) lg.add(*layers) mf.save() webapp.Created("%s/maps/%s/layergroups/%s%s" % (web.ctx.home, map_name, lg.name, (".%s" % format) if format else ""))
def POST(self, ws_name, ds_name, format): """Create a new feature type. It create the associated layer by defaut. This layer is added in the mapfile: <layer.map> """ ws = get_workspace(ws_name) data = get_data(name="featureType", mandatory=["name"], authorized=["name", "title", "abstract"]) # Creates first the feature type: with webapp.mightConflict("featureType", datastore=ds_name): with webapp.mightNotFound("featureType", datastore=ds_name): ws.create_featuretypemodel(ds_name, data["name"], data) ws.save() # Then creates the associated layer by default: model = ws.get_featuretypemodel(ds_name, data["name"]) mf = mra.get_available() with webapp.mightConflict(): mf.create_layer(model, data["name"], True) mf.save() webapp.Created("%s/workspaces/%s/datastores/%s/featuretypes/%s.%s" % ( web.ctx.home, ws.name, ds_name, data["name"], format))
def POST(self, ws_name, format): """Create a new data store.""" ws = get_workspace(ws_name) data = get_data(name="dataStore", mandatory=["name"], authorized=["name", "title", "abstract", "connectionParameters"]) ds_name = data.pop("name") with webapp.mightConflict("dataStore", workspace=ws_name): ws.create_datastore(ds_name, data) ws.save() webapp.Created("%s/workspaces/%s/datastores/%s.%s" % ( web.ctx.home, ws_name, ds_name, format))
def POST(self, map_name, format): mf = get_mapfile(map_name) params = web.input(name=None) name = params.name if name == None: raise webapp.BadRequest(message="no parameter 'name' given.") with webapp.mightConflict(message="style {exception} already exists."): if name in tools.iter_styles(mf): raise webapp.KeyExists(name) data = web.data() path = tools.mk_style_path(name) with open(path, "w") as f: f.write(data)
def POST(self, format): """Create a new layer group.""" mf = mra.get_available() data = get_data(name="layerGroup", mandatory=["name"], authorized=["name", "title", "abstract", "layers"]) lg_name = data.pop("name") with webapp.mightNotFound(): layers = [mf.get_layer(l_name) for l_name in data.pop("layers", [])] with webapp.mightConflict(): lg = mf.create_layergroup(lg_name, data) lg.add(*layers) mf.save() webapp.Created("%s/layergroups/%s.%s" % (web.ctx.home, lg.name, format))
def POST(self, format): """Create a new layer.""" data = get_data(name="layer", mandatory=["name", "resource"], authorized=["name", "title", "abstract", "resource", "enabled", "defaultStyle"]) l_name = data.pop("name") l_enabled = data.pop("enabled", True) href = data["resource"]["href"] try: ws_name, st_type, st_name, r_type, r_name = mra.href_parse(href, 5) except ValueError: raise webapp.NotFound(message="resource \"%s\" was not found." % href) st_type, r_type = st_type[:-1], r_type[:-1] # Remove trailing s. ws = get_workspace(ws_name) with webapp.mightNotFound(r_type, workspace=ws_name): try: model = ws.get_layermodel(r_type, st_name, r_name) except ValueError: raise KeyError(r_type) mf = mra.get_available() with webapp.mightConflict(): mf.create_layer(model, l_name, l_enabled) # If we have a defaultStyle apply it. s_name = data.get("defaultStyle", {}).get("name") if s_name: with webapp.mightNotFound(): style = mra.get_style(s_name) layer = mf.get_layer(l_name) layer.add_style_sld(mf, s_name, style) # Remove the automatic default style. for s_name in layer.iter_styles(): if s_name == tools.get_dflt_sld_name(layer.ms.type): layer.remove_style(s_name) mf.save() webapp.Created("%s/layers/%s.%s" % (web.ctx.home, l_name, format))
def POST(self, ws_name, format): """Create new coverage store.""" ws = get_workspace(ws_name) data = get_data(name="coverageStore", mandatory=["name"], authorized=["name", "title", "abstract", "connectionParameters"]) cs_name = data.pop("name") with webapp.mightConflict("coverageStore", workspace=ws_name): ws.create_coveragestore(cs_name, data) ws.save() # Then creates the associated layer by default: # model = ws.get_coveragetypemodel(cs_name, data["name"]) # mf = mra.get_available() # with webapp.mightConflict(): # mf.create_layer(model, data["name"], True) # mf.save() webapp.Created("%s/workspaces/%s/coveragestores/%s.%s" % ( web.ctx.home, ws_name, cs_name, format))
def PUT(self, ws_name, st_type, st_name, f_type, format): """Uploads a file from a local source. The body of the request is the file itself.""" import zipfile # TODO: According to geoserv's examples we might have to handle # directories as well as files, in that case we want to upload # all the files from the directory. # Lets first try to get the file. if f_type == "file": # Check if zip or not... data = web.data() elif f_type == "url": raise webapp.NotImplemented() elif f_type == "external": raise webapp.NotImplemented() ws = get_workspace(ws_name) # Now we make sure the store exists. try: ws.get_store_info(st_type, st_name) except KeyError: # Create the store if it seems legit and it does not exist. if st_type == "datastores" or st_type == "coveragestores": st_type = st_type[:-1] # Remove trailing 's' with webapp.mightConflict("Workspace", workspace=ws_name): ws.create_store(st_type, st_name, {}) # Finaly check if its OK now. with webapp.mightNotFound(message="Store {exception} does not exist " "and it could not be created."): ws.get_store_info(st_type, st_name) # Then we store the file. ext = web.ctx.env.get('CONTENT_TYPE', '').split("/")[-1] path = mra.create_file(st_name + (".%s" % ext) if ext else "", data=data) dest = os.path.join(os.path.split(path)[0], st_name) # We also unzip it if its ziped. ctype = web.ctx.env.get('CONTENT_TYPE', None) if ctype == "application/zip": z = zipfile.ZipFile(path) for f in z.namelist(): fp = mra.mk_path(mra.get_file_path(st_name, f)) # If the file has the correct target we might want it. if format and fp.endswith(format) and not tools.is_hidden(fp): path = fp z.extract(f, path=dest) # Set new connection parameters: ws.update_store(st_type, st_name, {"connectionParameters":{"url":"file:"+mra.pub_file_path(path)}}) ws.save() # Finally we might have to configure it. params = web.input(configure="none") if params.configure == "first": raise webapp.NotImplemented() elif params.configure == "none": pass elif params.configure == "all": raise webapp.NotImplemented() else: raise webapp.BadRequest(message="configure must be one of 'first', 'none' or 'all'.")