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
@@ -0,0 +1,428 @@
1
+ """
2
+ Datasets for translatable content
3
+
4
+ Each function returns a feincms3-data dataset entry: ``f3dumpdata`` uses its
5
+ ``"specs"``, and the ``"autotranslate"`` key tells the rest of this package
6
+ what the dumped models are, see ``registry()``.
7
+
8
+ The specs hold primary key lists (``pk__in``, ``parent__in``), so a dump
9
+ states exactly which rows it contains.
10
+ """
11
+
12
+ import dataclasses
13
+ from functools import cache, partial
14
+
15
+ from content_editor.models import PluginBase
16
+ from django.apps import apps
17
+ from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
18
+ from django.core.management.base import CommandError
19
+ from django.db import models
20
+
21
+ from feincms3_api.incubator.autotranslate.conf import first_language
22
+
23
+
24
+ @dataclasses.dataclass(frozen=True)
25
+ class Translatable:
26
+ """An object which is copied into the target language"""
27
+
28
+ model: type
29
+ # ``(model, name of its foreign key to the translatable model)``
30
+ related: tuple = ()
31
+ offline: dict | None = None
32
+ language_regions: tuple = ()
33
+ title_field: str = "title"
34
+ tree: bool = False
35
+
36
+
37
+ @dataclasses.dataclass(frozen=True)
38
+ class LanguageRegions:
39
+ """An object whose regions are languages, translated into itself"""
40
+
41
+ model: type
42
+ title_field: str = "title"
43
+ # Never copied, so neither of these applies.
44
+ related: tuple = ()
45
+ offline: dict | None = None
46
+ tree: bool = False
47
+
48
+
49
+ def label(model):
50
+ return model._meta.label_lower
51
+
52
+
53
+ def has_field(model, name):
54
+ try:
55
+ model._meta.get_field(name)
56
+ except FieldDoesNotExist:
57
+ return False
58
+ return True
59
+
60
+
61
+ @cache
62
+ def plugin_models(model):
63
+ """Every concrete django-content-editor plugin whose ``parent`` is ``model``"""
64
+ return [
65
+ m
66
+ for m in apps.get_models()
67
+ if issubclass(m, PluginBase)
68
+ and not m._meta.proxy
69
+ and m._meta.get_field("parent").related_model is model
70
+ ]
71
+
72
+
73
+ def specs_for_plugins(model, spec=None):
74
+ """feincms3-data specs for all plugins of ``model``"""
75
+ spec = {} if spec is None else spec
76
+ return [{**spec, "model": label(m)} for m in plugin_models(model)]
77
+
78
+
79
+ def _related(model, related):
80
+ result = []
81
+ for entry in related:
82
+ rel_model, name = entry if isinstance(entry, tuple) else (entry, None)
83
+ if name is None:
84
+ candidates = [
85
+ f.name
86
+ for f in rel_model._meta.concrete_fields
87
+ if f.many_to_one and f.related_model is model
88
+ ]
89
+ if len(candidates) != 1:
90
+ raise ImproperlyConfigured(
91
+ f"{label(rel_model)} needs exactly one foreign key to"
92
+ f" {label(model)}, it has {candidates!r}. Pass"
93
+ f" ({rel_model.__name__}, '<field name>') instead."
94
+ )
95
+ name = candidates[0]
96
+ result.append((rel_model, name))
97
+ return tuple(result)
98
+
99
+
100
+ def _check_language_regions(model):
101
+ keys = {region.key for region in getattr(model, "regions", ())}
102
+ if not keys:
103
+ raise ImproperlyConfigured(
104
+ f"{label(model)} has no regions, so it can't have a region per language."
105
+ )
106
+
107
+
108
+ def page_tree_dataset(page_model, *, language_regions=()):
109
+ for lr in language_regions:
110
+ _check_language_regions(lr)
111
+ config = Translatable(
112
+ model=page_model,
113
+ language_regions=tuple(language_regions),
114
+ tree=True,
115
+ )
116
+ return {"specs": partial(_page_specs, config), "autotranslate": config}
117
+
118
+
119
+ def object_dataset(
120
+ model, *, related=(), offline=None, language_regions=(), title_field="title"
121
+ ):
122
+ if not has_field(model, "language_code"):
123
+ raise ImproperlyConfigured(f"{label(model)} has no language_code field.")
124
+ if offline is None and not has_field(model, "is_active"):
125
+ raise ImproperlyConfigured(
126
+ f"{label(model)} has no is_active field. Its copies would go live"
127
+ f" the moment they're loaded; pass offline={{...}} to say how they"
128
+ f" stay offline."
129
+ )
130
+ for lr in language_regions:
131
+ _check_language_regions(lr)
132
+ config = Translatable(
133
+ model=model,
134
+ related=_related(model, related),
135
+ offline=offline,
136
+ language_regions=tuple(language_regions),
137
+ title_field=title_field,
138
+ )
139
+ return {"specs": partial(_object_specs, config), "autotranslate": config}
140
+
141
+
142
+ def language_regions_dataset(model, *, title_field="title"):
143
+ _check_language_regions(model)
144
+ config = LanguageRegions(model=model, title_field=title_field)
145
+ return {"specs": partial(_language_regions_specs, config), "autotranslate": config}
146
+
147
+
148
+ # Selection ##################################################################
149
+
150
+
151
+ @dataclasses.dataclass
152
+ class Selection:
153
+ argument: str
154
+ subtree: bool = False
155
+ lang: str = ""
156
+ site: str = ""
157
+
158
+
159
+ def parse_selection(argument):
160
+ """
161
+ ``Angebot``, ``Angebot|subtree``, ``*|lang=fr``, ``17|site=example.com``
162
+ """
163
+ argument, *flags = argument.split("|")
164
+ selection = Selection(argument=argument.strip(), lang=first_language())
165
+ for flag in flags:
166
+ key, sep, value = flag.partition("=")
167
+ if key == "subtree" and not sep:
168
+ selection.subtree = True
169
+ elif key in {"lang", "site"} and sep and value:
170
+ setattr(selection, key, value)
171
+ else:
172
+ raise CommandError(f"Unknown selection option {flag!r}.")
173
+ if not selection.argument:
174
+ raise CommandError("Select something: a title, a slug, a pk or '*'.")
175
+ return selection
176
+
177
+
178
+ def _describe(obj):
179
+ parts = [f"pk={obj.pk}"]
180
+ if site := getattr(obj, "site", None):
181
+ parts.append(f"site={getattr(site, 'host', site)}")
182
+ if language_code := getattr(obj, "language_code", None):
183
+ parts.append(f"language={language_code}")
184
+ if path := getattr(obj, "path", None):
185
+ parts.append(f"path={path}")
186
+ elif slug := getattr(obj, "slug", None):
187
+ parts.append(f"slug={slug}")
188
+ return f"{obj} ({', '.join(parts)})"
189
+
190
+
191
+ def resolve(queryset, argument, *, title_field):
192
+ """
193
+ Find one object: exact title, exact slug, or the pk if the argument is
194
+ all digits
195
+ """
196
+ model = queryset.model
197
+ lookups = []
198
+ if title_field and has_field(model, title_field):
199
+ lookups.append({title_field: argument})
200
+ if has_field(model, "slug"):
201
+ lookups.append({"slug": argument})
202
+ if argument.isdigit():
203
+ lookups.append({"pk": int(argument)})
204
+
205
+ for lookup in lookups:
206
+ if matches := list(queryset.filter(**lookup)[:11]):
207
+ break
208
+ else:
209
+ matches = []
210
+
211
+ if len(matches) == 1:
212
+ return matches[0]
213
+ name = model._meta.verbose_name
214
+ if matches:
215
+ raise CommandError(
216
+ f"Several {model._meta.verbose_name_plural} match {argument!r}:\n"
217
+ + "\n".join(f" {_describe(obj)}" for obj in matches)
218
+ )
219
+ candidates = (
220
+ list(queryset.filter(**{f"{title_field}__icontains": argument})[:10])
221
+ if title_field and has_field(model, title_field)
222
+ else []
223
+ )
224
+ raise CommandError(
225
+ f"No {name} matches {argument!r}."
226
+ + (
227
+ "\nDid you mean one of these?\n"
228
+ + "\n".join(f" {_describe(obj)}" for obj in candidates)
229
+ if candidates
230
+ else ""
231
+ )
232
+ )
233
+
234
+
235
+ def _filter_site(queryset, selection):
236
+ if not selection.site:
237
+ return queryset
238
+ if not has_field(queryset.model, "site"):
239
+ raise CommandError(f"{label(queryset.model)} has no site; drop site=.")
240
+ return queryset.filter(site__host=selection.site)
241
+
242
+
243
+ def _language_region_pks(model, pks, lr_model):
244
+ """Objects of ``lr_model`` the plugins of ``pks`` point at"""
245
+ result = set()
246
+ for plugin in plugin_models(model):
247
+ for field in plugin._meta.concrete_fields:
248
+ if field.many_to_one and field.related_model is lr_model:
249
+ result.update(
250
+ plugin._default_manager.filter(parent__in=pks)
251
+ .exclude(**{field.attname: None})
252
+ .values_list(field.attname, flat=True)
253
+ )
254
+ return sorted(result)
255
+
256
+
257
+ def _object_specs_for_pks(config, pks, lang):
258
+ specs = [
259
+ {"model": label(config.model), "filter": {"pk__in": pks}},
260
+ *specs_for_plugins(config.model, {"filter": {"parent__in": pks}}),
261
+ *(
262
+ {"model": label(rel_model), "filter": {f"{name}__in": pks}}
263
+ for rel_model, name in config.related
264
+ ),
265
+ ]
266
+ for lr_model in config.language_regions:
267
+ if lr_pks := _language_region_pks(config.model, pks, lr_model):
268
+ specs.extend(_language_regions_specs_for_pks(lr_model, lr_pks, lang))
269
+ return specs
270
+
271
+
272
+ def _page_specs(config, argument):
273
+ selection = parse_selection(argument)
274
+ queryset = _filter_site(
275
+ config.model._default_manager.filter(language_code=selection.lang),
276
+ selection,
277
+ )
278
+ if selection.argument == "*":
279
+ # Roots are never copied: /de/ can't become /fr/, which exists.
280
+ pages = queryset.exclude(parent=None)
281
+ else:
282
+ page = resolve(queryset, selection.argument, title_field="title")
283
+ pages = (
284
+ page.descendants(include_self=True)
285
+ if selection.subtree
286
+ else config.model._default_manager.filter(pk=page.pk)
287
+ ).exclude(parent=None)
288
+ if not pages.exists():
289
+ raise CommandError(
290
+ f"{_describe(page)} is a root page, and roots are never copied."
291
+ f" Select the pages below it with '*' or by their titles."
292
+ )
293
+ pks = sorted(pages.values_list("pk", flat=True))
294
+ return _object_specs_for_pks(config, pks, selection.lang)
295
+
296
+
297
+ def _object_specs(config, argument):
298
+ selection = parse_selection(argument)
299
+ if selection.subtree:
300
+ raise CommandError("Only pages have subtrees.")
301
+ queryset = _filter_site(
302
+ config.model._default_manager.filter(language_code=selection.lang),
303
+ selection,
304
+ )
305
+ if selection.argument == "*":
306
+ pks = sorted(queryset.values_list("pk", flat=True))
307
+ else:
308
+ pks = [resolve(queryset, selection.argument, title_field=config.title_field).pk]
309
+ return _object_specs_for_pks(config, pks, selection.lang)
310
+
311
+
312
+ def _language_regions_specs_for_pks(model, pks, lang):
313
+ return [
314
+ {"model": label(model), "filter": {"pk__in": pks}},
315
+ *specs_for_plugins(model, {"filter": {"parent__in": pks, "region": lang}}),
316
+ ]
317
+
318
+
319
+ def _language_regions_specs(config, argument):
320
+ selection = parse_selection(argument)
321
+ if selection.subtree or selection.site:
322
+ raise CommandError("Objects with a region per language take only lang=.")
323
+ queryset = config.model._default_manager.all()
324
+ if selection.argument == "*":
325
+ pks = set()
326
+ for plugin in plugin_models(config.model):
327
+ pks.update(
328
+ plugin._default_manager.filter(region=selection.lang).values_list(
329
+ "parent", flat=True
330
+ )
331
+ )
332
+ pks = sorted(pks)
333
+ else:
334
+ pks = [resolve(queryset, selection.argument, title_field=config.title_field).pk]
335
+ return _language_regions_specs_for_pks(config.model, pks, selection.lang)
336
+
337
+
338
+ # Registry ###################################################################
339
+
340
+
341
+ @dataclasses.dataclass(frozen=True)
342
+ class Registry:
343
+ # label -> Translatable
344
+ translatable: dict
345
+ # label -> LanguageRegions
346
+ language_regions: dict
347
+ # label -> (owner label, name of the foreign key to the owner)
348
+ children: dict
349
+
350
+ def models(self):
351
+ """Every model whose rows a run copies, owners first"""
352
+ result = [config.model for config in self.translatable.values()]
353
+ for label_ in self.children:
354
+ result.append(apps.get_model(label_))
355
+ return result
356
+
357
+
358
+ def build_registry(datasets):
359
+ translatable = {}
360
+ language_regions = {}
361
+ for name, dataset in datasets.items():
362
+ config = dataset.get("autotranslate")
363
+ if isinstance(config, Translatable):
364
+ key = label(config.model)
365
+ if key in translatable and translatable[key] != config:
366
+ raise ImproperlyConfigured(
367
+ f"{key} is registered by several datasets with different"
368
+ f" options (again in {name!r})."
369
+ )
370
+ translatable[key] = config
371
+ for lr in config.language_regions:
372
+ language_regions.setdefault(label(lr), LanguageRegions(model=lr))
373
+ elif isinstance(config, LanguageRegions):
374
+ language_regions[label(config.model)] = config
375
+
376
+ if both := set(translatable) & set(language_regions):
377
+ raise ImproperlyConfigured(
378
+ f"{sorted(both)!r} are registered as translatable objects and as"
379
+ f" objects with a region per language."
380
+ )
381
+
382
+ children = {}
383
+ for key, config in [*translatable.items(), *language_regions.items()]:
384
+ for plugin in plugin_models(config.model):
385
+ children[label(plugin)] = (key, "parent")
386
+ for rel_model, name in getattr(config, "related", ()):
387
+ children[label(rel_model)] = (key, name)
388
+ return Registry(
389
+ translatable=translatable,
390
+ language_regions=language_regions,
391
+ children=children,
392
+ )
393
+
394
+
395
+ @cache
396
+ def registry():
397
+ from feincms3_data.data import datasets # noqa: PLC0415
398
+
399
+ return build_registry(datasets())
400
+
401
+
402
+ def is_translated_field(field):
403
+ """A column of a django-translated-fields field (``title_de``)"""
404
+ return hasattr(field, "_translated_field_language_code")
405
+
406
+
407
+ def translated_fields(model):
408
+ """``{name: {language: attname}}`` for django-translated-fields fields"""
409
+ result = {}
410
+ for field in model._meta.concrete_fields:
411
+ if language := getattr(field, "_translated_field_language_code", None):
412
+ name = field.name.removesuffix(f"_{language.replace('-', '_')}")
413
+ result.setdefault(name, {})[language] = field.attname
414
+ return result
415
+
416
+
417
+ def has_unique_slug(model):
418
+ """Whether ``slug`` takes part in any uniqueness rule"""
419
+ meta = model._meta
420
+ if not has_field(model, "slug"):
421
+ return False
422
+ if meta.get_field("slug").unique:
423
+ return True
424
+ return any("slug" in fields for fields in meta.unique_together) or any(
425
+ "slug" in constraint.fields
426
+ for constraint in meta.constraints
427
+ if isinstance(constraint, models.UniqueConstraint)
428
+ )
@@ -0,0 +1,213 @@
1
+ """
2
+ Gate 2: which strings are sent to the translator
3
+
4
+ The default goes by field type: editable ``CharField`` and ``TextField``
5
+ without ``choices``, minus slugs, emails, URLs and a few names. Three
6
+ adapters add what model fields don't show: django-prose-editor fields are
7
+ HTML, django-json-schema-editor data contributes its string properties, and
8
+ feincms3-forms fields keep their names, values and defaults.
9
+
10
+ Link fields (``URLField``, ``url``, ``*_url``, ``LINK_FIELDS``) aren't
11
+ translated but mapped, see ``feincms3_api.incubator.autotranslate.links``.
12
+
13
+ Kinds, JSON schema walking and the path notation are the package's own,
14
+ see ``feincms3_api.kinds`` and ``feincms3_api.paths``.
15
+ """
16
+
17
+ import dataclasses
18
+ import re
19
+ from functools import cache
20
+
21
+ from django.apps import apps
22
+ from django.db import models
23
+
24
+ from feincms3_api.incubator.autotranslate.conf import config
25
+ from feincms3_api.incubator.autotranslate.datasets import is_translated_field, label
26
+ from feincms3_api.kinds import (
27
+ CHOICES,
28
+ HTML,
29
+ LINK,
30
+ MACHINE_NAMES as EXCLUDED_NAMES,
31
+ TEXT,
32
+ is_link_name as _is_link_name,
33
+ json_string_kind,
34
+ schema_paths as _schema_paths,
35
+ )
36
+
37
+
38
+ try:
39
+ from django_prose_editor.fields import ProseEditorField
40
+ except ImportError: # pragma: no cover
41
+ ProseEditorField = None
42
+
43
+ try:
44
+ from django_json_schema_editor.plugins import JSONPluginBase
45
+ except ImportError: # pragma: no cover
46
+ JSONPluginBase = None
47
+
48
+ try:
49
+ from feincms3_forms.models import FormFieldBase
50
+ except ImportError: # pragma: no cover
51
+ FormFieldBase = None
52
+
53
+
54
+ @dataclasses.dataclass(frozen=True)
55
+ class FieldSpec:
56
+ path: str
57
+ kind: str
58
+
59
+ @property
60
+ def field(self):
61
+ return re.split(r"[.\[]", self.path, maxsplit=1)[0]
62
+
63
+ def __str__(self):
64
+ if self.kind == HTML:
65
+ return f"{self.path} (html)"
66
+ if self.kind == CHOICES:
67
+ return f"{self.path} (labels)"
68
+ return self.path
69
+
70
+
71
+ def json_proxies(model):
72
+ """The django-json-schema-editor proxies of a concrete JSON plugin model"""
73
+ if JSONPluginBase is None or not issubclass(model, JSONPluginBase):
74
+ return []
75
+ return list(getattr(model, "_proxy_types_map", {}).values())
76
+
77
+
78
+ def _schema_specs(schema, prefix, *, is_link):
79
+ for path, sub in _schema_paths(schema, prefix):
80
+ if (kind := json_string_kind(path, sub, is_link=is_link)) in {
81
+ TEXT,
82
+ HTML,
83
+ LINK,
84
+ }:
85
+ yield FieldSpec(path, kind)
86
+
87
+
88
+ def _schema(model):
89
+ return getattr(model, "SCHEMA", None)
90
+
91
+
92
+ def all_paths(model):
93
+ """Every path below ``model``, for validating the settings"""
94
+ paths = set()
95
+ for field in model._meta.concrete_fields:
96
+ paths.add(field.name)
97
+ if field.name == "data" and (schema := _schema(model)):
98
+ paths.update(path for path, _schema_ in _schema_paths(schema, "data"))
99
+ return paths
100
+
101
+
102
+ def _entries(setting):
103
+ return {entry.strip() for entry in config()[setting]}
104
+
105
+
106
+ def _is_listed(entries, model, path):
107
+ return f"{label(model)}.{path}" in entries or (
108
+ model._meta.proxy and f"{label(model._meta.concrete_model)}.{path}" in entries
109
+ )
110
+
111
+
112
+ @cache
113
+ def field_specs(model):
114
+ """
115
+ The strings and links of ``model``; for a JSON plugin, ``model`` is one
116
+ of its proxies
117
+ """
118
+ link_fields = _entries("LINK_FIELDS")
119
+ exclude_fields = _entries("EXCLUDE_FIELDS")
120
+ is_form_field = FormFieldBase is not None and issubclass(model, FormFieldBase)
121
+
122
+ specs = []
123
+ for field in model._meta.concrete_fields:
124
+ name = field.name
125
+ if (
126
+ not field.editable
127
+ or field.primary_key
128
+ or field.is_relation
129
+ or is_translated_field(field)
130
+ ):
131
+ continue
132
+ if isinstance(field, models.URLField) or (
133
+ isinstance(field, models.CharField) and _is_link_name(name)
134
+ ):
135
+ specs.append(FieldSpec(name, LINK))
136
+ continue
137
+ if _is_listed(link_fields, model, name):
138
+ specs.append(FieldSpec(name, LINK))
139
+ continue
140
+ if name == "data" and (schema := _schema(model)):
141
+ specs.extend(
142
+ _schema_specs(
143
+ schema,
144
+ "data",
145
+ is_link=lambda path: _is_listed(link_fields, model, path),
146
+ )
147
+ )
148
+ continue
149
+ if (
150
+ field.choices
151
+ or name in EXCLUDED_NAMES
152
+ or isinstance(field, (models.SlugField, models.EmailField))
153
+ or not isinstance(field, (models.CharField, models.TextField))
154
+ ):
155
+ continue
156
+ if is_form_field:
157
+ # The name is the key submissions are stored under, and the
158
+ # default value of a choice field names a value.
159
+ if name in {"name", "default_value"}:
160
+ continue
161
+ if name == "choices":
162
+ specs.append(FieldSpec(name, CHOICES))
163
+ continue
164
+ if ProseEditorField is not None and isinstance(field, ProseEditorField):
165
+ specs.append(FieldSpec(name, HTML))
166
+ else:
167
+ specs.append(FieldSpec(name, TEXT))
168
+
169
+ # Excluded links are neither translated nor mapped.
170
+ return tuple(
171
+ spec for spec in specs if not _is_listed(exclude_fields, model, spec.path)
172
+ )
173
+
174
+
175
+ def specs_for_raw(raw):
176
+ """The field specs of one serialized object of a dump"""
177
+ model = apps.get_model(raw["model"])
178
+ if proxies := json_proxies(model):
179
+ type = raw["fields"].get("type")
180
+ model = next((p for p in proxies if type == p.TYPE), model)
181
+ return field_specs(model)
182
+
183
+
184
+ def listed_models(registry):
185
+ """The models ``--list-fields`` prints, JSON plugins as their proxies"""
186
+ result = []
187
+ for model in registry.models():
188
+ if proxies := json_proxies(model):
189
+ result.extend(proxies)
190
+ else:
191
+ result.append(model)
192
+ return sorted(set(result), key=label)
193
+
194
+
195
+ def list_fields(registry):
196
+ lines = []
197
+ for model in listed_models(registry):
198
+ specs = field_specs(model)
199
+ strings = [str(spec) for spec in specs if spec.kind != LINK]
200
+ links = [spec.path for spec in specs if spec.kind == LINK]
201
+ if not strings and not links:
202
+ continue
203
+ adapter = ""
204
+ if json_proxies(model._meta.concrete_model):
205
+ adapter = " (json-schema-editor)"
206
+ elif FormFieldBase is not None and issubclass(model, FormFieldBase):
207
+ adapter = " (feincms3-forms)"
208
+ lines.append(f"{label(model)}{adapter}")
209
+ if strings:
210
+ lines.append(f" {', '.join(strings)}")
211
+ if links:
212
+ lines.append(f" links: {', '.join(links)}")
213
+ return "\n".join(lines) + "\n"