|
1 |
| -from django.core.checks import Error, register |
| 1 | +from django.core.checks import register |
2 | 2 |
|
| 3 | +from rest_registration.decorators import simple_check |
3 | 4 | from rest_registration.settings import registration_settings
|
4 | 5 |
|
5 | 6 |
|
6 |
| -def check(message, error_code): |
7 |
| - |
8 |
| - def decorator(predicate): |
9 |
| - |
10 |
| - def f(app_configs, **kwargs): |
11 |
| - errors = [] |
12 |
| - if not predicate(): |
13 |
| - errors.append( |
14 |
| - Error( |
15 |
| - message, |
16 |
| - hint=None, |
17 |
| - id='rest_registration.E{0}'.format(error_code), |
18 |
| - ) |
19 |
| - ) |
20 |
| - return errors |
21 |
| - |
22 |
| - return f |
23 |
| - |
24 |
| - return decorator |
| 7 | +class ErrorCode(object): |
| 8 | + NO_RESET_PASSWORD_VER_URL = 'E001' |
| 9 | + NO_REGISTER_VER_URL = 'E002' |
| 10 | + NO_REGISTER_EMAIL_VER_URL = 'E003' |
| 11 | + NO_VER_FROM_EMAIL = 'E004' |
25 | 12 |
|
26 | 13 |
|
27 | 14 | @register()
|
28 |
| -@check('RESET_PASSWORD_VERIFICATION_URL is not set', '001') |
| 15 | +@simple_check('RESET_PASSWORD_VERIFICATION_URL is not set', |
| 16 | + ErrorCode.NO_RESET_PASSWORD_VER_URL) |
29 | 17 | def reset_password_verification_url_check():
|
30 | 18 | return registration_settings.RESET_PASSWORD_VERIFICATION_URL
|
31 | 19 |
|
32 | 20 |
|
33 | 21 | @register()
|
34 |
| -@check('register verification is enabled,' |
35 |
| - ' but REGISTER_VERIFICATION_URL is not set', '002') |
| 22 | +@simple_check('register verification is enabled,' |
| 23 | + ' but REGISTER_VERIFICATION_URL is not set', |
| 24 | + ErrorCode.NO_REGISTER_VER_URL) |
36 | 25 | def register_verification_url_check():
|
37 | 26 | return (not registration_settings.REGISTER_VERIFICATION_ENABLED or
|
38 | 27 | registration_settings.REGISTER_VERIFICATION_URL)
|
39 | 28 |
|
40 | 29 |
|
41 | 30 | @register()
|
42 |
| -@check('register email verification is enabled,' |
43 |
| - ' but REGISTER_EMAIL_VERIFICATION_URL is not set', '003') |
| 31 | +@simple_check('register email verification is enabled,' |
| 32 | + ' but REGISTER_EMAIL_VERIFICATION_URL is not set', |
| 33 | + ErrorCode.NO_REGISTER_EMAIL_VER_URL) |
44 | 34 | def register_email_verification_url_check():
|
45 | 35 | return (not registration_settings.REGISTER_EMAIL_VERIFICATION_ENABLED or
|
46 | 36 | registration_settings.REGISTER_EMAIL_VERIFICATION_URL)
|
47 | 37 |
|
48 | 38 |
|
49 | 39 | @register()
|
50 |
| -@check('VERIFICATION_FROM_EMAIL is not set', '004') |
| 40 | +@simple_check('VERIFICATION_FROM_EMAIL is not set', |
| 41 | + ErrorCode.NO_VER_FROM_EMAIL) |
51 | 42 | def verification_from_check():
|
52 | 43 | return registration_settings.VERIFICATION_FROM_EMAIL
|
| 44 | + |
| 45 | + |
| 46 | +__ALL_CHECKS__ = [ |
| 47 | + reset_password_verification_url_check, |
| 48 | + register_verification_url_check, |
| 49 | + register_email_verification_url_check, |
| 50 | + verification_from_check, |
| 51 | +] |
0 commit comments