tum пре 1 година
комит
db1ef2c3ec

+ 113 - 0
.gitignore

@@ -0,0 +1,113 @@
1
+# Byte-compiled / optimized / DLL files
2
+__pycache__/
3
+*.py[cod]
4
+*$py.class
5
+
6
+# C extensions
7
+*.so
8
+
9
+# Django specific
10
+*.log
11
+local_settings.py
12
+db.sqlite3
13
+db.sqlite3-journal
14
+media/
15
+
16
+# If you are using the default SQLite database:
17
+*.sqlite3
18
+
19
+# Environment variables
20
+.env
21
+*.env
22
+
23
+# Virtual environment
24
+venv/
25
+env/
26
+ENV/
27
+*.virtualenv
28
+
29
+# Python egg files
30
+*.egg
31
+*.egg-info/
32
+dist/
33
+build/
34
+*.wheel
35
+
36
+# Coverage reports
37
+htmlcov/
38
+.coverage
39
+.coverage.*
40
+
41
+# Testing
42
+.pytest_cache/
43
+.tox/
44
+.nox/
45
+*.cover
46
+*.hypothesis/
47
+*.pytest_cache/
48
+.cache/
49
+
50
+# Jupyter Notebook
51
+.ipynb_checkpoints
52
+
53
+# IDEs and editors
54
+.vscode/
55
+.idea/
56
+*.swp
57
+*.swo
58
+*.swn
59
+*~
60
+
61
+# macOS specific files
62
+.DS_Store
63
+
64
+# Docker
65
+*.log
66
+docker-compose.override.yml
67
+*.pid
68
+*.crt
69
+*.key
70
+mssql.*
71
+
72
+# Generated static files
73
+staticfiles/
74
+node_modules/
75
+
76
+# Yarn and npm
77
+yarn.lock
78
+package-lock.json
79
+
80
+# Pipenv
81
+Pipfile
82
+Pipfile.lock
83
+
84
+# Gunicorn
85
+gunicorn.log
86
+gunicorn.err
87
+
88
+# MyPy
89
+.mypy_cache/
90
+dmypy.json
91
+dmypy.json.*/
92
+
93
+# Pyre
94
+.pyre/
95
+
96
+# Celery beat schedule file
97
+celerybeat-schedule
98
+
99
+# Sphinx documentation
100
+docs/_build/
101
+
102
+# Temporary files and backups
103
+*.bak
104
+*.tmp
105
+*.temp
106
+*.old
107
+*.orig
108
+*.swp
109
+*.swo
110
+*.sw~
111
+
112
+# Security sensitive files
113
+secrets.json

+ 27 - 0
Dockerfile

