settings.py 12 KB

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