Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

WIP allow flagging

Daoud Clarke082b2cec8fc4
diff --git a/mwmbl/models.py b/mwmbl/models.py
index e350315..c11c129 100644
--- a/mwmbl/models.py
+++ b/mwmbl/models.py
@@ -33,6 +33,11 @@ class Curation(models.Model):
class FlagCuration(models.Model):
+ class Meta:
+ permissions = [
+ ("change_flag_status", "Can change the flag status (approve or reject)"),
+ ]
+
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",
@@ -41,11 +46,18 @@ class FlagCuration(models.Model):
"OTHER": "Other",
}
+ FLAG_STATUS = {
+ "PENDING": "The flag has been submitted and is awaiting review",
+ "REJECTED": "The flag has been rejected",
+ "ACCEPTED": "The flag has been accepted and the curation has been reverted",
+ }
+
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)
+ reason = models.CharField(max_length=300, blank=True)
+ status = models.CharField(max_length=20, choices=[(k, v) for k, v in FLAG_STATUS.items()], default="PENDING")
class OldIndex(models.Model):
diff --git a/mwmbl/templates/mwmbl/curation_detail.html b/mwmbl/templates/mwmbl/curation_detail.html
index b3ee9ce..c44a217 100644
--- a/mwmbl/templates/mwmbl/curation_detail.html
+++ b/mwmbl/templates/mwmbl/curation_detail.html
@@ -6,7 +6,28 @@
<br>
<h1 class="center">Curation by {{ curation.user }} {{ curation.timestamp|naturaltime }}
for query <a href="/?q={{ curation.query|urlencode }}">{{ curation.query }}</a></h1>
- <br><br>
+ <br>
+ {# Only show if the user is logged in #}
+ {% if user.is_authenticated %}
+ {% if flags %}
+ <p><b>This curation has been flagged.</b> If this is your curation, you can take a look at the
+ <a href="https://book.mwmbl.org/page/curating/">guidelines</a> to understand the moderation process and
+ why it might have been flagged.</p>
+ <ul>
+ {% for flag in flags %}
+ <li>{{ flag.user }} flagged this curation {{ flag.timestamp|naturaltime }} for <b>{{ flag.flag|lower }}</b> {% if flag.reason %} with reason: “{{ flag.reason }}” {% endif %}. Status <b>{{ flag.status|lower }}</b>.
+ {% if perms.mwmbl.change_flag_status %}
+ <button name="status" value="accept" hx-put="{% url "flag_curation" curation.id %}" hx-swap="outerHTML">Accept</button>
+ <button name="status" value="reject" hx-put="{% url "flag_curation" curation.id %}" hx-swap="outerHTML">Reject</button>
+ {% endif %}
+ </li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ <br>
+ <a href="{% url "flag_curation" curation.id %}" hx-get="{% url "flag_curation" curation.id %}" hx-swap="outerHTML">Flag this curation</a>
+ {% endif %}
+ <br>
<div class="compare">
<div class="compare-item">
<h1 class="center">Original results</h1>
diff --git a/mwmbl/templates/mwmbl/flag_curation.html b/mwmbl/templates/mwmbl/flag_curation.html
index 140394a..b883927 100644
--- a/mwmbl/templates/mwmbl/flag_curation.html
+++ b/mwmbl/templates/mwmbl/flag_curation.html
@@ -1,4 +1,4 @@
-<form action="#" method="post">
+<form action="#" method="post" hx-post="{% url "flag_curation" curation_id %}">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
diff --git a/mwmbl/templates/mwmbl/flag_curation_updated.html b/mwmbl/templates/mwmbl/flag_curation_updated.html
new file mode 100644
index 0000000..bfceee3
--- /dev/null
+++ b/mwmbl/templates/mwmbl/flag_curation_updated.html
@@ -0,0 +1 @@
+Curation {{ curation_id }} updated.
diff --git a/mwmbl/views.py b/mwmbl/views.py
index 5a676e8..d0626a0 100644
--- a/mwmbl/views.py
+++ b/mwmbl/views.py
@@ -8,11 +8,11 @@ import justext
import requests
from django.conf import settings
from django.contrib.auth.decorators import login_required
-from django.forms import ModelForm
+from django.forms import ModelForm, ModelChoiceField, RadioSelect
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
-from django.views.generic import DetailView
+from django.views.generic import DetailView, ListView
from justext.core import html_to_dom, ParagraphMaker, classify_paragraphs, revise_paragraph_classification, \
LENGTH_LOW_DEFAULT, STOPWORDS_LOW_DEFAULT, MAX_LINK_DENSITY_DEFAULT, NO_HEADINGS_DEFAULT, LENGTH_HIGH_DEFAULT, \
STOPWORDS_HIGH_DEFAULT, MAX_HEADING_DISTANCE_DEFAULT, DEFAULT_ENCODING, DEFAULT_ENC_ERRORS, preprocessor
@@ -355,11 +355,18 @@ def _get_document_state(validated: bool, source: str) -> Optional[DocumentState]
class CurationDetailView(DetailView):
model = Curation
+ def get_context_data(self, **kwargs):
+ flags = FlagCuration.objects.filter(curation=self.object)
+ return super().get_context_data(flags=flags, **kwargs)
+
class CurationFlagForm(ModelForm):
class Meta:
model = FlagCuration
fields = ["flag", "reason"]
+ widgets = {
+ "flag": RadioSelect(),
+ }
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -371,12 +378,30 @@ 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()
+ curation = form.save(commit=False)
+ curation.user = request.user
+ curation.timestamp = datetime.now()
+ curation.curation_id = curation_id
+ curation.save()
return render(request, "mwmbl/flag_curation_success.html")
+ if request.method == "PUT":
+ new_status = request.POST.get("status")
+ if new_status not in FlagCuration.FLAG_STATUS:
+ return HttpResponseBadRequest("Invalid status")
+ curation = FlagCuration.objects.get(id=curation_id)
+ curation.status = new_status
+ curation.save()
+ return render(request, "mwmbl/flag_curation_updated.html")
else:
form = CurationFlagForm()
- return render(request, "mwmbl/flag_curation.html", {"form": form})
+ return render(request, "mwmbl/flag_curation.html", {"form": form, "curation_id": curation_id})
+
+
+class CurationFlagListView(ListView):
+ model = FlagCuration
+ template_name = "mwmbl/flag_curation_list.html"
+ context_object_name = "flags"
+ paginate_by = 20
+
+ def get_queryset(self):
+ return super().get_queryset().filter(user=self.request.user)