Coverage for django_napse/core/settings.py: 59%

71 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1import json 

2import logging 

3from pathlib import Path 

4 

5import environ 

6from django.conf import settings 

7 

8from django_napse.utils.constants import EXCHANGES 

9from django_napse.utils.errors import NapseError 

10 

11logger = logging.getLogger("django_napse") 

12 

13 

14class DjangoNapseSettings: 

15 """Main django_napse settings.""" 

16 

17 @property 

18 def NAPSE_EXCHANGE_CONFIGS(self) -> dict[str, dict[str, any]]: # noqa: D102 

19 return getattr( 

20 settings, 

21 "NAPSE_EXCHANGE_CONFIGS", 

22 { 

23 "BINANCE": { 

24 "description": "Binance exchange. More info: https://www.binance.com/en", 

25 }, 

26 }, 

27 ) 

28 

29 @property 

30 def NAPSE_IS_IN_PIPELINE(self) -> bool: # noqa: D102 

31 return environ.Env().bool("NAPSE_IS_IN_PIPELINE", default=False) 

32 

33 @property 

34 def NAPSE_SECRETS_FILE_PATH(self) -> Path: # noqa: D102 

35 if isinstance(settings.NAPSE_SECRETS_FILE_PATH, str): 

36 return Path(settings.NAPSE_SECRETS_FILE_PATH).absolute() 

37 

38 if isinstance(settings.NAPSE_SECRETS_FILE_PATH, Path): 

39 return settings.NAPSE_SECRETS_FILE_PATH 

40 

41 error_msg: str = "NAPSE_SECRETS_FILE_PATH must be a string or a Path object." 

42 raise ValueError(error_msg) 

43 

44 @property 

45 def NAPSE_ENV_FILE_PATH(self) -> Path: # noqa: D102 

46 if isinstance(settings.NAPSE_ENV_FILE_PATH, str): 

47 return Path(settings.NAPSE_ENV_FILE_PATH).absolute() 

48 

49 if isinstance(settings.NAPSE_ENV_FILE_PATH, Path): 

50 return settings.NAPSE_ENV_FILE_PATH 

51 

52 error_msg: str = "NAPSE_ENV_FILE_PATH must be a string or a Path object." 

53 raise ValueError(error_msg) 

54 

55 @property 

56 def NAPSE_EXCHANGES_TO_TEST(self) -> list[str]: # noqa: D102 

57 return getattr(settings, "NAPSE_EXCHANGES_TO_TEST", ["BINANCE"]) 

58 

59 @property 

60 def NAPSE_MASTER_KEY(self) -> str: # noqa: D102 

61 return getattr(settings, "NAPSE_MASTER_KEY", "insecure_master_key") 

62 

63 

64napse_settings = DjangoNapseSettings() 

65 

66if settings.configured: 

67 for app in [ 

68 "rest_framework", 

69 "rest_framework_api_key", 

70 "django_celery_beat", 

71 "corsheaders", 

72 ]: 

73 if app not in settings.INSTALLED_APPS: 

74 warning = f"{app} not found in settings.INSTALLED_APPS. Please add it to your settings file." 

75 logger.warning(warning) 

76 settings.INSTALLED_APPS += (app,) 

77 

78 if "REST_FRAMEWORK" not in settings.__dir__(): 

79 warning = "REST_FRAMEWORK not found in settings. Please add it to your settings file." 

80 logger.warning(warning) 

81 settings.REST_FRAMEWORK = { 

82 "DEFAULT_PERMISSION_CLASSES": ["django_napse.api.custom_permissions.HasAdminPermission"], 

83 } 

84 for permission in ["django_napse.api.custom_permissions.HasAdminPermission"]: 

85 if permission not in settings.REST_FRAMEWORK["DEFAULT_PERMISSION_CLASSES"]: 

86 warning = f"{permission} not found in settings. Please add it to your settings file." 

87 logger.warning(warning) 

88 

89 if "NAPSE_SECRETS_FILE_PATH" not in settings.__dir__(): 

90 warning = "NAPSE_SECRETS_FILE_PATH not found in settings. Please add it to your settings file." 

91 logger.warning(warning) 

92 if not napse_settings.NAPSE_SECRETS_FILE_PATH.exists(): 

93 logger.warning( 

94 "WARNING: No secrets file found at %s. Creating one now.", 

95 napse_settings.NAPSE_SECRETS_FILE_PATH, 

96 ) 

97 secrets_file = napse_settings.NAPSE_SECRETS_FILE_PATH.open("w") 

98 json.dump({"Exchange Accounts": {}}, secrets_file) 

99 secrets_file.close() 

100 

101 if "NAPSE_ENV_FILE_PATH" not in settings.__dir__(): 

102 warning = "NAPSE_ENV_FILE_PATH not found in settings. Please add it to your settings file." 

103 logger.warning(warning) 

104 

105 if not napse_settings.NAPSE_ENV_FILE_PATH.exists(): 

106 logger.warning( 

107 "WARNING: No env file found at %s. Creating one now.", 

108 napse_settings.NAPSE_ENV_FILE_PATH, 

109 ) 

110 env_file = napse_settings.NAPSE_ENV_FILE_PATH.open("w") 

111 env_file.write("") 

112 env_file.close() 

113 

114 if {*list(napse_settings.NAPSE_EXCHANGE_CONFIGS.keys())} != set(EXCHANGES): 

115 error_msg = "NAPSE_EXCHANGE_CONFIGS does not match the list of exchanges. Can't start the server." 

116 raise NapseError.SettingsError(error_msg)