backend.components.camera_verification.verificationController

  1from flask import Blueprint, request, jsonify
  2
  3from backend.components.workers.workerService import get_worker_from_qr_code
  4from backend.components.camera_verification.faceid.faceidService import verify_worker_face
  5from backend.components.camera_verification.error_handling.errorService import verification_response_handler
  6from backend.components.utils.imageUtils import parse_image
  7from backend.components.reports.reportService import log_worker_entry
  8
  9
 10bp = Blueprint('bp_verification', __name__)
 11
 12@bp.route('/api/skan', methods=['POST'])
 13def post_camera_scan():
 14    """
 15    Verifies a worker by scanning a QR code and matching their face.
 16
 17    **Request Body**:
 18    - `file` (FileStorage): The image file containing the QR code and the worker's face.
 19
 20    **Process**:
 21    - Extracts and decodes the QR code from the image.
 22    - Retrieves the worker associated with the QR code.
 23    - Verifies the worker's face matches the one stored in the database.
 24
 25    **Returns**:
 26    - `tuple`: A tuple containing the verification result and the HTTP status code.
 27      - On success:
 28        - HTTP 200: Verification successful.
 29      - On failure:
 30        - HTTP 400: Malformed request (e.g., missing file or invalid QR code).
 31        - HTTP 403: Permission denied (e.g., expired QR code or face mismatch).
 32        - HTTP 500: Internal server error.
 33
 34    **Example Response**:
 35    ```json
 36    {
 37        "code": 0,
 38        "message": "Weryfikacja udana.",
 39        "logged": true
 40    }
 41    ```
 42
 43    ---
 44    tags:
 45      - Verification
 46    consumes:
 47      - multipart/form-data
 48    parameters:
 49      - name: file
 50        in: formData
 51        type: file
 52        required: true
 53        description: The image file containing the QR code and the worker's face.
 54    responses:
 55      200:
 56        description: Verification successful.
 57        schema:
 58          type: object
 59          properties:
 60            code:
 61              type: integer
 62              description: Response code (0 = success).
 63              example: 0
 64            message:
 65              type: string
 66              description: Human-readable result message.
 67              example: "Weryfikacja udana."
 68            logged:
 69              type: boolean
 70              description: Indicates if the entry was logged in the database.
 71              example: true
 72      400:
 73        description: Malformed request (e.g., missing file).
 74        schema:
 75          type: object
 76          properties:
 77            error:
 78              type: string
 79              example: 'Brak pliku w żądaniu (oczekiwano klucza "file").'
 80      403:
 81        description: Permission denied (e.g., face mismatch, expired QR).
 82        schema:
 83          type: object
 84          properties:
 85            code:
 86              type: integer
 87              description: Error code indicating the specific failure reason.
 88              example: 12
 89            message:
 90              type: string
 91              example: "Twarz nie pasuje do wzorca."
 92            logged:
 93              type: boolean
 94              example: true
 95      500:
 96        description: Internal server error.
 97    """
 98    if 'file' not in request.files:
 99        return jsonify({'error': 'Brak pliku w żądaniu (oczekiwano klucza "file").'}), 400
