Newer
Older
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from myapp.models import News, NewsExpired, NewsSEO, NewsComplete, NewsExtended
from myapp_cms.models import News as cms_News, \
NewsComplete as cms_NewsComplete, \
NewsExtended as cms_NewsExtended
from myapp_hvad.models import News as hvad_News, \
NewsComplete as hvad_NewsComplete, \
NewsExtended as hvad_NewsExtended
from myapp_all.models import News as all_News, \
NewsComplete as all_NewsComplete, \
NewsExtended as all_NewsExtended
from cms.models.placeholdermodel import Placeholder
from cms.api import add_plugin
def get_fields_values(newsTypes, newsType, i, inherited):
fields_values={}
if newsType in inherited :
return fields_values
inherited.append(newsType)
for inheritedType in newsTypes[newsType]['overwrites']['inherit']:
fields_values.update(get_fields_values(newsTypes, inheritedType, i, []))
if i in newsTypes[newsType]['overwrites']:
fields_values.update(newsTypes[newsType]['overwrites'][i])
return fields_values
def create_tests_data(app=None):
#Translations of translatable fields
translations = {
'en':{
#default content is already in english
},
'fr':{
'newsbox_slug':'actualite-%d',
'newsbox_title':'Actualité %d',
'newsbox_summary':'Résumé de l\'actualité %d',
'newsbox_body':'Corps de l\'actualité %d',
'newsbox_meta_description':'Meta description de l\'actualité %d',
'newsbox_meta_keywords':'motclef1, motclef2, actualite-%d',
'content_field':'Contenu additionnel de l\'actualité %d',
'seo_field':'SEO additionnel de l\'actualité %d',
},
}
translatable_fields = translations['fr'].keys()
#array of available NewsTypes
#for each NewsType, we define a subarray with the model to use and
#default fields values in "overwrites" which is a subarray too :
# - inherit is a list of other NewsTypes to get default fields.from their conf
# - key 0 is the default values for all news
# - keys 1 to N is an array with values to overwrite for news 1 to N.
# Can also contain a "languages" list with language code for translations.
#Only used for HVAD Models. Translations are in "translations" variable.
newsTypes = {
'News' : {
'model':News,
'overwrites':{
'inherit':[],
#0 is for base values
0:{
'newsbox_slug':'news-%d',
'newsbox_title':'News %d',
'newsbox_date':'2014-02-22T10:00:00+02:00',
'newsbox_publication_start_date':'1985-07-02T10:00:00+02:00',
'newsbox_published':True,
'newsbox_summary':'Summary of the news %d',
'newsbox_body':'Body of the news %d',
},
1:{},
2:{'newsbox_published':False},
3:{'newsbox_publication_start_date':'2020-01-01T10:00:00+02:00'},
4:{'newsbox_body':''},
5:{'newsbox_published':False},
6:{'newsbox_date':'2005-07-15T10:00:00+02:00'},
7:{'newsbox_date':'2020-01-01T10:00:00+02:00'},
},
},
'NewsExpired' : {
'model':NewsExpired,
'overwrites':{
'inherit':['News',],
5:{
'newsbox_published':True,
'newsbox_publication_end_date':'2000-01-01T10:00:00+02:00',
},
},
},
'NewsSEO' : {
'model':NewsSEO,
'overwrites':{
'inherit':['News',],
0:{
'newsbox_indexed':True,
'newsbox_meta_description':'Meta description of the news %d',
'newsbox_meta_keywords':'keyword1, keyword2, news-%d',
},
6:{
'newsbox_indexed':False,
},
},
},
'NewsComplete' : {
'model':NewsComplete,
'overwrites':{
'inherit':['NewsExpired', 'NewsSEO'],
},
},
'NewsExtended' : {
'model':NewsExtended,
'overwrites':{
'inherit':['NewsComplete'],
0:{
'general_field':'Test',
'content_field':'Extra content of the news %d',
'seo_field':'Extra SEO of the news %d',
},
}
},
#CMS Models
'cms_News' : {
'model':cms_News,
'overwrites':{
'inherit':['News',],
0:{
'newsbox_summary':'<p>Summary of the news %d</p>',
'newsbox_body':'<p>Body of the news %d</p>',
},
},
},
'cms_NewsComplete' : {
'model':cms_NewsComplete,
'overwrites':{
'inherit':['NewsComplete', 'cms_News'],
},
},
'cms_NewsExtended' : {
'model':cms_NewsExtended,
'overwrites':{
'inherit':['cms_NewsComplete'],
},
},
#HVAD Models
'hvad_News' : {
'languages':['en', 'fr',],
'model':hvad_News,
'overwrites':{
'inherit':['News',],
},
},
'hvad_NewsComplete' : {
'languages':['en', 'fr',],
'model':hvad_NewsComplete,
'overwrites':{
'inherit':['NewsComplete', 'hvad_News'],
},
},
'hvad_NewsExtended' : {
'languages':['en', 'fr',],
'model':hvad_NewsExtended,
'overwrites':{
'inherit':['NewsExtended', 'hvad_NewsComplete'],
},
},
#ALL Models
'all_News' : {
'languages':['en', 'fr',],
'model':all_News,
'overwrites':{
'inherit':['News',],
0:{
'newsbox_summary':'<p>Summary of the news %d</p>',
'newsbox_body':'<p>Body of the news %d</p>',
},
},
},
'all_NewsComplete' : {
'languages':['en', 'fr',],
'model':all_NewsComplete,
'overwrites':{
'inherit':['NewsComplete', 'all_News'],
},
},
'all_NewsExtended' : {
'languages':['en', 'fr',],
'model':all_NewsExtended,
'overwrites':{
'inherit':['NewsExtended', 'all_NewsComplete'],
},
},
}
nb_news_a_creer = len(newsTypes['News']['overwrites'])-2
for newsType, infos in newsTypes.items():
if app and app +'_' not in newsType:
continue
base_kwargs = get_fields_values(newsTypes, newsType, 0, [])
print '\n%s\n' % newsType + '-' * len(newsType)
for i in range(1, nb_news_a_creer+1):
if not 'languages' in infos:
infos['languages'] = [None,]
for lang in infos['languages']:
kwargs = base_kwargs.copy()
kwargs.update(get_fields_values(newsTypes, newsType, i, []))
#Retrieve translated values if a lang is specified
if lang and lang in translations and translations[lang]:
for k in translations[lang]:
if k in kwargs :
if kwargs[k][0:3] == '<p>':
kwargs[k] = '<p>'+translations[lang][k]+'</p>'
else:
kwargs[k] = translations[lang][k]
#Add the "nth" informations for fields which need it
for k in kwargs:
if type(kwargs[k]) in (str, unicode) and '%d' in kwargs[k]:
kwargs[k] = kwargs[k] % i
try:
#Transform field into placeholders if needed
ph_names = getattr(infos['model']._meta, 'placeholder_field_names')
for ph_name in ph_names:
if not ph_name in kwargs:
continue
ph = Placeholder.objects.create(slot=ph_name)
add_plugin(ph, 'TextPlugin', lang if lang != None else 'en', body=kwargs[ph_name])
kwargs[ph_name] = ph
except AttributeError:
pass
translatable_values = {k:v
for k, v in kwargs.items()
if k in translatable_fields}
untranslatable_fields = list(set(kwargs) - set(translatable_fields))
untranslatable_values = {k:v
for k, v in kwargs.items()
if k in untranslatable_fields}
langstr = ' (%s)' % lang if lang else ''
print ' - %d%s : %s' % (i, langstr, kwargs['newsbox_title'])
if lang == infos['languages'][0]:
news = infos['model'](**untranslatable_values)
if lang :
news.translate(lang)
for k in translatable_values:
setattr(news, k, translatable_values[k])
news.save()