Browse Source

Add documentation for new registration hooks

Alec Nikolas Reiter 7 years ago
parent
commit
0120f4138a

+ 9 - 0
docs/development/api/registration.rst

@@ -19,6 +19,12 @@ Registration Interfaces
 .. autoclass:: UserRegistrationService
 .. autoclass:: UserRegistrationService
     :members:
     :members:
 
 
+.. autoclass:: RegistrationFailureHandler
+    :members:
+
+.. autoclass:: RegistrationPostProcessor
+    :members:
+
 
 
 Registration Provided Implementations
 Registration Provided Implementations
 -------------------------------------
 -------------------------------------
@@ -29,4 +35,7 @@ Registration Provided Implementations
 .. autoclass:: UsernameValidator
 .. autoclass:: UsernameValidator
 .. autoclass:: UsernameUniquenessValidator
 .. autoclass:: UsernameUniquenessValidator
 .. autoclass:: EmailUniquenessValidator
 .. autoclass:: EmailUniquenessValidator
+.. autoclass:: SendActivationPostProcessor
+.. autoclass:: AutologinPostProcessor
+.. autoclass:: AutoActivateUserPostProcessor
 .. autoclass:: RegistrationService
 .. autoclass:: RegistrationService

+ 15 - 0
docs/development/hooks/event.rst

@@ -5,11 +5,26 @@
 FlaskBB Event Hooks
 FlaskBB Event Hooks
 ===================
 ===================
 
 
+Post and Topic Events
+---------------------
+
 .. autofunction:: flaskbb_event_post_save_before
 .. autofunction:: flaskbb_event_post_save_before
 .. autofunction:: flaskbb_event_post_save_after
 .. autofunction:: flaskbb_event_post_save_after
 .. autofunction:: flaskbb_event_topic_save_before
 .. autofunction:: flaskbb_event_topic_save_before
 .. autofunction:: flaskbb_event_topic_save_after
 .. autofunction:: flaskbb_event_topic_save_after
+
+Registration Events
+-------------------
+
 .. autofunction:: flaskbb_event_user_registered
 .. autofunction:: flaskbb_event_user_registered
+.. autofunction:: flaskbb_gather_registration_validators
+.. autofunction:: flaskbb_registration_post_processor
+.. autofunction:: flaskbb_registration_failure_handler
+
+
+Authentication Events
+---------------------
+
 .. autofunction:: flaskbb_authenticate
 .. autofunction:: flaskbb_authenticate
 .. autofunction:: flaskbb_post_authenticate
 .. autofunction:: flaskbb_post_authenticate
 .. autofunction:: flaskbb_authentication_failed
 .. autofunction:: flaskbb_authentication_failed

+ 12 - 0
flaskbb/auth/services/registration.py

@@ -131,6 +131,12 @@ class EmailUniquenessValidator(UserValidator):
 
 
 
 
 class SendActivationPostProcessor(RegistrationPostProcessor):
 class SendActivationPostProcessor(RegistrationPostProcessor):
+    """
+    Sends an activation request after registration
+
+    :param account_activator:
+    :type account_activator: :class:`~flaskbb.core.auth.activation.AccountActivator`
+    """  # noqa
 
 
     def __init__(self, account_activator):
     def __init__(self, account_activator):
         self.account_activator = account_activator
         self.account_activator = account_activator
@@ -147,6 +153,9 @@ class SendActivationPostProcessor(RegistrationPostProcessor):
 
 
 
 
 class AutologinPostProcessor(RegistrationPostProcessor):
 class AutologinPostProcessor(RegistrationPostProcessor):
+    """
+    Automatically logs a user in after registration
+    """
 
 
     def post_process(self, user):
     def post_process(self, user):
         login_user(user)
         login_user(user)
@@ -157,6 +166,9 @@ class AutoActivateUserPostProcessor(RegistrationPostProcessor):
     """
     """
     Automatically marks the user as activated if activation isn't required
     Automatically marks the user as activated if activation isn't required
     for the forum.
     for the forum.
+
+    :param db: Configured Flask-SQLAlchemy extension object
+    :param config: Current flaskbb configuration object
     """
     """
 
 
     def __init__(self, db, config):
     def __init__(self, db, config):

+ 13 - 0
flaskbb/core/auth/registration.py

