File: utils.py

package info (click to toggle)
python-advanced-alchemy 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,904 kB
  • sloc: python: 36,227; makefile: 153; sh: 4
file content (42 lines) | stat: -rw-r--r-- 1,189 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
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
"""Flask-specific service classes.

This module provides Flask-specific service mixins and utilities for integrating
with the Advanced Alchemy service layer.
"""

from typing import Any

from flask import Response, current_app

from advanced_alchemy.extensions.flask.config import serializer


class FlaskServiceMixin:
    """Flask service mixin.

    This mixin provides Flask-specific functionality for services.
    """

    def jsonify(
        self,
        data: Any,
        *args: Any,
        status_code: int = 200,
        **kwargs: Any,
    ) -> Response:
        """Convert data to a Flask JSON response.

        Args:
            data: Data to serialize to JSON.
            *args: Additional positional arguments passed to Flask's response class.
            status_code: HTTP status code for the response. Defaults to 200.
            **kwargs: Additional keyword arguments passed to Flask's response class.

        Returns:
            :class:`flask.Response`: A Flask response with JSON content type.
        """
        return current_app.response_class(
            serializer(data),
            status=status_code,
            mimetype="application/json",
        )