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

Fetch /gsoc command data from Algolia instead of DB #712

Merged
merged 11 commits into from
Feb 4, 2025
25 changes: 25 additions & 0 deletions backend/apps/owasp/api/search/gsoc.py
arkid15r marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""OWASP app project gsoc search API."""

from functools import lru_cache

from algoliasearch_django import raw_search


@lru_cache
def get_gsoc_projects(year, attributes=None):
"""Return GSoC projects from Algolia."""
from apps.owasp.models.project import Project

query = f"gsoc{year}"
searchable_attributes = ["idx_custom_tags"]
Dishant1804 marked this conversation as resolved.
Show resolved Hide resolved

params = {
"attributesToHighlight": [],
"restrictSearchableAttributes": searchable_attributes,
}

if attributes:
params["attributesToRetrieve"] = attributes

results = raw_search(Project, query, params)
return results.get("hits", [])
9 changes: 0 additions & 9 deletions backend/apps/owasp/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,6 @@ def bulk_save(projects, fields=None):
"""Bulk save projects."""
BulkSaveModel.bulk_save(Project, projects, fields=fields)

@staticmethod
def get_gsoc_projects(year, attributes=None):
"""Return GSoC projects."""
projects = Project.objects.filter(custom_tags__contains=[f"gsoc{year}"])
if attributes:
projects = projects.values(*attributes)

return projects

@staticmethod
def update_data(gh_repository, repository, save=True):
"""Update project data."""
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/slack/commands/gsoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def gsoc_handler(ack, command, client):
"""Slack /gsoc command handler."""
from apps.owasp.models.project import Project
from apps.owasp.api.search.gsoc import get_gsoc_projects
from apps.slack.common.gsoc import GSOC_GENERAL_INFORMATION_BLOCKS

ack()
Expand All @@ -35,7 +35,7 @@ def gsoc_handler(ack, command, client):
elif command_text.isnumeric():
year = int(command_text)
if year in SUPPORTED_YEARS:
gsoc_projects = Project.get_gsoc_projects(year)
gsoc_projects = get_gsoc_projects(year)
gsoc_projects_markdown = f"{NL}".join(
f" • <{gp.nest_url}|{gp.owasp_name}>"
for gp in sorted(gsoc_projects, key=lambda p: p.owasp_name)
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/slack/commands/gsoc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_handler_with_projects(self, mock_slack_client):
settings.SLACK_COMMANDS_ENABLED = True

with patch(
"apps.owasp.models.project.Project.get_gsoc_projects",
"apps.owasp.api.search.gsoc.get_gsoc_projects",
return_value=mock_projects,
):
gsoc_handler(ack=MagicMock(), command=command, client=mock_slack_client)
Expand Down