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

Add search_keywords to Community model (TS-2350) #1111

Open
wants to merge 1 commit into
base: master
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
36 changes: 36 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_community_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,39 @@ def __query_api(client: APIClient, query: str = "", response_status_code=200) ->
response = client.get(f"{url}?{query}")
assert response.status_code == response_status_code
return response.json()


@pytest.mark.django_db
@pytest.mark.parametrize(
"query, search_keywords, name, should_match",
[
("repo", ["repo"], "R.E.P.O", True),
("repo", None, "R.E.P.O", False),
("ror2", ["ror2"], "Risk of Rain 2", True),
("ror2", None, "Risk of Rain 2", False),
("ror", ["ror"], "Risk of Rain 2", True),
("ror", None, "Risk of Rain 2", False),
("lethal", ["lethal", "lc", "lethalcompany"], "Lethal Company", True),
("lc", ["lethal", "lc", "lethalcompany"], "Lethal Company", True),
("lethalcompany", ["lethal", "lc", "lethalcompany"], "Lethal Company", True),
("hello", ["lethal", "lc", "lethalcompany"], "Lethal Company", False),
("LETHAL", ["lethal", "lc"], "Lethal Company", True),
("Lc", ["lethal", "LC"], "Lethal Company", True),
],
)
def test_api_cyberstorm_community_search_with_keywords(
api_client: APIClient,
query: str,
search_keywords: List[str],
name: str,
should_match: bool,
) -> None:
CommunityFactory(name=name, search_keywords=search_keywords)
data = __query_api(api_client, query=f"search={query}")

if should_match:
assert data["count"] == 1
assert data["results"][0]["name"] == name
else:
assert data["count"] == 0
assert data["results"] == []
2 changes: 1 addition & 1 deletion django/thunderstore/api/cyberstorm/views/community_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CommunityListAPIView(CyberstormAutoSchemaMixin, ListAPIView):
pagination_class = CommunityPaginator
queryset = Community.objects.listed()
filter_backends = [SearchFilter, StrictOrderingFilter]
search_fields = ["name"]
search_fields = ["name", "search_keywords"]
ordering_fields = [
"aggregated_fields__download_count",
"aggregated_fields__package_count",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.1.7 on 2025-03-05 08:35

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("community", "0033_add_mod_manager_support_field"),
]

operations = [
migrations.AddField(
model_name="community",
name="search_keywords",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=512),
blank=True,
default=list,
null=True,
size=None,
),
),
]
8 changes: 8 additions & 0 deletions django/thunderstore/community/models/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import lru_cache
from typing import TYPE_CHECKING, Optional

from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models import Manager, QuerySet
Expand Down Expand Up @@ -117,6 +118,13 @@ class Community(TimestampMixin, models.Model):
# Will hide/show "Install with Mod Manager" button on package pages
has_mod_manager_support = models.BooleanField(default=True)

search_keywords = ArrayField(
models.CharField(max_length=512),
blank=True,
null=True,
default=list,
)

@property
def aggregated(self) -> "AggregatedFields":
return (
Expand Down
Loading