Beispiel #1
0
def _post_init(sender, instance, **kwargs):
    fields = REGISTRY[sender]
    for field in fields.values():
        lang_code = get_current_language()
        fname = get_real_fieldname(field.name, lang_code)
        if getattr(instance, fname):
            setattr(instance, field.name, getattr(instance, fname))
Beispiel #2
0
 def __get__(self, instance, owner):
     return 'NOP'
     lang_code = get_current_language()
     key = instance.__dict__[self.name]
     if not key:
         return u''
     return KeyValue.objects.lookup(key, lang_code)
Beispiel #3
0
    def get_current_language():
        """
        Get the current language
        """
        lang = translation.get_language() or 'en'
        current = [l[0] for l in settings.LANGUAGES if l[0] == lang]
        if len(current) == 0:
            lang = lang.split('-')[0]
            current = [l[0] for l in settings.LANGUAGES if l[0] == lang]
        if len(current) == 0:
            # Fallback to default language code
            return get_default_language()
        return current[0]


current_language = get_current_language()[:2]


def get_activity_items(recipe_ids=None):
    if recipe_ids:
        activity_items = []
        ids = []
        for recipe_id in recipe_ids:
            recipe = xapi_recipes[recipe_id]
            activity_ids = recipe['activity_types']
            for activity_id in activity_ids:
                if not activity_id in ids:
                    activity_items.append(
                        [activity_id, xapi_activities[activity_id]])
                    ids.append(activity_id)
    else:
Beispiel #4
0
def translate(request, model, pk, app_label=''):
    languages = getattr(settings, 'LANGUAGES', [])
    if len(languages) < 2:
        raise Http404(u'No multiple languages in setting.')
    user = request.user
    context = {}
    action_url = '/datatrans/translate/%s/%s/' % (model, pk)
    content_type = ContentType.objects.get(model=model)
    model_class = content_type.model_class()
    registry = utils.get_registry()
    if not model_class in registry:
        raise Http404(u'No registered model found for given query.')
    translatable_fields = registry[model_class]
    object = model_class.objects.get(pk=pk)
    can_translate_method = getattr(object, 'can_translate', None)
    assert not can_translate_method or object.can_translate(request)
    model_entry = translate_map[model]
    url_pattern = model_entry[0]
    url_field = model_entry[1]
    object_url = url_pattern % getattr(object, url_field)
    title_field = model_entry[2]
    object_title = getattr(object, title_field)
    edit_form_path = model_entry[3]
    components = edit_form_path.split('.')
    module = __import__(components[0])
    for component in components[1:]:
        module = getattr(module, component)
    edit_form_class = module
    edit_form = edit_form_class()
    translatable_fields = [
        field_name for field_name in edit_form.fields
        if field_name in translatable_fields
    ]
    if request.method == 'POST':
        post_dict = request.POST
        for key, value in post_dict.items():
            if key.startswith('csrf') or key.startswith('save'):
                continue
            if key.count('_'):
                splitted = key.split('_')
                language_code = splitted[-1]
                field_name = '_'.join(splitted[:-1])
                new_value = value
                keyvalues = KeyValue.objects.filter(
                    content_type=content_type,
                    object_id=int(pk),
                    field=field_name,
                    language=language_code).order_by('-updated')
                if keyvalues:
                    keyvalue = keyvalues[0]
                    value = keyvalue.value
                    if new_value:
                        keyvalue.value = new_value
                        keyvalue.edited = True
                        keyvalue.fuzzy = False
                        keyvalue.user = user
                        keyvalue.save()
                        if keyvalues.count() > 1:
                            for keyvalue in keyvalues[1:]:
                                keyvalue.delete()
                    else:
                        for keyvalue in keyvalues:
                            keyvalue.delete()
                else:
                    if new_value:
                        keyvalue = KeyValue(content_type=content_type,
                                            object_id=int(pk),
                                            field=field_name,
                                            language=language_code,
                                            value=new_value,
                                            updated=True,
                                            fuzzy=False)
                        keyvalue.user = user
                        keyvalue.save()
            else:
                raise Http404(u'?')
    original_language = object.original_language
    if original_language:
        try:
            user_languages = user.get_languages()
            languages = [
                l for l in languages
                if l[0] == original_language or l[0] in user_languages
            ]
        except:
            pass
    current_language = get_current_language()
    context['current_language_code'] = current_language
    context['current_language_name'] = dict(settings.LANGUAGES).get(
        current_language, _('unknown'))
    context['object'] = object
    context['original_language'] = original_language
    context['model'] = model
    context['model_title'] = model_class._meta.verbose_name
    context['object_title'] = object_title
    context['object_url'] = object_url
    if original_language:
        form_sets = []
        for field_name in translatable_fields:
            field_forms = []
            form_field = edit_form.fields[field_name]
            for language in languages:
                initial = {}
                language_code = language[0]
                translation_field_name = '%s_%s' % (field_name, language_code)
                if language_code == original_language:
                    value = object.__dict__[field_name]
                else:
                    keyvalues = KeyValue.objects.filter(
                        content_type=content_type,
                        object_id=int(pk),
                        field=field_name,
                        language=language_code).order_by('-updated')
                    if keyvalues:
                        keyvalue = keyvalues[0]
                        value = keyvalue.value
                    else:
                        value = None
                initial[translation_field_name] = value
                field_form = ModelTranslationForm(
                    form_field=form_field,
                    translation_field_name=translation_field_name,
                    initial=initial)
                field_forms.append([language_code, field_form])
            form_sets.append(field_forms)
        context['form_sets'] = form_sets
        context['action_url'] = action_url
    return render(request, 'datatrans/translate.html', context)