def edit_glossary(request, pk): term = get_object_or_404(Term, id=pk) if not term.check_perm(request.user, "glossary.edit"): raise PermissionDenied() if request.method == "POST": form = TermForm(term.glossary.project, data=request.POST, instance=term) if form.is_valid(): term.edit( request, form.cleaned_data["source"], form.cleaned_data["target"], form.cleaned_data["glossary"], ) return redirect_next( request.POST.get("next"), reverse( "show_glossary", kwargs={ "project": term.glossary.project.slug, "lang": term.language.code, }, ), ) else: form = TermForm(term.glossary.project, instance=term) last_changes = Change.objects.last_changes( request.user).filter(glossary_term=term)[:10] return render( request, "edit_glossary.html", { "title": dict_title(term.glossary, term.language), "project": term.glossary.project, "language": term.language, "form": form, "next": request.POST.get("next") or request.GET.get("next"), "last_changes": last_changes, "last_changes_url": urlencode(( ("project", term.glossary.project.slug), ("lang", term.language.code), ("action", Change.ACTION_DICTIONARY_NEW), ("action", Change.ACTION_DICTIONARY_EDIT), ("action", Change.ACTION_DICTIONARY_UPLOAD), )), }, )
def add_glossary_term(request, unit_id): unit = get_object_or_404(Unit, pk=int(unit_id)) component = unit.translation.component request.user.check_access_component(component) code = 403 results = "" details = "" terms = [] if request.user.has_perm("glossary.add", component.project): form = TermForm(unit, request.POST) if form.is_valid(): translation = form.cleaned_data["translation"] added = translation.add_unit(request, **form.as_kwargs()) terms = form.cleaned_data["terms"] terms.append(added.pk) code = 200 results = render_to_string( "snippets/glossary.html", { "glossary": ( # distinct is needed as get_glossary_terms is distict # and mixed queries can not be combined get_glossary_terms(unit) | translation.unit_set.filter(pk__in=terms).distinct()), "unit": unit, "user": request.user, }, ) else: messages = [] for error in form.non_field_errors(): messages.append(error) for field in form: for error in field.errors: messages.append( _("Error in parameter %(field)s: %(error)s") % { "field": field.name, "error": error }, ) details = "\n".join(messages) return JsonResponse( data={ "responseCode": code, "responseDetails": details, "results": results, "terms": ",".join(str(x) for x in terms), })
def add_glossary_term(request, unit_id): unit = get_object_or_404(Unit, pk=int(unit_id)) component = unit.translation.component request.user.check_access_component(component) code = 403 results = "" terms = [] if request.user.has_perm("glossary.add", component.project): form = TermForm(unit, request.POST) if form.is_valid(): translation = form.cleaned_data["translation"] context = "" suffix = 0 source = form.cleaned_data["source"] while translation.unit_set.filter(context=context, source=source).exists(): suffix += 1 context = str(suffix) translation.add_units( request, [(context, source, form.cleaned_data["target"])]) terms = form.cleaned_data["terms"] terms.append( translation.unit_set.get(context=context, source=source).pk) code = 200 results = render_to_string( "snippets/glossary.html", { "glossary": ( # distinct is needed as get_glossary_terms is distict # and mixed queries can not be combined get_glossary_terms(unit) | translation.unit_set.filter(pk__in=terms).distinct()), "unit": unit, "user": request.user, }, ) return JsonResponse( data={ "responseCode": code, "results": results, "terms": ",".join(str(x) for x in terms), })
def add_glossary_term(request, unit_id): unit = get_object_or_404(Unit, pk=int(unit_id)) request.user.check_access_component(unit.translation.component) component = unit.translation.component prj = component.project lang = unit.translation.language code = 403 results = "" terms = [] if request.user.has_perm("glossary.add", prj): form = TermForm(prj, request.POST) if form.is_valid(): term = Term.objects.create( request.user, language=lang, source=form.cleaned_data["source"], target=form.cleaned_data["target"], glossary=form.cleaned_data["glossary"], ) terms = form.cleaned_data["terms"] terms.append(term.id) code = 200 results = render_to_string( "snippets/glossary.html", { "glossary": (Term.objects.get_terms(unit).order() | Term.objects.for_project( prj, component.source_language).filter( pk__in=terms).order()), "unit": unit, "user": request.user, }, ) return JsonResponse( data={ "responseCode": code, "results": results, "terms": ",".join(str(x) for x in terms), })
def show_glossary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) if request.method == "POST" and request.user.has_perm("glossary.add", prj): form = TermForm(prj, request.POST) if form.is_valid(): Term.objects.create( request.user, language=lang, source=form.cleaned_data["source"], target=form.cleaned_data["target"], glossary=form.cleaned_data["glossary"], ) return redirect_next(request.POST.get("next"), request.get_full_path()) form = TermForm(prj,) uploadform = GlossaryUploadForm(prj) terms = Term.objects.for_project(prj).filter(language=lang).order() letterform = LetterForm(request.GET) searchform = OneTermForm(request.GET) if searchform.is_valid() and searchform.cleaned_data["term"] != "": terms = terms.filter(source__substring=searchform.cleaned_data["term"]) search = searchform.cleaned_data["term"] else: search = "" if letterform.is_valid() and letterform.cleaned_data["letter"] != "": terms = terms.filter(source__istartswith=letterform.cleaned_data["letter"]) letter = letterform.cleaned_data["letter"] else: letter = "" terms = get_paginator(request, terms) last_changes = ( Change.objects.last_changes(request.user) .filter(project=prj, language=lang) .exclude(glossary_term=None)[:10] )
def show_glossary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) if request.method == "POST" and request.user.has_perm("glossary.add", prj): form = TermForm(prj, request.POST) if form.is_valid(): Term.objects.create( request.user, language=lang, source=form.cleaned_data["source"], target=form.cleaned_data["target"], glossary=form.cleaned_data["glossary"], ) return redirect_next(request.POST.get("next"), request.get_full_path()) form = TermForm(prj) uploadform = GlossaryUploadForm(prj) terms = Term.objects.for_project(prj).filter(language=lang).order() letterform = LetterForm(request.GET) searchform = OneTermForm(request.GET) if searchform.is_valid() and searchform.cleaned_data["term"] != "": terms = terms.filter(source__substring=searchform.cleaned_data["term"]) search = searchform.cleaned_data["term"] else: search = "" if letterform.is_valid() and letterform.cleaned_data["letter"] != "": terms = terms.filter( source__istartswith=letterform.cleaned_data["letter"]) letter = letterform.cleaned_data["letter"] else: letter = "" terms = get_paginator(request, terms) last_changes = (Change.objects.last_changes(request.user).filter( project=prj, language=lang).exclude(glossary_term=None)[:10]) exporters = EXPORTERS.list_exporters_filter(EXPORT_TYPES) return render( request, "glossary.html", { "title": dict_title(prj, lang), "project": prj, "language": lang, "page_obj": terms, "exporters": exporters, "form": form, "query_string": urlencode({ "term": search, "letter": letter }), "uploadform": uploadform, "letterform": letterform, "searchform": searchform, "letter": letter, "last_changes": last_changes, "last_changes_url": urlencode(( ("project", prj.slug), ("lang", lang.code), ("action", Change.ACTION_DICTIONARY_NEW), ("action", Change.ACTION_DICTIONARY_EDIT), ("action", Change.ACTION_DICTIONARY_UPLOAD), )), }, )