Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13638: Utilize {% querystring %} template tag for pagination #13910

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 27 additions & 93 deletions weblate/templates/paginator.html
Original file line number Diff line number Diff line change
@@ -1,95 +1,29 @@
{% load i18n %}
{% load icons %}
{% load querystring %}

{% if page_obj.paginator.num_pages > 1 %}
<ul class="pagination">
<li {% if page_obj.number == 1 %}class="disabled"{% endif %}>
<a href="?page=1&amp;limit={{ page_obj.paginator.per_page }}{% if page_obj.paginator.sort_by %}&amp;sort_by={{ page_obj.paginator.sort_by }}{% endif %}{% if query_string %}&amp;{{ query_string }}{% endif %}{% if anchor %}#{{ anchor }}{% endif %}"
class="green">
{% if LANGUAGE_BIDI %}
{% icon "page-last.svg" %}
{% else %}
{% icon "page-first.svg" %}
{% endif %}
</a>
</li>
<li {% if not page_obj.has_previous %}class="disabled"{% endif %}>
<a
{% if page_obj.has_previous %}
rel="prev"
{# djlint:off #}
href="?page={{ page_obj.previous_page_number }}&amp;limit={{ page_obj.paginator.per_page }}{% if page_obj.paginator.sort_by %}&amp;sort_by={{ page_obj.paginator.sort_by }}{% endif %}{% if query_string %}&amp;{{ query_string }}{% endif %}{% if anchor %}#{{ anchor }}{% endif %}"
{# djlint:on #}
{% endif %}
class="green">
{% if LANGUAGE_BIDI %}
{% icon "page-next.svg" %}
{% else %}
{% icon "page-previous.svg" %}
{% endif %}
</a>
</li>
<li>
<a class="position-input" title="{% trans "Click to edit position" %}">
{% blocktrans with page_obj.number as position and page_obj.paginator.num_pages as total %}{{ position }} / {{ total }}{% endblocktrans %}
</a>
<a class="position-input-editable" title="{% trans "Go to position" %}">
{% if not paginator_form %}<form method="get">{% endif %}
{% for key, value in search_items %}
<input {% if paginator_form %}form="{{ paginator_form }}"{% endif %}
type="hidden"
name="{{ key }}"
value="{{ value }}"
aria-label="{{ value }}" />
{% endfor %}
<input {% if paginator_form %}form="{{ paginator_form }}"{% endif %}
type="hidden"
name="limit"
value="{{ page_obj.paginator.per_page }}"
aria-label="{{ page_obj.paginator.per_page }}" />
<div class="input-group">
<input {% if paginator_form %}form="{{ paginator_form }}"{% endif %}
type="number"
min="1"
max="{{ page_obj.paginator.num_pages }}"
name="page"
class="form-control"
value="{{ page_obj.number }}"
aria-label="{% trans "Jump to" %}"
id="position-input-editable-input">
<span class="input-group-addon">
{% comment %}Translators: This is partial position indicator shown when editing position{% endcomment %}
{% blocktrans with page_obj.paginator.num_pages as total %}/ {{ total }}{% endblocktrans %}
</span>
</div>
{% if not paginator_form %}</form>{% endif %}
</a>
</li>
<li {% if not page_obj.has_next %}class="disabled"{% endif %}>
<a
{% if page_obj.has_next %}
rel="next"
{# djlint:off #}
href="?page={{ page_obj.next_page_number }}&amp;limit={{ page_obj.paginator.per_page }}{% if page_obj.paginator.sort_by %}&amp;sort_by={{ page_obj.paginator.sort_by }}{% endif %}{% if query_string %}&amp;{{ query_string }}{% endif %}{% if anchor %}#{{ anchor }}{% endif %}"
{# djlint:on #}
{% endif %}
class="green">
{% if not LANGUAGE_BIDI %}
{% icon "page-next.svg" %}
{% else %}
{% icon "page-previous.svg" %}
{% endif %}
</a>
</li>
<li {% if page_obj.paginator.num_pages == page_obj.number %}class="disabled"{% endif %}>
<a href="?page={{ page_obj.paginator.num_pages }}&amp;limit={{ page_obj.paginator.per_page }}{% if page_obj.paginator.sort_by %}&amp;sort_by={{ page_obj.paginator.sort_by }}{% endif %}{% if query_string %}&amp;{{ query_string }}{% endif %}{% if anchor %}#{{ anchor }}{% endif %}"
class="green">
{% if not LANGUAGE_BIDI %}
{% icon "page-last.svg" %}
{% else %}
{% icon "page-first.svg" %}
{% endif %}
</a>
</li>
<nav class="pagination">
<ul>
{% if page_obj.has_previous %}
<li>
<a href="?{% querystring page=page_obj.previous_page_number %}">&laquo; Previous</a>
</li>
{% endif %}

{% for page_number in page_obj.paginator.page_range %}
{% if page_number == page_obj.number %}
<li class="active">
<span>{{ page_number }}</span>
</li>
{% else %}
<li>
<a href="?{% querystring page=page_number %}">{{ page_number }}</a>
</li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li>
<a href="?{% querystring page=page_obj.next_page_number %}">Next &raquo;</a>
</li>
{% endif %}
</ul>
Copy link
Member

Choose a reason for hiding this comment

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

Can you please bring back the original markup?

  • Page navigation is missing
  • UI icons are no longer used
  • RTL languages are no longer getting correct icons
  • Strings are no longer localized

{% endif %}
</nav>
1 change: 0 additions & 1 deletion weblate/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
{% endif %}
{% endblock %}


{% block content %}

{% if show_results %}
Expand Down
88 changes: 44 additions & 44 deletions weblate/trans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
lang_label,
)
plural = translation.plural
placeables_set = set()

Check failure on line 332 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "placeables_set" (hint: "placeables_set: set[<type>] = ...")
for text in plurals:
placeables_set.update(hl[2] for hl in highlight_string(text, unit))
placeables = list(placeables_set)
Expand Down Expand Up @@ -530,7 +530,7 @@
if unit.readonly:
for field in ["target", "fuzzy", "review"]:
self.fields[field].widget.attrs["readonly"] = 1
self.fields["review"].choices = [

Check failure on line 533 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
(state, label)
for state, label in StringState.choices
if state == STATE_READONLY
Expand All @@ -540,7 +540,7 @@
self.fields["review"].widget.attrs["class"] = "review_radio"
# Avoid failing validation on untranslated string
if args:
self.fields["review"].choices.append((STATE_EMPTY, ""))

Check failure on line 543 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
self.helper = FormHelper()
self.helper.form_method = "post"
self.helper.form_tag = False
Expand Down Expand Up @@ -640,7 +640,7 @@

def __init__(self, translation: Translation, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.fields["format"].choices = [

Check failure on line 643 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
(x.name, x.verbose) for x in EXPORTERS.values() if x.supports(translation)
]
self.helper = FormHelper(self)
Expand Down Expand Up @@ -687,12 +687,12 @@
self.helper.form_tag = False

def get_field_doc(self, field: forms.Field) -> tuple[str, str] | None:
return ("user/files", f"upload-{field.name}")

Check failure on line 690 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "name"

def remove_translation_choice(self, value) -> None:
"""Remove given file upload method from choices."""
choices = self.fields["method"].choices

Check failure on line 694 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
self.fields["method"].choices = [

Check failure on line 695 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
choice for choice in choices if choice[0] != value
]

Expand Down Expand Up @@ -732,11 +732,11 @@
form = ExtraUploadForm
kwargs["initial"] = {"author_name": user.full_name, "author_email": user.email}
elif user.has_perm("upload.overwrite", translation):
form = UploadForm

Check failure on line 735 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible types in assignment (expression has type "type[UploadForm]", variable has type "type[ExtraUploadForm]")
else:
form = SimpleUploadForm

Check failure on line 737 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible types in assignment (expression has type "type[SimpleUploadForm]", variable has type "type[ExtraUploadForm]")
result = form(*args, **kwargs)
for method in [x[0] for x in result.fields["method"].choices]:

Check failure on line 739 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / mypy

"Field" has no attribute "choices"
if not check_upload_method_permissions(user, translation, method):
result.remove_translation_choice(method)
# Remove approved choice for non review projects
Expand Down Expand Up @@ -804,50 +804,50 @@
self.cleaned_data["offset"] = 1
return self.cleaned_data["offset"]

def items(self):
items = []
# Skip checksum and offset as these change
ignored = {"offset", "checksum"}
for param in sorted(self.cleaned_data):
value = self.cleaned_data[param]
# We don't care about empty values or ignored ones
if value is None or param in ignored:
continue
if isinstance(value, bool):
# Only store true values
if value:
items.append((param, "1"))
elif isinstance(value, int):
# Avoid storing 0 values
if value > 0:
items.append((param, str(value)))
elif isinstance(value, datetime):
# Convert date to string
items.append((param, value.date().isoformat()))
elif isinstance(value, list):
items.extend((param, val) for val in value)
elif isinstance(value, User):
items.append((param, value.username))
elif value:
# It should be a string here
items.append((param, value))
return items

def urlencode(self):
return urlencode(self.items())

def reset_offset(self):
"""
Reset form offset.

This is needed to avoid issues when using the form as the default for
any new search.
"""
data = copy.copy(self.data) # pylint: disable=access-member-before-definition
data["offset"] = "1"
data["checksum"] = ""
self.data = data
return self
# def items(self):
Copy link
Member

Choose a reason for hiding this comment

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

If the code is no longer used, just remove it.

# items = []

Check failure on line 808 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:808:5: ERA001 Found commented-out code
# # Skip checksum and offset as these change
# ignored = {"offset", "checksum"}

Check failure on line 810 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:810:5: ERA001 Found commented-out code
# for param in sorted(self.cleaned_data):
# value = self.cleaned_data[param]

Check failure on line 812 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:812:5: ERA001 Found commented-out code
# # We don't care about empty values or ignored ones
# if value is None or param in ignored:
# continue

Check failure on line 815 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:815:5: ERA001 Found commented-out code
# if isinstance(value, bool):
# # Only store true values
# if value:
# items.append((param, "1"))

Check failure on line 819 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:819:5: ERA001 Found commented-out code
# elif isinstance(value, int):

Check failure on line 820 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:820:5: ERA001 Found commented-out code
# # Avoid storing 0 values
# if value > 0:
# items.append((param, str(value)))

Check failure on line 823 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:823:5: ERA001 Found commented-out code
# elif isinstance(value, datetime):

Check failure on line 824 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:824:5: ERA001 Found commented-out code
# # Convert date to string
# items.append((param, value.date().isoformat()))

Check failure on line 826 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:826:5: ERA001 Found commented-out code
# elif isinstance(value, list):

Check failure on line 827 in weblate/trans/forms.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (ERA001)

weblate/trans/forms.py:827:5: ERA001 Found commented-out code
# items.extend((param, val) for val in value)
# elif isinstance(value, User):
# items.append((param, value.username))
# elif value:
# # It should be a string here
# items.append((param, value))
# return items

# def urlencode(self):
# return urlencode(self.items())

# def reset_offset(self):
# """
# Reset form offset.

# This is needed to avoid issues when using the form as the default for
# any new search.
# """
# data = copy.copy(self.data) # pylint: disable=access-member-before-definition
# data["offset"] = "1"
# data["checksum"] = ""
# self.data = data
# return self


class PositionSearchForm(SearchForm):
Expand Down
Loading