예제 #1
0
파일: views.py 프로젝트: JMassapina/pootle
def ajax_add_tag_to_tp_in_project(request, project):
    """Return an HTML snippet with the failed form or blank if valid."""

    add_tag_form = TranslationProjectTagForm(request.POST, project=project)

    if add_tag_form.is_valid():
        translation_project = add_tag_form.cleaned_data['translation_project']
        new_tag_like_object = add_tag_form.save()
        return _add_tag(request, translation_project, new_tag_like_object)
    else:
        # If the form is invalid, perhaps it is because the tag already
        # exists, so instead of creating the tag just retrieve it and add
        # it to the translation project.
        try:
            # Try to retrieve the translation project.
            kwargs = {
                'pk': add_tag_form.data['translation_project'],
            }
            translation_project = TranslationProject.objects.get(**kwargs)

            # Check if the tag (or goal) is already added to the translation
            # project, or try adding it.
            criteria = {
                'name': add_tag_form.data['name'],
                'slug': add_tag_form.data['slug'],
            }
            if len(translation_project.tags.filter(**criteria)) == 1:
                # If the tag is already applied to the translation project then
                # avoid reloading the page.
                return HttpResponse(status=204)
            elif len(translation_project.goals.filter(**criteria)) == 1:
                # If the goal is already applied to the translation project
                # then avoid reloading the page.
                return HttpResponse(status=204)
            else:
                # Else add the tag (or goal) to the translation project.
                if criteria['name'].startswith("goal:"):
                    tag_like_object = Goal.objects.get(**criteria)
                else:
                    tag_like_object = Tag.objects.get(**criteria)
                return _add_tag(request, translation_project, tag_like_object)
        except Exception:
            # If the form is invalid and the tag (or goal) doesn't exist yet
            # then display the form with the error messages.
            url_kwargs = {
                'project_code': project.code,
            }
            context = {
                'add_tag_form':
                add_tag_form,
                'add_tag_action_url':
                reverse('pootle-xhr-tag-tp-in-project', kwargs=url_kwargs)
            }
            return render_to_response('core/xhr_add_tag_form.html', context,
                                      RequestContext(request))
예제 #2
0
파일: views.py 프로젝트: cmpitg/pootle
def ajax_add_tag_to_tp_in_project(request, project_code):
    """Return an HTML snippet with the failed form or blank if valid."""

    if not check_permission('administrate', request):
        raise PermissionDenied(_("You do not have rights to add tags."))

    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    project = get_object_or_404(Project, code=project_code)

    add_tag_form = TranslationProjectTagForm(request.POST, project=project)

    if add_tag_form.is_valid():
        translation_project = add_tag_form.cleaned_data['translation_project']
        new_tag = add_tag_form.save()
        return _add_tag(request, translation_project, new_tag)
    else:
        # If the form is invalid, perhaps it is because the tag already
        # exists, so instead of creating the tag just retrieve it and add
        # it to the translation project.
        try:
            # Try to retrieve the translation project.
            kwargs = {
                'pk': add_tag_form.data['translation_project'],
            }
            translation_project = TranslationProject.objects.get(**kwargs)

            # Check if the tag is already added to the translation project, or
            # try adding it.
            criteria = {
                'name': add_tag_form.data['name'],
                'slug': add_tag_form.data['slug'],
            }
            if len(translation_project.tags.filter(**criteria)) == 1:
                # If the tag is already applied to the translation project then
                # avoid reloading the page.
                return HttpResponse(status=204)
            else:
                # Else add the tag to the translation project.
                tag = Tag.objects.get(**criteria)
                return _add_tag(request, translation_project, tag)
        except Exception:
            # If the form is invalid and the tag doesn't exist yet then display
            # the form with the error messages.
            url_kwargs = {
                'project_code': project.code,
            }
            context = {
                'add_tag_form': add_tag_form,
                'add_tag_action_url': reverse('project.ajax_add_tag_to_tp',
                                              kwargs=url_kwargs)
            }
            return render_to_response('common/xhr_add_tag_form.html',
                                      context, RequestContext(request))
