def translations_management_backfill_view(request): """Takes start and end dates and a model and backfills translations""" date_start = smart_date(request.POST.get('date_start')) date_end = smart_date(request.POST.get('date_end')) model_path = smart_str(request.POST.get('model')) if request.method == 'POST' and date_start and date_end and model_path: # NB: We just let the errors propagate because this is an # admin page. That way we get a traceback and all that detail. # We add one day to date_end so that it picks up the entire day of # date_end. # # FIXME: We should do this in a less goofy way. date_end = date_end + timedelta(days=1) model_cls = import_by_path(model_path) # Get list of ids of all objects that need translating. id_list = list( model_cls.objects.need_translations( date_start=date_start, date_end=date_end ) .values_list('id', flat=True) ) num = len(id_list) CHUNK_SIZE = 100 # Need to generate a bunch of separate tasks because they take # a long time to run, so we do CHUNK_SIZE per task. while id_list: chunk = id_list[:CHUNK_SIZE] id_list = id_list[CHUNK_SIZE:] translate_tasks_by_id_list.delay(model_path, chunk) messages.success( request, u'Task created to backfill translations for %s instances' % num ) return HttpResponseRedirect(request.path) from fjord.translations.tasks import REGISTERED_MODELS model_classes = [ cls.__module__ + '.' + cls.__name__ for cls in REGISTERED_MODELS ] return render(request, 'admin/translations_backfill.html', { 'title': 'Translations - General Maintenance - Backfill', 'settings': settings, 'model_classes': model_classes, 'date_start': request.POST.get('date_start', ''), 'date_end': request.POST.get('date_end', ''), 'model': request.POST.get('model', '') })
def translations_management_backfill_view(request): """Takes start and end dates and a model and backfills translations""" date_start = smart_date(request.POST.get('date_start')) date_end = smart_date(request.POST.get('date_end')) model_path = smart_str(request.POST.get('model')) if request.method == 'POST' and date_start and date_end and model_path: # NB: We just let the errors propagate because this is an # admin page. That way we get a traceback and all that detail. # We add one day to date_end so that it picks up the entire day of # date_end. # # FIXME: We should do this in a less goofy way. date_end = date_end + timedelta(days=1) model_cls = import_by_path(model_path) # Get list of ids of all objects that need translating. id_list = list( model_cls.objects.need_translations(date_start=date_start, date_end=date_end).values_list( 'id', flat=True)) num = len(id_list) CHUNK_SIZE = 100 # Need to generate a bunch of separate tasks because they take # a long time to run, so we do CHUNK_SIZE per task. while id_list: chunk = id_list[:CHUNK_SIZE] id_list = id_list[CHUNK_SIZE:] translate_tasks_by_id_list.delay(model_path, chunk) messages.success( request, u'Task created to backfill translations for %s instances' % num) return HttpResponseRedirect(request.path) from fjord.translations.tasks import REGISTERED_MODELS model_classes = [ cls.__module__ + '.' + cls.__name__ for cls in REGISTERED_MODELS ] return render( request, 'admin/translations_backfill.html', { 'title': 'Translations - General Maintenance - Backfill', 'settings': settings, 'model_classes': model_classes, 'date_start': request.POST.get('date_start', ''), 'date_end': request.POST.get('date_end', ''), 'model': request.POST.get('model', '') })
def test_one(self): """Test the basic case""" model_path = SuperModel.__module__ + '.' + SuperModel.__name__ obj = SuperModel(locale='br', desc=u'This is a test string') obj.save() # Verify no translation, yet assert obj.trans_desc == u'' translate_tasks_by_id_list.delay(model_path, [obj.id]) # Fetch the object from the db to verify it's been translated. obj = SuperModel.objects.get(id=obj.id) assert obj.trans_desc == u'THIS IS A TEST STRING'
def test_many(self): model_path = SuperModel.__module__ + '.' + SuperModel.__name__ objs = [] for i in range(50): obj = SuperModel(locale='br', desc=u'string %d' % i) obj.save() objs.append(obj) translate_tasks_by_id_list.delay( model_path, [obj_.id for obj_ in objs]) for obj in objs: obj = SuperModel.objects.get(id=obj.id) # Note: The fake translation just uppercases things. We're # abusing inner knowledge of that here. assert obj.trans_desc == obj.desc.upper()