settings.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import dj_database_url
  2. import os
  3. import sys
  4. from django.utils.translation import gettext_lazy as _
  5. from dotenv import load_dotenv
  6. # Tap vrobbler.conf if it's available
  7. if os.path.exists("vrobbler.conf"):
  8. load_dotenv("vrobbler.conf")
  9. elif os.path.exists("/etc/vrobbler.conf"):
  10. load_dotenv("/etc/vrobbler.conf")
  11. elif os.path.exists("/usr/local/etc/vrobbler.conf"):
  12. load_dotenv("/usr/local/etc/vrobbler.conf")
  13. from pathlib import Path
  14. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  15. BASE_DIR = Path(__file__).resolve().parent.parent
  16. # Quick-start development settings - unsuitable for production
  17. # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
  18. # SECURITY WARNING: keep the secret key used in production secret!
  19. SECRET_KEY = os.getenv("VROBBLER_SECRET_KEY", "not-a-secret-234lkjasdflj132")
  20. # SECURITY WARNING: don't run with debug turned on in production!
  21. DEBUG = os.getenv("VROBBLER_DEBUG", False)
  22. TESTING = len(sys.argv) > 1 and sys.argv[1] == "test"
  23. KEEP_DETAILED_SCROBBLE_LOGS = os.getenv(
  24. "VROBBLER_KEEP_DETAILED_SCROBBLE_LOGS", False
  25. )
  26. DELETE_STALE_SCROBBLES = os.getenv("VROBBLER_DELETE_STALE_SCROBBLES", True)
  27. TMDB_API_KEY = os.getenv("VROBBLER_TMDB_API_KEY", "")
  28. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  29. ALLOWED_HOSTS = ["*"]
  30. CSRF_TRUSTED_ORIGINS = [
  31. os.getenv("vrobbler_TRUSTED_ORIGINS", "http://localhost:8000")
  32. ]
  33. X_FRAME_OPTIONS = "SAMEORIGIN"
  34. REDIS_URL = os.getenv("VROBBLER_REDIS_URL", None)
  35. CELERY_TASK_ALWAYS_EAGER = os.getenv("VROBBLER_SKIP_CELERY", False)
  36. CELERY_BROKER_URL = REDIS_URL if REDIS_URL else "memory://localhost/"
  37. CELERY_RESULT_BACKEND = "django-db"
  38. CELERY_TIMEZONE = os.getenv("VROBBLER_TIME_ZONE", "EST")
  39. CELERY_TASK_TRACK_STARTED = True
  40. INSTALLED_APPS = [
  41. "django.contrib.admin",
  42. "django.contrib.auth",
  43. "django.contrib.contenttypes",
  44. "django.contrib.sessions",
  45. "django.contrib.messages",
  46. "django.contrib.staticfiles",
  47. "django.contrib.sites",
  48. "django_filters",
  49. "django_extensions",
  50. 'rest_framework.authtoken',
  51. "scrobbles",
  52. "videos",
  53. "rest_framework",
  54. "allauth",
  55. "allauth.account",
  56. "django_celery_results",
  57. ]
  58. SITE_ID = 1
  59. MIDDLEWARE = [
  60. "django.middleware.security.SecurityMiddleware",
  61. "whitenoise.middleware.WhiteNoiseMiddleware",
  62. "django.contrib.sessions.middleware.SessionMiddleware",
  63. "django.middleware.common.CommonMiddleware",
  64. "django.middleware.csrf.CsrfViewMiddleware",
  65. "django.contrib.auth.middleware.AuthenticationMiddleware",
  66. "django.contrib.messages.middleware.MessageMiddleware",
  67. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  68. "django.middleware.gzip.GZipMiddleware",
  69. ]
  70. ROOT_URLCONF = "vrobbler.urls"
  71. TEMPLATES = [
  72. {
  73. "BACKEND": "django.template.backends.django.DjangoTemplates",
  74. "DIRS": [str(BASE_DIR.joinpath("templates"))], # new
  75. "APP_DIRS": True,
  76. "OPTIONS": {
  77. "context_processors": [
  78. "django.template.context_processors.debug",
  79. "django.template.context_processors.request",
  80. "django.contrib.auth.context_processors.auth",
  81. "django.contrib.messages.context_processors.messages",
  82. ],
  83. },
  84. },
  85. ]
  86. WSGI_APPLICATION = "vrobbler.wsgi.application"
  87. DATABASES = {
  88. "default": dj_database_url.config(
  89. default=os.getenv("vrobbler_DATABASE_URL", "sqlite:///db.sqlite3"),
  90. conn_max_age=600,
  91. ),
  92. }
  93. if TESTING:
  94. DATABASES = {
  95. "default": dj_database_url.config(default="sqlite:///testdb.sqlite3")
  96. }
  97. CACHES = {
  98. "default": {
  99. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  100. "LOCATION": "unique-snowflake",
  101. }
  102. }
  103. if REDIS_URL:
  104. CACHES["default"][
  105. "BACKEND"
  106. ] = "django.core.cache.backends.redis.RedisCache"
  107. CACHES["default"]["LOCATION"] = REDIS_URL
  108. SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
  109. AUTHENTICATION_BACKENDS = [
  110. "django.contrib.auth.backends.ModelBackend",
  111. "allauth.account.auth_backends.AuthenticationBackend",
  112. ]
  113. REST_FRAMEWORK = {
  114. "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.AllowAny",),
  115. 'DEFAULT_AUTHENTICATION_CLASSES': [
  116. 'rest_framework.authentication.BasicAuthentication',
  117. 'rest_framework.authentication.TokenAuthentication',
  118. 'rest_framework.authentication.SessionAuthentication',
  119. ],
  120. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
  121. "DEFAULT_FILTER_BACKENDS": [
  122. "django_filters.rest_framework.DjangoFilterBackend"
  123. ],
  124. 'DEFAULT_PARSER_CLASSES': [
  125. 'rest_framework.parsers.JSONParser',
  126. ],
  127. 'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'vrobbler.negotiation.IgnoreClientContentNegotiation',
  128. "PAGE_SIZE": 100,
  129. }
  130. LOGIN_REDIRECT_URL = "/"
  131. AUTH_PASSWORD_VALIDATORS = [
  132. {
  133. "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
  134. },
  135. {
  136. "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
  137. },
  138. {
  139. "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
  140. },
  141. {
  142. "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
  143. },
  144. ]
  145. # Internationalization
  146. # https://docs.djangoproject.com/en/3.1/topics/i18n/
  147. LANGUAGE_CODE = "en-us"
  148. TIME_ZONE = os.getenv("vrobbler_TIME_ZONE", "EST")
  149. USE_I18N = True
  150. USE_L10N = True
  151. USE_TZ = True
  152. # Static files (CSS, JavaScript, Images)
  153. # https://docs.djangoproject.com/en/3.1/howto/static-files/
  154. STATIC_URL = "static/"
  155. STATIC_ROOT = os.getenv(
  156. "VROBBLER_STATIC_ROOT", os.path.join(BASE_DIR, "static")
  157. )
  158. if not DEBUG:
  159. STATICFILES_STORAGE = (
  160. "whitenoise.storage.CompressedManifestStaticFilesStorage"
  161. )
  162. MEDIA_URL = "/media/"
  163. MEDIA_ROOT = os.getenv("VROBBLER_MEDIA_ROOT", os.path.join(BASE_DIR, "media"))
  164. JSON_LOGGING = os.getenv("VROBBLER_JSON_LOGGING", False)
  165. LOG_TYPE = "json" if JSON_LOGGING else "log"
  166. default_level = "INFO"
  167. if DEBUG:
  168. default_level = "DEBUG"
  169. LOG_LEVEL = os.getenv("VROBBLER_LOG_LEVEL", default_level)
  170. LOG_FILE_PATH = os.getenv("VROBBLER_LOG_FILE_PATH", "/tmp/")
  171. LOGGING = {
  172. "version": 1,
  173. "disable_existing_loggers": False,
  174. "root": {
  175. "handlers": ["console", "file"],
  176. "level": LOG_LEVEL,
  177. "propagate": True,
  178. },
  179. "formatters": {
  180. "color": {
  181. "()": "colorlog.ColoredFormatter",
  182. # \r returns caret to line beginning, in tests this eats the silly dot that removes
  183. # the beautiful alignment produced below
  184. "format": "\r"
  185. "{log_color}{levelname:8s}{reset} "
  186. "{bold_cyan}{name}{reset}:"
  187. "{fg_bold_red}{lineno}{reset} "
  188. "{thin_yellow}{funcName} "
  189. "{thin_white}{message}"
  190. "{reset}",
  191. "style": "{",
  192. },
  193. "log": {"format": "%(asctime)s %(levelname)s %(message)s"},
  194. "json": {
  195. "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
  196. "format": "%(levelname)s %(name) %(funcName) %(lineno) %(asctime)s %(message)s",
  197. },
  198. },
  199. "handlers": {
  200. "console": {
  201. "class": "logging.StreamHandler",
  202. "formatter": "color",
  203. "level": LOG_LEVEL,
  204. },
  205. "null": {
  206. "class": "logging.NullHandler",
  207. "level": LOG_LEVEL,
  208. },
  209. "file": {
  210. "class": "logging.handlers.RotatingFileHandler",
  211. "filename": "".join([LOG_FILE_PATH, "vrobbler.log"]),
  212. "formatter": LOG_TYPE,
  213. "level": LOG_LEVEL,
  214. },
  215. "requests_file": {
  216. "class": "logging.handlers.RotatingFileHandler",
  217. "filename": "".join([LOG_FILE_PATH, "vrobbler_requests.log"]),
  218. "formatter": LOG_TYPE,
  219. "level": LOG_LEVEL,
  220. },
  221. },
  222. "loggers": {
  223. # Quiet down our console a little
  224. "django": {
  225. "handlers": ["file"],
  226. "propagate": True,
  227. },
  228. "django.db.backends": {"handlers": ["null"]},
  229. "vrobbler": {
  230. "handlers": ["console", "file"],
  231. "propagate": True,
  232. },
  233. },
  234. }
  235. if DEBUG:
  236. # We clear out a db with lots of games all the time in dev
  237. DATA_UPLOAD_MAX_NUMBER_FIELDS = 3000