""" Service MG/sources/calls/create to create the view to preview the content. """ from io import StringIO import csv from tools import const from tools.layer import Layer def post(task, user=None, input_layer: Layer = None, params: dict = None, overwrite=None): """ Create a foreign table through wrapper to obtain its information and then be able to create a view to preview the attributes selected by the user. Parameters ---------- user: str User ID input_layer : Layer Object Input Layer params: dict Dict with attributes, name view or sql clauses overwrite: bool Bool to show whether the view could be overwrite if exist Returns ------- dict Dict with status and message CSV Examples -------- >>> import requests >>> input_layer = {'user': '', ... 'password': '', ... 'ip': '192.168.1.3', ... 'protocol': 'HTTP', ... 'type': 'VECTOR', ... 'driver_type': 'SHAPE', ... 'port': '8001', ... "domain": '', ... "source": 'a/SHP/AREAS_CANINAS.shp', ... 'layer_name': 'AREAS_CANINAS'} >>> params = {'view': 'areas_caninas', ... 'select': '*', ... 'where': '', ... 'groupBy': '', ... 'having': '', ... 'orderBy': '', ... 'limit': ''} >>> parameters={'user':'TEST', "input_layer":input_layer, "params":params, "overwrite":True} >>> response = requests.post(':/MG/sources/calls/create', ... json=parameters) >>> response.json() {'task_id': 'XoiRl9'} """ response = input_layer.create(params=params, overwrite=overwrite) if response is None: obj = {'status': 'Error', 'message': 'Format type {} - {} not supported as PostgreSQL layer. ' 'Try to save the layer with the original parameters'.format( input_layer["type"], input_layer["driver_type"])} elif response[0] == [True]: output = StringIO() csv_writer = csv.writer(output, dialect='excel', delimiter=',') csv_writer.writerows([response[1], *response[2]]) string_csv = output.getvalue() obj = {'status': 'ok', 'message': string_csv} else: obj = {'status': 'Error', 'message': 'psycopg2.errors.DuplicateTable: ' '"{}" already exists'.format(params["view"])} return obj POST_REQUEST = { 'function': post, 'parser': const.PARSER_SOURCES }