Enhancement: implement document link custom field (#4799)

This commit is contained in:
shamoon
2023-12-05 08:16:56 -08:00
committed by GitHub
parent 66efaedcbb
commit 4481f12e32
16 changed files with 542 additions and 136 deletions

View File

@@ -1,23 +0,0 @@
# Generated by Django 4.2.7 on 2023-11-30 17:44
from django.db import migrations
from django.db import models
class Migration(migrations.Migration):
dependencies = [
("documents", "1041_alter_consumptiontemplate_sources"),
]
operations = [
migrations.AddField(
model_name="consumptiontemplate",
name="assign_custom_fields",
field=models.ManyToManyField(
blank=True,
related_name="+",
to="documents.customfield",
verbose_name="assign these custom fields",
),
),
]

View File

@@ -0,0 +1,47 @@
# Generated by Django 4.2.7 on 2023-12-04 04:03
from django.db import migrations
from django.db import models
class Migration(migrations.Migration):
dependencies = [
("documents", "1041_alter_consumptiontemplate_sources"),
]
operations = [
migrations.AddField(
model_name="consumptiontemplate",
name="assign_custom_fields",
field=models.ManyToManyField(
blank=True,
related_name="+",
to="documents.customfield",
verbose_name="assign these custom fields",
),
),
migrations.AddField(
model_name="customfieldinstance",
name="value_document_ids",
field=models.JSONField(null=True),
),
migrations.AlterField(
model_name="customfield",
name="data_type",
field=models.CharField(
choices=[
("string", "String"),
("url", "URL"),
("date", "Date"),
("boolean", "Boolean"),
("integer", "Integer"),
("float", "Float"),
("monetary", "Monetary"),
("documentlink", "Document Link"),
],
editable=False,
max_length=50,
verbose_name="data type",
),
),
]

View File

@@ -756,6 +756,7 @@ class CustomField(models.Model):
INT = ("integer", _("Integer"))
FLOAT = ("float", _("Float"))
MONETARY = ("monetary", _("Monetary"))
DOCUMENTLINK = ("documentlink", _("Document Link"))
created = models.DateTimeField(
_("created"),
@@ -834,6 +835,8 @@ class CustomFieldInstance(models.Model):
value_monetary = models.DecimalField(null=True, decimal_places=2, max_digits=12)
value_document_ids = models.JSONField(null=True)
class Meta:
ordering = ("created",)
verbose_name = _("custom field instance")
@@ -868,6 +871,8 @@ class CustomFieldInstance(models.Model):
return self.value_float
elif self.field.data_type == CustomField.FieldDataType.MONETARY:
return self.value_monetary
elif self.field.data_type == CustomField.FieldDataType.DOCUMENTLINK:
return self.value_document_ids
raise NotImplementedError(self.field.data_type)

View File

@@ -440,6 +440,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
CustomField.FieldDataType.INT: "value_int",
CustomField.FieldDataType.FLOAT: "value_float",
CustomField.FieldDataType.MONETARY: "value_monetary",
CustomField.FieldDataType.DOCUMENTLINK: "value_document_ids",
}
# An instance is attached to a document
document: Document = validated_data["document"]

View File

@@ -34,7 +34,9 @@ class TestCustomField(DirectoriesMixin, APITestCase):
("date", "Invoiced Date"),
("integer", "Invoice #"),
("boolean", "Is Active"),
("float", "Total Paid"),
("float", "Average Value"),
("monetary", "Total Paid"),
("documentlink", "Related Documents"),
]:
resp = self.client.post(
self.ENDPOINT,
@@ -96,6 +98,10 @@ class TestCustomField(DirectoriesMixin, APITestCase):
name="Test Custom Field Monetary",
data_type=CustomField.FieldDataType.MONETARY,
)
custom_field_documentlink = CustomField.objects.create(
name="Test Custom Field Doc Link",
data_type=CustomField.FieldDataType.DOCUMENTLINK,
)
date_value = date.today()
@@ -131,6 +137,10 @@ class TestCustomField(DirectoriesMixin, APITestCase):
"field": custom_field_monetary.id,
"value": 11.10,
},
{
"field": custom_field_documentlink.id,
"value": [1, 2, 3],
},
],
},
format="json",
@@ -150,11 +160,12 @@ class TestCustomField(DirectoriesMixin, APITestCase):
{"field": custom_field_url.id, "value": "https://example.com"},
{"field": custom_field_float.id, "value": 12.3456},
{"field": custom_field_monetary.id, "value": 11.10},
{"field": custom_field_documentlink.id, "value": [1, 2, 3]},
],
)
doc.refresh_from_db()
self.assertEqual(len(doc.custom_fields.all()), 7)
self.assertEqual(len(doc.custom_fields.all()), 8)
def test_change_custom_field_instance_value(self):
"""