Browse Source

Small code cleanups before removing global state from create_user

rafalp 6 years ago
parent
commit
ffe17db1a2

+ 3 - 0
misago/conf/__init__.py

@@ -3,3 +3,6 @@ from .gateway import settings, db_settings  # noqa
 default_app_config = 'misago.conf.apps.MisagoConfConfig'
 default_app_config = 'misago.conf.apps.MisagoConfConfig'
 
 
 SETTINGS_CACHE = "settings"
 SETTINGS_CACHE = "settings"
+
+# Feature flag for easy changing between old and new settings mechanism
+ENABLE_GLOBAL_STATE = True  # FIXME: delete after API changes

+ 8 - 0
misago/conf/dbsettings.py

@@ -46,6 +46,10 @@ class DBSettingsDeprecated(object):
         return public_settings
         return public_settings
 
 
     def get_lazy_setting(self, setting):
     def get_lazy_setting(self, setting):
+        from . import ENABLE_GLOBAL_STATE
+        if not ENABLE_GLOBAL_STATE:
+            raise Exception("Trying to access lazy dynamic setting: %s" % name)
+
         from .models import Setting
         from .models import Setting
 
 
         try:
         try:
@@ -64,6 +68,10 @@ class DBSettingsDeprecated(object):
         cache.delete(CACHE_KEY)
         cache.delete(CACHE_KEY)
 
 
     def __getattr__(self, attr):
     def __getattr__(self, attr):
+        from . import ENABLE_GLOBAL_STATE
+        if not ENABLE_GLOBAL_STATE:
+            raise Exception("Trying to access dynamic setting: %s" % name)
+
         try:
         try:
             return self._settings[attr]['value']
             return self._settings[attr]['value']
         except KeyError:
         except KeyError:

+ 4 - 0
misago/conf/gateway.py

@@ -6,6 +6,10 @@ from .dbsettings import db_settings
 
 
 class SettingsGateway(object):
 class SettingsGateway(object):
     def __getattr__(self, name):
     def __getattr__(self, name):
+        from . import ENABLE_GLOBAL_STATE
+        if not ENABLE_GLOBAL_STATE and name.lower() == name:
+            raise Exception("Trying to access dynamic setting: %s" % name)
+
         try:
         try:
             return getattr(dj_settings, name)
             return getattr(dj_settings, name)
         except AttributeError:
         except AttributeError:

+ 3 - 2
misago/users/models/user.py

@@ -26,8 +26,9 @@ from .rank import Rank
 class UserManager(BaseUserManager):
 class UserManager(BaseUserManager):
     @transaction.atomic
     @transaction.atomic
     def create_user(
     def create_user(
-            self, username, email, password=None, create_audit_trail=False,
-            joined_from_ip=None, set_default_avatar=False, **extra_fields):
+        self, username, email, password=None, create_audit_trail=False,
+        joined_from_ip=None, set_default_avatar=False, **extra_fields
+    ):
         from misago.users.validators import validate_email, validate_username
         from misago.users.validators import validate_email, validate_username
 
 
         email = self.normalize_email(email)
         email = self.normalize_email(email)

+ 16 - 14
misago/users/validators.py

@@ -22,6 +22,15 @@ UserModel = get_user_model()
 
 
 
 
 # E-mail validators
 # E-mail validators
+
+def validate_email(value, exclude=None):
+    """shortcut function that does complete validation of email"""
+    validate_email_content(value)
+    validate_email_available(value, exclude)
+    validate_email_banned(value)
+
+
+
 def validate_email_available(value, exclude=None):
 def validate_email_available(value, exclude=None):
     try:
     try:
         user = UserModel.objects.get_by_email(value)
         user = UserModel.objects.get_by_email(value)
@@ -41,14 +50,15 @@ def validate_email_banned(value):
             raise ValidationError(_("This e-mail address is not allowed."))
             raise ValidationError(_("This e-mail address is not allowed."))
 
 
 
 
-def validate_email(value, exclude=None):
-    """shortcut function that does complete validation of email"""
-    validate_email_content(value)
-    validate_email_available(value, exclude)
-    validate_email_banned(value)
+# Username validators
+def validate_username(value, exclude=None):
+    """shortcut function that does complete validation of username"""
+    validate_username_length(value)
+    validate_username_content(value)
+    validate_username_available(value, exclude)
+    validate_username_banned(value)
 
 
 
 
-# Username validators
 def validate_username_available(value, exclude=None):
 def validate_username_available(value, exclude=None):
     try:
     try:
         user = UserModel.objects.get_by_username(value)
         user = UserModel.objects.get_by_username(value)
@@ -91,14 +101,6 @@ def validate_username_length(value):
         raise ValidationError(message % {'limit_value': settings.username_length_max})
         raise ValidationError(message % {'limit_value': settings.username_length_max})
 
 
 
 
-def validate_username(value, exclude=None):
-    """shortcut function that does complete validation of username"""
-    validate_username_length(value)
-    validate_username_content(value)
-    validate_username_available(value, exclude)
-    validate_username_banned(value)
-
-
 # New account validators
 # New account validators
 SFS_API_URL = 'http://api.stopforumspam.org/api?email=%(email)s&ip=%(ip)s&f=json&confidence'  # noqa
 SFS_API_URL = 'http://api.stopforumspam.org/api?email=%(email)s&ip=%(ip)s&f=json&confidence'  # noqa