feincms3-api 0.0.1__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.
Files changed (53) hide show
  1. feincms3_api/__init__.py +1 -0
  2. feincms3_api/adapters.py +303 -0
  3. feincms3_api/admin.py +93 -0
  4. feincms3_api/api.py +66 -0
  5. feincms3_api/apps.py +70 -0
  6. feincms3_api/auth.py +29 -0
  7. feincms3_api/cabinet.py +78 -0
  8. feincms3_api/codecs.py +495 -0
  9. feincms3_api/conf.py +31 -0
  10. feincms3_api/content.py +312 -0
  11. feincms3_api/errors.py +53 -0
  12. feincms3_api/forms.py +36 -0
  13. feincms3_api/formsets.py +135 -0
  14. feincms3_api/http.py +195 -0
  15. feincms3_api/incubator/__init__.py +6 -0
  16. feincms3_api/incubator/autotranslate/__init__.py +0 -0
  17. feincms3_api/incubator/autotranslate/apps.py +47 -0
  18. feincms3_api/incubator/autotranslate/backends/__init__.py +78 -0
  19. feincms3_api/incubator/autotranslate/backends/anthropic.py +98 -0
  20. feincms3_api/incubator/autotranslate/conf.py +59 -0
  21. feincms3_api/incubator/autotranslate/datasets.py +428 -0
  22. feincms3_api/incubator/autotranslate/fields.py +213 -0
  23. feincms3_api/incubator/autotranslate/links.py +122 -0
  24. feincms3_api/incubator/autotranslate/loader.py +1250 -0
  25. feincms3_api/incubator/autotranslate/management/__init__.py +0 -0
  26. feincms3_api/incubator/autotranslate/management/commands/__init__.py +0 -0
  27. feincms3_api/incubator/autotranslate/management/commands/f3autotranslate.py +162 -0
  28. feincms3_api/incubator/autotranslate/management/commands/f3autotranslateload.py +92 -0
  29. feincms3_api/incubator/autotranslate/review.py +104 -0
  30. feincms3_api/incubator/autotranslate/strings.py +313 -0
  31. feincms3_api/incubator/autotranslate/translate.py +260 -0
  32. feincms3_api/kinds.py +86 -0
  33. feincms3_api/management/__init__.py +0 -0
  34. feincms3_api/management/commands/__init__.py +0 -0
  35. feincms3_api/management/commands/f3api_cleanup.py +18 -0
  36. feincms3_api/management/commands/f3api_schema.py +32 -0
  37. feincms3_api/management/commands/f3api_token.py +39 -0
  38. feincms3_api/migrations/0001_initial.py +91 -0
  39. feincms3_api/migrations/0002_upload.py +61 -0
  40. feincms3_api/migrations/__init__.py +0 -0
  41. feincms3_api/models.py +189 -0
  42. feincms3_api/paths.py +43 -0
  43. feincms3_api/reading.py +120 -0
  44. feincms3_api/references.py +123 -0
  45. feincms3_api/resources.py +580 -0
  46. feincms3_api/schema.py +80 -0
  47. feincms3_api/snapshot.py +110 -0
  48. feincms3_api/templates/admin/feincms3_api/apitoken/created.html +19 -0
  49. feincms3_api/writing.py +255 -0
  50. feincms3_api-0.0.1.dist-info/METADATA +69 -0
  51. feincms3_api-0.0.1.dist-info/RECORD +53 -0
  52. feincms3_api-0.0.1.dist-info/WHEEL +4 -0
  53. feincms3_api-0.0.1.dist-info/licenses/LICENSE +29 -0
