Make separate properties, assign_custom_fields_values

This commit is contained in:
shamoon
2025-03-01 11:20:49 -08:00
parent 6964b184d1
commit 9de605d2e6
16 changed files with 181 additions and 377 deletions

View File

@@ -114,11 +114,8 @@ class DocumentMetadataOverrides:
).values_list("id", flat=True),
)
overrides.custom_fields = {
custom_field.id: value
for custom_field, value in doc.custom_fields.all().values_list(
"id",
"value",
)
custom_field.id: custom_field.value
for custom_field in doc.custom_fields.all()
}
groups_with_perms = get_groups_with_perms(

View File

@@ -1,42 +0,0 @@
# Generated by Django 5.1.6 on 2025-03-01 04:49
from django.db import migrations
from django.db import models
import documents.models
def convert_assign_custom_fields(apps, schema_editor):
# Convert the old assign_custom_fields ManyToManyField to the new assign_custom_fields_w_values JSONField
WorkflowAction = apps.get_model("documents", "WorkflowAction")
for workflow_action in WorkflowAction.objects.all():
if workflow_action.assign_custom_fields.exists():
workflow_action.assign_custom_fields_w_values = {
custom_field.id: None
for custom_field in workflow_action.assign_custom_fields.all()
}
workflow_action.save()
class Migration(migrations.Migration):
dependencies = [
("documents", "1063_paperlesstask_type_alter_paperlesstask_task_name_and_more"),
]
operations = [
migrations.AddField(
model_name="workflowaction",
name="assign_custom_fields_w_values",
field=models.JSONField(
blank=True,
help_text="assign these custom fields, with optional values",
null=True,
verbose_name=documents.models.CustomField,
),
),
migrations.RunPython(convert_assign_custom_fields, migrations.RunPython.noop),
migrations.RemoveField(
model_name="workflowaction",
name="assign_custom_fields",
),
]

View File

@@ -0,0 +1,24 @@
# Generated by Django 5.1.6 on 2025-03-01 18:10
from django.db import migrations
from django.db import models
class Migration(migrations.Migration):
dependencies = [
("documents", "1063_paperlesstask_type_alter_paperlesstask_task_name_and_more"),
]
operations = [
migrations.AddField(
model_name="workflowaction",
name="assign_custom_fields_values",
field=models.JSONField(
blank=True,
help_text="Optional values to assign to the custom fields.",
null=True,
verbose_name="custom field values",
default={},
),
),
]

View File

@@ -1264,13 +1264,21 @@ class WorkflowAction(models.Model):
verbose_name=_("grant change permissions to these groups"),
)
assign_custom_fields_w_values = models.JSONField(
assign_custom_fields = models.ManyToManyField(
CustomField,
blank=True,
related_name="+",
verbose_name=_("assign these custom fields"),
)
assign_custom_fields_values = models.JSONField(
_("custom field values"),
null=True,
blank=True,
help_text=_(
"assign these custom fields, with optional values",
"Optional values to assign to the custom fields.",
),
default={},
)
remove_tags = models.ManyToManyField(

View File

@@ -2017,7 +2017,8 @@ class WorkflowActionSerializer(serializers.ModelSerializer):
"assign_view_groups",
"assign_change_users",
"assign_change_groups",
"assign_custom_fields_w_values",
"assign_custom_fields",
"assign_custom_fields_values",
"remove_all_tags",
"remove_tags",
"remove_all_correspondents",
@@ -2135,6 +2136,7 @@ class WorkflowSerializer(serializers.ModelSerializer):
assign_view_groups = action.pop("assign_view_groups", None)
assign_change_users = action.pop("assign_change_users", None)
assign_change_groups = action.pop("assign_change_groups", None)
assign_custom_fields = action.pop("assign_custom_fields", None)
remove_tags = action.pop("remove_tags", None)
remove_correspondents = action.pop("remove_correspondents", None)
remove_document_types = action.pop("remove_document_types", None)
@@ -2184,6 +2186,8 @@ class WorkflowSerializer(serializers.ModelSerializer):
action_instance.assign_change_users.set(assign_change_users)
if assign_change_groups is not None:
action_instance.assign_change_groups.set(assign_change_groups)
if assign_custom_fields is not None:
action_instance.assign_custom_fields.set(assign_custom_fields)
if remove_tags is not None:
action_instance.remove_tags.set(remove_tags)
if remove_correspondents is not None:

View File

@@ -769,29 +769,34 @@ def run_workflows(
),
)
if action.assign_custom_fields_w_values:
if action.assign_custom_fields.exists():
if not use_overrides:
for field_id in action.assign_custom_fields_w_values:
for field in action.assign_custom_fields.all():
if not CustomFieldInstance.objects.filter(
field_id=field_id,
field=field,
document=document,
).exists():
# can be triggered on existing docs, so only add the field if it doesn't already exist
field = CustomField.objects.get(pk=field_id)
value_field_name = CustomFieldInstance.get_value_field_name(
data_type=field.data_type,
)
args = {
"field": field,
"document": document,
value_field_name: action.assign_custom_fields_w_values[
field_id
],
value_field_name: action.assign_custom_fields_values.get(
field.pk,
None,
),
}
CustomFieldInstance.objects.create(**args)
else:
if overrides.custom_fields is None:
overrides.custom_fields = {}
overrides.custom_fields.update(
action.assign_custom_fields_w_values,
{
field.pk: action.assign_custom_fields_values.get(field.pk, None)
for field in action.assign_custom_fields.all()
},
)
def removal_action():

View File

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