100
101    # Load and parse image
102    img_bytes = request.files['file'].read()
103    img_parsed = parse_image(img_bytes)
104
105    http_code, response, worker = None, None, None
106    try:
107        worker = get_worker_from_qr_code(img_parsed)
108        verify_worker_face(worker, img_parsed)
109        http_code = 200
110        response = verification_response_handler()
111
112    except Exception as e:
113        response = verification_response_handler(e)
114        if response.code == -1:
115            http_code = 500  # Unknown internal server error
116        elif response.code < 10:
117            http_code = 400  # Malformed request
118        elif response.code % 10 == 0:
119            http_code = 500  # Known internal server error
120        else:
121            http_code = 403  # Permission denied
122
123    finally:
124        if response.logged:
125            log_worker_entry(response.code, response.message, worker, img_bytes)
126        return jsonify(response.asdict()), http_code
bp = <Blueprint 'bp_verification'>
@bp.route('/api/skan', methods=['POST'])
def post_camera_scan():
 13@bp.route('/api/skan', methods=['POST'])
 14def post_camera_scan():
 15    """
 16    Verifies a worker by scanning a QR code and matching their face.
 17
 18    **Request Body**:
 19    - `file` (FileStorage): The image file containing the QR code and the worker's face.
 20
 21    **Process**:
 22    - Extracts and decodes the QR code from the image.
 23    - Retrieves the worker associated with the QR code.
 24    - Verifies the worker's face matches the one stored in the database.
 25
 26    **Returns**:
 27    - `tuple`: A tuple containing the verification result and the HTTP status code.
 28      - On success:
 29        - HTTP 200: Verification successful.
 30      - On failure:
 31        - HTTP 400: Malformed request (e.g., missing file or invalid QR code).
 32        - HTTP 403: Permission denied (e.g., expired QR code or face mismatch).
 33        - HTTP 500: Internal server error.
 34
 35    **Example Response**:
 36    ```json
 37    {
 38        "code": 0,
 39        "message": "Weryfikacja udana.",
 40        "logged": true
 41    }
 42    ```
 43
 44    ---
 45    tags:
 46      - Verification
 47    consumes:
 48      - multipart/form-data
 49    parameters:
 50      - name: file
 51        in: formData
 52        type: file
 53        required: true
 54        description: The image file containing the QR code and the worker's face.
 55    responses:
 56      200:
 57        description: Verification successful.
 58        schema:
 59          type: object
 60          properties:
 61            code:
 62              type: integer
 63              description: Response code (0 = success).
 64              example: 0
 65            message:
 66              type: string
 67              description: Human-readable result message.
 68              example: "Weryfikacja udana."
 69            logged:
 70              type: boolean
 71              description: Indicates if the entry was logged in the database.
 72              example: true
 73      400:
 74        description: Malformed request (e.g., missing file).
 75        schema:
 76          type: object
 77          properties:
 78            error:
 79              type: string
 80              example: 'Brak pliku w żądaniu (oczekiwano klucza "file").'
 81      403:
 82        description: Permission denied (e.g., face mismatch, expired QR).
 83        schema:
 84          type: object
 85          properties:
 86            code:
 87              type: integer
 88              description: Error code indicating the specific failure reason.
 89              example: 12
 90            message:
 91              type: string
 92              example: "Twarz nie pasuje do wzorca."
 93            logged:
 94              type: boolean
 95              example: true
 96      500:
 97        description: Internal server error.
 98    """
 99    if 'file' not in request.files:
100        return jsonify({'error': 'Brak pliku w żądaniu (oczekiwano klucza "file").'}), 400
101
102    # Load and parse image
103    img_bytes = request.files['file'].read()
104    img_parsed = parse_image(img_bytes)
105
106    http_code, response, worker = None, None, None
107    try:
108        worker = get_worker_from_qr_code(img_parsed)
109        verify_worker_face(worker, img_parsed)
110        http_code = 200
111        response = verification_response_handler()
112
113    except Exception as e:
114        response = verification_response_handler(e)
115        if response.code == -1:
116            http_code = 500  # Unknown internal server error
117        elif response.code < 10:
118            http_code = 400  # Malformed request
119        elif response.code % 10 == 0:
120            http_code = 500  # Known internal server error
121        else:
122            http_code = 403  # Permission denied
123
124    finally:
125        if response.logged:
126            log_worker_entry(response.code, response.message, worker, img_bytes)
127        return jsonify(response.asdict()), http_code

Verifies a worker by scanning a QR code and matching their face.

Request Body:

  • file (FileStorage): The image file containing the QR code and the worker's face.

Process:

  • Extracts and decodes the QR code from the image.
  • Retrieves the worker associated with the QR code.
  • Verifies the worker's face matches the one stored in the database.

Returns:

  • tuple: A tuple containing the verification result and the HTTP status code.
    • On success:
      • HTTP 200: Verification successful.
    • On failure:
      • HTTP 400: Malformed request (e.g., missing file or invalid QR code).
      • HTTP 403: Permission denied (e.g., expired QR code or face mismatch).
      • HTTP 500: Internal server error.

Example Response:

{
    "code": 0,
    "message": "Weryfikacja udana.",
    "logged": true
}

tags:

  • Verification consumes:
  • multipart/form-data parameters:
  • name: file in: formData type: file required: true description: The image file containing the QR code and the worker's face. responses: 200: description: Verification successful. schema: type: object properties: code: type: integer description: Response code (0 = success). example: 0 message: type: string description: Human-readable result message. example: "Weryfikacja udana." logged: type: boolean description: Indicates if the entry was logged in the database. example: true 400: description: Malformed request (e.g., missing file). schema: type: object properties: error: type: string example: 'Brak pliku w żądaniu (oczekiwano klucza "file").' 403: description: Permission denied (e.g., face mismatch, expired QR). schema: type: object properties: code: type: integer description: Error code indicating the specific failure reason. example: 12 message: type: string example: "Twarz nie pasuje do wzorca." logged: type: boolean example: true 500: description: Internal server error.