Example #1
0
def edit(request, project_slug, string_id):
    string = get_object_or_404(String, id=string_id)
    project = get_object_or_404(Project, slug=project_slug)
    base_locale_translation = Translation.objects.order_by('-date_created').get(string=string, locale=project.base_locale)        
    target_locale_translation = Translation.objects.order_by('-date_created').get(string=string, locale=request.session['target_locale'])
    versions = Version.objects.get_for_object(target_locale_translation).order_by('-id')

    if request.method == 'POST':
        string_form = StringForm(request.POST, instance=string)
        base_locale_translation_form = TranslationForm(request.POST, instance=base_locale_translation, prefix="base-locale")
        target_locale_translation_form = TranslationForm(request.POST, instance=target_locale_translation, prefix="target-locale")
        messages = []

        if string_form.is_valid() and base_locale_translation_form.is_valid() and target_locale_translation_form.is_valid():
            string = string_form.save(commit=False)
            string.project = project
            string.save()

            base_locale_translation = base_locale_translation_form.save(commit=False)
            base_locale_translation.string = string
            with reversion.revision:
                base_locale_translation.save()

            target_locale_translation = target_locale_translation_form.save(commit=False)
            target_locale_translation.string = string
            with reversion.revision:
                target_locale_translation.save()

        return http.HttpResponseRedirect(
            reverse('projects_show', kwargs=dict(slug=project.slug)))
    else:
        string_form = StringForm(instance=string)
        base_locale_translation_form = TranslationForm(instance=base_locale_translation, prefix="base-locale")
        target_locale_translation_form = TranslationForm(instance=target_locale_translation, prefix="target-locale")
        return render_to_response('strings/edit.html', {
            'string': string,
            'project': project,
            'string_form': string_form,
            'base_locale_translation_form': base_locale_translation_form,
            'target_locale_translation_form': target_locale_translation_form,
            'versions': versions,
        }, context_instance=RequestContext(request))
Example #2
0
def create(request, project_slug):
    assert request.method == 'POST'
    project = get_object_or_404(Project, slug=project_slug)

    string_form = StringForm(request.POST)
    base_locale_translation_form = TranslationForm(request.POST, prefix='base-locale')
    target_locale_translation_form = TranslationForm(request.POST, prefix='target-locale')
    messages = []
    if string_form.is_valid() and base_locale_translation_form.is_valid() and target_locale_translation_form.is_valid():
        string = string_form.save(commit=False)
        string.project = project
        string.save()

        base_locale_translation = base_locale_translation_form.save(commit=False)
        base_locale_translation.string = string
        base_locale_translation.save()

      # TODO: target should be optional when creating a new string.
        target_locale_translation = target_locale_translation_form.save(commit=False)
        target_locale_translation.string = string
        target_locale_translation.save()

        # Create translation for all other languages.
        for lang in settings.LANGUAGES:
            if lang[0] != project.base_locale and (lang[0] != request.session['target_locale']):
                translation = Translation(text='', locale=lang[0])
                translation.string = string
                translation.save()

        messages.append(_('Your new string has been created.'))
        return http.HttpResponseRedirect(
            reverse('projects_show', kwargs=dict(slug=project.slug)))
    else:
        messages.append(_("There was a problem creating your string."))
    return render_to_response('strings/new.html', {
        'string_form': string_form,
        'base_locale_translation_form': base_locale_translation_form,
        'target_locale_translation_form': target_locale_translation_form,
        'project': project,
    }, context_instance=RequestContext(request))