Start working on updating workflows usage

[ci skip]
This commit is contained in:
shamoon 2025-02-28 23:39:42 -08:00
parent 0e76b86066
commit 6964b184d1
6 changed files with 53 additions and 35 deletions

View File

@ -806,13 +806,18 @@ class ConsumerPlugin(
} }
set_permissions_for_object(permissions=permissions, object=document) set_permissions_for_object(permissions=permissions, object=document)
if self.metadata.custom_field_ids: if self.metadata.custom_fields:
for field_id in self.metadata.custom_field_ids: for field_id in self.metadata.custom_fields:
field = CustomField.objects.get(pk=field_id) field = CustomField.objects.get(pk=field_id)
CustomFieldInstance.objects.create( value_field_name = CustomFieldInstance.get_value_field_name(
field=field, data_type=field.data_type,
document=document, )
) # adds to document args = {
"field": field,
"document": document,
value_field_name: self.metadata.custom_fields[field_id],
}
CustomFieldInstance.objects.create(**args) # adds to document
def _write(self, storage_type, source, target): def _write(self, storage_type, source, target):
with ( with (

View File

@ -29,7 +29,7 @@ class DocumentMetadataOverrides:
view_groups: list[int] | None = None view_groups: list[int] | None = None
change_users: list[int] | None = None change_users: list[int] | None = None
change_groups: list[int] | None = None change_groups: list[int] | None = None
custom_field_ids: list[int] | None = None custom_fields: dict | None = None
def update(self, other: "DocumentMetadataOverrides") -> "DocumentMetadataOverrides": def update(self, other: "DocumentMetadataOverrides") -> "DocumentMetadataOverrides":
""" """
@ -81,11 +81,10 @@ class DocumentMetadataOverrides:
self.change_groups.extend(other.change_groups) self.change_groups.extend(other.change_groups)
self.change_groups = list(set(self.change_groups)) self.change_groups = list(set(self.change_groups))
if self.custom_field_ids is None: if self.custom_fields is None:
self.custom_field_ids = other.custom_field_ids self.custom_fields = other.custom_fields
elif other.custom_field_ids is not None: elif other.custom_fields is not None:
self.custom_field_ids.extend(other.custom_field_ids) self.custom_fields.update(other.custom_fields)
self.custom_field_ids = list(set(self.custom_field_ids))
return self return self
@ -114,9 +113,13 @@ class DocumentMetadataOverrides:
only_with_perms_in=["change_document"], only_with_perms_in=["change_document"],
).values_list("id", flat=True), ).values_list("id", flat=True),
) )
overrides.custom_field_ids = list( overrides.custom_fields = {
doc.custom_fields.values_list("field", flat=True), custom_field.id: value
) for custom_field, value in doc.custom_fields.all().values_list(
"id",
"value",
)
}
groups_with_perms = get_groups_with_perms( groups_with_perms = get_groups_with_perms(
doc, doc,

View File

@ -769,26 +769,29 @@ def run_workflows(
), ),
) )
if action.assign_custom_fields.exists(): if action.assign_custom_fields_w_values:
if not use_overrides: if not use_overrides:
for field in action.assign_custom_fields.all(): for field_id in action.assign_custom_fields_w_values:
if not CustomFieldInstance.objects.filter( if not CustomFieldInstance.objects.filter(
field=field, field_id=field_id,
document=document, document=document,
).exists(): ).exists():
# can be triggered on existing docs, so only add the field if it doesn't already exist # can be triggered on existing docs, so only add the field if it doesn't already exist
CustomFieldInstance.objects.create( field = CustomField.objects.get(pk=field_id)
field=field, value_field_name = CustomFieldInstance.get_value_field_name(
document=document, data_type=field.data_type,
) )
args = {
"field": field,
"document": document,
value_field_name: action.assign_custom_fields_w_values[
field_id
],
}
CustomFieldInstance.objects.create(**args)
else: else:
overrides.custom_field_ids = list( overrides.custom_fields.update(
set( action.assign_custom_fields_w_values,
(overrides.custom_field_ids or [])
+ list(
action.assign_custom_fields.values_list("pk", flat=True),
),
),
) )
def removal_action(): def removal_action():
@ -946,18 +949,18 @@ def run_workflows(
if not use_overrides: if not use_overrides:
CustomFieldInstance.objects.filter(document=document).delete() CustomFieldInstance.objects.filter(document=document).delete()
else: else:
overrides.custom_field_ids = None overrides.custom_fields = None
elif action.remove_custom_fields.exists(): elif action.remove_custom_fields.exists():
if not use_overrides: if not use_overrides:
CustomFieldInstance.objects.filter( CustomFieldInstance.objects.filter(
field__in=action.remove_custom_fields.all(), field__in=action.remove_custom_fields.all(),
document=document, document=document,
).delete() ).delete()
elif overrides.custom_field_ids: elif overrides.custom_fields:
for field in action.remove_custom_fields.filter( for field in action.remove_custom_fields.filter(
pk__in=overrides.custom_field_ids, pk__in=overrides.custom_fields.keys(),
): ):
overrides.custom_field_ids.remove(field.pk) overrides.custom_fields.pop(field.pk, None)
def email_action(): def email_action():
if not settings.EMAIL_ENABLED: if not settings.EMAIL_ENABLED:

View File

@ -1362,7 +1362,7 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
self.assertEqual(input_doc.original_file.name, "simple.pdf") self.assertEqual(input_doc.original_file.name, "simple.pdf")
self.assertEqual(overrides.filename, "simple.pdf") self.assertEqual(overrides.filename, "simple.pdf")
self.assertEqual(overrides.custom_field_ids, [custom_field.id]) self.assertEqual(overrides.custom_fields, {custom_field.id: None})
def test_upload_with_webui_source(self): def test_upload_with_webui_source(self):
""" """

View File

@ -408,7 +408,9 @@ class TestConsumer(
with self.get_consumer( with self.get_consumer(
self.get_test_file(), self.get_test_file(),
DocumentMetadataOverrides(custom_field_ids=[cf1.id, cf3.id]), DocumentMetadataOverrides(
custom_fields={cf1.id: "value1", cf3.id: "http://example.com"},
),
) as consumer: ) as consumer:
consumer.run() consumer.run()
@ -420,6 +422,11 @@ class TestConsumer(
self.assertIn(cf1, fields_used) self.assertIn(cf1, fields_used)
self.assertNotIn(cf2, fields_used) self.assertNotIn(cf2, fields_used)
self.assertIn(cf3, fields_used) self.assertIn(cf3, fields_used)
self.assertEqual(document.custom_fields.get(field=cf1).value, "value1")
self.assertEqual(
document.custom_fields.get(field=cf3).value,
"http://example.com",
)
self._assert_first_last_send_progress() self._assert_first_last_send_progress()
def testOverrideAsn(self): def testOverrideAsn(self):

View File

@ -1471,7 +1471,7 @@ class PostDocumentView(GenericAPIView):
created=created, created=created,
asn=archive_serial_number, asn=archive_serial_number,
owner_id=request.user.id, owner_id=request.user.id,
custom_field_ids=custom_field_ids, custom_fields={cf_id: None for cf_id in custom_field_ids}, # for now
) )
async_task = consume_file.delay( async_task = consume_file.delay(