|
1 | | -from django.utils import timezone |
| 1 | +import logging |
| 2 | +from dataclasses import dataclass |
| 3 | + |
2 | 4 | from .token_manager import TokenManager |
| 5 | +from django.utils import timezone |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
3 | 8 |
|
4 | 9 |
|
5 | | -class _UserActivationProcess: |
| 10 | +@dataclass |
| 11 | +class UserActivationProcess: |
6 | 12 | """ |
7 | | - This class is pretty self.explanatory... |
| 13 | + This class handles the process of activating a user account |
| 14 | + by verifying a token associated with the user's email. |
| 15 | +
|
| 16 | + Attributes |
| 17 | + ---------- |
| 18 | + token_manager : TokenManager |
| 19 | + An instance of TokenManager used to manage token operations. |
| 20 | +
|
| 21 | + Methods |
| 22 | + ------- |
| 23 | + activate_user(encoded_email: str, encoded_token: str) -> None: |
| 24 | + Activates the user account after verifying the provided token. |
| 25 | +
|
8 | 26 | Exceptions Raised |
9 | 27 | ------------------ |
10 | | - - MaxRetriesExceeded (by) verify_user() |
11 | | - - BadSignature (by) verify_user() |
12 | | - - SignatureExpired (by) verify_user() |
13 | | - - ValueError (by) verify_user() |
14 | | - - TypeError (by) verify_user() |
15 | | - - InvalidToken (by) verify_user() |
| 28 | + - MaxRetriesExceeded: Raised when the maximum number of retries for verification is exceeded. |
| 29 | + - BadSignature: Raised when the token signature is invalid. |
| 30 | + - SignatureExpired: Raised when the token has expired. |
| 31 | + - ValueError: Raised for invalid values during verification. |
| 32 | + - TypeError: Raised for incorrect types during verification. |
| 33 | + - InvalidToken: Raised when the provided token is invalid. |
16 | 34 | """ |
17 | 35 |
|
18 | | - def __init__(self): |
19 | | - self.token_manager = TokenManager() |
| 36 | + token_manager: TokenManager = TokenManager() |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def activate_user(cls, encoded_email: str, encoded_token: str) -> None: |
| 40 | + """ |
| 41 | + Steps to activate a user account: |
| 42 | + 1. Verify the token. |
| 43 | + 2. Set the user as active. |
| 44 | + 3. Update the last login time. |
20 | 45 |
|
21 | | - @staticmethod |
22 | | - def __activate_user(user): |
23 | | - user.is_active = True |
24 | | - user.last_login = timezone.now() |
25 | | - user.save() |
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + encoded_email : str |
| 49 | + The encoded email address of the user. |
| 50 | + encoded_token : str |
| 51 | + The encoded token for user verification. |
26 | 52 |
|
27 | | - def verify_token(self, useremail, usertoken): |
28 | | - user = self.token_manager.decrypt_link(useremail, usertoken) |
29 | | - if user: |
30 | | - self.__activate_user(user) |
31 | | - return True |
32 | | - return False |
| 53 | + Raises |
| 54 | + ------ |
| 55 | + MaxRetriesExceeded |
| 56 | + If the maximum number of retries for token verification is exceeded. |
| 57 | + BadSignature |
| 58 | + If the token signature is invalid. |
| 59 | + SignatureExpired |
| 60 | + If the token has expired. |
| 61 | + ValueError |
| 62 | + If an invalid value is encountered during verification. |
| 63 | + TypeError |
| 64 | + If an incorrect type is provided. |
| 65 | + InvalidToken |
| 66 | + If the provided token is invalid. |
33 | 67 |
|
| 68 | + Notes |
| 69 | + ----- |
| 70 | + This method instantiates the class to access its methods. This |
| 71 | + approach may be revised in the future as part of the class design |
| 72 | + considerations. |
| 73 | + """ |
| 74 | + self = cls() # Consider if instantiation is necessary here |
| 75 | + try: |
| 76 | + # Verify token and retrieve user object |
| 77 | + user = self.token_manager.decrypt_token_and_get_user( |
| 78 | + encoded_email, encoded_token |
| 79 | + ) |
34 | 80 |
|
35 | | -def verify_user(useremail, usertoken): |
36 | | - return _UserActivationProcess().verify_token(useremail, usertoken) |
| 81 | + # Activate the user account |
| 82 | + user.is_active = True |
| 83 | + user.last_login = timezone.now() |
| 84 | + user.save() |
| 85 | + except Exception as err: |
| 86 | + logger.exception(err) |
| 87 | + # Perform any necessary cleanup if required |
| 88 | + raise |
0 commit comments