Example #1
0
    def get(self, request):
        """

        :param request:
        :type request:
        :return:
        :rtype:
        """
        store = HomeView.store  # 'new_vforwater_gis'
        workspace = HomeView.workspace  # 'CAOS_update'
        test_geoserver_env(store, workspace)

        if 'csv' in request.GET:
            # if 'download_data' in request.GET:
            s_id = json.loads(request.GET.get('csv'))
            accessible_data = get_accessible_data(request, s_id)
            error_list = accessible_data['blocked']
            accessible_data = accessible_data['open']

            if len(accessible_data) > 0:
                # TODO: There are 3 Solutions to get data from the different tables.
                #  Solution 1 produces many None fields, hence is discarded.
                #  Solution 2 makes a query from 'Entries' to get datatype and builds another query from 'Entries'
                #  according to the result.
                #  Solution 3 gets the datatype and 'eval' a query for the respective table.
                #  Decide if solution 2 or 3 is better.
                #  Check if results are the right datasets!!!
                #  (Unused solutions deleted. Check commit from Sept 3, 2020)

                # Solution 2:
                # ===========
                rows = get_dataset(accessible_data[0])

                pseudo_buffer = Echo()
                writer = csv.writer(pseudo_buffer)
                response = StreamingHttpResponse(
                    (writer.writerow(row) for row in rows),
                    content_type="text/csv")
                response[
                    'Content-Disposition'] = 'attachment; filename="somefilename.csv"'
            return response

        if 'shp' in request.GET:
            s_id = request.GET.get('shp')
            accessible_data = get_accessible_data(request, s_id)
            error_list = accessible_data['blocked']
            accessible_data = accessible_data['open']

            if len(accessible_data) > 0:
                layer_name = 'shp_{}_{}_{}'.format(request.user,
                                                   request.user.id, s_id)
                srid = 4326
                # create layer on geoserver to request shp file
                create_layer(request, layer_name, store, workspace, s_id)
                # use GEOSERVER shape-zip
                url = '{0}/{1}/ows?service=wfs&version=1.0.0&request=GetFeature&typeName={1}:{' \
                      '2}&outputFormat=shape-zip&srsname=EPSG:{3}'.format(LOCAL_GEOSERVER, workspace, layer_name, srid)
                request = requests.get(url)
                pzfile = PyZip().from_bytes(request.content)
                try:
                    del pzfile['wfsrequest.txt']
                except KeyError:
                    pass

                # clean up right after request:
                delete_layer(layer_name, store, workspace)
            return HttpResponse(pzfile.to_bytes(),
                                content_type='application/zip')

        # TODO: schema Location shows too much information for possible intruder. Figure out how to improve?
        if 'xml' in request.GET:
            id = request.GET.get('xml')
            accessible_data = get_accessible_data(request, id)
            error_list = accessible_data['blocked']
            accessible_data = accessible_data['open']

            if len(accessible_data) > 0:
                layer_name = 'XML_{}_{}_{}'.format(request.user,
                                                   request.user.id, id)
                srid = 4326
                # create layer on geoserver to request xml file
                create_layer(request, layer_name, store, workspace, id)
                # use GEOSERVER GML
                url = '{0}/{1}/ows?service=wfs&version=1.0.0&request=GetFeature&typeName={1}:{2}&outputFormat=' \
                      'text%2Fxml%3B%20subtype%3Dgml%2F2.1.2&&srsname=EPSG:{3}'.format(LOCAL_GEOSERVER,
                                                                                       workspace, layer_name, srid)
                request = urllib.request.Request(url)
                response = urllib.request.urlopen(request)
                # clean up right after request:
                delete_layer(layer_name, store, workspace)
            return HttpResponse(response.read().decode('utf-8'))