From c954fe62f74cddb112f4ea1f1b9ab71c7e1982fe Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 4 Dec 2023 19:54:00 -0800 Subject: [PATCH] Use JSON field for document ids --- .../document-link.component.spec.ts | 15 ++++++++++++ .../document-link/document-link.component.ts | 23 +++++++++++-------- ...ntemplate_assign_custom_fields_and_more.py | 7 +----- src/documents/models.py | 7 +----- src/documents/serialisers.py | 7 +----- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts b/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts index c99777205..27b20b55e 100644 --- a/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts +++ b/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts @@ -79,6 +79,21 @@ describe('DocumentLinkComponent', () => { component.documentsInput$.next('foo') }) + it('should load values correctly', () => { + jest.spyOn(documentService, 'getCachedMany').mockImplementation((ids) => { + return of(documents.filter((d) => ids.includes(d.id))) + }) + component.writeValue([12, 23]) + expect(component.value).toEqual([12, 23]) + expect(component.selectedDocuments).toEqual([documents[1], documents[2]]) + component.writeValue(null) + expect(component.value).toEqual([]) + expect(component.selectedDocuments).toEqual([]) + component.writeValue([]) + expect(component.value).toEqual([]) + expect(component.selectedDocuments).toEqual([]) + }) + it('should support unselect', () => { const getSpy = jest.spyOn(documentService, 'getCachedMany') getSpy.mockImplementation((ids) => { diff --git a/src-ui/src/app/components/common/input/document-link/document-link.component.ts b/src-ui/src/app/components/common/input/document-link/document-link.component.ts index 23be11259..dd7118074 100644 --- a/src-ui/src/app/components/common/input/document-link/document-link.component.ts +++ b/src-ui/src/app/components/common/input/document-link/document-link.component.ts @@ -52,15 +52,20 @@ export class DocumentLinkComponent } writeValue(documentIDs: number[]): void { - this.loading = true - this.documentsService - .getCachedMany(documentIDs) - .pipe(takeUntil(this.unsubscribeNotifier)) - .subscribe((documents) => { - this.loading = false - this.selectedDocuments = documents - super.writeValue(documentIDs) - }) + if (!documentIDs || documentIDs.length === 0) { + this.selectedDocuments = [] + super.writeValue([]) + } else { + this.loading = true + this.documentsService + .getCachedMany(documentIDs) + .pipe(takeUntil(this.unsubscribeNotifier)) + .subscribe((documents) => { + this.loading = false + this.selectedDocuments = documents + super.writeValue(documentIDs) + }) + } } private loadDocs() { diff --git a/src/documents/migrations/1042_consumptiontemplate_assign_custom_fields_and_more.py b/src/documents/migrations/1042_consumptiontemplate_assign_custom_fields_and_more.py index 150b4c2af..ffd0dbefa 100644 --- a/src/documents/migrations/1042_consumptiontemplate_assign_custom_fields_and_more.py +++ b/src/documents/migrations/1042_consumptiontemplate_assign_custom_fields_and_more.py @@ -1,6 +1,5 @@ # Generated by Django 4.2.7 on 2023-12-04 04:03 -import django.core.validators from django.db import migrations from django.db import models @@ -24,11 +23,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name="customfieldinstance", name="value_document_ids", - field=models.CharField( - max_length=128, - null=True, - validators=[django.core.validators.int_list_validator], - ), + field=models.JSONField(null=True), ), migrations.AlterField( model_name="customfield", diff --git a/src/documents/models.py b/src/documents/models.py index a0da315a0..250a9d35b 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -15,7 +15,6 @@ from django.contrib.auth.models import Group from django.contrib.auth.models import User from django.core.validators import MaxValueValidator from django.core.validators import MinValueValidator -from django.core.validators import int_list_validator from django.db import models from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -836,11 +835,7 @@ class CustomFieldInstance(models.Model): value_monetary = models.DecimalField(null=True, decimal_places=2, max_digits=12) - value_document_ids = models.CharField( - validators=[int_list_validator], - max_length=128, - null=True, - ) + value_document_ids = models.JSONField(null=True) class Meta: ordering = ("created",) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index e0ecdd01a..f01d1fc3a 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1,5 +1,4 @@ import datetime -import json import math import re import zoneinfo @@ -460,11 +459,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer): return instance def get_value(self, obj: CustomFieldInstance): - return ( - obj.value - if (obj.field.data_type != CustomField.FieldDataType.DOCUMENTLINK) - else json.loads(obj.value) - ) + return obj.value def validate(self, data): """