Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict admin to delete default site #1439

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions home/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig

class HomeConfig(AppConfig):
name = "home"

def ready(self):
import home.signals

15 changes: 15 additions & 0 deletions home/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.dispatch import receiver
from wagtail.core.models import Site
from django.db.models.signals import pre_save, pre_delete
from wagtail.admin import messages


@receiver(pre_save, sender=Site)
def save_site(instance, **kwargs):
if Site.objects.exclude(pk=instance.pk).count() == 0:
instance.is_default_site = True

@receiver(pre_delete, sender=Site)
def delete_site(sender, instance, using, **kwargs):
if Site.objects.exclude(pk=instance.pk).count() == 1:
Site.objects.exclude(pk=instance.pk).update(is_default_site=True)
23 changes: 23 additions & 0 deletions home/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.test import TestCase
from django.http import HttpRequest
from django.urls import reverse
from translation_manager.models import TranslationEntry
from wagtail.core.models import Site
from wagtail_localize.operations import TranslationCreator
Expand All @@ -9,6 +10,8 @@
from wagtail_factories import SiteFactory, PageFactory
from bs4 import BeautifulSoup

from iogt_users.factories import AdminUserFactory


class LimitPageChooserHookTests(TestCase):
def setUp(self):
Expand Down Expand Up @@ -98,3 +101,23 @@ def test_media_block_translation_of_bengali_language(self):
self.assertEqual(response.status_code, 200)
self.assertContains(response, f"উপরের ভিডিও দেখা না গেলে <a href=\"{self.en_article.body[0].value.url}\" download> এর পরিবর্তে এটা </a> ডাউনলোড করুন", count=1)
self.assertContains(response, f"উপরের অডিও শুনতে না পেলে <a href=\"{self.en_article.body[1].value.url}\" download> এর পরিবর্তে এটা </a> ডাউনলোড করুন", count=1)


class SiteTest(TestCase):
def setUp(self):
self.site01 = SiteFactory(hostname='localhost', port=8000, is_default_site=True)
self.site02 = SiteFactory(hostname='testserver', port=80)
admin_user = AdminUserFactory()
self.client.force_login(admin_user)

def test_hide_delete_button_on_default_site(self):
response = self.client.get(reverse('wagtailsites:edit', args=(self.site01.pk,)))

self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'Delete site')

def test_display_delete_button_on_non_default_site(self):
response = self.client.get(reverse('wagtailsites:edit', args=(self.site02.pk,)))

self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Delete site')
2 changes: 1 addition & 1 deletion iogt/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# Application definition

INSTALLED_APPS = [
'home',
'home.apps.HomeConfig',
'search',
'iogt_users',
'comments',
Expand Down
13 changes: 13 additions & 0 deletions iogt/templates/wagtailadmin/generic/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "wagtailadmin/generic/form.html" %}
{% load i18n %}

{% block actions %}
<input type="submit" value="{% trans 'Save' %}" class="button" />

{# TO DO: Update this file on wagtail upgrade #}
{% if can_delete and site is None %}
<a href="{{ view.get_delete_url }}" class="button button-secondary no">{{ view.delete_item_label }}</a>
{% elif can_delete and site is not None and not site.is_default_site %}
<a href="{{ view.get_delete_url }}" class="button button-secondary no">{{ view.delete_item_label }}</a>
{% endif %}
{% endblock %}