Skip to content

Commit

Permalink
Add app settings
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
abhiabhi94 committed Nov 3, 2020
1 parent b3324bc commit 3e33c75
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down
20 changes: 20 additions & 0 deletions flag/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions flag/conf/defaults.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 3 additions & 6 deletions flag/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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 (
Expand All @@ -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')))

Expand Down
2 changes: 1 addition & 1 deletion flag/templatetags/flag_tags.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
3 changes: 2 additions & 1 deletion testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

# flag app
FLAGS_ALLOWED = 0
FLAG_ALLOWED = 0
7 changes: 4 additions & 3 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,6 +54,7 @@ def setUp(self):
'reason': FlagInstance.reason_values[0],
'info': ''
}
self.addCleanup(patch.stopall)

@classmethod
def create_post(cls):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -139,6 +141,5 @@ def setUp(self):
self.factory = RequestFactory()


@override_settings(FLAGS_ALLOWED=0)
class BaseFlagAPITest(BaseFlagTestUtils, APITestCase):
pass
24 changes: 14 additions & 10 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_template_tags.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 1 addition & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.conf import settings

from flag.conf import settings
from tests.base import BaseFlagViewTest, Client, Flag, FlagInstance


Expand Down

0 comments on commit 3e33c75

Please sign in to comment.