feincms3_api/codecs.py ADDED
@@ -0,0 +1,495 @@
1
+ """
2
+ Codecs: one form field in wire format, and its JSON schema
3
+
4
+ Codecs are looked up by form field class along its MRO, so a project's
5
+ subclass of a known form field uses that field's codec without being
6
+ registered. A form field without a codec isn't guessed at: the field is left
7
+ out, and ``/schema/`` and the snapshot list it as excluded.
8
+ """
9
+
10
+ import copy
11
+ import dataclasses
12
+ import datetime as dt
13
+ import decimal
14
+
15
+ from django import forms
16
+ from django.core.exceptions import FieldDoesNotExist
17
+ from django.db import models
18
+
19
+ from feincms3_api.errors import WireError
20
+ from feincms3_api.kinds import (
21
+ CHOICES,
22
+ HTML,
23
+ LINK,
24
+ MACHINE,
25
+ TEXT,
26
+ is_link_name,
27
+ is_machine_name,
28
+ json_string_kind,
29
+ schema_paths,
30
+ )
31
+ from feincms3_api.paths import tokens
32
+
33
+
34
+ try:
35
+ from django_prose_editor.fields import ProseEditorFormField
36
+ except ImportError: # pragma: no cover
37
+ ProseEditorFormField = None
38
+
39
+ try:
40
+ from django_json_schema_editor.forms import JSONEditorField
41
+ except ImportError: # pragma: no cover
42
+ JSONEditorField = None
43
+
44
+ try:
45
+ from feincms3_forms.models import FormFieldBase
46
+ except ImportError: # pragma: no cover
47
+ FormFieldBase = None
48
+
49
+
50
+ KIND = "x-feincms3-kind"
51
+ REF = "x-feincms3-ref"
52
+
53
+
54
+ def label(model):
55
+ return model._meta.label_lower
56
+
57
+
58
+ @dataclasses.dataclass
59
+ class Field:
60
+ """One field of a form, as the API exposes it"""
61
+
62
+ name: str
63
+ form_field: forms.Field
64
+ model: type[models.Model]
65
+ read_only: bool = False
66
+
67
+ @property
68
+ def model_field(self):
69
+ try:
70
+ return self.model._meta.get_field(self.name)
71
+ except FieldDoesNotExist:
72
+ return None
73
+
74
+
75
+ class Codec:
76
+ """Values pass through as they are; subclasses describe their type"""
77
+
78
+ def to_wire(self, value, field):
79
+ return value
80
+
81
+ def from_wire(self, value, field):
82
+ """
83
+ The value the form field's ``clean()`` gets; raise ``WireError`` if
84
+ the value can't be one
85
+ """
86
+ return value
87
+
88
+ def json_schema(self, field):
89
+ return {}
90
+
91
+ def references(self, value, field):
92
+ """``(type, id)`` of the objects a wire value refers to"""
93
+ return []
94
+
95
+
96
+ def _nullable(schema, field):
97
+ if field.form_field.required:
98
+ return schema
99
+ model_field = field.model_field
100
+ if model_field is not None and getattr(model_field, "null", False):
101
+ schema["type"] = [schema["type"], "null"]
102
+ return schema
103
+
104
+
105
+ def string_kind(field):
106
+ """The kind of a plain string form field"""
107
+ form_field, name = field.form_field, field.name
108
+ if isinstance(form_field, forms.URLField) or is_link_name(name):
109
+ return LINK
110
+ if is_machine_name(name) or isinstance(
111
+ form_field,
112
+ (
113
+ forms.SlugField,
114
+ forms.EmailField,
115
+ forms.RegexField,
116
+ forms.GenericIPAddressField,
117
+ forms.UUIDField,
118
+ ),
119
+ ):
120
+ return MACHINE
121
+ if FormFieldBase is not None and issubclass(field.model, FormFieldBase):
122
+ # The name is the key submissions are stored under, and the default
123
+ # value of a choice field names a value.
124
+ if name in {"name", "default_value"}:
125
+ return MACHINE
126
+ if name == "choices":
127
+ return CHOICES
128
+ return TEXT
129
+
130
+
131
+ class CharCodec(Codec):
132
+ kind = None
133
+
134
+ def from_wire(self, value, field):
135
+ if value is None:
136
+ return ""
137
+ if not isinstance(value, str):
138
+ raise WireError("Expected a string.")
139
+ return value
140
+
141
+ def json_schema(self, field):
142
+ form_field = field.form_field
143
+ schema = {"type": "string", KIND: self.kind or string_kind(field)}
144
+ if form_field.max_length is not None:
145
+ schema["maxLength"] = form_field.max_length
146
+ if form_field.min_length is not None:
147
+ schema["minLength"] = form_field.min_length
148
+ if isinstance(form_field, forms.URLField):
149
+ schema["format"] = "uri"
150
+ return _nullable(schema, field)
151
+
152
+
153
+ class HTMLCodec(CharCodec):
154
+ kind = HTML
155
+
156
+ def json_schema(self, field):
157
+ return {**super().json_schema(field), "contentMediaType": "text/html"}
158
+
159
+
160
+ class BooleanCodec(Codec):
161
+ def from_wire(self, value, field):
162
+ if value is None and isinstance(field.form_field, forms.NullBooleanField):
163
+ return None
164
+ if not isinstance(value, bool):
165
+ raise WireError("Expected true or false.")
166
+ return value
167
+
168
+ def json_schema(self, field):
169
+ if isinstance(field.form_field, forms.NullBooleanField):
170
+ return {"type": ["boolean", "null"]}
171
+ return {"type": "boolean"}
172
+
173
+
174
+ class IntegerCodec(Codec):
175
+ def json_schema(self, field):
176
+ schema = {"type": "integer"}
177
+ if (minimum := field.form_field.min_value) is not None:
178
+ schema["minimum"] = minimum
179
+ if (maximum := field.form_field.max_value) is not None:
180
+ schema["maximum"] = maximum
181
+ return _nullable(schema, field)
182
+
183
+
184
+ class FloatCodec(IntegerCodec):
185
+ def json_schema(self, field):
186
+ return {**super().json_schema(field), "type": "number"}
187
+
188
+
189
+ class DecimalCodec(Codec):
190
+ """Decimals as strings, so no precision is lost on the way"""
191
+
192
+ def to_wire(self, value, field):
193
+ return None if value is None else str(value)
194
+
195
+ def from_wire(self, value, field):
196
+ if isinstance(value, float):
197
+ raise WireError("Expected a string, not a float, to keep the precision.")
198
+ return value
199
+
200
+ def json_schema(self, field):
201
+ return _nullable({"type": "string", "format": "decimal"}, field)
202
+
203
+
204
+ class TemporalCodec(Codec):
205
+ def __init__(self, format):
206
+ self.format = format
207
+
208
+ def to_wire(self, value, field):
209
+ if isinstance(value, (dt.date, dt.time)):
210
+ return value.isoformat()
211
+ if isinstance(value, dt.timedelta):
212
+ return str(value)
213
+ return value
214
+
215
+ def json_schema(self, field):
216
+ return _nullable({"type": "string", "format": self.format}, field)
217
+
218
+
219
+ def _choice_values(choices):
220
+ values = []
221
+ for key, value in choices:
222
+ if isinstance(value, (list, tuple)):
223
+ values.extend(_choice_values(value))
224
+ else:
225
+ values.append(key.value if hasattr(key, "value") else key)
226
+ return values
227
+
228
+
229
+ def _json_value(value):
230
+ if isinstance(value, decimal.Decimal):
231
+ return str(value)
232
+ return value
233
+
234
+
235
+ class ChoiceCodec(Codec):
236
+ def json_schema(self, field):
237
+ values = [_json_value(v) for v in _choice_values(field.form_field.choices)]
238
+ schema = {"enum": values}
239
+ if all(isinstance(v, str) for v in values):
240
+ schema = {"type": "string", **schema, KIND: MACHINE}
241
+ return schema
242
+
243
+
244
+ class SplitDateTimeCodec(TemporalCodec):
245
+ """The admin's date and time inputs: an ISO date-time on the wire"""
246
+
247
+ def __init__(self):
248
+ super().__init__("date-time")
249
+
250
+ def from_wire(self, value, field):
251
+ if value in {None, ""}:
252
+ return [None, None]
253
+ try:
254
+ parsed = dt.datetime.fromisoformat(value)
255
+ except (TypeError, ValueError) as exc:
256
+ raise WireError("Expected an ISO 8601 date and time.") from exc
257
+ return [parsed.date().isoformat(), parsed.time().isoformat()]
258
+
259
+
260
+ class MultipleChoiceCodec(Codec):
261
+ def to_wire(self, value, field):
262
+ return list(value or ())
263
+
264
+ def from_wire(self, value, field):
265
+ if not isinstance(value, list):
266
+ raise WireError("Expected a list.")
267
+ return value
268
+
269
+ def json_schema(self, field):
270
+ values = [_json_value(v) for v in _choice_values(field.form_field.choices)]
271
+ return {"type": "array", "items": {"enum": values}}
272
+
273
+
274
+ def reference(obj_or_pk, model):
275
+ if obj_or_pk is None or obj_or_pk == "":
276
+ return None
277
+ pk = getattr(obj_or_pk, "pk", obj_or_pk)
278
+ return {"type": label(model), "id": pk}
279
+
280
+
281
+ def reference_pk(value, model):
282
+ """The primary key of a reference: ``{"type", "id"}`` or the bare id"""
283
+ if value is None:
284
+ return None
285
+ if isinstance(value, dict):
286
+ if value.get("type") != label(model):
287
+ raise WireError(f"Expected a reference to {label(model)}.")
288
+ if "id" not in value:
289
+ raise WireError("A reference needs an id.")
290
+ return value["id"]
291
+ if isinstance(value, (int, str)):
292
+ return value
293
+ raise WireError(f"Expected a reference to {label(model)}.")
294
+
295
+
296
+ def reference_schema(model):
297
+ return {
298
+ "type": "object",
299
+ "properties": {
300
+ "type": {"const": label(model)},
301
+ "id": {},
302
+ },
303
+ "required": ["type", "id"],
304
+ REF: label(model),
305
+ }
306
+
307
+
308
+ class ModelChoiceCodec(Codec):
309
+ def to_wire(self, value, field):
310
+ return reference(value, field.form_field.queryset.model)
311
+
312
+ def references(self, value, field):
313
+ return [(value["type"], value["id"])] if isinstance(value, dict) else []
314
+
315
+ def from_wire(self, value, field):
316
+ return reference_pk(value, field.form_field.queryset.model)
317
+
318
+ def json_schema(self, field):
319
+ schema = reference_schema(field.form_field.queryset.model)
320
+ if not field.form_field.required:
321
+ schema["type"] = ["object", "null"]
322
+ return schema
323
+
324
+
325
+ class ModelMultipleChoiceCodec(Codec):
326
+ def to_wire(self, value, field):
327
+ model = field.form_field.queryset.model
328
+ return [reference(item, model) for item in value or ()]
329
+
330
+ def references(self, value, field):
331
+ return [
332
+ (item["type"], item["id"]) for item in value or () if isinstance(item, dict)
333
+ ]
334
+
335
+ def from_wire(self, value, field):
336
+ if not isinstance(value, list):
337
+ raise WireError("Expected a list of references.")
338
+ model = field.form_field.queryset.model
339
+ return [reference_pk(item, model) for item in value]
340
+
341
+ def json_schema(self, field):
342
+ return {
343
+ "type": "array",
344
+ "items": reference_schema(field.form_field.queryset.model),
345
+ }
346
+
347
+
348
+ @dataclasses.dataclass(frozen=True)
349
+ class UploadRef:
350
+ """``{"upload": id}``, resolved against the user's uploads when binding"""
351
+
352
+ id: str
353
+
354
+
355
+ class FileCodec(Codec):
356
+ """
357
+ A file of the object itself (not a django-cabinet reference): its name
358
+ and URL. Written through ``/uploads/``, see the spec.
359
+ """
360
+
361
+ def to_wire(self, value, field):
362
+ if not value:
363
+ return None
364
+ try:
365
+ url = value.url
366
+ except ValueError:
367
+ url = None
368
+ return {"name": value.name, "url": url}
369
+
370
+ def from_wire(self, value, field):
371
+ """
372
+ ``None`` clears the file; what was read keeps it; ``{"upload": id}``
373
+ is a file from ``/uploads/``
374
+ """
375
+ if value is None:
376
+ if field.form_field.required:
377
+ # Django's FileField would keep the file without a word.
378
+ raise WireError("Required, it can't be cleared.")
379
+ return False # Like the admin's "clear" checkbox
380
+ if isinstance(value, dict) and "upload" in value:
381
+ return UploadRef(str(value["upload"]))
382
+ if isinstance(value, dict) and "name" in value:
383
+ return None # Unchanged
384
+ raise WireError('Expected null, the file as read, or {"upload": id}.')
385
+
386
+ def json_schema(self, field):
387
+ return {
388
+ "type": ["object", "null"],
389
+ "properties": {
390
+ "name": {"type": "string", KIND: MACHINE},
391
+ "url": {"type": ["string", "null"], KIND: MACHINE},
392
+ },
393
+ "x-feincms3-file": True,
394
+ }
395
+
396
+
397
+ class JSONCodec(Codec):
398
+ """JSON without a schema: passed through, with an empty schema"""
399
+
400
+
401
+ def annotate_json_schema(schema, prefix):
402
+ """A copy of ``schema`` with the kind of every string in it"""
403
+ schema = copy.deepcopy(schema)
404
+ for path, sub in schema_paths(schema, prefix):
405
+ if (kind := json_string_kind(path, sub)) is not None:
406
+ sub[KIND] = kind
407
+ return schema
408
+
409
+
410
+ class JSONEditorCodec(Codec):
411
+ """
412
+ django-json-schema-editor data: the editor's own schema
413
+
414
+ References inside the data (``format: foreign_key``) stay bare ids, as
415
+ stored; the editor's ``foreign_key_descriptions`` say what they refer
416
+ to, and where if they're given as paths (``foreign_key_paths``).
417
+ """
418
+
419
+ def json_schema(self, field):
420
+ schema = annotate_json_schema(field.form_field._schema or {}, field.name)
421
+ by_path = dict(schema_paths(schema, field.name))
422
+ for type_label, getter in _foreign_key_descriptions(field):
423
+ for jmespath in getattr(getter, "keywords", {}).get("paths", ()):
424
+ # The simple ones are paths in our notation too
425
+ path = f"{field.name}.{jmespath.replace('[*]', '[]')}"
426
+ if (sub := by_path.get(path)) is not None:
427
+ sub[REF] = type_label
428
+ return schema
429
+
430
+ def references(self, value, field):
431
+ refs = []
432
+ for type_label, getter in _foreign_key_descriptions(field):
433
+ try:
434
+ pks = getter(value or {})
435
+ except (TypeError, ValueError):
436
+ continue
437
+ refs.extend((type_label, pk) for pk in pks)
438
+ return refs
439
+
440
+
441
+ def _foreign_key_descriptions(field):
442
+ return getattr(field.form_field, "_foreign_key_descriptions", None) or ()
443
+
444
+
445
+ class CodecRegistry:
446
+ def __init__(self):
447
+ self._codecs = {}
448
+
449
+ def register(self, form_field_class, codec):
450
+ self._codecs[form_field_class] = codec
451
+
452
+ def copy(self):
453
+ registry = CodecRegistry()
454
+ registry._codecs = dict(self._codecs)
455
+ return registry
456
+
457
+ def lookup(self, form_field):
458
+ for cls in type(form_field).__mro__:
459
+ if (codec := self._codecs.get(cls)) is not None:
460
+ return codec
461
+ return None
462
+
463
+
464
+ codecs = CodecRegistry()
465
+ codecs.register(forms.CharField, CharCodec())
466
+ codecs.register(forms.BooleanField, BooleanCodec())
467
+ codecs.register(forms.IntegerField, IntegerCodec())
468
+ codecs.register(forms.FloatField, FloatCodec())
469
+ codecs.register(forms.DecimalField, DecimalCodec())
470
+ codecs.register(forms.DateField, TemporalCodec("date"))
471
+ codecs.register(forms.DateTimeField, TemporalCodec("date-time"))
472
+ codecs.register(forms.SplitDateTimeField, SplitDateTimeCodec())
473
+ codecs.register(forms.TimeField, TemporalCodec("time"))
474
+ codecs.register(forms.DurationField, TemporalCodec("duration"))
475
+ codecs.register(forms.ChoiceField, ChoiceCodec())
476
+ codecs.register(forms.MultipleChoiceField, MultipleChoiceCodec())
477
+ codecs.register(forms.ModelChoiceField, ModelChoiceCodec())
478
+ codecs.register(forms.ModelMultipleChoiceField, ModelMultipleChoiceCodec())
479
+ codecs.register(forms.JSONField, JSONCodec())
480
+ codecs.register(forms.FileField, FileCodec())
481
+ if ProseEditorFormField is not None:
482
+ codecs.register(ProseEditorFormField, HTMLCodec())
483
+ if JSONEditorField is not None:
484
+ codecs.register(JSONEditorField, JSONEditorCodec())
485
+
486
+
487
+ __all__ = [
488
+ "Codec",
489
+ "CodecRegistry",
490
+ "Field",
491
+ "codecs",
492
+ "label",
493
+ "reference",
494
+ "tokens",
495
+ ]
feincms3_api/conf.py ADDED
@@ -0,0 +1,31 @@
1
+ import datetime as dt
2
+
3
+ from django.conf import settings
4
+
5
+
6
+ DEFAULTS = {
7
+ # ``app_label.model_name.field``, or a path into JSON data
8
+ # (``app_label.model_name.data.figures[].unit``): never exposed.
9
+ "EXCLUDE_FIELDS": [],
10
+ "MAX_PAGE_SIZE": 200,
11
+ "DEFAULT_PAGE_SIZE": 50,
12
+ # About one editing session; a new token for the next one
13
+ "TOKEN_LIFETIME": dt.timedelta(hours=8),
14
+ # Staged uploads: unused ones are removed by ``f3api_cleanup``
15
+ "UPLOAD_LIFETIME": dt.timedelta(days=1),
16
+ "UPLOAD_STORAGE": "default",
17
+ }
18
+
19
+
20
+ def config():
21
+ return DEFAULTS | getattr(settings, "FEINCMS3_API", {})
22
+
23
+
24
+ def excluded_paths(type_label):
25
+ """The excluded paths of one type, without the type: ``["data.unit"]``"""
26
+ prefix = f"{type_label}."
27
+ return [
28
+ entry.strip().removeprefix(prefix)
29
+ for entry in config()["EXCLUDE_FIELDS"]
30
+ if entry.strip().startswith(prefix)
31
+ ]