From 3bf883118fb57845a9ec6001a73533a1dabecfcf Mon Sep 17 00:00:00 2001 From: Alihassanc5 Date: Tue, 25 Oct 2022 14:32:57 +0500 Subject: [PATCH 1/7] Restrict admin to delete default site --- home/apps.py | 8 ++++ home/signals.py | 9 ++++ iogt/templates/wagtailadmin/generic/edit.html | 45 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 home/apps.py create mode 100644 home/signals.py create mode 100644 iogt/templates/wagtailadmin/generic/edit.html diff --git a/home/apps.py b/home/apps.py new file mode 100644 index 000000000..1bfa33661 --- /dev/null +++ b/home/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + +class HomeConfig(AppConfig): + name = "home" + + def ready(self): + import home.signals + diff --git a/home/signals.py b/home/signals.py new file mode 100644 index 000000000..e3fcb61f7 --- /dev/null +++ b/home/signals.py @@ -0,0 +1,9 @@ +from django.dispatch import receiver +from wagtail.core.models import Site +from django.db.models.signals import pre_save + + +@receiver(pre_save, sender=Site) +def save_site(instance, **kwargs): + if Site.objects.exclude(pk=instance.pk).filter(is_default_site=True).first() is None: + instance.is_default_site = True diff --git a/iogt/templates/wagtailadmin/generic/edit.html b/iogt/templates/wagtailadmin/generic/edit.html new file mode 100644 index 000000000..9756b6a87 --- /dev/null +++ b/iogt/templates/wagtailadmin/generic/edit.html @@ -0,0 +1,45 @@ +{% extends "wagtailadmin/base.html" %} +{% load i18n %} + +{% block titletag %}{{ view.page_title }} {{ view.get_page_subtitle }}{% endblock %} + +{% block content %} + + {% include "wagtailadmin/shared/header.html" with title=view.page_title subtitle=view.get_page_subtitle icon=view.header_icon %} + +
+ {% block before_form %}{% endblock %} +
+ {% csrf_token %} + + {% block hidden_fields %} + {% for field in form.hidden_fields %}{{ field }}{% endfor %} + {% endblock %} + +
    + {% block visible_fields %} + {% for field in form.visible_fields %} + {% include "wagtailadmin/shared/field_as_li.html" %} + {% endfor %} + {% endblock %} + +
  • + + {% if can_delete and not site.is_default_site %} + {{ view.delete_item_label }} + {% endif %} +
  • +