@@ -0,0 +1,27 @@
1
+# Use Python 3.10-slim as a base
2
+FROM python:3.10-slim
3
+
4
+# Install system dependencies for MS SQL ODBC (comment out if not using MSSQL)
5
+# Install system dependencies for MS SQL ODBC and command-line tools
6
+RUN apt-get update && apt-get install -y curl gnupg apt-transport-https \
7
+    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
8
+    && curl https://packages.microsoft.com/config/debian/11/prod.list \
9
+       | tee /etc/apt/sources.list.d/mssql-release.list \
10
+    && apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools18 unixodbc-dev \
11
+    && echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc \
12
+    && apt-get clean && rm -rf /var/lib/apt/lists/*
13
+# Set the working directory
14
+WORKDIR /app
15
+
16
+# Copy requirements and install Python dependencies
17
+COPY requirements.txt .
18
+RUN pip install --no-cache-dir -r requirements.txt
19
+
20
+# Copy the Django project (app/ folder) into the container
21
+COPY app/ /app/
22
+
23
+# Expose Django's default port
24
+EXPOSE 8000
25
+
26
+# Default command: run Django's development server
27
+CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

+ 0 - 0
app/coi/__init__.py


+ 16 - 0
app/coi/asgi.py

@@ -0,0 +1,16 @@
1
+"""
2
+ASGI config for coi project.
3
+
4
+It exposes the ASGI callable as a module-level variable named ``application``.
5
+
6
+For more information on this file, see
7
+https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
8
+"""
9
+
10
+import os
11
+
12
+from django.core.asgi import get_asgi_application
13
+
14
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coi.settings")
15
+
16
+application = get_asgi_application()

+ 133 - 0
app/coi/settings.py

@@ -0,0 +1,133 @@
1
+"""
2
+Django settings for coi project.
3
+
4
+Generated by 'django-admin startproject' using Django 5.1.
5
+
6
+For more information on this file, see
7
+https://docs.djangoproject.com/en/5.1/topics/settings/
8
+
9
+For the full list of settings and their values, see
10
+https://docs.djangoproject.com/en/5.1/ref/settings/
11
+"""
12
+
13
+from pathlib import Path
14
+import os
15
+
16
+# Build paths inside the project like this: BASE_DIR / 'subdir'.
17
+BASE_DIR = Path(__file__).resolve().parent.parent
18
+
19
+
20
+# Quick-start development settings - unsuitable for production
21
+# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
22
+
23
+# SECURITY WARNING: keep the secret key used in production secret!
24
+SECRET_KEY = "django-insecure-#td578+^qcir9s!0pb7lxh!4tefib7t&x7^p4+=5$vpvujjumn"
25
+
26
+# SECURITY WARNING: don't run with debug turned on in production!
27
+DEBUG = True
28
+
29
+ALLOWED_HOSTS = []
30
+
31
+
32
+# Application definition
33
+
34
+INSTALLED_APPS = [
35
+    "django.contrib.admin",
36
+    "django.contrib.auth",
37
+    "django.contrib.contenttypes",
38
+    "django.contrib.sessions",
39
+    "django.contrib.messages",
40
+    "django.contrib.staticfiles",
41
+    "core.apps.CoreConfig"
42
+]
43
+
44
+MIDDLEWARE = [
45
+    "django.middleware.security.SecurityMiddleware",
46
+    "django.contrib.sessions.middleware.SessionMiddleware",
47
+    "django.middleware.common.CommonMiddleware",
48
+    "django.middleware.csrf.CsrfViewMiddleware",
49
+    "django.contrib.auth.middleware.AuthenticationMiddleware",
50
+    "django.contrib.messages.middleware.MessageMiddleware",
51
+    "django.middleware.clickjacking.XFrameOptionsMiddleware",
52
+]
53
+
54
+ROOT_URLCONF = "coi.urls"
55
+
56
+TEMPLATES = [
57
+    {
58
+        "BACKEND": "django.template.backends.django.DjangoTemplates",
59
+        "DIRS": [],
60
+        "APP_DIRS": True,
61
+        "OPTIONS": {
62
+            "context_processors": [
63
+                "django.template.context_processors.debug",
64
+                "django.template.context_processors.request",
65
+                "django.contrib.auth.context_processors.auth",
66
+                "django.contrib.messages.context_processors.messages",
67
+            ],
68
+        },
69
+    },
70
+]
71
+
72
+WSGI_APPLICATION = "coi.wsgi.application"
73
+
74
+
75
+# Database
76
+# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
77
+
78
+DATABASES = {
79
+    'default': {
80
+        'ENGINE': 'mssql',
81
+        'NAME': os.environ.get('DB_NAME', 'coi_db'),
82
+        'USER': os.environ.get('DB_USER', 'sa'),
83
+        'PASSWORD': os.environ.get('DB_PASSWORD', ''),
84
+        'HOST': os.environ.get('DB_HOST', 'db'),
85
+        'PORT': os.environ.get('DB_PORT', '1433'),
86
+        'OPTIONS': {
87
+            'driver': 'ODBC Driver 18 for SQL Server',
88
+            'extra_params':  'Encrypt=no',
89
+        },
90
+    },
91
+}
92
+
93
+
94
+# Password validation
95
+# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
96
+
97
+AUTH_PASSWORD_VALIDATORS = [
98
+    {
99
+        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
100
+    },
101
+    {
102
+        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
103
+    },
104
+    {
105
+        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
106
+    },
107
+    {
108
+        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
109
+    },
110
+]
111
+
112
+
113
+# Internationalization
114
+# https://docs.djangoproject.com/en/5.1/topics/i18n/
115
+
116
+LANGUAGE_CODE = "en-us"
117
+
118
+TIME_ZONE = "UTC"
119
+
120
+USE_I18N = True
121
+
122
+USE_TZ = True
123
+
124
+
125
+# Static files (CSS, JavaScript, Images)
126
+# https://docs.djangoproject.com/en/5.1/howto/static-files/
127
+
128
+STATIC_URL = "static/"
129
+
130
+# Default primary key field type
131
+# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
132
+
133
+DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

+ 23 - 0
app/coi/urls.py

@@ -0,0 +1,23 @@
1
+"""
2
+URL configuration for coi project.
3
+
4
+The `urlpatterns` list routes URLs to views. For more information please see:
5
+    https://docs.djangoproject.com/en/5.1/topics/http/urls/
6
+Examples:
7
+Function views
8
+    1. Add an import:  from my_app import views
9
+    2. Add a URL to urlpatterns:  path('', views.home, name='home')
10
+Class-based views
11
+    1. Add an import:  from other_app.views import Home
12
+    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
13
+Including another URLconf
14
+    1. Import the include() function: from django.urls import include, path
15
+    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
16
+"""
17
+
18
+from django.contrib import admin
19
+from django.urls import path
20
+
21
+urlpatterns = [
22
+    path("admin/", admin.site.urls),
23
+]

+ 16 - 0
app/coi/wsgi.py

@@ -0,0 +1,16 @@
1
+"""
2
+WSGI config for coi project.
3
+
4
+It exposes the WSGI callable as a module-level variable named ``application``.
5
+
6
+For more information on this file, see
7
+https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
8
+"""
9
+
10
+import os
11
+
12
+from django.core.wsgi import get_wsgi_application
13
+
14
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coi.settings")
15
+
16
+application = get_wsgi_application()

+ 0 - 0
app/core/__init__.py


+ 9 - 0
app/core/admin.py

@@ -0,0 +1,9 @@
1
+from django.contrib import admin
2
+
3
+# Register your models here.
4
+from django.contrib import admin
5
+from .models import Report
6
+
7
+@admin.register(Report)
8
+class ReportAdmin(admin.ModelAdmin):
9
+    list_display = ('name', 'created_by', 'created_at', 'updated_at')

+ 6 - 0
app/core/apps.py

@@ -0,0 +1,6 @@
1
+from django.apps import AppConfig
2
+
3
+
4
+class CoreConfig(AppConfig):
5
+    default_auto_field = 'django.db.models.BigAutoField'
6
+    name = 'core'

+ 27 - 0
app/core/migrations/0001_initial.py

@@ -0,0 +1,27 @@
1
+# Generated by Django 5.0 on 2024-12-21 13:45
2
+
3
+import django.db.models.deletion
4
+from django.conf import settings
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    initial = True
11
+
12
+    dependencies = [
13
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14
+    ]
15
+
16
+    operations = [
17
+        migrations.CreateModel(
18
+            name='Report',
19
+            fields=[
20
+                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21
+                ('name', models.CharField(max_length=255)),
22
+                ('created_at', models.DateTimeField(auto_now_add=True)),
23
+                ('updated_at', models.DateTimeField(auto_now=True)),
24
+                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reports', to=settings.AUTH_USER_MODEL)),
25
+            ],
26
+        ),
27
+    ]

+ 0 - 0
app/core/migrations/__init__.py


+ 17 - 0
app/core/models.py

@@ -0,0 +1,17 @@
1
+from django.db import models
2
+
3
+# Create your models here.
4
+from django.contrib.auth.models import User  # Assuming you're using Django's built-in User model
5
+
6
+class Report(models.Model):
7
+    name = models.CharField(max_length=255)  # Name of the report
8
+    created_by = models.ForeignKey(
9
+        User,
10
+        on_delete=models.CASCADE,
11
+        related_name="reports"
12
+    )  # Reference to the user who created the report
13
+    created_at = models.DateTimeField(auto_now_add=True)  # Automatically set when created
14
+    updated_at = models.DateTimeField(auto_now=True)  # Automatically updated when modified
15
+
16
+    def __str__(self):
17
+        return self.name

+ 3 - 0
app/core/tests.py

@@ -0,0 +1,3 @@
1
+from django.test import TestCase
2
+
3
+# Create your tests here.

+ 3 - 0
app/core/views.py

@@ -0,0 +1,3 @@
1
+from django.shortcuts import render
2
+
3
+# Create your views here.

+ 22 - 0
app/manage.py

@@ -0,0 +1,22 @@
1
+#!/usr/bin/env python
2
+"""Django's command-line utility for administrative tasks."""
3
+import os
4
+import sys
5
+
6
+
7
+def main():
8
+    """Run administrative tasks."""
9
+    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coi.settings")
10
+    try:
11
+        from django.core.management import execute_from_command_line
12
+    except ImportError as exc:
13
+        raise ImportError(
14
+            "Couldn't import Django. Are you sure it's installed and "
15
+            "available on your PYTHONPATH environment variable? Did you "
16
+            "forget to activate a virtual environment?"
17
+        ) from exc
18
+    execute_from_command_line(sys.argv)
19
+
20
+
21
+if __name__ == "__main__":
22
+    main()

+ 5 - 0
db-init/create-database.sql

@@ -0,0 +1,5 @@
1
+IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'coi_db')
2
+BEGIN
3
+    CREATE DATABASE coi_db;
4
+END;
5
+GO

+ 46 - 0
docker-compose.yml

@@ -0,0 +1,46 @@
1
+version: '3.9'
2
+
3
+services:
4
+  db:
5
+    image: mcr.microsoft.com/mssql/server:2019-latest
6
+    user: root
7
+    container_name: mssql_db
8
+    environment:
9
+      SA_PASSWORD: "StrongPassw0rd!"
10
+      ACCEPT_EULA: "Y"
11
+    ports:
12
+      - "1433:1433"
13
+    volumes:
14
+      - db_data:/var/opt/mssql/data
15
+      - ./db-init:/init-scripts  # Add this line to mount the initialization script
16
+      - ./entrypoint.sh:/usr/local/bin/entrypoint.sh
17
+    #command: /bin/bash -c "/opt/mssql/bin/sqlservr && sleep 5 && /opt/mssql-tools18/bin/sqlcmd -C -S db -U sa -P StrongPassw0rd! -i /init-scripts/create-database.sql"
18
+    #healthcheck:
19
+      #test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -C -S localhost -U sa -P StrongPassw0rd! -Q 'SELECT 1'"]
20
+      #interval: 10s
21
+      #timeout: 5s
22
+      #retries: 5
23
+    entrypoint: ["/usr/local/bin/entrypoint.sh"]
24
+    restart: always
25
+
26
+  web:
27
+    build: .
28
+    container_name: django_web
29
+    volumes:
30
+      - ./app:/app
31
+    ports:
32
+      - "8000:8000"
33
+    depends_on:
34
+      db:
35
+        condition: service_started
36
+        #condition: service_healthy
37
+    environment:
38
+      DB_NAME: "coi_db"
39
+      DB_USER: "sa"
40
+      DB_PASSWORD: "StrongPassw0rd!"
41
+      DB_HOST: "db"
42
+      DB_PORT: "1433"
43
+    restart: always
44
+
45
+volumes:
46
+  db_data:

+ 15 - 0
entrypoint.sh

@@ -0,0 +1,15 @@
1
+#!/bin/bash
2
+
3
+# Start SQL Server in the background
4
+/opt/mssql/bin/sqlservr &
5
+
6
+# Wait for SQL Server to start
7
+echo "Waiting for SQL Server to start..."
8
+sleep 15
9
+
10
+# Run the database initialization script
11
+echo "Running initialization script..."
12
+/opt/mssql-tools18/bin/sqlcmd -C -S localhost -U sa -P StrongPassw0rd! -i /init-scripts/create-database.sql
13
+
14
+# Wait indefinitely to keep the container running
15
+wait

+ 39 - 0
requirements.txt

@@ -0,0 +1,39 @@
1
+Django==5.0
2
+mssql-django
3
+pyodbc==4.0.39
4
+# For filtering and querying
5
+django-filter  # Simplified filtering
6
+
7
+# For tables/grid view
8
+django-tables2  # HTML table rendering
9
+
10
+# For exporting to Excel
11
+openpyxl  # Generating Excel files
12
+
13
+# For API development (optional)
14
+djangorestframework  # REST API support
15
+drf-yasg  # Swagger and ReDoc for API documentation (optional)
16
+
17
+# Admin enhancements (optional)
18
+django-import-export  # Export/import for admin
19
+
20
+# Authentication enhancements (optional)
21
+django-allauth  # For user authentication (optional)
22
+
23
+# Debugging and performance
24
+django-debug-toolbar  # Debugging SQL queries and performance issues
25
+
26
+# Testing (optional)
27
+pytest
28
+pytest-django
29
+factory-boy  # Test data creation
30
+
31
+# Frontend styling (if using Bootstrap or TailwindCSS)
32
+django-crispy-forms  # Styling for forms
33
+crispy-bootstrap5  # For Bootstrap 5 support (optional)
34
+
35
+# If using TailwindCSS instead of Bootstrap
36
+django-tailwind  # For TailwindCSS integration
37
+
38
+# Cache optimization (optional)
39
+django-cacheops  # Caching ORM QuerySets