1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
from django.apps import apps
from django.conf import settings
from django.utils.translation import activate, ugettext as _
import datetime
from jinja2 import Template
import os
from ishtar_common.version import get_version
from ishtar_common.models_common import Address, GeoItem
# TODO: à traduire
TYPES = {
'IntegerField': "Entier",
'PositiveIntegerField': "Entier positif",
'FloatField': "Nombre à virgule",
'TextField': "Texte",
'CharField': "Chaîne de caractères",
'DateField': "Date",
'DateTimeField': "Date/heure",
'EmailField': "Courriel",
'NullBooleanField': "Booléen",
'BooleanField': "Booléen",
'ImageField': "Image",
'FileField': "Fichier",
'URLField': "Adresse web",
'PointField': "Point",
'LineStringField': "Ligne",
'MultiPolygonField': "Multi-polygone",
}
TEMPLATES = (
("fr/source/_templates/annexe-tech-3-variables-gen.jinja",
"fr/source/annexe-tech-3-variables-gen.rst",
"fr-fr"),
)
TPL_MODEL = {
"fr-fr": """{% for field_name, desc in fields %}
- **{{field_name}}** : {{desc}}{% endfor %}"""
}
EXCLUDED = [
"id", "data", "history_creator", "history_m2m", "history_modifier",
"imports", "last_modified", "lock_user", "locked", "search_vector",
"uuid", "need_update", "merge_key", "merge_candidate", "merge_exclusion",
"archived", "ishtaruser", "profiles", "properties",
"relation_bitmap_image_above", "relation_bitmap_image_below",
"relation_dot", "relation_dot_above", "relation_dot_below",
"relation_image_above", "relation_image_below", "auto_external_id",
"history_date"
]
EXCLUDED += ["parcel_owner"] # temporary
MODELS = {
"ishtar_common": ["person", "organization", "author", "document"],
"archaeological_operations": ["operation", "archaeologicalsite",
"parcel", "administrativeact"],
"archaeological_files": ["file"],
"archaeological_context_records": ["dating", "contextrecord"],
"archaeological_finds": ["basefind", "find", "treatment", "treatmentfile"],
"archaeological_warehouse": ["warehouse", "container"],
}
MESSAGES = {
'value_only_message': _("Field only available for template generation")
}
def get_values(tpl_model, model, excluded, model_types, messages):
fields = {}
related_fields = {}
for field in model._meta.get_fields():
if field.name in excluded:
continue
help_text = ""
if getattr(field, "help_text", None):
h = field.help_text
if callable(h):
h = h()
help_text = str(h)
if getattr(field, "related_model", None):
key = field.name + "__"
rel = field.related_model
related_fields[key] = "*"
if getattr(field, "multiple", False):
related_fields[key] += "→ " + str(
rel._meta.verbose_name_plural)
else:
related_fields[key] += "→ " + str(
rel._meta.verbose_name)
extra = ""
if not getattr(field, "verbose_name", None) and \
hasattr(field.remote_field, "verbose_name"):
extra = str(field.remote_field.verbose_name).lower()
if hasattr(rel, "get_documentation_string"):
if extra:
extra += " - "
related_fields[key] += "* ({}{})".format(
extra,
rel.get_documentation_string())
elif extra:
related_fields[key] += " ({})*".format(extra)
else:
related_fields[key] += "*"
if getattr(field, "verbose_name", None):
related_fields[key] += " - " + str(field.verbose_name) + " "
if help_text:
if related_fields[key]:
related_fields[key] += " - "
related_fields[key] += help_text + " "
elif getattr(field, "verbose_name", None):
type_desc = ""
field_classes = [field.__class__.__name__] + [
sub.__name__ for sub in field.__class__.__subclasses__()]
for model_class in model_types:
if model_class in field_classes:
type_desc = model_types[model_class]
break
if getattr(field, "max_length", None):
type_desc += " ({})".format(field.max_length)
if not type_desc:
print("Missing type", field.name, field_classes)
fields[field.name] = str(field.verbose_name)
if type_desc:
fields[field.name] = "*{}* - {}".format(
type_desc, fields[field.name])
if help_text:
fields[field.name] += " - " + help_text
else:
print("No doc for the field: " + field.name)
if hasattr(model, "DOC_VALUES"):
for k, v in model.DOC_VALUES:
fields[k] = "{} - **{}**".format(
v, messages["value_only_message"])
fields = sorted(list(fields.items()) + list(related_fields.items()))
return {
"fields": [k for k, __ in fields],
"render": tpl_model.render({"fields": fields})
}
for template_name, template_name_dest, language in TEMPLATES:
tpl_model = Template(TPL_MODEL[language])
activate(language)
render_dct = dict((app, {}) for app in MODELS)
render_dct['current_date'] = datetime.date.today().strftime("%Y-%m-%d")
render_dct['version'] = get_version()
excluded = EXCLUDED[:]
address_values = get_values(tpl_model, Address, excluded=excluded,
model_types=TYPES, messages=MESSAGES)
render_dct["ishtar_common"]["address"] = address_values
excluded += address_values["fields"]
geo_values = get_values(tpl_model, GeoItem, excluded=excluded,
model_types=TYPES, messages=MESSAGES)
render_dct["ishtar_common"]["geoitem"] = geo_values
excluded += geo_values["fields"]
for app_name in MODELS:
# print([m._meta.model_name
# for m in apps.get_app_config(app_name).get_models()])
for model_name in MODELS[app_name]:
model = apps.get_app_config(app_name).get_model(model_name)
render_dct[app_name][model_name] = get_values(
tpl_model, model, excluded, TYPES, messages=MESSAGES)
template_name = os.path.abspath(
settings.ROOT_PATH + "../docs/" + template_name)
template_name_dest = os.path.abspath(
settings.ROOT_PATH + "../docs/" + template_name_dest)
with open(template_name, "r") as tpl:
rendered = Template(tpl.read())
with open(template_name_dest, "w") as tpl_dest:
tpl_dest.write(rendered.render(render_dct))
|