settings.py 13 KB

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