Skip to content

Commit

Permalink
project initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
behshadrhp committed Oct 1, 2023
1 parent 2256ff2 commit dded3ba
Show file tree
Hide file tree
Showing 19 changed files with 549 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# migrations
*/migrations/*
!*/migrations/__init__.py

# PyCache
*/__pycache__/*
*/*/__pycache__/*

# database
*.sqlite3

# venv
*env
*Pipfile
*Pipfile.lock

# IDE
*.vscode
*.idea

# uploads media
media/*
!media/default/

# static files
staticfiles/*
Empty file added core/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions core/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.development')

application = get_asgi_application()
141 changes: 141 additions & 0 deletions core/settings/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import os
from pathlib import Path

# jazzmin imported settings configuration
from utils.jazzmin_settings import jazzmin_settings

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


# Application definition
INSTALLED_APPS = [
# Admin Panel
'jazzmin',
# Main App
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Internal App

# External App
'debug_toolbar',
'axes',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Third-Party Middleware
'whitenoise.middleware.WhiteNoiseMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'axes.middleware.AxesMiddleware',
'django_session_timeout.middleware.SessionTimeoutMiddleware',
]

ROOT_URLCONF = 'core.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'core.wsgi.application'

# Password validation
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
STATIC_ROOT = BASE_DIR / 'staticfiles'

# media
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Axes Configuration Settings
AUTHENTICATION_BACKENDS = [
# AxesStandaloneBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
'axes.backends.AxesStandaloneBackend',

# Django ModelBackend is the default authentication backend.
'django.contrib.auth.backends.ModelBackend',
]

AXES_FAILURE_LIMIT: 3 # how many times a user can fail a login
AXES_COOLOFF_TIME: 2 # Wait 2 hours before attempting to login again
AXES_RESET_ON_SUCCESS = True
# AXES_LOCKOUT_TEMPLATE = 'account-locked.html' --> if need -> enable

# Jazzmin settings configuration
JAZZMIN_SETTINGS = jazzmin_settings

# Session setting configuration
SESSION_EXPIRE_SECONDS = 604800 # 1 week -> Expire
SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True
SESSION_TIMEOUT_REDIRECT = '/admin/'

# Caches setting configuration
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-cache-key-for-chat-request-limit',
'TIMEOUT': 60 * 60 * 24, # Cache timeout set to 24 hours (1 day)
}
}

# email settings configuration
EMAIL_BACKEND = os.environ.get('EMAIL_BACKEND')
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_PORT = os.environ.get('EMAIL_PORT')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL')

# ُTimeOut system
TIMEOUT = 300
22 changes: 22 additions & 0 deletions core/settings/development.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from .common import *


# Default secret key in debug mode
SECRET_KEY = 'll1*tq$z$%t7-$x@8*ow+*xn-av!swn!aux@)gs!c*jx=1&h64'

# Debug mode
DEBUG = True


# Debug Toolbar
INTERNAL_IPS = [
'127.0.0.1',
]

# Default Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
50 changes: 50 additions & 0 deletions core/settings/production.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os

from .common import *

import dj_database_url

from dotenv import load_dotenv

# Loading environment variable's
load_dotenv()

# Django secret key
SECRET_KEY = os.environ.get('SECRET_KEY')

# Enable sever mode
DEBUG = False

# Allowed run server in this host
FIRST_HOST = os.environ.get('FIRST_HOST')
SECOND_HOST = os.environ.get('SECOND_HOST')

ALLOWED_HOSTS = ['127.0.0.1', FIRST_HOST, SECOND_HOST]

# Final database
DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL')),
}


# Configure Cache system
CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_SECONDS = 604800 # 7 Days
CACHE_MIDDLEWARE_KEY_PREFIX = ''

# CSRF Attack
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# XSS Attack
SECURE_BROWSER_XSS_FILTER = True
SECURE_COUNT_TYPE_NOSNIFF = True

# CORS Origin Header settings configuration
CORS_ORIGIN = os.environ.get('CORS_ORIGIN')
CSRF_ORIGINS = os.environ.get('CSRF_ORIGINS')
CORS_ALL_ORIGINS = os.environ.get('CORS_ALLOW_ALL_ORIGINS')
CORS_ALLOW_CREDENTIALS = os.environ.get('CORS_ALLOW_CREDENTIALS')
CORS_ALLOWED_ORIGINS = [CORS_ORIGIN]
CSRF_TRUSTED_ORIGINS = [CSRF_ORIGINS]
CORS_ALLOW_ALL_ORIGINS = CORS_ALL_ORIGINS
33 changes: 33 additions & 0 deletions core/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

import debug_toolbar
from dotenv import load_dotenv

# Loading environment variable's
load_dotenv()

if settings.DEBUG:
ADMIN_DIRECTORY = os.environ.setdefault('ADMIN_DIRECTORY', 'admin')
else:
ADMIN_DIRECTORY = os.environ.get('ADMIN_DIRECTORY')

urlpatterns = [
path(f'{ADMIN_DIRECTORY}/', admin.site.urls),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

# Media static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# Debug toolbar
if settings.DEBUG:
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
7 changes: 7 additions & 0 deletions core/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.development')

application = get_wsgi_application()
44 changes: 44 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: "3.9"

services:
postgres_db:
image: postgres:latest
container_name: postgres_db
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/

pgadmin:
image: dpage/pgadmin4:latest
container_name: pgadmin
depends_on:
- postgres_db
ports:
- "5051:5050"

dj_backend:
build: .
ports:
- "8000:8000"
command: bash -c "python manage.py migrate --noinput && python manage.py collectstatic --noinput && exec gunicorn core.wsgi:application -b 0.0.0.0:8000 -w 4"
volumes:
- .:/app/
depends_on:
- postgres_db
environment:
- DJANGO_SETTINGS_MODULE=core.settings.production
- DEBUG=False

nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- dj_backend

volumes:
postgres_data:
17 changes: 17 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Pull base image
From python:3.10.4-slim-bullseye

# Set envirement variable
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /app/ShadStore

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.development')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Loading

0 comments on commit dded3ba

Please sign in to comment.