@@ -56,6 +56,13 @@ class RegistrationFailureHandler(ABC):
 
 
     @abstractmethod
     @abstractmethod
     def handle_failure(self, user_info, failures):
     def handle_failure(self, user_info, failures):
+        """
+        This method is abstract.
+
+        :param user_info: The provided registration information.
+        :param failures: Tuples of (attribute, message) from the failure
+        :type user_info: :class:`~flaskbb.core.auth.registration.UserRegistrationInfo`
+        """  # noqa
         pass
         pass
 
 
     def __call__(self, user_info, failures):
     def __call__(self, user_info, failures):
@@ -71,6 +78,12 @@ class RegistrationPostProcessor(ABC):
 
 
     @abstractmethod
     @abstractmethod
     def post_process(self, user):
     def post_process(self, user):
+        """
+        This method is abstract.
+
+        :param user: The registered, persisted user.
+        :type user: :class:`~flaskbb.user.models.User`
+        """
         pass
         pass
 
 
     def __call__(self, user):
     def __call__(self, user):

+ 41 - 6
flaskbb/plugins/spec.py

@@ -202,6 +202,11 @@ def flaskbb_event_topic_save_after(topic, is_new):
 def flaskbb_event_user_registered(username):
 def flaskbb_event_user_registered(username):
     """Hook for handling events after a user is registered
     """Hook for handling events after a user is registered
 
 
+    .. warning::
+
+        This hook is deprecated in favor of
+        :func:`~flaskbb.plugins.spec.flaskbb_registration_post_processor`
+
     :param username: The username of the newly registered user.
     :param username: The username of the newly registered user.
     """
     """
 
 
@@ -223,13 +228,16 @@ def flaskbb_gather_registration_validators():
                 raise ValidationError(('username', 'Cannot name user fred'))
                 raise ValidationError(('username', 'Cannot name user fred'))
 
 
         @impl
         @impl
-        def flaskbb_gather_validate_user_registration():
-            return cannot_be_named_fred
+        def flaskbb_gather_registration_validators():
+            return [cannot_be_named_fred]
 
 
     .. note::
     .. note::
 
 
         This is implemented as a hook that returns callables since the
         This is implemented as a hook that returns callables since the
-        callables are designed to raise exceptions.
+        callables are designed to raise exceptions that are aggregated to
+        form the failure message for the registration response.
+
+    See Also: :class:`~flaskbb.core.auth.registration.UserValidator`
     """
     """
 
 
 
 
@@ -239,13 +247,38 @@ def flaskbb_registration_failure_handler(user_info, failures):
     Hook for dealing with user registration failures, receives the info
     Hook for dealing with user registration failures, receives the info
     that user attempted to register with as well as the errors that failed
     that user attempted to register with as well as the errors that failed
     the registration.
     the registration.
-    """
+
+    Example::
+
+        from .utils import fuzz_username
+
+        def has_already_registered(failures):
+            return any(
+                attr = "username" and "already registered" in msg
+                for (attr, msg) in failures
+            )
+
+
+        def suggest_alternate_usernames(user_info, failures):
+            if has_already_registered(failures):
+                suggestions = fuzz_username(user_info.username)
+                failures.append(("username", "Try: {}".format(suggestions)))
+
+
+        @impl
+        def flaskbb_registration_failure_handler(user_info, failures):
+            suggest_alternate_usernames(user_info, failures)
+
+    See Also: :class:`~flaskbb.core.auth.registration.RegistrationFailureHandler`
+    """  # noqa
 
 
 
 
 @spec
 @spec
 def flaskbb_registration_post_processor(user):
 def flaskbb_registration_post_processor(user):
     """
     """
-    Hook for handling actions after a user has successfully registered.
+    Hook for handling actions after a user has successfully registered. This
+    spec receives the user object after it has been successfully persisted
+    to the database.
 
 
     Example::
     Example::
 
 
@@ -255,7 +288,9 @@ def flaskbb_registration_post_processor(user):
         @impl
         @impl
         def flaskbb_registration_post_processor(user):
         def flaskbb_registration_post_processor(user):
             greet_user(user)
             greet_user(user)
-    """
+
+    See Also: :class:`~flaskbb.core.auth.registration.RegistrationPostProcessor`
+    """  # noqa
 
 
 
 
 @spec(firstresult=True)
 @spec(firstresult=True)