clear-skies 2.0.3__py3-none-any.whl → 2.0.5__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.

Potentially problematic release.


This version of clear-skies might be problematic. Click here for more details.

Files changed (251) hide show
  1. clear_skies-2.0.5.dist-info/METADATA +74 -0
  2. clear_skies-2.0.5.dist-info/RECORD +4 -0
  3. {clear_skies-2.0.3.dist-info → clear_skies-2.0.5.dist-info}/WHEEL +1 -1
  4. clear_skies-2.0.3.dist-info/METADATA +0 -46
  5. clear_skies-2.0.3.dist-info/RECORD +0 -249
  6. clearskies/__init__.py +0 -59
  7. clearskies/action.py +0 -7
  8. clearskies/authentication/__init__.py +0 -15
  9. clearskies/authentication/authentication.py +0 -46
  10. clearskies/authentication/authorization.py +0 -16
  11. clearskies/authentication/authorization_pass_through.py +0 -20
  12. clearskies/authentication/jwks.py +0 -163
  13. clearskies/authentication/public.py +0 -5
  14. clearskies/authentication/secret_bearer.py +0 -553
  15. clearskies/autodoc/__init__.py +0 -8
  16. clearskies/autodoc/formats/__init__.py +0 -5
  17. clearskies/autodoc/formats/oai3_json/__init__.py +0 -7
  18. clearskies/autodoc/formats/oai3_json/oai3_json.py +0 -87
  19. clearskies/autodoc/formats/oai3_json/oai3_schema_resolver.py +0 -15
  20. clearskies/autodoc/formats/oai3_json/parameter.py +0 -35
  21. clearskies/autodoc/formats/oai3_json/request.py +0 -68
  22. clearskies/autodoc/formats/oai3_json/response.py +0 -28
  23. clearskies/autodoc/formats/oai3_json/schema/__init__.py +0 -11
  24. clearskies/autodoc/formats/oai3_json/schema/array.py +0 -9
  25. clearskies/autodoc/formats/oai3_json/schema/default.py +0 -13
  26. clearskies/autodoc/formats/oai3_json/schema/enum.py +0 -7
  27. clearskies/autodoc/formats/oai3_json/schema/object.py +0 -29
  28. clearskies/autodoc/formats/oai3_json/test.json +0 -1985
  29. clearskies/autodoc/py.typed +0 -0
  30. clearskies/autodoc/request/__init__.py +0 -15
  31. clearskies/autodoc/request/header.py +0 -6
  32. clearskies/autodoc/request/json_body.py +0 -6
  33. clearskies/autodoc/request/parameter.py +0 -8
  34. clearskies/autodoc/request/request.py +0 -38
  35. clearskies/autodoc/request/url_parameter.py +0 -6
  36. clearskies/autodoc/request/url_path.py +0 -6
  37. clearskies/autodoc/response/__init__.py +0 -5
  38. clearskies/autodoc/response/response.py +0 -9
  39. clearskies/autodoc/schema/__init__.py +0 -31
  40. clearskies/autodoc/schema/array.py +0 -10
  41. clearskies/autodoc/schema/base64.py +0 -8
  42. clearskies/autodoc/schema/boolean.py +0 -5
  43. clearskies/autodoc/schema/date.py +0 -5
  44. clearskies/autodoc/schema/datetime.py +0 -5
  45. clearskies/autodoc/schema/double.py +0 -5
  46. clearskies/autodoc/schema/enum.py +0 -17
  47. clearskies/autodoc/schema/integer.py +0 -6
  48. clearskies/autodoc/schema/long.py +0 -5
  49. clearskies/autodoc/schema/number.py +0 -6
  50. clearskies/autodoc/schema/object.py +0 -13
  51. clearskies/autodoc/schema/password.py +0 -5
  52. clearskies/autodoc/schema/schema.py +0 -11
  53. clearskies/autodoc/schema/string.py +0 -5
  54. clearskies/backends/__init__.py +0 -65
  55. clearskies/backends/api_backend.py +0 -1178
  56. clearskies/backends/backend.py +0 -136
  57. clearskies/backends/cursor_backend.py +0 -335
  58. clearskies/backends/memory_backend.py +0 -797
  59. clearskies/backends/secrets_backend.py +0 -106
  60. clearskies/column.py +0 -1233
  61. clearskies/columns/__init__.py +0 -71
  62. clearskies/columns/audit.py +0 -206
  63. clearskies/columns/belongs_to_id.py +0 -483
  64. clearskies/columns/belongs_to_model.py +0 -132
  65. clearskies/columns/belongs_to_self.py +0 -105
  66. clearskies/columns/boolean.py +0 -113
  67. clearskies/columns/category_tree.py +0 -275
  68. clearskies/columns/category_tree_ancestors.py +0 -51
  69. clearskies/columns/category_tree_children.py +0 -127
  70. clearskies/columns/category_tree_descendants.py +0 -48
  71. clearskies/columns/created.py +0 -95
  72. clearskies/columns/created_by_authorization_data.py +0 -116
  73. clearskies/columns/created_by_header.py +0 -99
  74. clearskies/columns/created_by_ip.py +0 -92
  75. clearskies/columns/created_by_routing_data.py +0 -97
  76. clearskies/columns/created_by_user_agent.py +0 -92
  77. clearskies/columns/date.py +0 -234
  78. clearskies/columns/datetime.py +0 -282
  79. clearskies/columns/email.py +0 -76
  80. clearskies/columns/float.py +0 -153
  81. clearskies/columns/has_many.py +0 -505
  82. clearskies/columns/has_many_self.py +0 -56
  83. clearskies/columns/has_one.py +0 -14
  84. clearskies/columns/integer.py +0 -160
  85. clearskies/columns/json.py +0 -126
  86. clearskies/columns/many_to_many_ids.py +0 -337
  87. clearskies/columns/many_to_many_ids_with_data.py +0 -274
  88. clearskies/columns/many_to_many_models.py +0 -158
  89. clearskies/columns/many_to_many_pivots.py +0 -134
  90. clearskies/columns/phone.py +0 -159
  91. clearskies/columns/select.py +0 -92
  92. clearskies/columns/string.py +0 -102
  93. clearskies/columns/timestamp.py +0 -164
  94. clearskies/columns/updated.py +0 -110
  95. clearskies/columns/uuid.py +0 -86
  96. clearskies/configs/README.md +0 -105
  97. clearskies/configs/__init__.py +0 -162
  98. clearskies/configs/actions.py +0 -43
  99. clearskies/configs/any.py +0 -13
  100. clearskies/configs/any_dict.py +0 -22
  101. clearskies/configs/any_dict_or_callable.py +0 -23
  102. clearskies/configs/authentication.py +0 -23
  103. clearskies/configs/authorization.py +0 -23
  104. clearskies/configs/boolean.py +0 -16
  105. clearskies/configs/boolean_or_callable.py +0 -18
  106. clearskies/configs/callable_config.py +0 -18
  107. clearskies/configs/columns.py +0 -34
  108. clearskies/configs/conditions.py +0 -30
  109. clearskies/configs/config.py +0 -24
  110. clearskies/configs/datetime.py +0 -18
  111. clearskies/configs/datetime_or_callable.py +0 -19
  112. clearskies/configs/endpoint.py +0 -23
  113. clearskies/configs/endpoint_list.py +0 -28
  114. clearskies/configs/float.py +0 -16
  115. clearskies/configs/float_or_callable.py +0 -18
  116. clearskies/configs/integer.py +0 -16
  117. clearskies/configs/integer_or_callable.py +0 -18
  118. clearskies/configs/joins.py +0 -30
  119. clearskies/configs/list_any_dict.py +0 -30
  120. clearskies/configs/list_any_dict_or_callable.py +0 -31
  121. clearskies/configs/model_class.py +0 -35
  122. clearskies/configs/model_column.py +0 -65
  123. clearskies/configs/model_columns.py +0 -56
  124. clearskies/configs/model_destination_name.py +0 -25
  125. clearskies/configs/model_to_id_column.py +0 -43
  126. clearskies/configs/readable_model_column.py +0 -9
  127. clearskies/configs/readable_model_columns.py +0 -9
  128. clearskies/configs/schema.py +0 -23
  129. clearskies/configs/searchable_model_columns.py +0 -9
  130. clearskies/configs/security_headers.py +0 -39
  131. clearskies/configs/select.py +0 -26
  132. clearskies/configs/select_list.py +0 -47
  133. clearskies/configs/string.py +0 -29
  134. clearskies/configs/string_dict.py +0 -32
  135. clearskies/configs/string_list.py +0 -32
  136. clearskies/configs/string_list_or_callable.py +0 -35
  137. clearskies/configs/string_or_callable.py +0 -18
  138. clearskies/configs/timedelta.py +0 -18
  139. clearskies/configs/timezone.py +0 -18
  140. clearskies/configs/url.py +0 -23
  141. clearskies/configs/validators.py +0 -45
  142. clearskies/configs/writeable_model_column.py +0 -9
  143. clearskies/configs/writeable_model_columns.py +0 -9
  144. clearskies/configurable.py +0 -76
  145. clearskies/contexts/__init__.py +0 -11
  146. clearskies/contexts/cli.py +0 -117
  147. clearskies/contexts/context.py +0 -98
  148. clearskies/contexts/wsgi.py +0 -76
  149. clearskies/contexts/wsgi_ref.py +0 -82
  150. clearskies/decorators.py +0 -33
  151. clearskies/di/__init__.py +0 -14
  152. clearskies/di/additional_config.py +0 -130
  153. clearskies/di/additional_config_auto_import.py +0 -17
  154. clearskies/di/di.py +0 -968
  155. clearskies/di/inject/__init__.py +0 -23
  156. clearskies/di/inject/by_class.py +0 -21
  157. clearskies/di/inject/by_name.py +0 -18
  158. clearskies/di/inject/di.py +0 -13
  159. clearskies/di/inject/environment.py +0 -14
  160. clearskies/di/inject/input_output.py +0 -20
  161. clearskies/di/inject/now.py +0 -13
  162. clearskies/di/inject/requests.py +0 -13
  163. clearskies/di/inject/secrets.py +0 -14
  164. clearskies/di/inject/utcnow.py +0 -13
  165. clearskies/di/inject/uuid.py +0 -15
  166. clearskies/di/injectable.py +0 -29
  167. clearskies/di/injectable_properties.py +0 -131
  168. clearskies/di/test_module/__init__.py +0 -6
  169. clearskies/di/test_module/another_module/__init__.py +0 -2
  170. clearskies/di/test_module/module_class.py +0 -5
  171. clearskies/end.py +0 -183
  172. clearskies/endpoint.py +0 -1310
  173. clearskies/endpoint_group.py +0 -310
  174. clearskies/endpoints/__init__.py +0 -23
  175. clearskies/endpoints/advanced_search.py +0 -526
  176. clearskies/endpoints/callable.py +0 -388
  177. clearskies/endpoints/create.py +0 -202
  178. clearskies/endpoints/delete.py +0 -139
  179. clearskies/endpoints/get.py +0 -275
  180. clearskies/endpoints/health_check.py +0 -181
  181. clearskies/endpoints/list.py +0 -573
  182. clearskies/endpoints/restful_api.py +0 -427
  183. clearskies/endpoints/simple_search.py +0 -286
  184. clearskies/endpoints/update.py +0 -190
  185. clearskies/environment.py +0 -104
  186. clearskies/exceptions/__init__.py +0 -17
  187. clearskies/exceptions/authentication.py +0 -2
  188. clearskies/exceptions/authorization.py +0 -2
  189. clearskies/exceptions/client_error.py +0 -2
  190. clearskies/exceptions/input_errors.py +0 -4
  191. clearskies/exceptions/moved_permanently.py +0 -3
  192. clearskies/exceptions/moved_temporarily.py +0 -3
  193. clearskies/exceptions/not_found.py +0 -2
  194. clearskies/functional/__init__.py +0 -7
  195. clearskies/functional/routing.py +0 -92
  196. clearskies/functional/string.py +0 -112
  197. clearskies/functional/validations.py +0 -76
  198. clearskies/input_outputs/__init__.py +0 -13
  199. clearskies/input_outputs/cli.py +0 -171
  200. clearskies/input_outputs/exceptions/__init__.py +0 -2
  201. clearskies/input_outputs/exceptions/cli_input_error.py +0 -2
  202. clearskies/input_outputs/exceptions/cli_not_found.py +0 -2
  203. clearskies/input_outputs/headers.py +0 -45
  204. clearskies/input_outputs/input_output.py +0 -138
  205. clearskies/input_outputs/programmatic.py +0 -69
  206. clearskies/input_outputs/py.typed +0 -0
  207. clearskies/input_outputs/wsgi.py +0 -77
  208. clearskies/model.py +0 -1922
  209. clearskies/py.typed +0 -0
  210. clearskies/query/__init__.py +0 -12
  211. clearskies/query/condition.py +0 -223
  212. clearskies/query/join.py +0 -136
  213. clearskies/query/query.py +0 -196
  214. clearskies/query/sort.py +0 -27
  215. clearskies/schema.py +0 -82
  216. clearskies/secrets/__init__.py +0 -6
  217. clearskies/secrets/additional_configs/__init__.py +0 -32
  218. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer.py +0 -61
  219. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +0 -160
  220. clearskies/secrets/akeyless.py +0 -182
  221. clearskies/secrets/exceptions/__init__.py +0 -1
  222. clearskies/secrets/exceptions/not_found.py +0 -2
  223. clearskies/secrets/secrets.py +0 -38
  224. clearskies/security_header.py +0 -15
  225. clearskies/security_headers/__init__.py +0 -11
  226. clearskies/security_headers/cache_control.py +0 -67
  227. clearskies/security_headers/cors.py +0 -50
  228. clearskies/security_headers/csp.py +0 -94
  229. clearskies/security_headers/hsts.py +0 -22
  230. clearskies/security_headers/x_content_type_options.py +0 -0
  231. clearskies/security_headers/x_frame_options.py +0 -0
  232. clearskies/test_base.py +0 -8
  233. clearskies/typing.py +0 -11
  234. clearskies/validator.py +0 -37
  235. clearskies/validators/__init__.py +0 -33
  236. clearskies/validators/after_column.py +0 -62
  237. clearskies/validators/before_column.py +0 -13
  238. clearskies/validators/in_the_future.py +0 -32
  239. clearskies/validators/in_the_future_at_least.py +0 -11
  240. clearskies/validators/in_the_future_at_most.py +0 -10
  241. clearskies/validators/in_the_past.py +0 -32
  242. clearskies/validators/in_the_past_at_least.py +0 -10
  243. clearskies/validators/in_the_past_at_most.py +0 -10
  244. clearskies/validators/maximum_length.py +0 -26
  245. clearskies/validators/maximum_value.py +0 -29
  246. clearskies/validators/minimum_length.py +0 -26
  247. clearskies/validators/minimum_value.py +0 -29
  248. clearskies/validators/required.py +0 -34
  249. clearskies/validators/timedelta.py +0 -59
  250. clearskies/validators/unique.py +0 -30
  251. {clear_skies-2.0.3.dist-info → clear_skies-2.0.5.dist-info/licenses}/LICENSE +0 -0
