Beispiel #1
0
 def test_default(self):
     """Test what happens when a project (or its outsource) are not
     in the gtranslate table.
     """
     self.assertTrue(is_gtranslate_allowed(self.project))
     outp = Project.objects.create(
         slug='outp', name='A new project'
     )
     self.project.outsource = outp
     self.project.save()
     self.assertTrue(is_gtranslate_allowed(self.project))
Beispiel #2
0
 def test_is_allowed(self):
     """Test if gtranslate is allowed for a particular project."""
     outp = Project.objects.create(slug='outsourced', name='outsourced')
     self.assertTrue(is_gtranslate_allowed(self.project))
     Gtranslate.objects.create(use_gtranslate=True, project=self.project)
     self.assertTrue(is_gtranslate_allowed(self.project))
     self.project.outsource = outp
     self.project.save()
     self.assertTrue(is_gtranslate_allowed(self.project))
     Gtranslate.objects.create(use_gtranslate=True, project=outp)
     self.assertTrue(is_gtranslate_allowed(self.project))
Beispiel #3
0
 def test_is_not_allowed(self):
     """Test if gtranslate is not allowed for a particular project."""
     outp = Project.objects.create(slug='outsource', name='outsource')
     g = Gtranslate.objects.create(use_gtranslate=False, project=self.project)
     self.assertFalse(is_gtranslate_allowed(self.project))
     self.project.outsource = outp
     self.project.save()
     self.assertFalse(is_gtranslate_allowed(self.project))
     Gtranslate.objects.create(use_gtranslate=False, project=outp)
     self.assertFalse(is_gtranslate_allowed(self.project))
     g1 = Gtranslate.objects.get(project=outp)
     g1.use_gtranslate=True
     g1.save()
     self.assertFalse(is_gtranslate_allowed(self.project))
     g.use_translate = True
     g.save()
     g1.use_translate = False
     g1.save()
     self.assertFalse(is_gtranslate_allowed(self.project))
Beispiel #4
0
def translate(request, project_slug, lang_code, resource_slug=None,
                     *args, **kwargs):
    """
    Main lotte view.
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project) and not\
        check.maintain(project):
        return permission_denied(request)

    resources = []
    if resource_slug:
        resource_list = [get_object_or_404(Resource, slug=resource_slug,
            project=project)]
    else:
        resource_list = Resource.objects.filter(project=project)

        # Return a page explaining that the project has multiple source langs and
        # cannot be translated as a whole.
        if resource_list.values('source_language').distinct().count() > 1:
            messages.info(request,
                          "There are multiple source languages for this project. "
                          "You will only be able to translate resources for one "
                          "source language at a time.")
            return HttpResponseRedirect(reverse('project_detail',
                                        args=[project_slug]),)

    # Filter resources that are not accepting translations
    for resource in resource_list:
        if resource.accept_translations:
            resources.append(resource)

    # If no resource accepting translations, raise a 403
    if not resources:
        return permission_denied(request)

    target_language = Language.objects.by_code_or_alias_or_404(lang_code)

    # If it is an attempt to edit the source language, redirect the user to
    # resource_detail and show him a message explaining the reason.
    if target_language == get_source_language(resources):
        messages.error(request,
                       "Cannot edit the source language because this would "
                       "result in translation mismatches! If you want to "
                       "update the source strings consider using the transifex "
                       "command-line client.")
        if resource_slug:
            return HttpResponseRedirect(reverse('resource_detail',
                                                args=[project_slug,
                                                      resource_slug]),)
        else:
            return HttpResponseRedirect(reverse('project_detail',
                                                args=[project_slug]),)

    total_strings = SourceEntity.objects.filter(
        resource__in = resources).count()

    translated_strings = Translation.objects.filter(
        source_entity__resource__in = resources,
        language = target_language,
        source_entity__pluralized=False,
        rule = 5).count()

    # Include counting of pluralized entities
    for pluralized_entity in SourceEntity.objects.filter(resource__in = resources,
                                                         pluralized=True):
        plurals_translated = Translation.objects.filter(
            language=target_language,
            source_entity=pluralized_entity).count()
        if plurals_translated == len(target_language.get_pluralrules()):
            translated_strings += 1

    if len(resources) > 1:
        translation_resource = None
    else:
        translation_resource = resources[0]

    contributors = User.objects.filter(pk__in=Translation.objects.filter(
        source_entity__resource__in = resources,
        language = target_language,
        rule = 5).values_list("user", flat=True))

    lotte_init.send(None, request=request, resources=resources,
        language=target_language)

    return render_to_response("translate.html",
        { 'project' : project,
          'resource' : translation_resource,
          'target_language' : target_language,
          'translated_strings': translated_strings,
          'untranslated_strings': total_strings - translated_strings,
          'contributors': contributors,
          'resources': resources,
          'resource_slug': resource_slug,
          'languages': Language.objects.all(),
          'gtranslate': is_gtranslate_allowed(project),
          'spellcheck_supported_langs' : SPELLCHECK_SUPPORTED_LANGS,
        },
        context_instance = RequestContext(request))