예제 #3
0
파일: views.py 프로젝트: Chipcius/pootle
def ajax_add_tag_to_tp_in_project(request, project):
    """Return an HTML snippet with the failed form or blank if valid."""

    add_tag_form = TranslationProjectTagForm(request.POST, project=project)

    if add_tag_form.is_valid():
        translation_project = add_tag_form.cleaned_data['translation_project']
        new_tag_like_object = add_tag_form.save()
        return _add_tag(request, translation_project, new_tag_like_object)
    else:
        # If the form is invalid, perhaps it is because the tag already
        # exists, so instead of creating the tag just retrieve it and add
        # it to the translation project.
        try:
            # Try to retrieve the translation project.
            kwargs = {
                'pk': add_tag_form.data['translation_project'],
            }
            translation_project = TranslationProject.objects.get(**kwargs)

            # Check if the tag (or goal) is already added to the translation
            # project, or try adding it.
            criteria = {
                'name': add_tag_form.data['name'],
                'slug': add_tag_form.data['slug'],
            }
            if len(translation_project.tags.filter(**criteria)) == 1:
                # If the tag is already applied to the translation project then
                # avoid reloading the page.
                return HttpResponse(status=204)
            elif len(translation_project.goals.filter(**criteria)) == 1:
                # If the goal is already applied to the translation project
                # then avoid reloading the page.
                return HttpResponse(status=204)
            else:
                # Else add the tag (or goal) to the translation project.
                if criteria['name'].startswith("goal:"):
                    tag_like_object = Goal.objects.get(**criteria)
                else:
                    tag_like_object = Tag.objects.get(**criteria)
                return _add_tag(request, translation_project, tag_like_object)
        except Exception:
            # If the form is invalid and the tag (or goal) doesn't exist yet
            # then display the form with the error messages.
            url_kwargs = {
                'project_code': project.code,
            }
            context = {
                'add_tag_form': add_tag_form,
                'add_tag_action_url': reverse('project.ajax_add_tag_to_tp',
                                              kwargs=url_kwargs)
            }
            return render_to_response('common/xhr_add_tag_form.html',
                                      context, RequestContext(request))
예제 #4
0
파일: views.py 프로젝트: qdinar/pootle
def ajax_add_tag_to_tp_in_project(request, project):
    """Return an HTML snippet with the failed form or blank if valid."""

    add_tag_form = TranslationProjectTagForm(request.POST, project=project)

    if add_tag_form.is_valid():
        translation_project = add_tag_form.cleaned_data["translation_project"]
        new_tag_like_object = add_tag_form.save()
        return _add_tag(request, translation_project, new_tag_like_object)
    else:
        # If the form is invalid, perhaps it is because the tag already
        # exists, so instead of creating the tag just retrieve it and add
        # it to the translation project.
        try:
            # Try to retrieve the translation project.
            kwargs = {"pk": add_tag_form.data["translation_project"]}
            translation_project = TranslationProject.objects.get(**kwargs)

            # Check if the tag (or goal) is already added to the translation
            # project, or try adding it.
            criteria = {"name": add_tag_form.data["name"], "slug": add_tag_form.data["slug"]}
            if len(translation_project.tags.filter(**criteria)) == 1:
                # If the tag is already applied to the translation project then
                # avoid reloading the page.
                return HttpResponse(status=204)
            elif len(translation_project.goals.filter(**criteria)) == 1:
                # If the goal is already applied to the translation project
                # then avoid reloading the page.
                return HttpResponse(status=204)
            else:
                # Else add the tag (or goal) to the translation project.
                if criteria["name"].startswith("goal:"):
                    tag_like_object = Goal.objects.get(**criteria)
                else:
                    tag_like_object = Tag.objects.get(**criteria)
                return _add_tag(request, translation_project, tag_like_object)
        except Exception:
            # If the form is invalid and the tag (or goal) doesn't exist yet
            # then display the form with the error messages.
            url_kwargs = {"project_code": project.code}
            ctx = {
                "add_tag_form": add_tag_form,
                "add_tag_action_url": reverse("pootle-xhr-tag-tp-in-project", kwargs=url_kwargs),
            }
            return render(request, "core/xhr_add_tag_form.html", ctx)