diff --git a/acronomy/settings.py b/acronomy/settings.py index 731411f..99571f3 100644 --- a/acronomy/settings.py +++ b/acronomy/settings.py @@ -24,6 +24,7 @@ ALLOWED_HOSTS = ["127.0.0.1", "localhost", "acronomy.lw1.at"] # Application definition + INSTALLED_APPS = [ 'acros.apps.AcrosConfig', 'django.contrib.admin', @@ -33,6 +34,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', + 'django.contrib.postgres', 'simple_history', 'debug_toolbar', 'rest_framework', diff --git a/acros/templates/acros/search.html b/acros/templates/acros/search.html new file mode 100644 index 0000000..24c861e --- /dev/null +++ b/acros/templates/acros/search.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} + +{% block title %}Search for "{{ query }}" ‐ Acronomy{% endblock %} + +{% block heading %} +

Search for "{{ query }}" did not find any results

+{% endblock %} + +{% block content %} + {% if results %} +

Did you maybe look for one of those acronyms?

+ + {% endif %} +{% endblock %} diff --git a/acros/urls.py b/acros/urls.py index daf1d7b..7652f24 100644 --- a/acros/urls.py +++ b/acros/urls.py @@ -37,7 +37,9 @@ urlpatterns = [ path('tag', RedirectView.as_view(pattern_name="tags")), path('tag/', views.TagAcroView.as_view(), name='tag'), path('integrations', views.IntegrationsView.as_view(), name="integrations"), - path('datachecks', views.DataCheckView.as_view(), name="datachecks") + path('datachecks', views.DataCheckView.as_view(), name="datachecks"), + path('search/suggest', views.search_suggestion_view, name="search_suggestion"), + path('search', views.search_view, name="search") ] if settings.DEBUG: diff --git a/acros/views.py b/acros/views.py index 1736b83..3e728b5 100644 --- a/acros/views.py +++ b/acros/views.py @@ -2,7 +2,8 @@ from datetime import date from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.messages.views import SuccessMessageMixin -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse +from django.shortcuts import redirect, render from django.urls import reverse from django.views import generic from rest_framework import viewsets, filters @@ -133,6 +134,35 @@ class DataCheckView(generic.TemplateView, LoginRequiredMixin): return data +#### Search Views #### + +def search_suggestion_view(request): + query = request.GET.get('q') + results = Acronym.objects.filter(slug__contains=query) + suggestions = [] + r: Acronym + for r in results: + suggestions.append(f"{r.name}: {r.full_name}") + response = [ + query, + suggestions + ] + return JsonResponse(response, safe=False) + + +def search_view(request): + query = request.GET.get('q') + query_acr = query.split(":")[0] + print(query_acr) + try: + acr = Acronym.objects.get(name__iexact=query_acr) + return redirect("detail", slug=acr.slug) + except Acronym.DoesNotExist: + pass + results = Acronym.objects.filter(name__trigram_similar=query_acr) + return render(request, "acros/search.html", {"results": results, "query": query_acr}) + + #### API Views #### class AcronymViewSet(viewsets.ReadOnlyModelViewSet):