• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / iwsims-demo / #111

06 May 2025 07:15AM UTC coverage: 86.248% (+0.02%) from 86.231%
#111

push

coveralls-python

web-flow
Merge pull request #32 from akvo/feature/31-eng-1328-new-question-type-attachment

Feature/31 eng 1328 new question type attachment

2615 of 3148 branches covered (83.07%)

Branch coverage included in aggregate %.

5952 of 6785 relevant lines covered (87.72%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
backend/api/v1/v1_files/serializers.py
1
from django.core.validators import FileExtensionValidator
1✔
2
from rest_framework import serializers
1✔
3
from utils.custom_serializer_fields import CustomFileField
1✔
4

5

6
class UploadImagesSerializer(serializers.Serializer):
1✔
7
    file = CustomFileField(
1✔
8
        validators=[FileExtensionValidator(["jpg", "png", "jpeg"])]
9
    )
10

11

12
class AttachmentsSerializer(serializers.Serializer):
1✔
13
    file = CustomFileField(
1✔
14
        allow_empty_file=False,
15
        required=True,
16
    )
17

18
    def validate_file(self, value):
1✔
19
        allowed_file_types = self.context.get("allowed_file_types", [])
1✔
20
        # Skip validation if no allowed file types are provided
21
        if not allowed_file_types:
1✔
22
            return value
1✔
23
        """
24
        Validate the file extension against the allowed file types
25
        provided in the context.
26
        If the file type is not allowed, raise a ValidationError.
27
        The allowed file types should be a list of strings.
28
        Example: ["pdf", "docx", "doc"]
29
        The file extension should be in lowercase.
30
        """
31
        validator = FileExtensionValidator(allowed_file_types)
1✔
32
        try:
1✔
33
            validator(value)
1✔
34
            return value
1✔
35
        except serializers.ValidationError as e:
1✔
36
            raise serializers.ValidationError(
×
37
                f"File type not allowed. {str(e)}"
38
            )
39

40
    class Meta:
1✔
41
        fields = ["file"]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc