Exemple #1
0
    def post(self, request):
        """
        The response gives a triple store format file
        ---
        omit_serializer: true
        response_serializer: TODO
        consumes: ["multipart/form-data"]
        parameters:
            - name: shp
              type: file
              required: true
              paramType: form
            - name: shx
              type: file
              required: true
              paramType: form
            - name: dbf
              type: file
              required: true
              paramType: form
            - name: prj
              type: file
              required: true
              paramType: form
        """
        shape_serializer = ShapeFileSerializer(data=request.data)
        if shape_serializer.is_valid(raise_exception=True):
            # create tmp dir
            tmp_dir = save_shape_in_tmp_dir(
                [request.data['shp'], request.data['shx'],
                 request.data['dbf'], request.data['prj']])
            request.data['input_file'] = tmp_dir + request.data['shp'].name
            out_file_path = tmp_dir + request.data['shp'].name[:-4]
            out_file_path += '.' + request.data['format_file'].lower()
            request.data['output_file'] = out_file_path

            triple_store_serializer = TripleStoreSerializer(data=request.data)
            if triple_store_serializer.is_valid(raise_exception=True):
                params = rename_params(request.data.dict())
                print post_node_server(data=params, token=None,
                                       url='/convertShape')

                """
                try:
                    f = open(out_file_path)
                    file_content = f.read()
                    f.close()
                    delete_dir(tmp_dir)
                except IOError:
                    return Response({'detail': 'Server error'},
                                        status=
                                        status.HTTP_500_INTERNAL_SERVER_ERROR)
                return file_content
                """

                # return Response({'triple-store_url': ''},
                #                 status=status.HTTP_200_OK)

            else:
                return Response(triple_store_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(shape_serializer.errors,
                            status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
Exemple #2
0
    def process(file_shp, params, store_in_semantic_db=False):

        # create auth token
        token = NodeToken.objects.create(user=self.request.user)

        if store_in_semantic_db:
            # params = request.data.dict()
            params['input_file'] = file_shp.shp.name

            pos = params['input_file'].rfind('/')
            params['feature_string'] = params['input_file'] \
                    [pos+1:-4]

            params['output_file'] = settings.UPLOAD_TRIPLE_STORE + \
                    '/' + params['feature_string'] + '.' + params['format_file']
            params['owner'] = self.request.user

            # create commit message for geogit
            # if params.has_key('commit_msg'):
            # if params['commit_msg'] != '':
            # commit_msg = params['commit_msg']
            # else:
            # commit_msg = 'shape file '+ \
            # params['feature_string']+' imported.'
            # params.pop('commit_msg')
            # else:
            # return Response({'commit_msg':
            #                 ["This query parameter is required."]},
            # status=status.HTTP_400_BAD_REQUEST)

            # TODO - vedi se possibile togliere triple_store
            triple_store = TripleStore.objects.create(**params)
            triple_store.shp.add(file_shp)

            params = rename_params(params)

            # call geogit and commit uploaded shp
            # geogit = Git(owner=self.request.user)
            # geogit.push(shp=file_shp.shp.name, commit_msg=commit_msg)

            result = post_node_server(data=params, token=token,
                                      url='/loadShape')
        else:
            result = post_node_server(data={'input_file': file_shp.shp.name},
                                      token=token, url='/loadShpInGeonode')

        return Response({'detail': result['details']},
                        status=result['status'])

        # if result == True:
        # return Response({'detail': result['details'],
        #                  status=result['status'])
        # else:
        # return Response({'detail': result}, status=status.HTTP_200_OK)


        if request.QUERY_PARAMS.has_key('type'):
            upload_type = request.QUERY_PARAMS['type'].lower()
            create_dir(settings.UPLOAD_SHAPE)
            shape_serializer = ShapeFileSerializer(data=request.data)

            if upload_type == 'base':
                if shape_serializer.is_valid(raise_exception=True):
                    file_shp = self.save_shp(request.data, self.request.user)
            # save_ckan_resource(file_shp.id, params)
                    return process(file_shp, params,
                                   store_in_semantic_db=False)
                elif u'zip' in request.data:
                    f = get_shp_from_zip(request.data['zip'])
                    if f:
                        file_shp = self.save_shp(f, self.request.user)
                        return process(file_shp, params,
                                       store_in_semantic_db=False)
                    else:
                        return Response(shape_serializer.errors,
                                        status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
                else:
                    return Response(shape_serializer.errors,
                                    status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)

            elif upload_type == 'all':
                if shape_serializer.is_valid(raise_exception=True):
                    triple_store_serializer = TripleStoreSerializer(
                        data=request.data)
                    if triple_store_serializer.is_valid(raise_exception=True):
                        file_shp = self.save_shp(request.data,
                                                 self.request.user)
                        save_ckan_resource(file_shp.id, params)
                        return process(file_shp, params,
                                       store_in_semantic_db=True)
                    else:
                        return Response(triple_store_serializer.errors,
                                        status=status.HTTP_400_BAD_REQUEST)
                elif u'zip' in request.data:
                    f = get_shp_from_zip(request.data['zip'])
                    if f:
                        file_shape = self.save_shp(f, self.request.user)
                        return process(file_shape, params,
                                       store_in_semantic_db=True)
                else:
                    return Response(shape_serializer.errors,
                                    status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
            else:
                return Response({'type': ['Select a valid choice!']},
                                status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response({'type': ["This query parameter is required."]},
                            status=status.HTTP_400_BAD_REQUEST)