Skip to content

Commit

Permalink
search: consolidate logic
Browse files Browse the repository at this point in the history
- use path lookup for language wide search
- add missing ProjectLanguage logic (fixes #9716)
- raise TypeError in case of missing implementation
- use new path_object_breadcrumbs helper
  • Loading branch information
nijel committed Aug 10, 2023
1 parent f071e6f commit 278318e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion weblate/templates/language.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</div>

<div class="tab-pane" id="search">
<form action="{% url 'search' lang=object.code %}" method="GET">
<form action="{% url 'search' path=object.get_url_path %}" method="GET">
{% include "snippets/search-form.html" %}
</form>
</div>
Expand Down
18 changes: 6 additions & 12 deletions weblate/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
{% load crispy_forms_tags %}

{% block breadcrumbs %}
{% if project %}
<li><a href="{{ project.get_absolute_url }}">{{ project.name }}</a></li>
{% if component %}
{% include "snippets/component-breadcrumb.html" with object=component %}
{% endif %}
{% if language %}
<li><a href="{% url 'project-language' project=project.slug lang=language.code %}">{{ language }}</a></li>
{% endif %}
{% elif language %}
<li><a href="{{ language.get_absolute_url }}">{{ language }}</a></li>
{% endif %}
<li><a href="{% if back_url %}{{ back_url }}#search{% else %}{% url 'search' %}{% endif %}">{% trans "Search" %}</a></li>
{% if path_object %}
{% path_object_breadcrumbs path_object %}
<li><a href="{{ back_url }}#search">{% trans "Search" %}</a></li>
{% else %}
<li><a href="{% url 'search' %}">{% trans "Search" %}</a></li>
{% endif %}
{% endblock %}

{% block content %}
Expand Down
36 changes: 25 additions & 11 deletions weblate/trans/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.shortcuts import redirect
from django.utils.translation import gettext, ngettext
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_POST
Expand Down Expand Up @@ -116,14 +116,16 @@ def search_replace(request, path):


@never_cache
def search(request, path=None, lang=None):
def search(request, path=None):
"""Perform site-wide search on units."""
is_ratelimited = not check_rate_limit("search", request)
search_form = SearchForm(user=request.user, data=request.GET)
sort = get_sort_name(request)
context = {"search_form": search_form}
obj = parse_path(
request, path, (Component, Project, ProjectLanguage, Translation, None)
request,
path,
(Component, Project, ProjectLanguage, Translation, Language, None),
)
context["back_url"] = obj.get_absolute_url() if obj is not None else None
if isinstance(obj, Component):
Expand All @@ -136,10 +138,12 @@ def search(request, path=None, lang=None):
context["project"] = obj.component.project
elif isinstance(obj, Project):
context["project"] = obj
elif lang:
s_language = get_object_or_404(Language, code=lang)
context["language"] = s_language
context["back_url"] = s_language.get_absolute_url()
elif isinstance(obj, Language):
context["language"] = obj
elif obj is None:
pass
else:
raise TypeError(f"Not implemented search for {obj}")

if not is_ratelimited and request.GET and search_form.is_valid():
# This is ugly way to hide query builder when showing results
Expand All @@ -149,17 +153,26 @@ def search(request, path=None, lang=None):
search_form.is_valid()
# Filter results by ACL
units = Unit.objects.prefetch_full().prefetch()
if isinstance(obj, Component):
if isinstance(obj, Translation):
units = units.filter(translation=obj)
elif isinstance(obj, Component):
units = units.filter(translation__component=obj)
elif isinstance(obj, Project):
units = units.filter(translation__component__project=obj)
else:
elif isinstance(obj, ProjectLanguage):
units = units.filter(
translation__component__project=obj.project,
translation__language=obj.language,
)
elif isinstance(obj, Language):
units = units.filter_access(request.user).filter(translation__language=obj)
elif obj is None:
units = units.filter_access(request.user)
else:
raise TypeError(f"Not implemented search for {obj}")
units = units.search(
search_form.cleaned_data.get("q", ""), project=context.get("project")
)
if lang:
units = units.filter(translation__language=context["language"])

units = get_paginator(
request, units.order_by_request(search_form.cleaned_data, obj)
Expand All @@ -170,6 +183,7 @@ def search(request, path=None, lang=None):
"search_form": search_form,
"show_results": True,
"page_obj": units,
"path_obj": obj,
"title": gettext("Search for %s") % (search_form.cleaned_data["q"]),
"query_string": search_form.urlencode(),
"search_url": search_form.urlencode(),
Expand Down
5 changes: 0 additions & 5 deletions weblate/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,6 @@
path(
"search/<object_path:path>/", weblate.trans.views.search.search, name="search"
),
path(
"search-languages/<name:lang>/",
weblate.trans.views.search.search,
name="search",
),
# Health check
path("healthz/", weblate.trans.views.basic.healthz, name="healthz"),
# Aliases for static files
Expand Down

2 comments on commit 278318e

@yilmazdurmaz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change in "weblate/templates/search.html" introduced a visual bug.
after making a search in the project root (on all components), the "search results" page does not have the link to the project root in the breadcrumbs. it simply shows a "Search" item in it.

@nijel
Copy link
Member Author

@nijel nijel commented on 278318e Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yilmazdurmaz Thanks for spotting, fixed in 664383c

Please sign in to comment.