|
| 1 | +from dataclasses import dataclass |
| 2 | + |
1 | 3 | from django.conf import settings |
| 4 | +from .interface import DefaultConfig |
2 | 5 |
|
3 | 6 |
|
| 7 | +@dataclass |
4 | 8 | class GetFieldFromSettings: |
5 | 9 | """ |
6 | 10 | This class fetches the attributes that are defined in settings.py of your project by user OR Django itself. |
7 | | - self.default_configs : is a dict with keys as the names used in this app and values being a tuple of |
| 11 | + self.default_configs : is a dict with keys as the names used in this app and values being a tuple of |
8 | 12 | attributes defined in settings.py and their corresponding default values if not found. |
9 | | - |
10 | | - There is a special case in "get" method, if you set "VERIFICATION_SUCCESS_TEMPLATE" as None is settings.py, it |
| 13 | +
|
| 14 | + There is a special case in "get" method, if you set "VERIFICATION_SUCCESS_TEMPLATE" as None is settings.py, it |
11 | 15 | will skip the intermediate page where success information is displayed. (This is better explained in docs.) |
12 | 16 |
|
13 | | - The "get" method takes the name of the attributes as input, checks for it in settings.py, |
| 17 | + The "get" method takes the name of the attributes as input, checks for it in settings.py, |
14 | 18 | if found: |
15 | 19 | returns the corresponding value. |
16 | 20 | else: |
17 | 21 | returns the default value from "self.defaults_configs". |
18 | 22 | """ |
19 | 23 |
|
20 | | - def __init__(self): |
21 | | - |
| 24 | + def __post_init__(self): |
22 | 25 | self.defaults_configs = { |
23 | | - 'debug_settings': ( |
24 | | - 'DEBUG', |
25 | | - False |
26 | | - ), |
27 | | - |
28 | | - 'subject': ( |
29 | | - "SUBJECT", |
30 | | - "Email Verification Mail" |
31 | | - ), |
32 | | - |
33 | | - 'email_field_name': ( |
34 | | - "EMAIL_FIELD_NAME", |
35 | | - "email", |
36 | | - ), |
37 | | - |
38 | | - 'html_message_template': ( |
39 | | - "HTML_MESSAGE_TEMPLATE", |
40 | | - 'verify_email/email_verification_msg.html' |
41 | | - ), |
42 | | - |
43 | | - 'from_alias': ( |
44 | | - "DEFAULT_FROM_EMAIL", |
45 | | - |
46 | | - ), |
47 | | - |
48 | | - 'login_page': ( |
49 | | - 'LOGIN_URL', |
50 | | - 'accounts_login' |
51 | | - ), |
52 | | - |
53 | | - 'verification_success_template': ( |
54 | | - 'VERIFICATION_SUCCESS_TEMPLATE', |
55 | | - 'verify_email/email_verification_successful.html' |
56 | | - ), |
57 | | - |
58 | | - 'verification_success_msg': ( |
59 | | - 'VERIFICATION_SUCCESS_MSG', |
60 | | - "Your Email is verified successfully and account has been activated. " |
61 | | - "You can login with the credentials now..." |
62 | | - ), |
63 | | - |
64 | | - 'verification_failed_template': ( |
65 | | - 'VERIFICATION_FAILED_TEMPLATE', |
66 | | - 'verify_email/email_verification_failed.html' |
67 | | - ), |
68 | | - |
69 | | - 'link_expired_template': ( |
70 | | - 'LINK_EXPIRED_TEMPLATE', |
71 | | - 'verify_email/link_expired.html' |
72 | | - ), |
73 | | - |
74 | | - 'verification_failed_msg': ( |
75 | | - 'VERIFICATION_FAILED_MSG', |
76 | | - "There is something wrong with this link, can't verify the user..." |
77 | | - ), |
78 | | - 'request_new_email_template': ( |
79 | | - 'REQUEST_NEW_EMAIL_TEMPLATE', |
80 | | - 'verify_email/request_new_email.html' |
81 | | - ), |
82 | | - 'new_email_sent_template': ( |
83 | | - 'NEW_EMAIL_SENT_TEMPLATE', |
84 | | - 'verify_email/new_email_sent.html' |
85 | | - ), |
86 | | - |
87 | | - 'salt': ( |
88 | | - 'HASH_SALT', |
89 | | - None |
90 | | - ), |
91 | | - |
92 | | - 'sep': ( |
93 | | - 'SEPARATOR', |
94 | | - ':' |
95 | | - ), |
96 | | - |
97 | | - 'key': ( |
98 | | - 'HASHING_KEY', |
99 | | - None |
100 | | - ), |
101 | | - |
102 | | - 'max_age': ( |
103 | | - 'EXPIRE_AFTER', |
104 | | - None |
105 | | - ), |
106 | | - |
107 | | - 'max_retries': ( |
108 | | - 'MAX_RETRIES', |
109 | | - 2 |
110 | | - ) |
| 26 | + "debug": DefaultConfig(setting_field="DEBUG", default_value=False), |
| 27 | + "subject": DefaultConfig( |
| 28 | + setting_field="SUBJECT", default_value="Email Verification Mail" |
| 29 | + ), |
| 30 | + "email_field_name": DefaultConfig( |
| 31 | + setting_field="EMAIL_FIELD_NAME", default_value="email" |
| 32 | + ), |
| 33 | + "html_message_template": DefaultConfig( |
| 34 | + setting_field="HTML_MESSAGE_TEMPLATE", |
| 35 | + default_value="verify_email/email_verification_msg.html", |
| 36 | + ), |
| 37 | + "from_alias": DefaultConfig( |
| 38 | + setting_field="DEFAULT_FROM_EMAIL", |
| 39 | + default_value="<noreply<[email protected]>", |
| 40 | + ), |
| 41 | + "login_page": DefaultConfig( |
| 42 | + setting_field="LOGIN_URL", default_value="accounts_login" |
| 43 | + ), |
| 44 | + "verification_success_template": DefaultConfig( |
| 45 | + setting_field="VERIFICATION_SUCCESS_TEMPLATE", |
| 46 | + default_value="verify_email/email_verification_successful.html", |
| 47 | + ), |
| 48 | + "verification_success_msg": DefaultConfig( |
| 49 | + setting_field="VERIFICATION_SUCCESS_MSG", |
| 50 | + default_value=( |
| 51 | + "Your Email is verified successfully and account has been activated." |
| 52 | + " You can login with the credentials now..." |
| 53 | + ), |
| 54 | + ), |
| 55 | + "verification_failed_template": DefaultConfig( |
| 56 | + setting_field="VERIFICATION_FAILED_TEMPLATE", |
| 57 | + default_value="verify_email/email_verification_failed.html", |
| 58 | + ), |
| 59 | + "link_expired_template": DefaultConfig( |
| 60 | + setting_field="LINK_EXPIRED_TEMPLATE", |
| 61 | + default_value="verify_email/link_expired.html", |
| 62 | + ), |
| 63 | + "verification_failed_msg": DefaultConfig( |
| 64 | + setting_field="VERIFICATION_FAILED_MSG", |
| 65 | + default_value="There is something wrong with this link, can't verify the user...", |
| 66 | + ), |
| 67 | + "request_new_email_template": DefaultConfig( |
| 68 | + setting_field="REQUEST_NEW_EMAIL_TEMPLATE", |
| 69 | + default_value="verify_email/request_new_email.html", |
| 70 | + ), |
| 71 | + "new_email_sent_template": DefaultConfig( |
| 72 | + setting_field="NEW_EMAIL_SENT_TEMPLATE", |
| 73 | + default_value="verify_email/new_email_sent.html", |
| 74 | + ), |
| 75 | + "salt": DefaultConfig(setting_field="HASH_SALT", default_value=None), |
| 76 | + "sep": DefaultConfig(setting_field="SEPARATOR", default_value=":"), |
| 77 | + "key": DefaultConfig(setting_field="HASHING_KEY", default_value=None), |
| 78 | + "max_age": DefaultConfig(setting_field="EXPIRE_AFTER", default_value=None), |
| 79 | + "max_retries": DefaultConfig(setting_field="MAX_RETRIES", default_value=2), |
111 | 80 | } |
112 | 81 |
|
113 | 82 | def get(self, field_name, raise_exception=True, default_type=str): |
114 | 83 | attr = getattr( |
115 | 84 | settings, |
116 | | - self.defaults_configs[field_name][0], # get field from settings |
117 | | - self.defaults_configs[field_name][1] # get default value if field not defined |
| 85 | + self.defaults_configs[field_name].setting_field, # get field from settings |
| 86 | + self.defaults_configs[ |
| 87 | + field_name |
| 88 | + ].default_value, # get default value if field not defined |
118 | 89 | ) |
119 | | - if (attr == '' or attr is None or not isinstance(field_name, default_type)) and raise_exception: |
120 | | - if field_name == 'verification_success_template' and attr is None: |
| 90 | + if not attr and not isinstance(field_name, default_type) and raise_exception: |
| 91 | + if field_name == "verification_success_template" and attr is None: |
121 | 92 | return None |
122 | 93 | raise AttributeError |
123 | 94 | return attr |
0 commit comments