some fixes
[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 from optparse import make_option
6
7 from modeltranslation.translator import translator
8 from modeltranslation.utils import build_localized_fieldname
9
10
11 class Command(NoArgsCommand):
12     help = 'Updates the default translation fields of all or the specified' \
13            'translated application using the value of the original field.'
14
15     option_list = BaseCommand.option_list + (
16         make_option('-d', '--default', action='store', dest='default', default=None), )
17
18
19     def handle(self, **options):
20         if options['default']:
21             default_lang = options['default']
22         else:
23             default_lang = settings.LANGUAGES[0][0]
24         print "Using default language:", default_lang
25
26         for model, trans_opts in translator._registry.items():
27             print "Updating data of model '%s'" % model
28             for obj in model.objects.all():
29                 for fieldname in trans_opts.fields:
30                     def_lang_fieldname = \
31                         build_localized_fieldname(fieldname, default_lang)
32                     #print "setting %s from %s to %s." % \
33                           #(def_lang_fieldname, fieldname,
34                            #obj.__dict__[fieldname])
35                     if not getattr(obj, def_lang_fieldname):
36                         setattr(obj, def_lang_fieldname,
37                                 obj.__dict__[fieldname])
38                 obj.save()