Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Add a view for flagging curations

Daoud Clarke69e0159e9069
diff --git a/mwmbl/migrations/0006_flagcuration.py b/mwmbl/migrations/0006_flagcuration.py
new file mode 100644
index 0000000..b8b7929
--- /dev/null
+++ b/mwmbl/migrations/0006_flagcuration.py
@@ -0,0 +1,26 @@
+# Generated by Django 4.2.11 on 2024-04-02 06:09
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('mwmbl', '0005_oldindex_squashed'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='FlagCuration',
+ fields=[
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('timestamp', models.DateTimeField()),
+ ('flag', models.CharField(choices=[('RELEVANCE', 'The curation is unlikely to be useful to a large number of users'), ('LANGUAGE', 'The curation is for a query in an unsupported language'), ('PROMOTION', 'The curation promotes a specific website or product'), ('OFFENSIVE', 'The curation contains offensive content'), ('OTHER', 'Other')], max_length=20)),
+ ('reason', models.CharField(max_length=300)),
+ ('curation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mwmbl.curation')),
+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ ]
diff --git a/mwmbl/models.py b/mwmbl/models.py
index e91e3cd..e350315 100644
--- a/mwmbl/models.py
+++ b/mwmbl/models.py
@@ -32,6 +32,22 @@ class Curation(models.Model):
num_changes = models.IntegerField(default=0)
+class FlagCuration(models.Model):
+ FLAG_TYPES = {
+ "RELEVANCE": "The curation is unlikely to be useful to a large number of users",
+ "LANGUAGE": "The curation is for a query in an unsupported language",
+ "PROMOTION": "The curation promotes a specific website or product",
+ "OFFENSIVE": "The curation contains offensive content",
+ "OTHER": "Other",
+ }
+
+ user = models.ForeignKey(MwmblUser, on_delete=models.CASCADE)
+ timestamp = models.DateTimeField()
+ curation = models.ForeignKey(Curation, on_delete=models.CASCADE)
+ flag = models.CharField(max_length=20, choices=[(k, v) for k, v in FLAG_TYPES.items()])
+ reason = models.CharField(max_length=300)
+
+
class OldIndex(models.Model):
index_path = models.CharField(max_length=300, primary_key=True)
last_copied_time = models.DateTimeField(null=True, blank=True)
diff --git a/mwmbl/templates/mwmbl/flag_curation.html b/mwmbl/templates/mwmbl/flag_curation.html
new file mode 100644
index 0000000..140394a
--- /dev/null
+++ b/mwmbl/templates/mwmbl/flag_curation.html
@@ -0,0 +1,5 @@
+<form action="#" method="post">
+ {% csrf_token %}
+ {{ form }}
+ <input type="submit" value="Submit">
+</form>
diff --git a/mwmbl/templates/mwmbl/flag_curation_success.html b/mwmbl/templates/mwmbl/flag_curation_success.html
new file mode 100644
index 0000000..64fc0af
--- /dev/null
+++ b/mwmbl/templates/mwmbl/flag_curation_success.html
@@ -0,0 +1 @@
+Curation {{ curation_id }} flagged.
\ No newline at end of file
diff --git a/mwmbl/urls.py b/mwmbl/urls.py
index 4bd21ab..f072a2e 100644
--- a/mwmbl/urls.py
+++ b/mwmbl/urls.py
@@ -20,7 +20,8 @@ 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, revert_current_curation, CurationDetailView
+from mwmbl.views import home_fragment, add_url, index, approve, revert_current_curation, CurationDetailView, \
+ flag_curation
urlpatterns = [
path('admin/', admin.site.urls),
@@ -32,6 +33,7 @@ urlpatterns = [
path('app/approve/', approve, name="approve"),
path('app/revert-current/', revert_current_curation, name="revert_current"),
path('app/curation/<int:pk>/', CurationDetailView.as_view(), name="curation"),
+ path('app/curation/<int:curation_id>/flag/', flag_curation, name="flag_curation"),
# 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 8b5e137..5a676e8 100644
--- a/mwmbl/views.py
+++ b/mwmbl/views.py
@@ -8,6 +8,7 @@ import justext
import requests
from django.conf import settings
from django.contrib.auth.decorators import login_required
+from django.forms import ModelForm
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
@@ -17,7 +18,7 @@ from justext.core import html_to_dom, ParagraphMaker, classify_paragraphs, revis
STOPWORDS_HIGH_DEFAULT, MAX_HEADING_DISTANCE_DEFAULT, DEFAULT_ENCODING, DEFAULT_ENC_ERRORS, preprocessor
from requests.exceptions import RequestException
-from mwmbl.models import Curation
+from mwmbl.models import Curation, FlagCuration
from mwmbl.search_setup import ranker, index_path
from mwmbl.settings import NUM_EXTRACT_CHARS
from mwmbl.tinysearchengine.indexer import Document, DocumentState, TinyIndex
@@ -353,3 +354,29 @@ def _get_document_state(validated: bool, source: str) -> Optional[DocumentState]
class CurationDetailView(DetailView):
model = Curation
+
+
+class CurationFlagForm(ModelForm):
+ class Meta:
+ model = FlagCuration
+ fields = ["flag", "reason"]
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.fields["flag"].widget.attrs["class"] = "form-select"
+
+
+@login_required
+def flag_curation(request, curation_id):
+ if request.method == "POST":
+ form = CurationFlagForm(request.POST)
+ if form.is_valid():
+ flag_curation = form.save(commit=False)
+ flag_curation.user = request.user
+ flag_curation.timestamp = datetime.now()
+ flag_curation.curation_id = curation_id
+ flag_curation.save()
+ return render(request, "mwmbl/flag_curation_success.html")
+ else:
+ form = CurationFlagForm()
+ return render(request, "mwmbl/flag_curation.html", {"form": form})