Adds site setting to disable federation

This commit is contained in:
Mouse Reeve 2024-08-27 13:01:40 -07:00
parent 23dfe3924d
commit 901ca17d16
8 changed files with 126 additions and 2 deletions

View file

@ -177,6 +177,13 @@ class ServerForm(CustomForm):
exclude = ["remote_id"]
class FederationSettings(CustomForm):
class Meta:
model = models.SiteSettings
fields = [
"disable_federation",
]
class AutoModRuleForm(CustomForm):
class Meta:
model = models.AutoMod

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.15 on 2024-08-27 19:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0209_user_show_ratings"),
]
operations = [
migrations.AddField(
model_name="sitesettings",
name="disable_federation",
field=models.BooleanField(default=False),
),
]

View file

@ -83,6 +83,7 @@ class SiteSettings(SiteModel):
invite_question_text = models.CharField(
max_length=255, blank=True, default="What is your favourite book?"
)
# images
logo = models.ImageField(upload_to="logos/", null=True, blank=True)
logo_small = models.ImageField(upload_to="logos/", null=True, blank=True)
@ -103,6 +104,7 @@ class SiteSettings(SiteModel):
import_limit_reset = models.IntegerField(default=0)
user_exports_enabled = models.BooleanField(default=False)
user_import_time_limit = models.IntegerField(default=48)
disable_federation = models.BooleanField(default=False)
field_tracker = FieldTracker(fields=["name", "instance_tagline", "logo"])

View file

@ -0,0 +1,48 @@
{% extends 'settings/layout.html' %}
{% load i18n %}
{% block title %}{% trans "Federation Settings" %}{% endblock %}
{% block header %}{% trans "Federated Settings" %}{% endblock %}
{% block panel %}
{% if success %}
<div class="notification is-success is-light">
<span class="icon icon-check" aria-hidden="true"></span>
<span>
{% trans "Settings saved" %}
</span>
</div>
{% endif %}
{% if form.errors %}
<div class="notification is-danger is-light">
<span class="icon icon-x" aria-hidden="true"></span>
<span>
{% trans "Unable to save settings" %}
</span>
</div>
{% endif %}
<form
name="federation-settings"
class="content"
action="{% url 'settings-federation-settings' %}"
method="POST"
>
{% csrf_token %}
<section class="block">
<div class="field">
<label class="label" for="id_disable_federation">
{{ form.disable_federation }}
{% trans "Disable federation" %}
</label>
</div>
</section>
<footer class="block">
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
</footer>
</form>
{% endblock %}

View file

@ -41,12 +41,19 @@
{% url 'settings-invites' as alt_url %}
<a href="{{ url }}"{% if url in request.path or request.path in alt_url %} class="is-active" aria-selected="true"{% endif %}>{% trans "Invites" %}</a>
</li>
{% if perms.bookwyrm.control_federation %}
</ul>
{% endif %}
{% if perms.bookwyrm.control_federation %}
<h2 class="menu-label">{% trans "Federation" %}</h2>
<ul class="menu-list">
<li>
{% url 'settings-federation-settings' as url %}
<a href="{{ url }}"{% if url in request.path %} class="is-active" aria-selected="true"{% endif %}>{% trans "Federation Settings" %}</a>
</li>
<li>
{% url 'settings-federation' as url %}
<a href="{{ url }}"{% if url in request.path %} class="is-active" aria-selected="true"{% endif %}>{% trans "Federated Instances" %}</a>
</li>
{% endif %}
</ul>
{% endif %}
{% if perms.bookwyrm.moderate_user %}

View file

@ -162,6 +162,11 @@ urlpatterns = [
views.ActivateUserAdmin.as_view(),
name="settings-activate-user",
),
re_path(
r"^settings/federation-settings/?$",
views.FederationSettings.as_view(),
name="settings-federation-settings",
),
re_path(
r"^settings/federation/(?P<status>(federated|blocked))?/?$",
views.Federation.as_view(),

View file

@ -10,6 +10,7 @@ from .admin.dashboard import Dashboard
from .admin.federation import Federation, FederatedServer
from .admin.federation import AddFederatedServer, ImportServerBlocklist
from .admin.federation import block_server, unblock_server, refresh_server
from .admin.federation_settings import FederationSettings
from .admin.email_blocklist import EmailBlocklist
from .admin.email_config import EmailConfig
from .admin.imports import (

View file

@ -0,0 +1,36 @@
""" big picture settings about how the instance shares data """
from django.contrib.auth.decorators import login_required, permission_required
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import forms, models
# pylint: disable= no-self-use
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.control_federation", raise_exception=True),
name="dispatch",
)
class FederationSettings(View):
"""what servers do we federate with"""
def get(self, request):
"""show the current settings"""
site = models.SiteSettings.objects.get()
data = {
"form": forms.FederationSettings(instance=site),
}
return TemplateResponse(request, "settings/federation/settings.html", data)
def post(self, request):
"""Update federation settings"""
site = models.SiteSettings.objects.get()
form = forms.FederationSettings(request.POST, instance=site)
if not form.is_valid():
data = {"form": form}
return TemplateResponse(request, "settings/federation/settings.html", data)
form.save(request)
data = {"form": forms.FederationSettings(instance=site), "success": True}
return TemplateResponse(request, "settings/federation/settings.html", data)