@@ -1,234 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import datetime
4
- from typing import TYPE_CHECKING, Any, Callable, Self, overload
5
-
6
- import dateparser # type: ignore
7
-
8
- import clearskies.decorators
9
- import clearskies.typing
10
- from clearskies import configs
11
- from clearskies.autodoc.schema import Datetime as AutoDocDatetime
12
- from clearskies.autodoc.schema import Schema as AutoDocSchema
13
- from clearskies.columns.datetime import Datetime
14
- from clearskies.query import Condition
15
-
16
- if TYPE_CHECKING:
17
- from clearskies import Model
18
-
19
-
20
- class Date(Datetime):
21
- """
22
- Stores date data in a column.
23
-
24
- This is specifically for a column that only stores date information - not time information. When processing user input,
25
- this value is passed through `dateparser.parse()` to decide if it is a proper date string. This makes for relatively
26
- flexible input validation. Example:
27
-
28
- ```python
29
- import clearskies
30
-
31
-
32
- class MyModel(clearskies.Model):
33
- backend = clearskies.backends.MemoryBackend()
34
- id_column_name = "id"
35
-
36
- id = clearskies.columns.Uuid()
37
- name = clearskies.columns.String()
38
- my_date = clearskies.columns.Date()
39
-
40
-
41
- wsgi = clearskies.contexts.WsgiRef(
42
- clearskies.endpoints.Create(
43
- MyModel,
44
- writeable_column_names=["name", "my_date"],
45
- readable_column_names=["id", "name", "my_date"],
46
- ),
47
- classes=[MyModel],
48
- )
49
- wsgi()
50
- ```
51
-
52
- And when invoked:
53
-
54
- ```bash
55
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_date":"May 5th 2025"}' | jq
56
- {
57
- "status": "success",
58
- "error": "",
59
- "data": {
60
- "id": "a8c8ac79-bc28-4b24-9728-e85f13fc4104",
61
- "name": "Bob",
62
- "my_date": "2025-05-05"
63
- },
64
- "pagination": {},
65
- "input_errors": {}
66
- }
67
-
68
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_date":"2025-05-03"}' | jq
69
- {
70
- "status": "success",
71
- "error": "",
72
- "data": {
73
- "id": "21376ae7-4090-4c2b-a50b-8d932ad5dac1",
74
- "name": "Bob",
75
- "my_date": "2025-05-03"
76
- },
77
- "pagination": {},
78
- "input_errors": {}
79
- }
80
-
81
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_date":"not a date"}' | jq
82
- {
83
- "status": "input_errors",
84
- "error": "",
85
- "data": [],
86
- "pagination": {},
87
- "input_errors": {
88
- "my_date": "given value did not appear to be a valid date"
89
- }
90
- }
91
- ```
92
- """
93
-
94
- date_format = configs.String(default="%Y-%m-%d")
95
- backend_default = configs.String(default="0000-00-00")
96
-
97
- default = configs.Datetime() # type: ignore
98
- setable = configs.DatetimeOrCallable(default=None) # type: ignore
99
-
100
- _allowed_search_operators = ["<=>", "!=", "<=", ">=", ">", "<", "=", "in", "is not null", "is null"]
101
-
102
- auto_doc_class: type[AutoDocSchema] = AutoDocDatetime
103
- _descriptor_config_map = None
104
-
105
- @clearskies.decorators.parameters_to_properties
106
- def __init__(
107
- self,
108
- date_format: str = "%Y-%m-%d",
109
- backend_default: str = "0000-00-00",
110
- default: datetime.datetime | None = None,
111
- setable: datetime.datetime | Callable[..., datetime.datetime] | None = None,
112
- is_readable: bool = True,
113
- is_writeable: bool = True,
114
- is_searchable: bool = True,
115
- is_temporary: bool = False,
116
- validators: clearskies.typing.validator | list[clearskies.typing.validator] = [],
117
- on_change_pre_save: clearskies.typing.action | list[clearskies.typing.action] = [],
118
- on_change_post_save: clearskies.typing.action | list[clearskies.typing.action] = [],
119
- on_change_save_finished: clearskies.typing.action | list[clearskies.typing.action] = [],
120
- created_by_source_type: str = "",
121
- created_by_source_key: str = "",
122
- created_by_source_strict: bool = True,
123
- ):
124
- pass
125
-
126
- def from_backend(self, value) -> datetime.date | None: # type: ignore
127
- if not value or value == self.backend_default:
128
- return None
129
- if isinstance(value, str):
130
- value = dateparser.parse(value)
131
- if not isinstance(value, datetime.datetime):
132
- raise TypeError(
133
- f"I was expecting to get a datetime from the backend but I didn't get anything recognizable. I have a value of type '{value.__class__.__name__}'. I need either a datetime object or a datetime serialized as a string."
134
- )
135
-
136
- return datetime.date(value.year, value.month, value.day)
137
-
138
- def to_backend(self, data: dict[str, Any]) -> dict[str, Any]:
139
- if self.name not in data or isinstance(data[self.name], str) or data[self.name] is None:
140
- return data
141
-
142
- value = data[self.name]
143
- if not isinstance(data[self.name], datetime.datetime) and not isinstance(data[self.name], datetime.date):
144
- raise TypeError(
145
- f"I was expecting a stringified-date or a datetime object to send to the backend, but instead I found a value of {value.__class__.__name__}"
146
- )
147
-
148
- return {
149
- **data,
150
- self.name: value.strftime(self.date_format),
151
- }
152
-
153
- @overload # type: ignore
154
- def __get__(self, instance: None, cls: type[Model]) -> Self:
155
- pass
156
-
157
- @overload
158
- def __get__(self, instance: Model, cls: type[Model]) -> datetime.date:
159
- pass
160
-
161
- def __get__(self, instance, cls):
162
- return super().__get__(instance, cls)
163
-
164
- def __set__(self, instance, value: datetime.datetime | datetime.date) -> None:
165
- # this makes sure we're initialized
166
- if "name" not in self._config: # type: ignore
167
- instance.get_columns()
168
-
169
- instance._next_data[self.name] = value
170
-
171
- def equals(self, value: str | datetime.datetime | datetime.date) -> Condition:
172
- return super().equals(value) # type: ignore
173
-
174
- def spaceship(self, value: str | datetime.datetime | datetime.date) -> Condition:
175
- return super().spaceship(value) # type: ignore
176
-
177
- def not_equals(self, value: str | datetime.datetime | datetime.date) -> Condition:
178
- return super().not_equals(value) # type: ignore
179
-
180
- def less_than_equals(self, value: str | datetime.datetime | datetime.date) -> Condition:
181
- return super().less_than_equals(value) # type: ignore
182
-
183
- def greater_than_equals(self, value: str | datetime.datetime | datetime.date) -> Condition:
184
- return super().greater_than_equals(value) # type: ignore
185
-
186
- def less_than(self, value: str | datetime.datetime | datetime.date) -> Condition:
187
- return super().less_than(value) # type: ignore
188
-
189
- def greater_than(self, value: str | datetime.datetime | datetime.date) -> Condition:
190
- return super().greater_than(value) # type: ignore
191
-
192
- def is_in(self, values: list[str | datetime.datetime | datetime.date]) -> Condition: # type: ignore
193
- return super().is_in(values) # type: ignore
194
-
195
- def input_error_for_value(self, value, operator=None):
196
- value = dateparser.parse(value)
197
- if not value:
198
- return "given value did not appear to be a valid date"
199
- return ""
200
-
201
- def values_match(self, value_1, value_2):
202
- """Compare two values to see if they are the same."""
203
- # in this function we deal with data directly out of the backend, so our date is likely
204
- # to be string-ified and we want to look for default (e.g. null) values in string form.
205
- if type(value_1) == str and ("0000-00-00" in value_1 or value_1 == self.backend_default):
206
- value_1 = None
207
- if type(value_2) == str and ("0000-00-00" in value_2 or value_2 == self.backend_default):
208
- value_2 = None
209
- number_values = 0
210
- if value_1:
211
- number_values += 1
212
- if value_2:
213
- number_values += 1
214
- if number_values == 0:
215
- return True
216
- if number_values == 1:
217
- return False
218
-
219
- if type(value_1) == str:
220
- value_1 = dateparser.parse(value_1)
221
- value_1 = datetime.date(value_1.year, value_1.month, value_1.day)
222
- if type(value_2) == str:
223
- value_2 = dateparser.parse(value_2)
224
- value_2 = datetime.date(value_2.year, value_2.month, value_2.day)
225
-
226
- # two times can be the same but if one is datetime-aware and one is not, python will treat them as not equal.
227
- # we want to treat such times as being the same. Therefore, check for equality but ignore the timezone.
228
- for to_check in ["year", "month", "day"]:
229
- if getattr(value_1, to_check) != getattr(value_2, to_check):
230
- return False
231
-
232
- # and since we already converted the timezones to match (or one has a timezone and one doesn't), we're good to go.
233
- # if we passed the above loop then the times are the same.
234
- return True
@@ -1,282 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import datetime
4
- from typing import TYPE_CHECKING, Any, Callable, Self, overload
5
-
6
- import dateparser # type: ignore
7
-
8
- import clearskies.decorators
9
- import clearskies.typing
10
- from clearskies import configs
11
- from clearskies.autodoc.schema import Datetime as AutoDocDatetime
12
- from clearskies.autodoc.schema import Schema as AutoDocSchema
13
- from clearskies.column import Column
14
- from clearskies.query import Condition
15
-
16
- if TYPE_CHECKING:
17
- from clearskies import Model
18
-
19
-
20
- class Datetime(Column):
21
- """
22
- Stores date+time data in a column.
23
-
24
- When processing user input, this value is passed through `dateparser.parse()` to decide if it is a proper date string.
25
- This makes for relatively flexible input validation. Example:
26
-
27
- ```python
28
- import clearskies
29
-
30
-
31
- class MyModel(clearskies.Model):
32
- backend = clearskies.backends.MemoryBackend()
33
- id_column_name = "id"
34
-
35
- id = clearskies.columns.Uuid()
36
- name = clearskies.columns.String()
37
- my_datetime = clearskies.columns.Datetime()
38
-
39
-
40
- wsgi = clearskies.contexts.WsgiRef(
41
- clearskies.endpoints.Create(
42
- MyModel,
43
- writeable_column_names=["name", "my_datetime"],
44
- readable_column_names=["id", "name", "my_datetime"],
45
- ),
46
- classes=[MyModel],
47
- )
48
- wsgi()
49
- ```
50
-
51
- And when invoked:
52
-
53
- ```bash
54
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_datetime":"2025-05-13 12:35:45+00:00"}' | jq
55
- {
56
- "status": "success",
57
- "error": "",
58
- "data": {
59
- "id": "68095d0d-c909-4ab3-8c15-bd2667b7b074",
60
- "name": "Bob",
61
- "my_datetime": "2025-05-13T12:35:45+00:00"
62
- },
63
- "pagination": {},
64
- "input_errors": {}
65
- }
66
-
67
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_datetime":"May 13th 2025 2:35:45UTC"}' | jq
68
- {
69
- "status": "success",
70
- "error": "",
71
- "data": {
72
- "id": "9fea6933-86ac-4dd1-b9e0-a9fa50608410",
73
- "name": "Bob",
74
- "my_datetime": "2025-05-13T12:35:45+00:00"
75
- },
76
- "pagination": {},
77
- "input_errors": {}
78
- }
79
-
80
- $ curl 'http://localhost:8080' -d '{"name":"Bob", "my_datetime":"not a date"}' | jq
81
- {
82
- "status": "input_errors",
83
- "error": "",
84
- "data": [],
85
- "pagination": {},
86
- "input_errors": {
87
- "my_datetime": "given value did not appear to be a valid date"
88
- }
89
- }
90
- ```
91
- """
92
-
93
- """
94
- Whether or not to make datetime objects timezone-aware
95
- """
96
- timezone_aware = configs.Boolean(default=True)
97
-
98
- """
99
- The timezone to use for the datetime object (if it is timezone aware)
100
- """
101
- timezone = configs.Timezone(default=datetime.timezone.utc)
102
-
103
- """
104
- The format string to use when sending to the backend (default: %Y-%m-%d %H:%M:%S)
105
- """
106
- date_format = configs.String(default="%Y-%m-%d %H:%M:%S")
107
-
108
- """
109
- A default value to set for this column.
110
-
111
- The default is only used when creating a record for the first time, and only if
112
- a value for this column has not been set.
113
- """
114
- default = configs.Datetime() # type: ignore
115
-
116
- """
117
- Sets a default date that the backend is going to provide.
118
-
119
- Some backends, depending on configuration, may provide a default value for the column
120
- instead of null. By setting this equal to that default value, clearskies can detect
121
- when a given value is actually a non-value.
122
- """
123
- backend_default = configs.String(default="0000-00-00 00:00:00")
124
-
125
- setable = configs.DatetimeOrCallable(default=None) # type: ignore
126
- _allowed_search_operators = ["<=>", "!=", "<=", ">=", ">", "<", "=", "in", "is not null", "is null"]
127
- auto_doc_class: type[AutoDocSchema] = AutoDocDatetime
128
- _descriptor_config_map = None
129
-
130
- @clearskies.decorators.parameters_to_properties
131
- def __init__(
132
- self,
133
- date_format: str = "%Y-%m-%d %H:%M:%S",
134
- backend_default: str = "0000-00-00 00:00:00",
135
- timezone_aware: bool = True,
136
- timezone: datetime.timezone = datetime.timezone.utc,
137
- default: datetime.datetime | None = None,
138
- setable: datetime.datetime | Callable[..., datetime.datetime] | None = None,
139
- is_readable: bool = True,
140
- is_writeable: bool = True,
141
- is_searchable: bool = True,
142
- is_temporary: bool = False,
143
- validators: clearskies.typing.validator | list[clearskies.typing.validator] = [],
144
- on_change_pre_save: clearskies.typing.action | list[clearskies.typing.action] = [],
145
- on_change_post_save: clearskies.typing.action | list[clearskies.typing.action] = [],
146
- on_change_save_finished: clearskies.typing.action | list[clearskies.typing.action] = [],
147
- created_by_source_type: str = "",
148
- created_by_source_key: str = "",
149
- created_by_source_strict: bool = True,
150
- ):
151
- pass
152
-
153
- def from_backend(self, value) -> datetime.datetime | None:
154
- if not value or value == self.backend_default:
155
- return None
156
- if isinstance(value, str):
157
- value = dateparser.parse(value)
158
- if not isinstance(value, datetime.datetime):
159
- raise TypeError(
160
- f"I was expecting to get a datetime from the backend but I didn't get anything recognizable. I have a value of type '{value.__class__.__name__}'. I need either a datetime object or a datetime serialized as a string."
161
- )
162
- if self.timezone_aware:
163
- if not value.tzinfo:
164
- value = value.replace(tzinfo=self.timezone)
165
- elif value.tzinfo != self.timezone:
166
- value = value.astimezone(self.timezone)
167
- else:
168
- value = value.replace(tzinfo=None)
169
-
170
- return value
171
-
172
- def to_backend(self, data: dict[str, Any]) -> dict[str, Any]:
173
- if self.name not in data or isinstance(data[self.name], str) or data[self.name] is None:
174
- return data
175
-
176
- value = data[self.name]
177
- if not isinstance(data[self.name], datetime.datetime):
178
- raise TypeError(
179
- f"I was expecting a stringified-date or a datetime object to send to the backend, but instead I found a value of {value.__class__.__name__}"
180
- )
181
-
182
- return {
183
- **data,
184
- self.name: value.strftime(self.date_format),
185
- }
186
-
187
- def to_json(self, model: clearskies.model.Model) -> dict[str, Any]:
188
- """Grabs the column out of the model and converts it into a representation that can be turned into JSON."""
189
- value = self.__get__(model, model.__class__)
190
- if value and (isinstance(value, datetime.datetime) or isinstance(value, datetime.date)):
191
- value = value.isoformat() # type: ignore
192
-
193
- return {self.name: value}
194
-
195
- @overload
196
- def __get__(self, instance: None, cls: type[Model]) -> Self:
197
- pass
198
-
199
- @overload
200
- def __get__(self, instance: Model, cls: type[Model]) -> datetime.datetime:
201
- pass
202
-
203
- def __get__(self, instance, cls):
204
- return super().__get__(instance, cls)
205
-
206
- def __set__(self, instance, value: datetime.datetime) -> None:
207
- # this makes sure we're initialized
208
- if "name" not in self._config: # type: ignore
209
- instance.get_columns()
210
-
211
- instance._next_data[self.name] = value
212
-
213
- def equals(self, value: str | datetime.datetime) -> Condition:
214
- return super().equals(value)
215
-
216
- def spaceship(self, value: str | datetime.datetime) -> Condition:
217
- return super().spaceship(value)
218
-
219
- def not_equals(self, value: str | datetime.datetime) -> Condition:
220
- return super().not_equals(value)
221
-
222
- def less_than_equals(self, value: str | datetime.datetime) -> Condition:
223
- return super().less_than_equals(value)
224
-
225
- def greater_than_equals(self, value: str | datetime.datetime) -> Condition:
226
- return super().greater_than_equals(value)
227
-
228
- def less_than(self, value: str | datetime.datetime) -> Condition:
229
- return super().less_than(value)
230
-
231
- def greater_than(self, value: str | datetime.datetime) -> Condition:
232
- return super().greater_than(value)
233
-
234
- def is_in(self, values: list[str | datetime.datetime]) -> Condition:
235
- return super().is_in(values)
236
-
237
- def input_error_for_value(self, value, operator=None):
238
- value = dateparser.parse(value)
239
- if not value:
240
- return "given value did not appear to be a valid date"
241
- if not value.tzinfo and self.timezone_aware:
242
- return "date is missing timezone information"
243
- return ""
244
-
245
- def values_match(self, value_1, value_2):
246
- """Compare two values to see if they are the same."""
247
- # in this function we deal with data directly out of the backend, so our date is likely
248
- # to be string-ified and we want to look for default (e.g. null) values in string form.
249
- if type(value_1) == str and ("0000-00-00" in value_1 or value_1 == self.backend_default):
250
- value_1 = None
251
- if type(value_2) == str and ("0000-00-00" in value_2 or value_2 == self.backend_default):
252
- value_2 = None
253
- number_values = 0
254
- if value_1:
255
- number_values += 1
256
- if value_2:
257
- number_values += 1
258
- if number_values == 0:
259
- return True
260
- if number_values == 1:
261
- return False
262
-
263
- if type(value_1) == str:
264
- value_1 = dateparser.parse(value_1)
265
- if type(value_2) == str:
266
- value_2 = dateparser.parse(value_2)
267
-
268
- # we need to make sure we're comparing in the same timezones. For our purposes, a difference in timezone
269
- # is fine as long as they represent the same time (e.g. 16:00EST == 20:00UTC). For python, same time in different
270
- # timezones is treated as different datetime objects.
271
- if value_1.tzinfo is not None and value_2.tzinfo is not None:
272
- value_1 = value_1.astimezone(value_2.tzinfo)
273
-
274
- # two times can be the same but if one is datetime-aware and one is not, python will treat them as not equal.
275
- # we want to treat such times as being the same. Therefore, check for equality but ignore the timezone.
276
- for to_check in ["year", "month", "day", "hour", "minute", "second", "microsecond"]:
277
- if getattr(value_1, to_check) != getattr(value_2, to_check):
278
- return False
279
-
280
- # and since we already converted the timezones to match (or one has a timezone and one doesn't), we're good to go.
281
- # if we passed the above loop then the times are the same.
282
- return True
@@ -1,76 +0,0 @@
1
- import re
2
-
3
- from clearskies.columns.string import String
4
-
5
-
6
- class Email(String):
7
- """
8
- A string column that specifically expects an email.
9
-
10
- ```python
11
- import clearskies
12
-
13
-
14
- class MyModel(clearskies.Model):
15
- backend = clearskies.backends.MemoryBackend()
16
- id_column_name = "id"
17
-
18
- id = clearskies.columns.Uuid()
19
- email = clearskies.columns.Email()
20
-
21
-
22
- wsgi = clearskies.contexts.WsgiRef(
23
- clearskies.endpoints.Create(
24
- MyModel,
25
- writeable_column_names=["email"],
26
- readable_column_names=["id", "email"],
27
- ),
28
- classes=[MyModel],
29
- )
30
- wsgi()
31
- ```
32
-
33
- And when invoked:
34
-
35
- ```bash
36
- $ curl 'http://localhost:8080' -d '{"email":"test@example.com"}' | jq
37
- {
38
- "status": "success",
39
- "error": "",
40
- "data": {
41
- "id": "2a72a895-c469-45b0-b5cd-5a3cbb3a6e99",
42
- "email": "test@example.com"
43
- },
44
- "pagination": {},
45
- "input_errors": {}
46
- }
47
-
48
- $ curl 'http://localhost:8080' -d '{"email":"asdf"}' | jq
49
- {
50
- "status": "input_errors",
51
- "error": "",
52
- "data": [],
53
- "pagination": {},
54
- "input_errors": {
55
- "email": "Invalid email address"
56
- }
57
- }
58
- ```
59
- """
60
-
61
- _descriptor_config_map = None
62
-
63
- """
64
- A column that always requires an email address.
65
- """
66
-
67
- def input_error_for_value(self, value: str, operator: str | None = None) -> str:
68
- if type(value) != str:
69
- return f"Value must be a string for {self.name}"
70
- if operator and operator.lower() == "like":
71
- # don't check for an email if doing a fuzzy search, since we may be searching
72
- # for a partial email
73
- return ""
74
- if re.search(r"^[^@\s]+@[^@]+\.[^@]+$", value):
75
- return ""
76
- return "Invalid email address"