Skip to content

Commit

Permalink
setup smtp mail and add logging config (#179)
Browse files Browse the repository at this point in the history
* Ui fixes

* add payment url

* setup smtp email

* add logging config

* update .env.template

* configure request logging

* fix log path

---------

Co-authored-by: tushit03 <[email protected]>
  • Loading branch information
arminpatel and tushitshukla03 authored Jun 5, 2024
1 parent fd293c8 commit b6c6cc3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
7 changes: 6 additions & 1 deletion backend/.env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
DJANGO_SECRET=
DATABASE_URL=
UPIServer=
UPI_API_KEY=
EMAIL_HOST=
EMAIL_HOST_USER=
EMAIL_HOST_PASSWORD=
DJANGO_ADMINS=
104 changes: 77 additions & 27 deletions backend/core/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import os
from pathlib import Path
from dotenv import load_dotenv
import logging

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

load_dotenv()

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET', 'default')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY', 'default')
STRIPE_WEBHOOK_SECRET = os.environ.get('STRIPE_WEBHOOK_SECRET', 'default')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG') != 'False'

ALLOWED_HOSTS = ['*']
# ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS').split(' ')

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = ['http://localhost:3000',
'https://merch-site.netlify.app',
'https://deploy-preview-*--merch-site.netlify.app'
]

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand All @@ -51,8 +43,6 @@
]

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
Expand All @@ -63,6 +53,7 @@
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'request_logging.middleware.LoggingMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
Expand Down Expand Up @@ -91,13 +82,6 @@

AUTH_USER_MODEL = 'accounts.Account'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
Expand All @@ -114,9 +98,6 @@
]


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
Expand All @@ -126,15 +107,84 @@
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
ADMINS = [(name.strip(), email.strip()) for entry in os.getenv('DJANGO_ADMINS', '').split(';') if entry for name, email in [entry.split(',')]]


# Logging

LOG_DIRECTORY = os.getenv('LOG_DIRECTORY', '~/logs/getit/')
if not os.path.exists(LOG_DIRECTORY):
os.makedirs(LOG_DIRECTORY)

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module}:{process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'maxBytes': 1024*1024*10, # 10MB
'backupCount': 10,
'filename': os.path.join(LOG_DIRECTORY, 'backend.log'),
'formatter': 'verbose',
},
'mail_admins': {
'level': 'CRITICAL',
'class': 'django.utils.log.AdminEmailHandler',
},
},
'loggers': {
'root': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'django': {
'handlers': ['console', 'file', 'mail_admins'],
'level': 'INFO',
'propagate': False,
},
'django.request': {
'handlers': ['console', 'file'],
'level': 'INFO',
'propagate': False,
},
'getit': {
'handlers': ['console', 'file', 'mail_admins'],
'level': 'DEBUG',
'propagate': False,
},
},
}

REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=logging.WARNING
REQUEST_LOGGING_ENABLE_COLORIZE=False
3 changes: 3 additions & 0 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from django.http import HttpResponse
import stripe
from django.shortcuts import get_object_or_404
from request_logging.decorators import no_logging

from django.conf import settings
from tickets.models import Ticket
from orders.models import Order

@csrf_exempt
@no_logging
def stripe_webhook(request) :
payload = request.body
sig_header = request.META.get('HTTP_STRIPE_SIGNATURE', None)
Expand Down Expand Up @@ -41,6 +43,7 @@ def stripe_webhook(request) :

@csrf_exempt
@require_POST
@no_logging
def upi_webhook(request) :
data = request.POST

Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Django==4.2
django-cors-headers==4.1.0
django-extensions==3.2.1
django-filter==22.1
django-request-logging==0.7.5
djangorestframework==3.14.0
djangorestframework-simplejwt==5.2.2
exceptiongroup==1.1.2
Expand All @@ -25,4 +26,4 @@ tomli==2.0.1
typing_extensions==4.6.3
tzdata==2022.6
stripe
drf-yasg==1.21.7
drf-yasg==1.21.7

0 comments on commit b6c6cc3

Please sign in to comment.