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

[DO NOT MERGE] Saved search spike #2415

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
name="sprite",
),
path("audit/", include("audit.urls")),
path("dissem/", include("dissemination.urls")),
# Keep last so we can use short urls for content pages like home page etc.
path("", include("cms.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Expand Down
10 changes: 10 additions & 0 deletions backend/dissemination/templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<form action="/dissem/search" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% for entry in results %}
<p>{{ entry.value }}</p>
{% endfor %}
</html>
10 changes: 10 additions & 0 deletions backend/dissemination/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from dissemination import views

app_name = "dissemination"

urlpatterns = [
path("search", views.Search.as_view(), name="Search"),
path("search/<str:search_id>", views.SavedSearch.as_view(), name="SavedSearch"),
]
67 changes: 65 additions & 2 deletions backend/dissemination/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
# from django.shortcuts import render
from django import forms
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.generic import View

# Create your views here.
from hashlib import sha1
import json
import random

search_db = {}
results_db = {}


class SearchForm(forms.Form):
value = forms.CharField(label="Value")


def execute_search(search_id):
search_key = search_db[search_id]

# we won't actually save search results, just doing this for demo purposes
if search_id in results_db.keys():
result = results_db[search_id]
else:
result = [
{"value": random.randrange(0, 100)},
{"value": random.randrange(0, 100)},
{"value": random.randrange(0, 100)},
]

results_db[search_id] = result

return search_key, search_id, result


def gen_search_id(form):
search_key = json.dumps(form.cleaned_data)
search_id = sha1(search_key.encode("utf-8")).hexdigest()
search_db[search_id] = search_key
return search_key, search_id


class Search(View):
def get(self, request, *args, **kwargs):
form = SearchForm()

return render(request, "search.html", {"form": form})

def post(self, request, *args, **kwargs):
form = SearchForm(request.POST)

if form.is_valid():
_, search_id = gen_search_id(form)

return redirect(reverse("dissemination:SavedSearch", args=[search_id]))


class SavedSearch(View):
def get(self, request, search_id):
search_key, search_id, results = execute_search(search_id)

search_params = json.loads(search_key)

form = SearchForm(search_params)

return render(request, "search.html", {"form": form, "results": results})