diff --git a/.gitignore b/.gitignore index e9dbbab..56d8c05 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ db.sqlite3 .env staticfiles/ media/ -.vscode \ No newline at end of file +.vscode +.venv/ \ No newline at end of file diff --git a/products/templates/products/list.html b/products/templates/products/list.html index 5342e04..5307ae6 100644 --- a/products/templates/products/list.html +++ b/products/templates/products/list.html @@ -12,7 +12,7 @@
{% if product.image %} - {{ product.name }} + {{ product.name }} {% endif %}
{{ product.name }}
diff --git a/products/views.py b/products/views.py index c18c187..99fb7e6 100644 --- a/products/views.py +++ b/products/views.py @@ -1,11 +1,34 @@ import os +import time +from django.http import HttpResponse +from django.template.loader import render_to_string from django.shortcuts import render from .models import Product + def product_list(request): - items = Product.objects.all() env_type = os.getenv("ENV_TYPE", "undefined") - return render(request, 'products/list.html', { + + # Measure DB query time (force evaluation) + db_start = time.time() + items = list(Product.objects.all()) + db_time = time.time() - db_start + + # First render to measure render time + ctx = { 'products': items, - 'env_type': env_type - }) + 'env_type': env_type, + 'db_time': db_time, + 'total_time': None, + } + + render_start = time.time() + _ = render_to_string('products/list.html', ctx, request=request) + render_time = time.time() - render_start + + total_time = db_time + render_time + + # Final render with measured times + ctx['total_time'] = total_time + html = render_to_string('products/list.html', ctx, request=request) + return HttpResponse(html) diff --git a/scripts/init_project.ps1 b/scripts/init_project.ps1 new file mode 100644 index 0000000..231f0bb --- /dev/null +++ b/scripts/init_project.ps1 @@ -0,0 +1,55 @@ +param( + [string]$Username = "ack", + [string]$Email = "admin@example.com", + [string]$Password = "123" +) + +Write-Host "Initializing project environment..." + +if (-not (Test-Path ".venv")) { + Write-Host "Creating virtual environment .venv..." + python -m venv .venv +} else { + Write-Host "Virtual environment .venv already exists." +} + +$venvPython = Join-Path -Path ".venv" -ChildPath "Scripts\python.exe" +$venvPip = Join-Path -Path ".venv" -ChildPath "Scripts\pip.exe" + +Write-Host "Upgrading pip and installing requirements..." +& $venvPython -m pip install --upgrade pip +& $venvPip install -r requirements.txt + +Write-Host "Applying database migrations..." +& $venvPython manage.py migrate + +Write-Host "Collecting static files..." +& $venvPython manage.py collectstatic --noinput + +if (-not $Password) { + $secure = Read-Host "Enter superuser password (input hidden)" -AsSecureString + $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) + $plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr) + [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) +} else { + $plainPassword = $Password +} + +Write-Host "Creating superuser (if it doesn't exist)..." +$createUserPy = @" +from django.contrib.auth import get_user_model +User = get_user_model() +username = '$Username' +email = '$Email' +password = '$plainPassword' +if not User.objects.filter(username=username).exists(): + User.objects.create_superuser(username=username, email=email, password=password) + print('Superuser created.') +else: + print('Superuser already exists.') +"@ + +# pipe the python snippet into manage.py shell +$createUserPy | & $venvPython manage.py shell + +Write-Host "Initialization complete. You can now run the development server with: .\.venv\Scripts\python.exe manage.py runserver" diff --git a/templates/base.html b/templates/base.html index 9ea2fbd..c53adc6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -7,6 +7,14 @@ {% block title %}Мой проект{% endblock %} + {% block extra_css %}{% endblock %} diff --git a/templates/components/footer.html b/templates/components/footer.html index 8a8f37c..7d5a18c 100644 --- a/templates/components/footer.html +++ b/templates/components/footer.html @@ -2,5 +2,10 @@

© 2023 Мой проект. Все права защищены.

+

+ ENV: {{ env_type|default:"unknown" }} + | Page generated in {{ total_time|default:"-"|floatformat:3 }}s + (DB: {{ db_time|default:"-"|floatformat:3 }}s) +

\ No newline at end of file diff --git a/templates/components/navbar.html b/templates/components/navbar.html index 6efb009..3430f51 100644 --- a/templates/components/navbar.html +++ b/templates/components/navbar.html @@ -15,6 +15,9 @@