settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 emus.conf if it's available
  7. if os.path.exists("emus.conf"):
  8. load_dotenv("emus.conf")
  9. elif os.path.exists("/etc/emus.conf"):
  10. load_dotenv("/etc/emus.conf")
  11. elif os.path.exists("/usr/local/etc/emus.conf"):
  12. load_dotenv("/usr/local/etc/emus.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("EMUS_SECRET_KEY", "not-a-secret-234lkjasdflj132")
  20. # SECURITY WARNING: don't run with debug turned on in production!
  21. DEBUG = os.getenv("EMUS_DEBUG", False)
  22. TESTING = len(sys.argv) > 1 and sys.argv[1] == "test"
  23. FEATURED_GAME_DURATION = os.getenv("EMUS_FEATURED_GAME_DURATION", 60 * 60 * 18)
  24. ALLOWED_HOSTS = ["*"]
  25. CSRF_TRUSTED_ORIGINS = [os.getenv("EMUS_TRUSTED_ORIGINS", "http://localhost:8000")]
  26. X_FRAME_OPTIONS = "SAMEORIGIN"
  27. REDIS_URL = os.getenv("EMUS_REDIS_URL", None)
  28. CELERY_TASK_ALWAYS_EAGER = os.getenv("EMUS_SKIP_CELERY", False)
  29. CELERY_BROKER_URL = REDIS_URL if REDIS_URL else "memory://localhost/"
  30. CELERY_RESULT_BACKEND = "django-db"
  31. CELERY_TIMEZONE = os.getenv("EMUS_TIME_ZONE", "EST")
  32. CELERY_TASK_TRACK_STARTED = True
  33. INSTALLED_APPS = [
  34. "django.contrib.admin",
  35. "django.contrib.auth",
  36. "django.contrib.contenttypes",
  37. "django.contrib.sessions",
  38. "django.contrib.messages",
  39. "django.contrib.staticfiles",
  40. "django.contrib.sites",
  41. "django_filters",
  42. "django_extensions",
  43. "markdownify.apps.MarkdownifyConfig",
  44. "taggit",
  45. "emus",
  46. "mathfilters",
  47. "search",
  48. "games",
  49. "rest_framework",
  50. "allauth",
  51. "allauth.account",
  52. "django_celery_results",
  53. ]
  54. SITE_ID = 1
  55. MIDDLEWARE = [
  56. "django.middleware.security.SecurityMiddleware",
  57. "django.contrib.sessions.middleware.SessionMiddleware",
  58. "django.middleware.common.CommonMiddleware",
  59. "django.middleware.csrf.CsrfViewMiddleware",
  60. "django.contrib.auth.middleware.AuthenticationMiddleware",
  61. "django.contrib.messages.middleware.MessageMiddleware",
  62. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  63. "django.middleware.gzip.GZipMiddleware",
  64. ]
  65. ROOT_URLCONF = "emus.urls"
  66. TEMPLATES = [
  67. {
  68. "BACKEND": "django.template.backends.django.DjangoTemplates",
  69. "DIRS": [str(BASE_DIR.joinpath("templates"))], # new
  70. "APP_DIRS": True,
  71. "OPTIONS": {
  72. "context_processors": [
  73. "django.template.context_processors.debug",
  74. "django.template.context_processors.request",
  75. "django.contrib.auth.context_processors.auth",
  76. "django.contrib.messages.context_processors.messages",
  77. "games.context_processors.game_systems",
  78. ],
  79. },
  80. },
  81. ]
  82. WSGI_APPLICATION = "emus.wsgi.application"
  83. DATABASES = {
  84. "default": dj_database_url.config(
  85. default=os.getenv("EMUS_DATABASE_URL", "sqlite:///db.sqlite3"), conn_max_age=600
  86. ),
  87. }
  88. if TESTING:
  89. DATABASES = {"default": dj_database_url.config(default="sqlite:///testdb.sqlite3")}
  90. CACHES = {
  91. "default": {
  92. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  93. "LOCATION": "unique-snowflake",
  94. }
  95. }
  96. if REDIS_URL:
  97. CACHES["default"]["BACKEND"] = "django.core.cache.backends.redis.RedisCache"
  98. CACHES["default"]["LOCATION"] = REDIS_URL
  99. AUTHENTICATION_BACKENDS = [
  100. "django.contrib.auth.backends.ModelBackend",
  101. "allauth.account.auth_backends.AuthenticationBackend",
  102. ]
  103. REST_FRAMEWORK = {
  104. "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
  105. "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
  106. "DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
  107. "PAGE_SIZE": 100,
  108. }
  109. LOGIN_REDIRECT_URL = "/"
  110. AUTH_PASSWORD_VALIDATORS = [
  111. {
  112. "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
  113. },
  114. {
  115. "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
  116. },
  117. {
  118. "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
  119. },
  120. {
  121. "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
  122. },
  123. ]
  124. # Internationalization
  125. # https://docs.djangoproject.com/en/3.1/topics/i18n/
  126. LANGUAGE_CODE = "en-us"
  127. TIME_ZONE = os.getenv("EMUS_TIME_ZONE", "EST")
  128. USE_I18N = True
  129. USE_L10N = True
  130. USE_TZ = True
  131. # Static files (CSS, JavaScript, Images)
  132. # https://docs.djangoproject.com/en/3.1/howto/static-files/
  133. STATIC_URL = "static/"
  134. STATIC_ROOT = os.getenv("EMUS_STATIC_ROOT", os.path.join(BASE_DIR, "static"))
  135. MEDIA_URL = "/media/"
  136. MEDIA_ROOT = os.getenv("EMUS_MEDIA_ROOT", os.path.join(BASE_DIR, "media"))
  137. ROMS_DIR = os.path.join(MEDIA_ROOT, "roms")
  138. COLLECTIONS_DIR = os.path.join(ROMS_DIR, "emulationstation-collections")
  139. SCRAPER_BIN_PATH = os.getenv("EMUS_SCRAPER_BINPATH", "Skyscraper")
  140. SCRAPER_CONFIG_FILE = os.getenv("EMUS_SCRAPER_CONFIG_FILE", "skyscraper.ini")
  141. SCRAPER_SITE = os.getenv("EMUS_SCRAPER_SITE", "screenscraper")
  142. JSON_LOGGING = os.getenv("EMUS_JSON_LOGGING", False)
  143. LOG_TYPE = "json" if JSON_LOGGING else "log"
  144. default_level = "INFO"
  145. if DEBUG:
  146. default_level = "DEBUG"
  147. LOG_LEVEL = os.getenv("EMUS_LOG_LEVEL", default_level)
  148. LOG_FILE_PATH = os.getenv("EMUS_LOG_FILE_PATH", "/tmp/")
  149. LOGGING = {
  150. "version": 1,
  151. "disable_existing_loggers": False,
  152. "root": {"handlers": ["console", "file"], "level": LOG_LEVEL, "propagate": True},
  153. "formatters": {
  154. "color": {
  155. "()": "colorlog.ColoredFormatter",
  156. # \r returns caret to line beginning, in tests this eats the silly dot that removes
  157. # the beautiful alignment produced below
  158. "format": "\r"
  159. "{log_color}{levelname:8s}{reset} "
  160. "{bold_cyan}{name}{reset}:"
  161. "{fg_bold_red}{lineno}{reset} "
  162. "{thin_yellow}{funcName} "
  163. "{thin_white}{message}"
  164. "{reset}",
  165. "style": "{",
  166. },
  167. "log": {"format": "%(asctime)s %(levelname)s %(message)s"},
  168. "json": {
  169. "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
  170. "format": "%(levelname)s %(name) %(funcName) %(lineno) %(asctime)s %(message)s",
  171. },
  172. },
  173. "handlers": {
  174. "console": {
  175. "class": "logging.StreamHandler",
  176. "formatter": "color",
  177. "level": LOG_LEVEL,
  178. },
  179. "null": {
  180. "class": "logging.NullHandler",
  181. "level": LOG_LEVEL,
  182. },
  183. "file": {
  184. "class": "logging.handlers.RotatingFileHandler",
  185. "filename": "".join([LOG_FILE_PATH, "emus.log"]),
  186. "formatter": LOG_TYPE,
  187. "level": LOG_LEVEL,
  188. },
  189. "requests_file": {
  190. "class": "logging.handlers.RotatingFileHandler",
  191. "filename": "".join([LOG_FILE_PATH, "emus_requests.log"]),
  192. "formatter": LOG_TYPE,
  193. "level": LOG_LEVEL,
  194. },
  195. },
  196. "loggers": {
  197. # Quiet down our console a little
  198. "django": {
  199. "handlers": ["file"],
  200. "propagate": True,
  201. },
  202. "django.db.backends": {"handlers": ["null"]},
  203. "emus": {
  204. "handlers": ["console", "file"],
  205. "propagate": True,
  206. },
  207. },
  208. }
  209. REMOVE_FROM_SLUGS = ["_", " ", "/"]
  210. if DEBUG:
  211. # We clear out a db with lots of games all the time in dev
  212. DATA_UPLOAD_MAX_NUMBER_FIELDS = 3000
  213. GAME_SYSTEM_DEFAULTS = {
  214. "3do": {
  215. "name": "3DO",
  216. "retroarch_core": "opera",
  217. },
  218. "3ds": {
  219. "name": "Nintendo 3DS",
  220. "retroarch_core": "citra",
  221. },
  222. "atarijaguar": {
  223. "name": "Atari Jaguar",
  224. "retroarch_core": "virtualjaguar",
  225. },
  226. "atarilynx": {
  227. "name": "Atari Lynx",
  228. "color": "FFBF00",
  229. "retroarch_core": "handy",
  230. },
  231. "coleco": {
  232. "name": "Colecovision",
  233. "retroarch_core": "bluemsx",
  234. },
  235. "daphne": {
  236. "name": "Daphne",
  237. "retroarch_core": "daphne",
  238. },
  239. "dreamcast": {
  240. "name": "Dreamcast",
  241. "color": "ED872D",
  242. "retroarch_core": "flycast",
  243. },
  244. "fds": {
  245. "name": "Famicom Disc System",
  246. "color": "B70E30",
  247. "retroarch_core": "nestopia",
  248. },
  249. "gb": {
  250. "name": "Game Boy",
  251. "color": "C0B8B1",
  252. "retroarch_core": "gambatte",
  253. },
  254. "gba": {
  255. "name": "Game Boy Advance",
  256. "color": "D5D5D5",
  257. "webretro_core": "mgba",
  258. "retroarch_core": "mgba",
  259. },
  260. "gbc": {
  261. "name": "Game Boy Color",
  262. "color": "77CCFF",
  263. "retroarch_core": "gambatte",
  264. },
  265. "gc": {
  266. "name": "GameCube",
  267. "color": "7461C7",
  268. "retroarch_core": "dolphin",
  269. },
  270. "mame-libretro": {
  271. "name": "Arcade",
  272. "color": "111111",
  273. "retroarch_core": "mame2010",
  274. },
  275. "mastersystem": {
  276. "name": "Sega Master System",
  277. "webretro_core": "genesis_plus_gx",
  278. "retroarch_core": "genesis_plus_gx",
  279. },
  280. "megadrive": {
  281. "name": "Genesis/Mega Drive",
  282. "color": "D03737",
  283. "webretro_core": "genesis_plus_gx",
  284. "retroarch_core": "genesis_plus_gx",
  285. },
  286. "model3": {
  287. "name": "Sega Model 3",
  288. "emulator": "Supermodel",
  289. },
  290. "gamegear": {
  291. "name": "Game Gear",
  292. "color": "3FA3C4",
  293. "retroarch_core": "genesis_plus_gx",
  294. },
  295. "msx": {
  296. "name": "MSX",
  297. "retroarch_core": "bluemsx",
  298. },
  299. "n64": {
  300. "name": "Nintendo 64",
  301. "color": "C76660",
  302. "webretro_core": "mupen64plus_next",
  303. "retroarch_core": "mupen64plus_next",
  304. },
  305. "nds": {
  306. "name": "Nintendo DS",
  307. "color": "39D0D0",
  308. "retroarch_core": "desmume",
  309. },
  310. "ngp": {
  311. "name": "Neo Geo Pocket",
  312. "retroarch_core": "mednafen_ngp",
  313. },
  314. "neogeo": {
  315. "name": "Neo Geo",
  316. "retroarch_core": "fbneo",
  317. },
  318. "ngpc": {
  319. "name": "Neo Geo Pocket Color",
  320. "retroarch_core": "mednafen_ngp",
  321. },
  322. "nes": {
  323. "name": "Nintendo",
  324. "color": "656565",
  325. "webretro_core": "nestopia",
  326. "retroarch_core": "nestopia",
  327. },
  328. "pcengine": {
  329. "name": "PC Engine/TurboGrafix 16",
  330. "color": "55B4CC",
  331. "retroarch_core": "mednafen_supergrafx",
  332. },
  333. "pcfx": {
  334. "name": "PC FX",
  335. "color": "55B4CC",
  336. "retroarch_core": "mednafen_pcfx",
  337. },
  338. "ps2": {
  339. "name": "Playstation 2",
  340. "color": "111CAA",
  341. "emulator": "PCSX2",
  342. },
  343. "psp": {
  344. "name": "Playstation Portable",
  345. "color": "",
  346. "retroarch_core": "ppsspp",
  347. },
  348. "psx": {
  349. "name": "Playstation",
  350. "color": "E9DD00",
  351. "retroarch_core": "mednafen_psx",
  352. },
  353. "ports": {
  354. "name": "Ports",
  355. },
  356. "saturn": {
  357. "name": "Saturn",
  358. "color": "0047AB",
  359. "retroarch_core": "mednafen_saturn",
  360. "emulator": "yabuse",
  361. },
  362. "scummvm": {
  363. "name": "ScummVM",
  364. "color": "E8B500",
  365. "retroarch_core": "scummvm",
  366. "emulator": "scummvm",
  367. },
  368. "sega32x": {
  369. "name": "Sega 32X",
  370. "color": "",
  371. "retroarch_core": "genesis_plus_gx",
  372. },
  373. "segacd": {
  374. "name": "Sega CD",
  375. "color": "",
  376. "retroarch_core": "genesis_plus_gx",
  377. },
  378. "snes": {
  379. "name": "Super Nintendo",
  380. "color": "A060C7",
  381. "retroarch_core": "snes9x",
  382. "webretro_core": "snes9x",
  383. },
  384. "virtualboy": {
  385. "name": "Virtual Boy",
  386. "color": "99AA11",
  387. "retroarch_core": "mednafen_vb",
  388. },
  389. "wonderswan": {
  390. "name": "WonderSwan",
  391. "color": "AA88CC",
  392. "retroarch_core": "mednafen_wswan",
  393. },
  394. "wonderswancolor": {
  395. "name": "WonderSwan Color",
  396. "color": "BB33CC",
  397. "retroarch_core": "mednafen_wswan",
  398. },
  399. "wii": {
  400. "name": "Wii",
  401. "color": "",
  402. "retroarch_core": "dolphin",
  403. },
  404. }