Ticket 775 - Table of Contens (books listing)
[wolnelektury.git] / apps / modeltranslation / management / commands / update_translation_fields.py
1 # -*- coding: utf-8 -*-
2 from django.conf import settings
3 from django.core.management.base import (BaseCommand, CommandError,
4                                          NoArgsCommand)
5
6 from modeltranslation.translator import translator
7 from modeltranslation.utils import build_localized_fieldname
8
9
10 class Command(NoArgsCommand):
11     help = 'Updates the default translation fields of all or the specified' \
12            'translated application using the value of the original field.'
13
14     def handle(self, **options):
15         default_lang = settings.LANGUAGES[0][0]
16         print "Using default language:", default_lang
17
18         for model, trans_opts in translator._registry.items():
19             print "Updating data of model '%s'" % model
20             for obj in model.objects.all():
21                 for fieldname in trans_opts.fields:
22                     def_lang_fieldname = \
23                         build_localized_fieldname(fieldname, default_lang)
24                     #print "setting %s from %s to %s." % \
25                           #(def_lang_fieldname, fieldname,
26                            #obj.__dict__[fieldname])
27                     if not getattr(obj, def_lang_fieldname):
28                         setattr(obj, def_lang_fieldname,
29                                 obj.__dict__[fieldname])
30                 obj.save()