1
0
Fork 0
mirror of https://github.com/Findus23/acronomy.git synced 2024-09-19 15:33:45 +02:00

add search for browser extenstion

This commit is contained in:
Lukas Winkler 2020-07-27 13:34:19 +02:00
parent 8b632fd5c7
commit 779437be95
Signed by: lukas
GPG key ID: 54DE4D798D244853
4 changed files with 54 additions and 2 deletions

View file

@ -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',

View file

@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% block title %}Search for "{{ query }}" ‐ Acronomy{% endblock %}
{% block heading %}
<h1>Search for "{{ query }}" did not find any results</h1>
{% endblock %}
{% block content %}
{% if results %}
<p>Did you maybe look for one of those acronyms?</p>
<ul>
{% for r in results %}
<li><a href="{% url "detail" r.slug %}"><strong>{{ r.name }}</strong>: {{ r.full_name }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

View file

@ -37,7 +37,9 @@ urlpatterns = [
path('tag', RedirectView.as_view(pattern_name="tags")),
path('tag/<str:slug>', 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:

View file

@ -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):