def ajax_add_tag_to_store(request, store_pk): """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.")) store = get_object_or_404(Store, pk=store_pk) add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): new_tag_like_object = add_tag_form.save() return _add_tag(request, store, new_tag_like_object) else: # If the form is invalid, perhaps it is because the tag already exists, # so check if the tag exists. try: criteria = { 'name': add_tag_form.data['name'], 'slug': add_tag_form.data['slug'], } if len(store.tags.filter(**criteria)) == 1: # If the tag is already applied to the store then avoid # reloading the page. return HttpResponse(status=204) elif len(store.goals.filter(**criteria)) == 1: # If the goal is already applied to the store then avoid # reloading the page. return HttpResponse(status=204) else: # Else add the tag (or goal) to the store. if criteria['name'].startswith("goal:"): tag_like_object = Goal.objects.get(**criteria) else: tag_like_object = Tag.objects.get(**criteria) return _add_tag(request, store, tag_like_object) except Exception: # If the form is invalid and the tag doesn't exist yet then display # the form with the error messages. context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('pootle-xhr-tag-store', args=[store.pk]) } return render_to_response('core/xhr_add_tag_form.html', context, RequestContext(request))
def ajax_add_tag_to_store(request, store_pk): """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.")) store = get_object_or_404(Store, pk=store_pk) add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): new_tag_like_object = add_tag_form.save() return _add_tag(request, store, new_tag_like_object) else: # If the form is invalid, perhaps it is because the tag already exists, # so check if the tag exists. try: criteria = { 'name': add_tag_form.data['name'], 'slug': add_tag_form.data['slug'], } if len(store.tags.filter(**criteria)) == 1: # If the tag is already applied to the store then avoid # reloading the page. return HttpResponse(status=204) elif len(store.goals.filter(**criteria)) == 1: # If the goal is already applied to the store then avoid # reloading the page. return HttpResponse(status=204) else: # Else add the tag (or goal) to the store. if criteria['name'].startswith("goal:"): tag_like_object = Goal.objects.get(**criteria) else: tag_like_object = Tag.objects.get(**criteria) return _add_tag(request, store, tag_like_object) except Exception: # If the form is invalid and the tag doesn't exist yet then display # the form with the error messages. context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('pootle-xhr-tag-store', args=[store.pk]) } return render_to_response('core/xhr_add_tag_form.html', context, RequestContext(request))
def ajax_add_tag_to_tp(request, translation_project): """Return an HTML snippet with the failed form or blank if valid.""" add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): 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 (or goal) # already exists, so check if the tag (or goal) exists. try: 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 = { 'language_code': translation_project.language.code, 'project_code': translation_project.project.code, } context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('pootle-xhr-tag-tp', kwargs=url_kwargs) } return render_to_response('core/xhr_add_tag_form.html', context, RequestContext(request))
def ajax_add_tag_to_tp(request, translation_project): """Return an HTML snippet with the failed form or blank if valid.""" add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): 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 (or goal) # already exists, so check if the tag (or goal) exists. try: 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 = { 'language_code': translation_project.language.code, 'project_code': translation_project.project.code, } context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('pootle-xhr-tag-tp', kwargs=url_kwargs) } return render_to_response('core/xhr_add_tag_form.html', context, RequestContext(request))
def ajax_add_tag_to_tp(request, translation_project): """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']) add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): 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 check if the tag exists. try: 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 = { 'language_code': translation_project.language.code, 'project_code': translation_project.project.code, } context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('tp.ajax_add_tag', kwargs=url_kwargs) } return render_to_response('common/xhr_add_tag_form.html', context, RequestContext(request))
def ajax_add_tag_to_tp(request, translation_project): """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']) add_tag_form = TagForm(request.POST) if add_tag_form.is_valid(): 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 check if the tag exists. try: 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 = { 'language_code': translation_project.language.code, 'project_code': translation_project.project.code, } context = { 'add_tag_form': add_tag_form, 'add_tag_action_url': reverse('tp.ajax_add_tag', kwargs=url_kwargs) } return render_to_response('common/xhr_add_tag_form.html', context, RequestContext(request))