settings.py 13 KB

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