From 3e33c75e11a0b1999c74e069cfd401b332b824cc Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Mon, 2 Nov 2020 18:05:42 +0530 Subject: [PATCH] Add app settings - helps in placing all default settings in one place. Also, change setting attribute for customizing allowed flags to `FLAG_ALLOWED` from `FLAGS_ALLOWED` -> maintains consistency, all custom settings will have a FLAG prefix --- docs/settings.rst | 2 +- flag/conf/__init__.py | 20 ++++++++++++++++++++ flag/conf/defaults.py | 10 ++++++++++ flag/models.py | 9 +++------ flag/templatetags/flag_tags.py | 2 +- testapp/settings.py | 3 ++- tests/base.py | 7 ++++--- tests/test_models.py | 24 ++++++++++++++---------- tests/test_signals.py | 5 ++--- tests/test_template_tags.py | 2 +- tests/test_views.py | 3 +-- 11 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 flag/conf/__init__.py create mode 100644 flag/conf/defaults.py diff --git a/docs/settings.rst b/docs/settings.rst index 19a50d6..1bbe12f 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -3,7 +3,7 @@ Settings django-flag-app has a few configuration options that allow you to customize it. -FLAGS_ALLOWED +FLAG_ALLOWED `````````````` The number of flags allowed before a content is set as flagged. Defaults to ``10``. diff --git a/flag/conf/__init__.py b/flag/conf/__init__.py new file mode 100644 index 0000000..685f438 --- /dev/null +++ b/flag/conf/__init__.py @@ -0,0 +1,20 @@ +from django.conf import settings as django_settings +from django.utils.functional import LazyObject + +from flag.conf import defaults as flag_settings + + +class LazySettings(LazyObject): + def _setup(self): + self._wrapped = Settings(flag_settings, django_settings) + + +class Settings(object): + def __init__(self, *args): + for item in args: + for attr in dir(item): + if attr == attr.upper(): + setattr(self, attr, getattr(item, attr)) + + +settings = LazySettings() diff --git a/flag/conf/defaults.py b/flag/conf/defaults.py new file mode 100644 index 0000000..7931948 --- /dev/null +++ b/flag/conf/defaults.py @@ -0,0 +1,10 @@ +from django.utils.translation import gettext_lazy as _ + +# reason displayed when flagging an object +FLAG_REASONS = [ + (1, _("Spam | Exists only to promote a service ")), + (2, _("Abusive | Intended at promoting hatred")), + ] + +# number of flags before an object is marked as flagged +FLAG_ALLOWED = 10 diff --git a/flag/models.py b/flag/models.py index 2257bf1..771a713 100644 --- a/flag/models.py +++ b/flag/models.py @@ -1,7 +1,6 @@ from collections import namedtuple from enum import IntEnum, unique -from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType @@ -11,6 +10,7 @@ from django.utils.translation import gettext_lazy as _ from flag.managers import FlagInstanceManager, FlagManager +from flag.conf import settings User = get_user_model() @@ -82,7 +82,7 @@ def toggle_state(self, state, moderator): self.save() def toggle_flagged_state(self): - allowed_flags = getattr(settings, 'FLAGS_ALLOWED', 10) + allowed_flags = settings.FLAG_ALLOWED self.refresh_from_db() field = 'state' if self.count > allowed_flags and ( @@ -99,10 +99,7 @@ def is_flagged(self): class FlagInstance(models.Model): - REASON = getattr(settings, "FLAG_REASONS", [ - (1, _("Spam | Exists only to promote a service ")), - (2, _("Abusive | Intended at promoting hatred")), - ]) + REASON = settings.FLAG_REASONS REASON.append((100, _('Something else'))) diff --git a/flag/templatetags/flag_tags.py b/flag/templatetags/flag_tags.py index ef4c4b5..4eb62dc 100644 --- a/flag/templatetags/flag_tags.py +++ b/flag/templatetags/flag_tags.py @@ -1,9 +1,9 @@ from django import template -from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.utils.translation import gettext_lazy as _ from flag.models import FlagInstance +from flag.conf import settings register = template.Library() diff --git a/testapp/settings.py b/testapp/settings.py index 3d4b42b..6f9ae02 100644 --- a/testapp/settings.py +++ b/testapp/settings.py @@ -142,5 +142,6 @@ EMAIL_PORT = 587 EMAIL_USE_TLS = True EMAIL_USE_SSL = False + # flag app -FLAGS_ALLOWED = 0 +FLAG_ALLOWED = 0 diff --git a/tests/base.py b/tests/base.py index 2c2fe9e..85f94d8 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,8 +1,10 @@ +from unittest.mock import patch + from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType from django.shortcuts import reverse -from django.test import Client, RequestFactory, TestCase, override_settings +from django.test import Client, RequestFactory, TestCase from rest_framework.test import APITestCase from flag.models import Flag, FlagInstance @@ -52,6 +54,7 @@ def setUp(self): 'reason': FlagInstance.reason_values[0], 'info': '' } + self.addCleanup(patch.stopall) @classmethod def create_post(cls): @@ -94,7 +97,6 @@ def set_flag(cls, model_obj=None, user=None, reason=None, info=None): ) -@override_settings(FLAGS_ALLOWED=0) class BaseFlagTest(BaseFlagTestUtils, TestCase): pass @@ -139,6 +141,5 @@ def setUp(self): self.factory = RequestFactory() -@override_settings(FLAGS_ALLOWED=0) class BaseFlagAPITest(BaseFlagTestUtils, APITestCase): pass diff --git a/tests/test_models.py b/tests/test_models.py index c47e673..b571d06 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,5 +1,8 @@ +from unittest.mock import patch + from django.core.exceptions import ValidationError +from flag.conf import settings from tests.base import BaseFlagModelTest, Flag, FlagInstance @@ -62,18 +65,19 @@ def test_toggle_flagged_state(self): self.assertEqual(self.flag.state, Flag.State.FLAGGED.value) - with self.settings(FLAGS_ALLOWED=1): - self.flag.count = 0 - self.flag.save() - self.flag.increase_count() - self.flag.toggle_flagged_state() + # set FLAG_ALLOWED to 1 + patch.object(settings, 'FLAG_ALLOWED', 1).start() + self.flag.count = 0 + self.flag.save() + self.flag.increase_count() + self.flag.toggle_flagged_state() - self.assertEqual(self.flag.state, Flag.State.UNFLAGGED.value) - # flag once more to toggle the state - self.flag.increase_count() - self.flag.toggle_flagged_state() + self.assertEqual(self.flag.state, Flag.State.UNFLAGGED.value) + # flag once more to toggle the state + self.flag.increase_count() + self.flag.toggle_flagged_state() - self.assertEqual(self.flag.state, Flag.State.FLAGGED.value) + self.assertEqual(self.flag.state, Flag.State.FLAGGED.value) class FlagManagerTest(BaseFlagModelTest): diff --git a/tests/test_signals.py b/tests/test_signals.py index e4bbebe..328c99a 100644 --- a/tests/test_signals.py +++ b/tests/test_signals.py @@ -1,8 +1,7 @@ from unittest.mock import patch -from django.conf import settings - from flag.signals import adjust_flagged_content +from flag.conf import settings from tests.base import BaseFlagModelTest, Flag, FlagInstance @@ -35,7 +34,7 @@ def test_unflagged_signal(self): self.assertEqual(flag.count, 0) self.assertEqual(flag.state, Flag.State.UNFLAGGED.value) - @patch.object(settings, 'FLAGS_ALLOWED', 1) + @patch.object(settings, 'FLAG_ALLOWED', 1) def test_adjust_flagged_contents(self): post_1 = self.create_post() post_2 = self.create_post() diff --git a/tests/test_template_tags.py b/tests/test_template_tags.py index ba6c2c9..0051a0c 100644 --- a/tests/test_template_tags.py +++ b/tests/test_template_tags.py @@ -1,8 +1,8 @@ from unittest.mock import patch -from django.conf import settings from django.core.exceptions import ImproperlyConfigured +from flag.conf import settings from flag.templatetags.flag_tags import get_app_name, get_model_name, has_flagged, render_flag_form, get_login_url from tests.base import BaseTemplateTagsTest, FlagInstance diff --git a/tests/test_views.py b/tests/test_views.py index 4d6468d..3ed84c3 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,5 +1,4 @@ -from django.conf import settings - +from flag.conf import settings from tests.base import BaseFlagViewTest, Client, Flag, FlagInstance