+
+
+{% endblock %} + +{% block extra_js %} + {{ block.super }} + {{ form.media.js }} +{% endblock %} + +{% block extra_css %} + {{ block.super }} + {{ form.media.css }} +{% endblock %} From a25acd5784a27d6852162c20b6e9e97a2bf24576 Mon Sep 17 00:00:00 2001 From: Alihassanc5 Date: Wed, 26 Oct 2022 11:31:18 +0500 Subject: [PATCH 2/7] minor change in settings --- iogt/settings/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iogt/settings/base.py b/iogt/settings/base.py index e4b7b9237..dba5c9d6f 100644 --- a/iogt/settings/base.py +++ b/iogt/settings/base.py @@ -31,7 +31,7 @@ # Application definition INSTALLED_APPS = [ - 'home', + 'home.apps.HomeConfig', 'search', 'iogt_users', 'comments', From 5d5a51f29db79127667f2a3171707b1dab7b0f8f Mon Sep 17 00:00:00 2001 From: Alihassanc5 Date: Wed, 26 Oct 2022 16:13:02 +0500 Subject: [PATCH 3/7] Some changes to avoid failure of sites --- home/models.py | 2 +- home/signals.py | 9 +++++++-- home/templatetags/home_tags.py | 2 +- iogt/middleware.py | 2 +- iogt_content_migration/management/commands/load_v1_db.py | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/home/models.py b/home/models.py index 2b36a6fe0..c14a6ef6f 100644 --- a/home/models.py +++ b/home/models.py @@ -759,7 +759,7 @@ class SiteSettings(BaseSetting): @classmethod def get_for_default_site(cls): - default_site = Site.objects.filter(is_default_site=True).first() + default_site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() return cls.for_site(default_site) def __str__(self): diff --git a/home/signals.py b/home/signals.py index e3fcb61f7..a97db8d6c 100644 --- a/home/signals.py +++ b/home/signals.py @@ -1,9 +1,14 @@ from django.dispatch import receiver from wagtail.core.models import Site -from django.db.models.signals import pre_save +from django.db.models.signals import pre_save, pre_delete @receiver(pre_save, sender=Site) def save_site(instance, **kwargs): - if Site.objects.exclude(pk=instance.pk).filter(is_default_site=True).first() is None: + if Site.objects.exclude(pk=instance.pk).count() == 0: instance.is_default_site = True + +@receiver(pre_delete, sender=Site) +def delete_site(instance, **kwargs): + if Site.objects.exclude(pk=instance.pk).count() == 1: + Site.objects.exclude(pk=instance.pk).update(is_default_site=True) diff --git a/home/templatetags/home_tags.py b/home/templatetags/home_tags.py index a856d16f1..4d746980b 100644 --- a/home/templatetags/home_tags.py +++ b/home/templatetags/home_tags.py @@ -117,7 +117,7 @@ def locale_set(locale, url): @register.simple_tag def translated_home_page_url(language_code): locale = Locale.objects.get(language_code=language_code) - default_home_page = Site.objects.filter(is_default_site=True).first().root_page + default_home_page = Site.objects.filter(is_default_site=True).first().root_page or Site.objects.first().root_page home_page = default_home_page.get_translation_or_none(locale) page = home_page or default_home_page return page.url diff --git a/iogt/middleware.py b/iogt/middleware.py index c2593a4ae..ff9043f65 100644 --- a/iogt/middleware.py +++ b/iogt/middleware.py @@ -106,7 +106,7 @@ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): - site = Site.objects.filter(is_default_site=True).first() + site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() locale = Locale.get_active() language_code = locale.language_code globals_.site = site diff --git a/iogt_content_migration/management/commands/load_v1_db.py b/iogt_content_migration/management/commands/load_v1_db.py index 55008663e..53d982c1a 100644 --- a/iogt_content_migration/management/commands/load_v1_db.py +++ b/iogt_content_migration/management/commands/load_v1_db.py @@ -2224,7 +2224,7 @@ def migrate_post_registration_survey(self): ) def get_admin_url(self, id): - site = Site.objects.filter(is_default_site=True).first() + site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() return f"{site.root_url}{reverse('wagtailadmin_pages:edit', args=(id,))}" def print_post_migration_report(self): From 3289f49352c509003a26d8520ea6309e70e3ec2c Mon Sep 17 00:00:00 2001 From: Alihassanc5 Date: Wed, 26 Oct 2022 18:18:04 +0500 Subject: [PATCH 4/7] minor changes --- home/signals.py | 3 ++- iogt/templates/wagtailadmin/generic/edit.html | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/home/signals.py b/home/signals.py index a97db8d6c..c09823513 100644 --- a/home/signals.py +++ b/home/signals.py @@ -1,6 +1,7 @@ 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) @@ -9,6 +10,6 @@ def save_site(instance, **kwargs): instance.is_default_site = True @receiver(pre_delete, sender=Site) -def delete_site(instance, **kwargs): +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) diff --git a/iogt/templates/wagtailadmin/generic/edit.html b/iogt/templates/wagtailadmin/generic/edit.html index 9756b6a87..299165855 100644 --- a/iogt/templates/wagtailadmin/generic/edit.html +++ b/iogt/templates/wagtailadmin/generic/edit.html @@ -25,7 +25,7 @@
  • - {% if can_delete and not site.is_default_site %} + {% if can_delete and site is not None and not site.is_default_site %} {{ view.delete_item_label }} {% endif %}
  • From 2a96035b77ba6a34ee6c7f073c9d47239864c19e Mon Sep 17 00:00:00 2001 From: Alihassanc5 Date: Wed, 26 Oct 2022 18:51:10 +0500 Subject: [PATCH 5/7] minor change --- iogt/templates/wagtailadmin/generic/edit.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iogt/templates/wagtailadmin/generic/edit.html b/iogt/templates/wagtailadmin/generic/edit.html index 299165855..49c1cc7d8 100644 --- a/iogt/templates/wagtailadmin/generic/edit.html +++ b/iogt/templates/wagtailadmin/generic/edit.html @@ -25,7 +25,9 @@
  • - {% if can_delete and site is not None and not site.is_default_site %} + {% if can_delete and site is None %} + {{ view.delete_item_label }} + {% elif can_delete and site is not None and not site.is_default_site %} {{ view.delete_item_label }} {% endif %}
  • From bdd89ec7a68a5174e12647581c89d4d6e823f0ec Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Fri, 20 Jan 2023 18:31:58 +0500 Subject: [PATCH 6/7] Refactor code --- home/models.py | 2 +- home/templatetags/home_tags.py | 2 +- iogt/middleware.py | 2 +- iogt/templates/wagtailadmin/generic/edit.html | 52 +++---------------- .../management/commands/load_v1_db.py | 2 +- 5 files changed, 12 insertions(+), 48 deletions(-) diff --git a/home/models.py b/home/models.py index c16ddfa5d..95c5dd89e 100644 --- a/home/models.py +++ b/home/models.py @@ -762,7 +762,7 @@ class SiteSettings(BaseSetting): @classmethod def get_for_default_site(cls): - default_site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() + default_site = Site.objects.filter(is_default_site=True).first() return cls.for_site(default_site) def __str__(self): diff --git a/home/templatetags/home_tags.py b/home/templatetags/home_tags.py index 4d746980b..a856d16f1 100644 --- a/home/templatetags/home_tags.py +++ b/home/templatetags/home_tags.py @@ -117,7 +117,7 @@ def locale_set(locale, url): @register.simple_tag def translated_home_page_url(language_code): locale = Locale.objects.get(language_code=language_code) - default_home_page = Site.objects.filter(is_default_site=True).first().root_page or Site.objects.first().root_page + default_home_page = Site.objects.filter(is_default_site=True).first().root_page home_page = default_home_page.get_translation_or_none(locale) page = home_page or default_home_page return page.url diff --git a/iogt/middleware.py b/iogt/middleware.py index ff9043f65..c2593a4ae 100644 --- a/iogt/middleware.py +++ b/iogt/middleware.py @@ -106,7 +106,7 @@ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): - site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() + site = Site.objects.filter(is_default_site=True).first() locale = Locale.get_active() language_code = locale.language_code globals_.site = site diff --git a/iogt/templates/wagtailadmin/generic/edit.html b/iogt/templates/wagtailadmin/generic/edit.html index 49c1cc7d8..f6346bd77 100644 --- a/iogt/templates/wagtailadmin/generic/edit.html +++ b/iogt/templates/wagtailadmin/generic/edit.html @@ -1,47 +1,11 @@ -{% extends "wagtailadmin/base.html" %} +{% extends "wagtailadmin/generic/form.html" %} {% load i18n %} -{% block titletag %}{{ view.page_title }} {{ view.get_page_subtitle }}{% endblock %} - -{% block content %} - - {% include "wagtailadmin/shared/header.html" with title=view.page_title subtitle=view.get_page_subtitle icon=view.header_icon %} - -
    - {% block before_form %}{% endblock %} -
    - {% csrf_token %} - - {% block hidden_fields %} - {% for field in form.hidden_fields %}{{ field }}{% endfor %} - {% endblock %} - -
      - {% block visible_fields %} - {% for field in form.visible_fields %} - {% include "wagtailadmin/shared/field_as_li.html" %} - {% endfor %} - {% endblock %} - -
    • - - {% if can_delete and site is None %} - {{ view.delete_item_label }} - {% elif can_delete and site is not None and not site.is_default_site %} - {{ view.delete_item_label }} - {% endif %} -
    • -
    -
    -
    -{% endblock %} - -{% block extra_js %} - {{ block.super }} - {{ form.media.js }} -{% endblock %} - -{% block extra_css %} - {{ block.super }} - {{ form.media.css }} +{% block actions %} + + {% if can_delete and site is None %} + {{ view.delete_item_label }} + {% elif can_delete and site is not None and not site.is_default_site %} + {{ view.delete_item_label }} + {% endif %} {% endblock %} diff --git a/iogt_content_migration/management/commands/load_v1_db.py b/iogt_content_migration/management/commands/load_v1_db.py index 47ac165a1..4370a3b8d 100644 --- a/iogt_content_migration/management/commands/load_v1_db.py +++ b/iogt_content_migration/management/commands/load_v1_db.py @@ -2214,7 +2214,7 @@ def migrate_post_registration_survey(self): ) def get_admin_url(self, id): - site = Site.objects.filter(is_default_site=True).first() or Site.objects.first() + site = Site.objects.filter(is_default_site=True).first() return f"{site.root_url}{reverse('wagtailadmin_pages:edit', args=(id,))}" def print_post_migration_report(self): From 27e03c0ad41ec45dbc715b060103b01b1cfac55d Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Fri, 20 Jan 2023 19:20:07 +0500 Subject: [PATCH 7/7] Adds test cases --- home/tests/tests.py | 23 +++++++++++++++++++ iogt/templates/wagtailadmin/generic/edit.html | 2 ++ 2 files changed, 25 insertions(+) diff --git a/home/tests/tests.py b/home/tests/tests.py index 41303c695..8a2405b71 100644 --- a/home/tests/tests.py +++ b/home/tests/tests.py @@ -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 @@ -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): @@ -98,3 +101,23 @@ def test_media_block_translation_of_bengali_language(self): self.assertEqual(response.status_code, 200) self.assertContains(response, f"উপরের ভিডিও দেখা না গেলে এর পরিবর্তে এটা ডাউনলোড করুন", count=1) self.assertContains(response, f"উপরের অডিও শুনতে না পেলে এর পরিবর্তে এটা ডাউনলোড করুন", 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') diff --git a/iogt/templates/wagtailadmin/generic/edit.html b/iogt/templates/wagtailadmin/generic/edit.html index f6346bd77..3809a59ff 100644 --- a/iogt/templates/wagtailadmin/generic/edit.html +++ b/iogt/templates/wagtailadmin/generic/edit.html @@ -3,6 +3,8 @@ {% block actions %} + + {# TO DO: Update this file on wagtail upgrade #} {% if can_delete and site is None %} {{ view.delete_item_label }} {% elif can_delete and site is not None and not site.is_default_site %}