Skip to content

armgit / mwmbl

Public Imported from GitHub
0

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.tinysearch
index cf51759..9ad8ec8 100644
Binary files a/devdata/index-v2.tinysearch and b/devdata/index-v2.tinysearch differ
diff --git a/mwmbl/templates/home.html b/mwmbl/templates/home.html
index 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.py
index 4a5d17d..efe9971 100644
--- a/mwmbl/urls.py
+++ b/mwmbl/urls.py
@@ -20,7 +20,7 @@ from django.urls import path, include
import mwmbl.crawler.app as crawler
from mwmbl.search_setup import queued_batches, ranker, batch_cache
from 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_curation
urlpatterns = [
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 over
path("search/", search.create_router(ranker, "0.1").urls),
diff --git a/mwmbl/views.py b/mwmbl/views.py
index 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]