Beispiel #1
0
 def __init__(self, project, post_data, user):
     self.project = project
     self.glossaries = (
         Glossary.objects.for_project(project).order_by("name").distinct()
     )
     self.data = {
         (item["term__language"], item["pk"]): item["term__count"]
         for item in self.glossaries.values("term__language", "pk").annotate(
             Count("term")
         )
     }
     try:
         self.post_id = int(post_data.get("edit_glossary", -1))
     except ValueError:
         self.post_id = -1
     self.languages = sort_objects(
         Language.objects.filter(translation__component__project=project).distinct()
     )
     self.forms = {
         glossary.id: GlossaryForm(
             user,
             glossary.project,
             post_data if self.post_id == glossary.id else None,
             instance=glossary,
             auto_id=f"id_edit_{glossary.id}_%s",
         )
         for glossary in self.glossaries
     }
Beispiel #2
0
 def __init__(self, project, post_data, user):
     self.project = project
     # The for_project can contain duplicates and we need to avoid that
     # for the annotate query later
     glossary_ids = set(
         Glossary.objects.for_project(project).values_list("id", flat=True))
     self.glossaries = Glossary.objects.filter(
         pk__in=glossary_ids).order_by("name")
     self.data = {(item["term__language"], item["pk"]): item["term__count"]
                  for item in self.glossaries.values(
                      "term__language", "pk").annotate(Count("term"))}
     try:
         self.post_id = int(post_data.get("edit_glossary", -1))
     except ValueError:
         self.post_id = -1
     self.languages = sort_objects(
         Language.objects.filter(
             translation__component__project=project).distinct())
     self.forms = {
         glossary.id: GlossaryForm(
             user,
             glossary.project,
             post_data if self.post_id == glossary.id else None,
             instance=glossary,
             auto_id=f"id_edit_{glossary.id}_%s",
         )
         for glossary in self.glossaries
     }
Beispiel #3
0
def show_glossaries(request, project):
    obj = get_project(request, project)
    language_glossaries = LanguageGlossary(obj, request.POST, request.user)
    try:
        initial = {
            "source_language": obj.component_set.all()[0].source_language
        }
    except IndexError:
        initial = {}
    new_form = GlossaryForm(request.user, obj, initial=initial)
    if request.method == "POST" and request.user.has_perm("project.edit", obj):
        if "delete_glossary" in request.POST:
            try:
                glossary = language_glossaries.glossaries.get(
                    pk=int(request.POST["delete_glossary"]))
                glossary.delete()
                return redirect("show_glossaries", project=obj.slug)
            except (Glossary.DoesNotExist, ValueError):
                messages.error(request, _("Glossary was not found."))
        elif language_glossaries.post_id == -1:
            new_form = GlossaryForm(request.user, obj, data=request.POST)
            if new_form.is_valid():
                new_form.instance.project = obj
                new_form.save()
                return redirect("show_glossaries", project=obj.slug)
        else:
            try:
                glossary = language_glossaries.get_edited_glossary()
                form = language_glossaries.forms[glossary.id]
                if form.is_valid():
                    form.save()
                    return redirect("show_glossaries", project=obj.slug)
            except Glossary.DoesNotExist:
                messages.error(request, _("Glossary was not found."))

    return render(
        request,
        "glossaries.html",
        {
            "title": _("Glossaries"),
            "object": obj,
            "language_glossaries": language_glossaries.get_glossaries(),
            "project": obj,
            "new_form": new_form,
        },
    )