Skip to content

Commit 4d76038

Browse files
Files local e production
1 parent ef6f7a0 commit 4d76038

File tree

9 files changed

+375
-30
lines changed

9 files changed

+375
-30
lines changed

config/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This will make sure the app is always imported when
2+
# Django starts so that shared_task will use this app.
3+
from .celery_app import app as celery_app
4+
5+
__all__ = ("celery_app",)

config/celery_app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
from celery import Celery
4+
5+
# set the default Django settings module for the 'celery' program.
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
7+
8+
app = Celery("core")
9+
10+
# Using a string here means the worker doesn't have to serialize
11+
# the configuration object to child processes.
12+
# - namespace='CELERY' means all celery-related configuration keys
13+
# should have a `CELERY_` prefix.
14+
app.config_from_object("django.conf:settings", namespace="CELERY")
15+
16+
# Load task modules from all registered Django app configs.
17+
app.autodiscover_tasks()

config/settings/base.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import environ
1616
import os
17+
18+
from datetime import timedelta
1719
from pathlib import Path
1820

1921
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -65,7 +67,7 @@
6567

6668
THIRD_PARTY_APPS = [
6769
"compressor",
68-
"wagtailautocomplete"
70+
"wagtailautocomplete",
6971
]
7072

7173
LOCAL_APPS = [
@@ -258,9 +260,27 @@
258260
CELERY_SEND_TASK_SENT_EVENT = True
259261
CELERYD_SEND_EVENTS = True
260262
CE_BUCKETS=1,2.5,5,10,30,60,300,600,900,1800
263+
261264
# Celery Results
262265
# ------------------------------------------------------------------------------
263266
# https: // django-celery-results.readthedocs.io/en/latest/getting_started.html
264267
CELERY_RESULT_BACKEND = "django-db"
265268
CELERY_CACHE_BACKEND = "django-cache"
266269
CELERY_RESULT_EXTENDED = True
270+
271+
# django rest-framework
272+
# ------------------------------------------------------------------------------
273+
REST_FRAMEWORK = {
274+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
275+
"PAGE_SIZE": env.int("DRF_PAGE_SIZE", default=10),
276+
"DEFAULT_AUTHENTICATION_CLASSES": (
277+
"rest_framework_simplejwt.authentication.JWTAuthentication",
278+
),
279+
}
280+
# JWT
281+
SIMPLE_JWT = {
282+
"AUTH_HEADER_TYPES": ("Bearer",), # na doc está JWT mas pode mudar pra Bearer.
283+
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
284+
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
285+
# "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
286+
}

config/settings/dev.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

config/settings/local.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from .base import *
2+
3+
# SECURITY WARNING: don't run with debug turned on in production!
4+
DEBUG = True
5+
6+
# SECURITY WARNING: keep the secret key used in production secret!
7+
SECRET_KEY = env(
8+
"DJANGO_SECRET_KEY",
9+
default="FMiraeekXCSl3zHfg7D4oHx7ufT46HRnwnsawKgTCC53BYajVkVzb8HhOvBOHakR",
10+
)
11+
12+
# CACHES
13+
# ------------------------------------------------------------------------------
14+
# https://docs.djangoproject.com/en/dev/ref/settings/#caches
15+
CACHES = {
16+
"default": {
17+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
18+
"LOCATION": "redis://redis:6379",
19+
"OPTIONS": {
20+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
21+
}
22+
}
23+
}
24+
25+
# SECURITY WARNING: define the correct hosts in production!
26+
ALLOWED_HOSTS = ["*"]
27+
28+
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
29+
30+
# EMAIL
31+
# ------------------------------------------------------------------------------
32+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-host
33+
EMAIL_HOST = env("EMAIL_HOST", default="mailhog")
34+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-port
35+
EMAIL_PORT = 1025
36+
37+
38+
39+
# https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config
40+
DEBUG_TOOLBAR_CONFIG = {
41+
"DISABLE_PANELS": ["debug_toolbar.panels.redirects.RedirectsPanel"],
42+
"SHOW_TEMPLATE_CONTEXT": True,
43+
}
44+
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#internal-ips
45+
INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"]
46+
if env("USE_DOCKER") == True:
47+
import socket
48+
49+
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
50+
INTERNAL_IPS += [".".join(ip.split(".")[:-1] + ["1"]) for ip in ips]
51+
52+
53+
INSTALLED_APPS += [
54+
"django_extensions",
55+
"debug_toolbar",
56+
"whitenoise.runserver_nostatic",
57+
] # noqa F405
58+
59+
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"] # noqa F405
60+
61+
# Celery
62+
# ------------------------------------------------------------------------------
63+
64+
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-eager-propagates
65+
CELERY_TASK_EAGER_PROPAGATES = True
66+
# Your stuff...
67+
# ------------------------------------------------------------------------------

config/settings/production.py

Lines changed: 195 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,197 @@
1-
from .base import *
1+
import logging
22

3-
DEBUG = False
3+
import sentry_sdk
4+
from sentry_sdk.integrations.celery import CeleryIntegration
5+
from sentry_sdk.integrations.django import DjangoIntegration
6+
from sentry_sdk.integrations.logging import LoggingIntegration
7+
from sentry_sdk.integrations.redis import RedisIntegration
48

5-
try:
6-
from .local import *
7-
except ImportError:
8-
pass
9+
from .base import * # noqa
10+
from .base import env
11+
12+
# GENERAL
13+
# ------------------------------------------------------------------------------
14+
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
15+
SECRET_KEY = env("DJANGO_SECRET_KEY")
16+
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
17+
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["example.com"])
18+
19+
# DATABASES
20+
# ------------------------------------------------------------------------------
21+
DATABASES["default"] = env.db("DATABASE_URL") # noqa F405
22+
DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405
23+
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405
24+
DATABASES["default"]["ENGINE"] = 'django.db.backends.postgresql'
25+
# CACHES
26+
# ------------------------------------------------------------------------------
27+
CACHES = {
28+
"default": {
29+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
30+
"LOCATION": env("REDIS_URL"),
31+
"OPTIONS": {
32+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
33+
# Mimicing memcache behavior.
34+
# https://github.com/jazzband/django-redis#memcached-exceptions-behavior
35+
"IGNORE_EXCEPTIONS": True,
36+
},
37+
}
38+
}
39+
40+
# SECURITY
41+
# ------------------------------------------------------------------------------
42+
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
43+
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
44+
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect
45+
SECURE_SSL_REDIRECT = env.bool("DJANGO_SECURE_SSL_REDIRECT", default=True)
46+
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
47+
SESSION_COOKIE_SECURE = env.bool("SESSION_COOKIE_SECURE", default=True)
48+
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
49+
CSRF_COOKIE_SECURE = env.bool("CSRF_COOKIE_SECURE", default=True)
50+
# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
51+
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-seconds
52+
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
53+
SECURE_HSTS_SECONDS = 60
54+
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
55+
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool(
56+
"DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS", default=True
57+
)
58+
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
59+
SECURE_HSTS_PRELOAD = env.bool("DJANGO_SECURE_HSTS_PRELOAD", default=True)
60+
# https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
61+
SECURE_CONTENT_TYPE_NOSNIFF = env.bool(
62+
"DJANGO_SECURE_CONTENT_TYPE_NOSNIFF", default=True
63+
)
64+
65+
# STATIC
66+
# ------------------------
67+
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
68+
# MEDIA
69+
# ------------------------------------------------------------------------------
70+
71+
# EMAIL
72+
# ------------------------------------------------------------------------------
73+
# https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
74+
DEFAULT_FROM_EMAIL = env(
75+
"DJANGO_DEFAULT_FROM_EMAIL",
76+
default="SciELO Content Manager <suporte.aplicacao@scielo.org>",
77+
)
78+
79+
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
80+
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)
81+
82+
# https://docs.djangoproject.com/en/dev/ref/settings/#std-setting-EMAIL_HOST
83+
EMAIL_HOST = env.str("DJANGO_EMAIL_HOST", default="mailrelay.scielo.org")
84+
85+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-port
86+
EMAIL_PORT = env.int("DJANGO_EMAIL_PORT", default=25)
87+
88+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-host-user
89+
EMAIL_HOST_USER = env.str("DJANGO_EMAIL_HOST_USER", default="suporte.aplicacao@scielo.org")
90+
91+
# https://docs.djangoproject.com/en/dev/ref/settings/#std-setting-EMAIL_HOST_PASSWORD
92+
EMAIL_HOST_PASSWORD = env.str("DJANGO_EMAIL_HOST_PASSWORD", default="")
93+
94+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-use-tls
95+
EMAIL_USE_TLS = env.bool("DJANGO_EMAIL_USE_TLS", default=False)
96+
97+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-use-ssl
98+
EMAIL_USE_SSL = env.bool("DJANGO_EMAIL_USE_SSL", default=False)
99+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
100+
EMAIL_SUBJECT_PREFIX = env(
101+
"DJANGO_EMAIL_SUBJECT_PREFIX",
102+
default="[SciELO Content Manager ]",
103+
)
104+
105+
# Anymail
106+
# ------------------------------------------------------------------------------
107+
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
108+
INSTALLED_APPS += ["anymail"] # noqa F405
109+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
110+
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
111+
# https://anymail.readthedocs.io/en/stable/esps
112+
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
113+
ANYMAIL = {}
114+
115+
# django-compressor
116+
# ------------------------------------------------------------------------------
117+
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_ENABLED
118+
COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", default=True)
119+
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_STORAGE
120+
COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"
121+
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_URL
122+
COMPRESS_URL = STATIC_URL # noqa F405
123+
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE
124+
COMPRESS_OFFLINE = True # Offline compression is required when using Whitenoise
125+
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_FILTERS
126+
COMPRESS_FILTERS = {
127+
"css": [
128+
"compressor.filters.css_default.CssAbsoluteFilter",
129+
"compressor.filters.cssmin.rCSSMinFilter",
130+
],
131+
"js": ["compressor.filters.jsmin.JSMinFilter"],
132+
}
133+
134+
# LOGGING
135+
# ------------------------------------------------------------------------------
136+
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
137+
# See https://docs.djangoproject.com/en/dev/topics/logging for
138+
# more details on how to customize your logging configuration.
139+
140+
LOGGING = {
141+
"version": 1,
142+
"disable_existing_loggers": True,
143+
"formatters": {
144+
"verbose": {
145+
"format": "%(levelname)s %(asctime)s %(module)s "
146+
"%(process)d %(thread)d %(message)s"
147+
}
148+
},
149+
"handlers": {
150+
"console": {
151+
"level": "DEBUG",
152+
"class": "logging.StreamHandler",
153+
"formatter": "verbose",
154+
}
155+
},
156+
"root": {"level": "INFO", "handlers": ["console"]},
157+
"loggers": {
158+
"django.db.backends": {
159+
"level": "ERROR",
160+
"handlers": ["console"],
161+
"propagate": False,
162+
},
163+
# Errors logged by the SDK itself
164+
"sentry_sdk": {"level": "ERROR", "handlers": ["console"], "propagate": False},
165+
"django.security.DisallowedHost": {
166+
"level": "ERROR",
167+
"handlers": ["console"],
168+
"propagate": False,
169+
},
170+
},
171+
}
172+
173+
# Sentry
174+
# ------------------------------------------------------------------------------
175+
SENTRY_DSN = env("SENTRY_DSN")
176+
SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)
177+
178+
sentry_logging = LoggingIntegration(
179+
level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
180+
event_level=logging.ERROR, # Send errors as events
181+
)
182+
integrations = [
183+
sentry_logging,
184+
DjangoIntegration(),
185+
CeleryIntegration(),
186+
RedisIntegration(),
187+
]
188+
sentry_sdk.init(
189+
dsn=SENTRY_DSN,
190+
integrations=integrations,
191+
environment=env("SENTRY_ENVIRONMENT", default="production"),
192+
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=1.0),
193+
enable_tracing=True,
194+
)
195+
196+
# Your stuff...
197+
# ------------------------------------------------------------------------------

config/wsgi.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
"""
99

1010
import os
11+
import sys
12+
from pathlib import Path
1113

1214
from django.core.wsgi import get_wsgi_application
1315

14-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
16+
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent
17+
sys.path.append(str(ROOT_DIR / "core"))
18+
19+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
1520

1621
application = get_wsgi_application()

0 commit comments

Comments
 (0)