Use JSON field for document ids

This commit is contained in:
shamoon 2023-12-04 19:54:00 -08:00
parent 1616f83bc7
commit c954fe62f7
5 changed files with 32 additions and 27 deletions

View File

@ -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) => {

View File

@ -52,6 +52,10 @@ export class DocumentLinkComponent
}
writeValue(documentIDs: number[]): void {
if (!documentIDs || documentIDs.length === 0) {
this.selectedDocuments = []
super.writeValue([])
} else {
this.loading = true
this.documentsService
.getCachedMany(documentIDs)
@ -62,6 +66,7 @@ export class DocumentLinkComponent
super.writeValue(documentIDs)
})
}
}
private loadDocs() {
this.foundDocuments$ = concat(

View File

@ -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",

View File

@ -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",)

View File

@ -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):
"""