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') 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', () => { it('should support unselect', () => {
const getSpy = jest.spyOn(documentService, 'getCachedMany') const getSpy = jest.spyOn(documentService, 'getCachedMany')
getSpy.mockImplementation((ids) => { getSpy.mockImplementation((ids) => {

View File

@ -52,15 +52,20 @@ export class DocumentLinkComponent
} }
writeValue(documentIDs: number[]): void { writeValue(documentIDs: number[]): void {
this.loading = true if (!documentIDs || documentIDs.length === 0) {
this.documentsService this.selectedDocuments = []
.getCachedMany(documentIDs) super.writeValue([])
.pipe(takeUntil(this.unsubscribeNotifier)) } else {
.subscribe((documents) => { this.loading = true
this.loading = false this.documentsService
this.selectedDocuments = documents .getCachedMany(documentIDs)
super.writeValue(documentIDs) .pipe(takeUntil(this.unsubscribeNotifier))
}) .subscribe((documents) => {
this.loading = false
this.selectedDocuments = documents
super.writeValue(documentIDs)
})
}
} }
private loadDocs() { private loadDocs() {

View File

@ -1,6 +1,5 @@
# Generated by Django 4.2.7 on 2023-12-04 04:03 # 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 migrations
from django.db import models from django.db import models
@ -24,11 +23,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name="customfieldinstance", model_name="customfieldinstance",
name="value_document_ids", name="value_document_ids",
field=models.CharField( field=models.JSONField(null=True),
max_length=128,
null=True,
validators=[django.core.validators.int_list_validator],
),
), ),
migrations.AlterField( migrations.AlterField(
model_name="customfield", 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.contrib.auth.models import User
from django.core.validators import MaxValueValidator from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.core.validators import int_list_validator
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ 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_monetary = models.DecimalField(null=True, decimal_places=2, max_digits=12)
value_document_ids = models.CharField( value_document_ids = models.JSONField(null=True)
validators=[int_list_validator],
max_length=128,
null=True,
)
class Meta: class Meta:
ordering = ("created",) ordering = ("created",)

View File

@ -1,5 +1,4 @@
import datetime import datetime
import json
import math import math
import re import re
import zoneinfo import zoneinfo
@ -460,11 +459,7 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
return instance return instance
def get_value(self, obj: CustomFieldInstance): def get_value(self, obj: CustomFieldInstance):
return ( return obj.value
obj.value
if (obj.field.data_type != CustomField.FieldDataType.DOCUMENTLINK)
else json.loads(obj.value)
)
def validate(self, data): def validate(self, data):
""" """