backend.components.reports.reportService
1from backend.app import db 2from backend.database.models import Entry, Worker 3from sqlalchemy import or_ 4from datetime import datetime, time 5from typing import List, Tuple, Optional 6 7def get_report_data(date_from: datetime = None, 8 date_to: datetime = None, 9 worker_id: int = None, 10 show_valid: bool = False, 11 show_invalid: bool = False) -> List[Tuple[Entry, Optional[Worker]]]: 12 ''' 13 Retrieves entry report data by joining Entry records with Worker data. 14 15 This function handles filtering by date range, specific worker, and entry validity status. 16 Results are sorted in descending order by date. 17 18 **Parameters**: 19 - `date_from` (datetime): Start date (inclusive). Defaults to None. 20 - `date_to` (datetime): End date (inclusive). Defaults to None. 21 - `worker_id` (int): Worker ID to filter by. Defaults to None. 22 - `show_valid` (bool): Whether to include valid entries (code 0). If selected together with show_invalid (or both are unselected), returns all types. 23 - `show_invalid` (bool): Whether to include invalid entries (code != 0). If selected together with show_valid (or both are unselected), returns all types. 24 25 **Returns**: 26 - `List[Tuple[Entry, Optional[Worker]]]` - A list of tuples where the first element is the entry object (Entry) and the second is the worker object (Worker) or None if the entry has no assigned worker. 27 ''' 28 query = db.session.query(Entry, Worker).outerjoin(Worker, Entry.worker_id == Worker.id) 29 30 if date_from: 31 query = query.filter(Entry.date >= date_from) 32 33 if date_to: 34 query = query.filter(Entry.date <= date_to) 35 36 if worker_id: 37 query = query.filter(Entry.worker_id == worker_id) 38 39 # Logika filtrów: 40 # 1. Oba zaznaczone -> Pokaż wszystko (pass) 41 # 2. Tylko valid -> kod == 0 42 # 3. Tylko invalid -> kod != 0 43 # 4. Żaden niezaznaczony -> Pokaż wszystko (domyślne zachowanie query bez filtrów) 44 if show_valid and show_invalid: 45 pass 46 elif show_valid: 47 query = query.filter(Entry.code == 0) 48 elif show_invalid: 49 query = query.filter(Entry.code != 0) 50 51 query = query.order_by(Entry.date.desc()) 52 53 return query.all() 54 55 56def log_worker_entry(code: int, message: str, worker: Worker|None = None, image=None) -> Entry: 57 ''' 58 Log a worker entry. 59 60 **Parameters**: 61 - `code` (int): Numeric verification response code for the entry. 62 - `message` (str): Human-readable verification response message. 63 - `worker` (Worker|None): Worker, if a worker was found. 64 - `image` (bytes|None): If the policy enforces saving image of the event, raw image bytes. 65 66 **Returns**: 67 - `Entry`: Database entry object. 68 ''' 69 70 if worker is None: 71 worker_id = None 72 else: 73 worker_id = worker.id 74 75 # Do not attach the image if the entry was successful 76 if code == 0: 77 image = None 78 79 entry = Entry( 80 worker_id = worker_id, 81 code = code, 82 message = message, 83 face_image = image 84 ) 85 db.session.add(entry) 86 db.session.commit() 87 return entry
def
get_report_data( date_from: datetime.datetime = None, date_to: datetime.datetime = None, worker_id: int = None, show_valid: bool = False, show_invalid: bool = False) -> List[Tuple[backend.database.models.Entry, Optional[backend.database.models.Worker]]]:
8def get_report_data(date_from: datetime = None, 9 date_to: datetime = None, 10 worker_id: int = None, 11 show_valid: bool = False, 12 show_invalid: bool = False) -> List[Tuple[Entry, Optional[Worker]]]: 13 ''' 14 Retrieves entry report data by joining Entry records with Worker data. 15 16 This function handles filtering by date range, specific worker, and entry validity status. 17 Results are sorted in descending order by date. 18 19 **Parameters**: 20 - `date_from` (datetime): Start date (inclusive). Defaults to None. 21 - `date_to` (datetime): End date (inclusive). Defaults to None. 22 - `worker_id` (int): Worker ID to filter by. Defaults to None. 23 - `show_valid` (bool): Whether to include valid entries (code 0). If selected together with show_invalid (or both are unselected), returns all types. 24 - `show_invalid` (bool): Whether to include invalid entries (code != 0). If selected together with show_valid (or both are unselected), returns all types. 25 26 **Returns**: 27 - `List[Tuple[Entry, Optional[Worker]]]` - A list of tuples where the first element is the entry object (Entry) and the second is the worker object (Worker) or None if the entry has no assigned worker. 28 ''' 29 query = db.session.query(Entry, Worker).outerjoin(Worker, Entry.worker_id == Worker.id) 30 31 if date_from: 32 query = query.filter(Entry.date >= date_from) 33 34 if date_to: 35 query = query.filter(Entry.date <= date_to) 36 37 if worker_id: 38 query = query.filter(Entry.worker_id == worker_id) 39 40 # Logika filtrów: 41 # 1. Oba zaznaczone -> Pokaż wszystko (pass) 42 # 2. Tylko valid -> kod == 0 43 # 3. Tylko invalid -> kod != 0 44 # 4. Żaden niezaznaczony -> Pokaż wszystko (domyślne zachowanie query bez filtrów) 45 if show_valid and show_invalid: 46 pass 47 elif show_valid: 48 query = query.filter(Entry.code == 0) 49 elif show_invalid: 50 query = query.filter(Entry.code != 0) 51 52 query = query.order_by(Entry.date.desc()) 53 54 return query.all()
Retrieves entry report data by joining Entry records with Worker data.
This function handles filtering by date range, specific worker, and entry validity status. Results are sorted in descending order by date.
Parameters:
date_from(datetime): Start date (inclusive). Defaults to None.date_to(datetime): End date (inclusive). Defaults to None.worker_id(int): Worker ID to filter by. Defaults to None.show_valid(bool): Whether to include valid entries (code 0). If selected together with show_invalid (or both are unselected), returns all types.show_invalid(bool): Whether to include invalid entries (code != 0). If selected together with show_valid (or both are unselected), returns all types.
Returns:
List[Tuple[Entry, Optional[Worker]]]- A list of tuples where the first element is the entry object (Entry) and the second is the worker object (Worker) or None if the entry has no assigned worker.
def
log_worker_entry( code: int, message: str, worker: backend.database.models.Worker | None = None, image=None) -> backend.database.models.Entry:
57def log_worker_entry(code: int, message: str, worker: Worker|None = None, image=None) -> Entry: 58 ''' 59 Log a worker entry. 60 61 **Parameters**: 62 - `code` (int): Numeric verification response code for the entry. 63 - `message` (str): Human-readable verification response message. 64 - `worker` (Worker|None): Worker, if a worker was found. 65 - `image` (bytes|None): If the policy enforces saving image of the event, raw image bytes. 66 67 **Returns**: 68 - `Entry`: Database entry object. 69 ''' 70 71 if worker is None: 72 worker_id = None 73 else: 74 worker_id = worker.id 75 76 # Do not attach the image if the entry was successful 77 if code == 0: 78 image = None 79 80 entry = Entry( 81 worker_id = worker_id, 82 code = code, 83 message = message, 84 face_image = image 85 ) 86 db.session.add(entry) 87 db.session.commit() 88 return entry
Log a worker entry.
Parameters:
code(int): Numeric verification response code for the entry.message(str): Human-readable verification response message.worker(Worker|None): Worker, if a worker was found.image(bytes|None): If the policy enforces saving image of the event, raw image bytes.
Returns:
Entry: Database entry object.