# -*- 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_i18n.models import News as i18n_News, \ NewsComplete as i18n_NewsComplete, \ NewsExtended as i18n_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 I18N 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'], }, }, #I18N Models 'i18n_News' : { 'languages':['en', 'fr',], 'model':i18n_News, 'overwrites':{ 'inherit':['News',], }, }, 'i18n_NewsComplete' : { 'languages':['en', 'fr',], 'model':i18n_NewsComplete, 'overwrites':{ 'inherit':['NewsComplete', 'i18n_News'], }, }, 'i18n_NewsExtended' : { 'languages':['en', 'fr',], 'model':i18n_NewsExtended, 'overwrites':{ 'inherit':['NewsExtended', 'i18n_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()