An open source, non-profit web search engine
Implement revert current curation
Daoud Clarkea84570d16cd0
diff --git a/devdata/index-v2.tinysearch b/devdata/index-v2.tinysearchindex cf51759..9ad8ec8 100644Binary files a/devdata/index-v2.tinysearch and b/devdata/index-v2.tinysearch differdiff --git a/mwmbl/templates/home.html b/mwmbl/templates/home.htmlindex 2e775b5..0c2994a 100644--- a/mwmbl/templates/home.html+++ b/mwmbl/templates/home.html@@ -16,7 +16,11 @@{% if query %}<button class="button curate-add" is="mwmbl-add-button">+ Add new</button>{% if curation %}- <button class="button">↺ Revert {{ curation.num_changes }} changes</button>+ <button class="button"+ hx-post="/app/revert-current/"+ hx-target=".main"+ hx-vals='{"curation_id": {{ curation.id }}}'+ hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>↺ Revert {{ curation.num_changes }} changes</button>{% else %}<button class="disabled-button">↺ Revert</button>{% endif %}diff --git a/mwmbl/urls.py b/mwmbl/urls.pyindex 4a5d17d..efe9971 100644--- a/mwmbl/urls.py+++ b/mwmbl/urls.py@@ -20,7 +20,7 @@ from django.urls import path, includeimport mwmbl.crawler.app as crawlerfrom mwmbl.search_setup import queued_batches, ranker, batch_cachefrom mwmbl.tinysearchengine import search-from mwmbl.views import home_fragment, add_url, index, approve+from mwmbl.views import home_fragment, add_url, index, approve, revert_current_curationurlpatterns = [path('admin/', admin.site.urls),@@ -30,6 +30,7 @@ urlpatterns = [path('app/home/', home_fragment, name="home"),path('app/add/', add_url, name="add_url"),path('app/approve/', approve, name="approve"),+ path('app/revert-current/', revert_current_curation, name="revert_current"),# TODO: this is the old API, deprecated and to be removed once all clients have moved overpath("search/", search.create_router(ranker, "0.1").urls),diff --git a/mwmbl/views.py b/mwmbl/views.pyindex b3273e2..7874c2f 100644--- a/mwmbl/views.py+++ b/mwmbl/views.py@@ -188,6 +188,38 @@ def approve(request):return response+@require_http_methods(["POST"])+def revert_current_curation(request):+ curation_id = request.POST.get("curation_id")+ curation = Curation.objects.get(id=curation_id)+ query = curation.query++ with TinyIndex(Document, index_path, 'w') as indexer:+ term = " ".join(tokenize(query))+ documents = [Document(**doc) for doc in curation.original_index_results]++ page_index = indexer.get_key_page_index(term)+ existing_documents = indexer.get_page(page_index)+ other_term_documents = [doc for doc in existing_documents if doc.term != term]++ # Replace all existing documents for the term with the original documents+ all_documents = documents + other_term_documents++ indexer.store_in_page(page_index, all_documents)++ # Delete the curation+ curation.delete()++ response = render(request, "home.html", {+ "results": documents,+ "query": query,+ "activity": None,+ "curation": None,+ })++ return response++def _get_curation(request, query, documents, reranked_documents):curation_id = request.POST.get("curation_id")reranked_document_dicts = [asdict(d) for d in reranked_documents]