Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        file_obj = request.data['file']
        create_site = 'create_site' in request.data and to_bool(request.data['create_site'])
        delete_previous = 'delete_previous' in request.data and to_bool(request.data['delete_previous'])
        strict = 'strict' in request.data and to_bool(request.data['strict'])

        if file_obj.content_type not in FileReader.SUPPORTED_TYPES:
            msg = "Wrong file type {}. Should be one of: {}".format(file_obj.content_type, SiteUploader.SUPPORTED_TYPES)
            return Response(msg, status=status.HTTP_501_NOT_IMPLEMENTED)

        if delete_previous:
            self.dataset.record_queryset.delete()
        generator = FileReader(file_obj)
        validator = get_record_validator_for_dataset(self.dataset)
        validator.schema_error_as_warning = not strict
        creator = RecordCreator(self.dataset, generator,
                                validator=validator, create_site=create_site, commit=True,
                                species_facade_class=self.species_facade_class)
        data = []
        has_error = False
        row = 1  # starts at 1 to match excel row id
        for record, validator_result in creator:
            row += 1
            result = {
                'row': row
            }
            if validator_result.has_errors:
                has_error = True
            else:
                result['recordId'] = record.id
            result.update(validator_result.to_dict())
            data.append(result)
        status_code = status.HTTP_200_OK if not has_error else status.HTTP_400_BAD_REQUEST
        return Response(data, status=status_code)
Exemplo n.º 2
0
 def filter_queryset(self, queryset):
     query = Q()
     # search
     search = self.request.query_params.get('search')
     if search:
         query &= Q(species_name__icontains=search)
     # strict
     strict = to_bool(self.request.query_params.get('strict', False))
     if strict:
         query &= ~Q(name_id=-1)
     return queryset.filter(query)
Exemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     try:
         # we want the uploaded file to be physically saved on disk (no InMemoryFileUploaded) so we force the file
         # upload handlers of the request.
         request.upload_handlers = [TemporaryFileUploadHandler(request)]
         file_obj = request.data.get('file')
         infer_dataset_type = to_bool(request.data.get('infer_dataset_type', True))
         if file_obj is None:
             response_data = {
                 'errors': 'Missing file'
             }
             return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
         file_format = FileReader.get_uploaded_file_format(file_obj)
         if file_format == FileReader.NOT_SUPPORTED_FORMAT:
             msg = "Wrong file type {}. Should be one of: {}".format(file_obj.content_type,
                                                                     FileReader.SUPPORTED_TYPES)
             return Response(msg, status=status.HTTP_400_BAD_REQUEST)
         dataset_name = path.splitext(file_obj.name)[0]
         builder = DataPackageBuilder.infer_from_file(
             file_obj.temporary_file_path(),
             name=dataset_name,
             format_=file_format,
             infer_dataset_type=infer_dataset_type
         )
         if builder.valid:
             response_data = {
                 'name': builder.title,  # use the data-package title instead of name (name is a slug)
                 'type': builder.dataset_type,
                 'data_package': builder.descriptor
             }
             return Response(response_data, status=status.HTTP_200_OK)
         else:
             errors = [str(e) for e in builder.errors]
             response_data = {
                 'errors': errors
             }
             return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         response_data = {
             'errors': str(e)
         }
         return Response(response_data, status=status.HTTP_400_BAD_REQUEST)