sibi-dst 0.3.63__py3-none-any.whl → 0.3.64__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,119 +0,0 @@
1
- import datetime
2
-
3
- from sqlalchemy import func, cast
4
- from sqlalchemy.sql.sqltypes import Date, Time
5
-
6
-
7
- class SqlAlchemyFilterHandler:
8
- @staticmethod
9
- def apply_filters_sqlalchemy(query, model, filters):
10
- """
11
- Apply Django-like filters to an SQLAlchemy query.
12
-
13
- Args:
14
- query: The base SQLAlchemy query.
15
- model: The SQLAlchemy model to filter.
16
- filters: A dictionary of filters with Django-like syntax.
17
-
18
- Returns:
19
- query: The filtered SQLAlchemy query.
20
- """
21
- # Define operators and their SQLAlchemy equivalents
22
- dt_operators = ["date", "time"]
23
- date_operators = ["year", "month", "day", "hour", "minute", "second", "week_day"]
24
-
25
- comparison_operators = [
26
- "gte", "lte", "gt", "lt", "exact", "in", "range",
27
- "contains", "startswith", "endswith", "isnull",
28
- ]
29
-
30
- operation_map = {
31
- "exact": lambda col, val: col == val,
32
- "gt": lambda col, val: col > val,
33
- "gte": lambda col, val: col >= val,
34
- "lt": lambda col, val: col < val,
35
- "lte": lambda col, val: col <= val,
36
- "in": lambda col, val: col.in_(val),
37
- "range": lambda col, val: col.between(val[0], val[1]),
38
- "contains": lambda col, val: col.like(f"%{val}%"),
39
- "startswith": lambda col, val: col.like(f"{val}%"),
40
- "endswith": lambda col, val: col.like(f"%{val}"),
41
- "isnull": lambda col, val: col.is_(None) if val else col.isnot(None),
42
- }
43
-
44
- def parse_filter_value(casting, value):
45
- """
46
- Convert filter value to appropriate type based on the casting (e.g., date).
47
- """
48
- if casting == "date":
49
- if isinstance(value, str):
50
- return datetime.date.fromisoformat(value)
51
- if isinstance(value, list):
52
- return [datetime.date.fromisoformat(v) for v in value]
53
- return value
54
-
55
- def handle_date_operator(column, date_op):
56
- """
57
- Handle filtering on specific datetime parts (e.g., year, month).
58
- """
59
- if date_op == "year":
60
- return func.extract("year", column)
61
- elif date_op == "month":
62
- return func.extract("month", column)
63
- elif date_op == "day":
64
- return func.extract("day", column)
65
- elif date_op == "hour":
66
- return func.extract("hour", column)
67
- elif date_op == "minute":
68
- return func.extract("minute", column)
69
- elif date_op == "second":
70
- return func.extract("second", column)
71
- elif date_op == "week_day":
72
- # SQLAlchemy uses 1 for Sunday, 2 for Monday, etc.
73
- return func.strftime("%w", column)
74
- else:
75
- raise ValueError(f"Unsupported date operator: {date_op}")
76
-
77
- for key, value in filters.items():
78
- parts = key.split("__")
79
- field_name = parts[0]
80
- casting = None
81
- operation = "exact"
82
-
83
- if len(parts) == 3:
84
- # Adjust logic based on the parts
85
- _, casting, operation = parts
86
- elif len(parts) == 2:
87
- # Could be either a casting or an operation
88
- if parts[1] in comparison_operators:
89
- operation = parts[1]
90
- elif parts[1] in dt_operators + date_operators:
91
- casting = parts[1]
92
-
93
- # Get the column from the model
94
- column = getattr(model, field_name, None)
95
- # column = model.__table__.columns.get(field_name)
96
- if not column:
97
- raise AttributeError(f"Field '{field_name}' not found in model '{model.__name__}'")
98
-
99
- # Convert the filter value to the correct type
100
- parsed_value = parse_filter_value(casting, value)
101
-
102
- # Handle casting (e.g., date, time)
103
- if casting == "date":
104
- column = cast(column, Date)
105
- elif casting == "time":
106
- column = cast(column, Time)
107
-
108
- # Handle specific datetime parts (e.g., year, month)
109
- if casting in date_operators:
110
- column = handle_date_operator(column, casting)
111
-
112
- # Apply the filter operation
113
- if operation in operation_map:
114
- condition = operation_map[operation](column, parsed_value)
115
- query = query.filter(condition)
116
- else:
117
- raise ValueError(f"Unsupported operation: {operation}")
118
-
119
- return query