Skip to content

serializers

A module that contains serializers of the files package.

Classes
  • FileSerializer: A class that serializer the File model.

FileSerializer

Bases: ModelSerializer

Serializer for the File model.

Attributes:

Name Type Description
html_file SerializerMethodField

The html file path

pdf_file SerializerMethodField

The pdf file path

Methods
  • get_html_file(file): A method to get the html file url
  • get_pdf_file(file): A method to get the pdf file url
Source code in backend/api/files/serializers.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class FileSerializer(ModelSerializer):
    """Serializer for the File model.

    Attributes:
        html_file: The html file path
        pdf_file: The pdf file path

    Methods:
        - `get_html_file(file)`: A method to get the html file url
        - `get_pdf_file(file)`: A method to get the pdf file url
    """

    html_file: SerializerMethodField = SerializerMethodField()
    pdf_file: SerializerMethodField = SerializerMethodField()

    class Meta:
        """Metaclass for FileSerializer.

        Attributes:
            model: The File model
            fields: A list of File fields that need to be serialized
        """

        model = File
        fields = [
            "file_id",
            "html_file",
            "pdf_file",
            "uploaded_at",
            "converted_at",
            "created_at",
            "updated_at"
        ]

    def get_html_file(self, file: File) -> str:
        """Get the URL for a File object's html_file field as a hyperlink.

        Args:
            file: A File object.

        Returns:
            The URL for the File object's html_file field as a hyperlink.
        """
        request = self.context.get('request')
        html_file = file.html_file.name.replace('backend/', '')
        return request.build_absolute_uri(html_file)

    def get_pdf_file(self, file: File) -> str:
        """Get the URL for a File object's pdf_file field as a hyperlink.

        Args:
            file: A File object.

        Returns:
            The URL for the File object's pdf_file field as a hyperlink.
        """
        request = self.context.get('request')
        pdf_file = file.pdf_file.name.replace('backend/', '')
        return request.build_absolute_uri(pdf_file)

Meta

Metaclass for FileSerializer.

Attributes:

Name Type Description
model

The File model

fields

A list of File fields that need to be serialized

Source code in backend/api/files/serializers.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Meta:
    """Metaclass for FileSerializer.

    Attributes:
        model: The File model
        fields: A list of File fields that need to be serialized
    """

    model = File
    fields = [
        "file_id",
        "html_file",
        "pdf_file",
        "uploaded_at",
        "converted_at",
        "created_at",
        "updated_at"
    ]

get_html_file(file)

Get the URL for a File object's html_file field as a hyperlink.

Parameters:

Name Type Description Default
file File

A File object.

required

Returns:

Type Description
str

The URL for the File object's html_file field as a hyperlink.

Source code in backend/api/files/serializers.py
47
48
49
50
51
52
53
54
55
56
57
58
def get_html_file(self, file: File) -> str:
    """Get the URL for a File object's html_file field as a hyperlink.

    Args:
        file: A File object.

    Returns:
        The URL for the File object's html_file field as a hyperlink.
    """
    request = self.context.get('request')
    html_file = file.html_file.name.replace('backend/', '')
    return request.build_absolute_uri(html_file)

get_pdf_file(file)

Get the URL for a File object's pdf_file field as a hyperlink.

Parameters:

Name Type Description Default
file File

A File object.

required

Returns:

Type Description
str

The URL for the File object's pdf_file field as a hyperlink.

Source code in backend/api/files/serializers.py
60
61
62
63
64
65
66
67
68
69
70
71
def get_pdf_file(self, file: File) -> str:
    """Get the URL for a File object's pdf_file field as a hyperlink.

    Args:
        file: A File object.

    Returns:
        The URL for the File object's pdf_file field as a hyperlink.
    """
    request = self.context.get('request')
    pdf_file = file.pdf_file.name.replace('backend/', '')
    return request.build_absolute_uri(pdf_file)