settings.py 12 KB

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