Browse Source

Change field type from FilePathField to FileField

pull/1370/head
Hironsan 3 years ago
parent
commit
51498453ee
2 changed files with 9 additions and 24 deletions
  1. 4
      backend/api/models.py
  2. 29
      backend/api/serializers.py

4
backend/api/models.py

@ -115,7 +115,7 @@ class Document(models.Model):
text = models.TextField()
project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
meta = models.JSONField(default=dict)
filename = models.FilePathField(default='')
filename = models.FileField(default='.')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
annotations_approved_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
@ -131,7 +131,7 @@ class Document(models.Model):
class Image(models.Model):
project = models.ForeignKey(Project, related_name='images', on_delete=models.CASCADE)
meta = models.JSONField(default=dict)
filename = models.FilePathField(default='')
filename = models.FileField(default='.')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
annotations_approved_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)

29
backend/api/serializers.py

@ -80,7 +80,7 @@ class TagSerializer(serializers.ModelSerializer):
read_only_fields = ('id', 'project')
class DocumentSerializer(serializers.ModelSerializer):
class BaseDataSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
annotation_approver = serializers.SerializerMethodField()
@ -100,35 +100,20 @@ class DocumentSerializer(serializers.ModelSerializer):
approver = instance.annotations_approved_by
return approver.username if approver else None
class Meta:
model = Document
fields = ('id', 'text', 'annotations', 'meta', 'annotation_approver', 'comment_count')
class DocumentSerializer(BaseDataSerializer):
class ImageSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
annotation_approver = serializers.SerializerMethodField()
filename = serializers.FilePathField(path='.')
class Meta:
model = Document
fields = ('id', 'filename', 'text', 'annotations', 'meta', 'annotation_approver', 'comment_count')
def get_annotations(self, instance):
request = self.context.get('request')
project = instance.project
model = project.get_annotation_class()
serializer = get_annotation_serializer(task=project.project_type)
annotations = model.objects.filter(document=instance.id)
if request and not project.collaborative_annotation:
annotations = annotations.filter(user=request.user)
serializer = serializer(annotations, many=True)
return serializer.data
@classmethod
def get_annotation_approver(cls, instance):
approver = instance.annotations_approved_by
return approver.username if approver else None
class ImageSerializer(BaseDataSerializer):
class Meta:
model = Image
fields = ('id', 'filename', 'annotations', 'meta', 'annotation_approver', 'comment_count')
read_only_fields = ['filename']
class ApproverSerializer(DocumentSerializer):

Loading…
Cancel
Save