Skip to content

Commit 8cbeb2a

Browse files
committed
ref: use a constants for the min length of searched text
1 parent 6080901 commit 8cbeb2a

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

legadilo/constants.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Legadilo
2+
# Copyright (C) 2023-2025 by Legadilo contributors.
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU Affero General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Affero General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Affero General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
SEARCHED_TEXT_MIN_LENGTH = 3

legadilo/feeds/models/feed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from legadilo.reading.models.tag import Tag
3333
from legadilo.users.models import User
3434

35+
from ...constants import SEARCHED_TEXT_MIN_LENGTH
3536
from ...users.models import Notification
3637
from ...utils.time_utils import utcnow
3738
from .. import constants as feeds_constants
@@ -216,7 +217,7 @@ def get_by_categories(
216217
.order_by("category__title", "id")
217218
)
218219

219-
if len(searched_text) > 3: # noqa: PLR2004 Magic value used in comparison
220+
if len(searched_text) > SEARCHED_TEXT_MIN_LENGTH:
220221
qs = qs.filter(title__icontains=searched_text)
221222

222223
for feed in qs:

legadilo/reading/models/tag.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from django.db.models.functions import Coalesce
2626
from slugify import slugify
2727

28+
from legadilo.constants import SEARCHED_TEXT_MIN_LENGTH
2829
from legadilo.core import constants as core_constants
2930
from legadilo.core.forms import FormChoices
3031
from legadilo.reading import constants
@@ -177,7 +178,7 @@ def list_for_admin(self, user: User, searched_text: str = "") -> list[Tag]:
177178
.order_by("title")
178179
)
179180

180-
if len(searched_text) > 3: # noqa: PLR2004 Magic value used in comparison
181+
if len(searched_text) > SEARCHED_TEXT_MIN_LENGTH:
181182
qs = qs.filter(title__icontains=searched_text)
182183

183184
return list(qs)

legadilo/reading/views/search_views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from legadilo.users.user_types import AuthenticatedHttpRequest
3838
from legadilo.utils.security import full_sanitize
3939

40+
from ...constants import SEARCHED_TEXT_MIN_LENGTH
4041
from ...users.models import User
4142
from ...utils.collections_utils import alist, aset
4243
from ...utils.validators import is_url_valid
@@ -45,7 +46,7 @@
4546

4647
class SearchForm(forms.Form):
4748
# Main fields.
48-
q = forms.CharField(required=True, min_length=4, label=_("Search query"))
49+
q = forms.CharField(required=True, min_length=SEARCHED_TEXT_MIN_LENGTH, label=_("Search query"))
4950
search_type = forms.ChoiceField(
5051
required=False,
5152
choices=constants.ArticleSearchType.choices,
@@ -125,7 +126,8 @@ def clean_q(self):
125126
q = full_sanitize(q)
126127
if len(q) < self.fields["q"].min_length: # type: ignore[attr-defined]
127128
raise ValidationError(
128-
"You must at least enter 3 characters", code="q-too-short-after-cleaning"
129+
f"You must at least enter {SEARCHED_TEXT_MIN_LENGTH} characters",
130+
code="q-too-short-after-cleaning",
129131
)
130132

131133
return q

0 commit comments

Comments
 (0)