Skip to content

views

A module that contains the converter views.

Classes
  • HtmlConvertAPI: A class that hold the endpoint of converting html files.

HtmlConvertAPI

Bases: views.APIView

API View for converting an HTML file to PDF asynchronously.

This class handles POST requests to convert an HTML file to a PDF file using Celery worker to process the conversion task asynchronously.

Attributes:

Name Type Description
`authentication_classes`

List of authentication classes. Default is JWTAuthentication.

Methods
  • post(request): Handles POST requests and performs the HTML to PDF conversion.
Source code in backend/api/converter/views.py
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
class HtmlConvertAPI(views.APIView):
    """API View for converting an HTML file to PDF asynchronously.

    This class handles POST requests to convert an HTML file to a PDF file
    using Celery worker to process the conversion task asynchronously.

    Attributes:
        `authentication_classes`: List of authentication classes. Default is JWTAuthentication.

    Methods:
        - `post(request)`: Handles POST requests and performs the HTML to PDF conversion.
    """

    authentication_classes = [JWTAuthentication]

    def post(self, request: Request) -> Response:
        """Perform HTML to PDF conversion and return a response.

        Args:
            request: HTTP request containing the HTML file to convert.

        Returns:
            Response with status code and message.
        """
        if not request.user:
            return Response({
                "status": status.HTTP_401_UNAUTHORIZED,
                "message": "Access denied!",
            })

        if 'html_file' not in request.data:
            return Response({
                "status": status.HTTP_400_BAD_REQUEST,
                "message": "You should provide an html file to convert!",
            })

        html_file = request.data.get('html_file')
        file = File.objects.create(
            html_file=html_file,
            uploaded_at=datetime.now(),
            user=request.user
        )

        convert_html_file.delay(file.file_id)

        return Response({
            "status": status.HTTP_200_OK,
            "message": "We are converting your file! Wait a moment!",
        })

post(request)

Perform HTML to PDF conversion and return a response.

Parameters:

Name Type Description Default
request Request

HTTP request containing the HTML file to convert.

required

Returns:

Type Description
Response

Response with status code and message.

Source code in backend/api/converter/views.py
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
def post(self, request: Request) -> Response:
    """Perform HTML to PDF conversion and return a response.

    Args:
        request: HTTP request containing the HTML file to convert.

    Returns:
        Response with status code and message.
    """
    if not request.user:
        return Response({
            "status": status.HTTP_401_UNAUTHORIZED,
            "message": "Access denied!",
        })

    if 'html_file' not in request.data:
        return Response({
            "status": status.HTTP_400_BAD_REQUEST,
            "message": "You should provide an html file to convert!",
        })

    html_file = request.data.get('html_file')
    file = File.objects.create(
        html_file=html_file,
        uploaded_at=datetime.now(),
        user=request.user
    )

    convert_html_file.delay(file.file_id)

    return Response({
        "status": status.HTTP_200_OK,
        "message": "We are converting your file! Wait a moment!",
    })