mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
Merge pull request #2279 from paperless-ngx/fix/2278-include-task-id
Bugfix: Return created task ID when posting document to API
This commit is contained in:
commit
20e586fa60
@ -793,6 +793,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload(self, m):
|
def test_upload(self, m):
|
||||||
|
|
||||||
|
m.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -816,6 +818,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_empty_metadata(self, m):
|
def test_upload_empty_metadata(self, m):
|
||||||
|
|
||||||
|
m.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -839,6 +843,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_invalid_form(self, m):
|
def test_upload_invalid_form(self, m):
|
||||||
|
|
||||||
|
m.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -853,6 +859,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_invalid_file(self, m):
|
def test_upload_invalid_file(self, m):
|
||||||
|
|
||||||
|
m.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.zip"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.zip"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -866,6 +874,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_title(self, async_task):
|
def test_upload_with_title(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -884,6 +895,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_correspondent(self, async_task):
|
def test_upload_with_correspondent(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
c = Correspondent.objects.create(name="test-corres")
|
c = Correspondent.objects.create(name="test-corres")
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
@ -903,6 +917,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_invalid_correspondent(self, async_task):
|
def test_upload_with_invalid_correspondent(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -917,6 +934,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_document_type(self, async_task):
|
def test_upload_with_document_type(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
dt = DocumentType.objects.create(name="invoice")
|
dt = DocumentType.objects.create(name="invoice")
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
@ -936,6 +956,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_invalid_document_type(self, async_task):
|
def test_upload_with_invalid_document_type(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
with open(
|
with open(
|
||||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||||
"rb",
|
"rb",
|
||||||
@ -950,6 +973,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_tags(self, async_task):
|
def test_upload_with_tags(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
t1 = Tag.objects.create(name="tag1")
|
t1 = Tag.objects.create(name="tag1")
|
||||||
t2 = Tag.objects.create(name="tag2")
|
t2 = Tag.objects.create(name="tag2")
|
||||||
with open(
|
with open(
|
||||||
@ -970,6 +996,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_invalid_tags(self, async_task):
|
def test_upload_with_invalid_tags(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
t1 = Tag.objects.create(name="tag1")
|
t1 = Tag.objects.create(name="tag1")
|
||||||
t2 = Tag.objects.create(name="tag2")
|
t2 = Tag.objects.create(name="tag2")
|
||||||
with open(
|
with open(
|
||||||
@ -986,6 +1015,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
@mock.patch("documents.views.consume_file.delay")
|
@mock.patch("documents.views.consume_file.delay")
|
||||||
def test_upload_with_created(self, async_task):
|
def test_upload_with_created(self, async_task):
|
||||||
|
|
||||||
|
async_task.return_value = celery.result.AsyncResult(id=str(uuid.uuid4()))
|
||||||
|
|
||||||
created = datetime.datetime(
|
created = datetime.datetime(
|
||||||
2022,
|
2022,
|
||||||
5,
|
5,
|
||||||
@ -2948,6 +2980,59 @@ class TestTasks(APITestCase):
|
|||||||
self.assertEqual(returned_task2["status"], celery.states.PENDING)
|
self.assertEqual(returned_task2["status"], celery.states.PENDING)
|
||||||
self.assertEqual(returned_task2["task_file_name"], task2.task_file_name)
|
self.assertEqual(returned_task2["task_file_name"], task2.task_file_name)
|
||||||
|
|
||||||
|
def test_get_single_task_status(self):
|
||||||
|
"""
|
||||||
|
GIVEN
|
||||||
|
- Query parameter for a valid task ID
|
||||||
|
WHEN:
|
||||||
|
- API call is made to get task status
|
||||||
|
THEN:
|
||||||
|
- Single task data is returned
|
||||||
|
"""
|
||||||
|
|
||||||
|
id1 = str(uuid.uuid4())
|
||||||
|
task1 = PaperlessTask.objects.create(
|
||||||
|
task_id=id1,
|
||||||
|
task_file_name="task_one.pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
_ = PaperlessTask.objects.create(
|
||||||
|
task_id=str(uuid.uuid4()),
|
||||||
|
task_file_name="task_two.pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.get(self.ENDPOINT + f"?task_id={id1}")
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(len(response.data), 1)
|
||||||
|
returned_task1 = response.data[0]
|
||||||
|
|
||||||
|
self.assertEqual(returned_task1["task_id"], task1.task_id)
|
||||||
|
|
||||||
|
def test_get_single_task_status_not_valid(self):
|
||||||
|
"""
|
||||||
|
GIVEN
|
||||||
|
- Query parameter for a non-existent task ID
|
||||||
|
WHEN:
|
||||||
|
- API call is made to get task status
|
||||||
|
THEN:
|
||||||
|
- No task data is returned
|
||||||
|
"""
|
||||||
|
task1 = PaperlessTask.objects.create(
|
||||||
|
task_id=str(uuid.uuid4()),
|
||||||
|
task_file_name="task_one.pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
_ = PaperlessTask.objects.create(
|
||||||
|
task_id=str(uuid.uuid4()),
|
||||||
|
task_file_name="task_two.pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.get(self.ENDPOINT + "?task_id=bad-task-id")
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(len(response.data), 0)
|
||||||
|
|
||||||
def test_acknowledge_tasks(self):
|
def test_acknowledge_tasks(self):
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
|
@ -617,7 +617,7 @@ class PostDocumentView(GenericAPIView):
|
|||||||
|
|
||||||
task_id = str(uuid.uuid4())
|
task_id = str(uuid.uuid4())
|
||||||
|
|
||||||
consume_file.delay(
|
async_task = consume_file.delay(
|
||||||
temp_filename,
|
temp_filename,
|
||||||
override_filename=doc_name,
|
override_filename=doc_name,
|
||||||
override_title=title,
|
override_title=title,
|
||||||
@ -628,7 +628,7 @@ class PostDocumentView(GenericAPIView):
|
|||||||
override_created=created,
|
override_created=created,
|
||||||
)
|
)
|
||||||
|
|
||||||
return Response("OK")
|
return Response(async_task.id)
|
||||||
|
|
||||||
|
|
||||||
class SelectionDataView(GenericAPIView):
|
class SelectionDataView(GenericAPIView):
|
||||||
@ -886,13 +886,18 @@ class TasksViewSet(ReadOnlyModelViewSet):
|
|||||||
permission_classes = (IsAuthenticated,)
|
permission_classes = (IsAuthenticated,)
|
||||||
serializer_class = TasksViewSerializer
|
serializer_class = TasksViewSerializer
|
||||||
|
|
||||||
queryset = (
|
def get_queryset(self):
|
||||||
PaperlessTask.objects.filter(
|
queryset = (
|
||||||
acknowledged=False,
|
PaperlessTask.objects.filter(
|
||||||
|
acknowledged=False,
|
||||||
|
)
|
||||||
|
.order_by("date_created")
|
||||||
|
.reverse()
|
||||||
)
|
)
|
||||||
.order_by("date_created")
|
task_id = self.request.query_params.get("task_id")
|
||||||
.reverse()
|
if task_id is not None:
|
||||||
)
|
queryset = PaperlessTask.objects.filter(task_id=task_id)
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class AcknowledgeTasksView(GenericAPIView):
|
class AcknowledgeTasksView(GenericAPIView):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user