Block or approve domains

This commit is contained in:
Mouse Reeve 2022-01-10 11:19:20 -08:00
parent 6b0967df39
commit f0ae37c331
5 changed files with 49 additions and 12 deletions

View file

@ -1,6 +1,7 @@
""" outlink data """
from urllib.parse import urlparse
from django.core.exceptions import PermissionDenied
from django.db import models
from django.utils.translation import gettext_lazy as _
@ -62,6 +63,11 @@ class LinkDomain(BookWyrmModel):
status = models.CharField(max_length=50, choices=StatusChoices, default="pending")
name = models.CharField(max_length=100)
def raise_not_editable(self, viewer):
if viewer.has_perm("moderate_post"):
return
raise PermissionDenied()
def save(self, *args, **kwargs):
"""set a default name"""
if not self.name:

View file

@ -91,17 +91,30 @@
{% include "settings/link_domains/edit_domain_modal.html" with domain=domain id=domain_modal %}
<form name="domain-{{ domain.id }}">
{% csrf_token %}
<div class="field has-addons">
<div class="control">
<button type="submit" class="button is-success is-light">{% trans "Approve" %}</button>
</div>
<div class="control">
<button type="submit" class="button is-danger is-light">{% trans "Block" %}</button>
</div>
</div>
</form>
<div class="field has-addons">
{% if status != "approved" %}
<form
name="domain-{{ domains.id }}-approve"
class="control"
method="post"
action="{% url 'settings-link-domain-status' domain.id 'approved' %}"
>
{% csrf_token %}
<button type="submit" class="button is-success is-light">{% trans "Approve" %}</button>
</form>
{% endif %}
{% if status != "blocked" %}
<form
name="domain-{{ domains.id }}-block"
class="control"
method="post"
action="{% url 'settings-link-domain-status' domain.id 'blocked' %}"
>
{% csrf_token %}
<button type="submit" class="button is-danger is-light">{% trans "Block" %}</button>
</form>
{% endif %}
</div>
</div>
{% endfor %}

View file

@ -163,6 +163,11 @@ urlpatterns = [
views.LinkDomain.as_view(),
name="settings-link-domain",
),
re_path(
r"^setting/link-domains/(?P<domain_id>\d+)/(?P<status>(pending|approved|blocked))/?$",
views.update_domain_status,
name="settings-link-domain-status",
),
re_path(
r"^settings/ip-blocklist/?$",
views.IPBlocklist.as_view(),

View file

@ -9,7 +9,7 @@ from .admin.email_blocklist import EmailBlocklist
from .admin.ip_blocklist import IPBlocklist
from .admin.invite import ManageInvites, Invite, InviteRequest
from .admin.invite import ManageInviteRequests, ignore_invite_request
from .admin.link_domains import LinkDomain
from .admin.link_domains import LinkDomain, update_domain_status
from .admin.reports import (
Report,
Reports,

View file

@ -4,6 +4,7 @@ from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST
from bookwyrm import forms, models
@ -35,3 +36,15 @@ class LinkDomain(View):
form = forms.LinkDomainForm(request.POST, instance=domain)
form.save()
return redirect('settings-link-domain', status=status)
@require_POST
@login_required
def update_domain_status(request, domain_id, status):
"""This domain seems fine"""
domain = get_object_or_404(models.LinkDomain, id=domain_id)
domain.raise_not_editable(request.user)
domain.status = status
domain.save()
return redirect('settings-link-domain', status="pending")