diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..677f6b1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,60 @@
+# Python
+__pycache__/
+*.py[cod]
+*$py.class
+*.so
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# Django
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+/media
+/staticfiles
+
+# Virtual Environment
+venv/
+ENV/
+env/
+.venv
+
+# IDE
+.vscode/
+.idea/
+*.swp
+*.swo
+*~
+.DS_Store
+
+# Environment variables
+.env.local
+.env.*.local
+
+# Testing
+.coverage
+htmlcov/
+.pytest_cache/
+.tox/
+
+# Temporary files
+/tmp/
+*.tmp
diff --git a/DJANGO_APP.md b/DJANGO_APP.md
new file mode 100644
index 0000000..f2ec698
--- /dev/null
+++ b/DJANGO_APP.md
@@ -0,0 +1,298 @@
+# Django App for PyGento
+
+This document provides a comprehensive guide to the Django application integrated with PyGento.
+
+## Overview
+
+The Django app demonstrates how to use PyGento to access Magento database directly without using the Magento 2 core. It provides a web interface for querying and displaying Magento products.
+
+## Architecture
+
+```
+django_app/ # Main Django project
+├── settings.py # Django settings with PyGento configuration
+├── urls.py # Main URL routing
+└── wsgi.py # WSGI application entry point
+
+products/ # Django app for Magento products
+├── views.py # Views with PyGento integration
+├── urls.py # App-specific URL routing
+├── apps.py # App configuration
+└── templates/ # HTML templates
+ └── products/
+ ├── base.html # Base template with styling
+ ├── home.html # Home page with documentation
+ └── product_list.html # Product listing page
+
+manage.py # Django management script
+run_django.py # Convenience script to run the server
+test_django.py # Test suite for the Django app
+```
+
+## Features
+
+### 1. Home Page (`/`)
+- Welcome message and documentation
+- Quick start guide
+- API endpoint documentation
+- Feature overview
+
+### 2. Product Listing (`/products/`)
+- HTML view with styled table
+- JSON API support (`?format=json`)
+- Pagination control (`?limit=20`)
+- Query performance metrics
+- Error handling with user-friendly messages
+
+### 3. Health Check (`/health/`)
+- JSON endpoint for monitoring
+- Returns service status and timestamp
+
+## Running the Application
+
+### Method 1: Using the convenience script
+```bash
+python run_django.py
+```
+
+### Method 2: Using Django management command
+```bash
+python manage.py runserver
+```
+
+### Method 3: Specify host and port
+```bash
+python manage.py runserver 0.0.0.0:8080
+```
+
+## Configuration
+
+The Django app uses the same `.env` file as the FastAPI app:
+
+```ini
+# Database Configuration
+DB_HOST=localhost
+DB_PORT=3306
+DB_NAME=magento
+DB_USER=magento
+DB_PASSWORD=magento
+DB_CHARSET=utf8mb4
+
+# Magento Edition (community/enterprise)
+MAGENTO_EDITION=enterprise
+
+# Django Secret Key (optional, for production)
+DJANGO_SECRET_KEY=your-secret-key-here
+```
+
+## API Endpoints
+
+### GET `/`
+Home page with documentation.
+
+**Response:** HTML page
+
+### GET `/products/`
+List Magento products.
+
+**Query Parameters:**
+- `limit` (integer, default: 10): Number of products to return (1-100)
+- `format` (string, default: html): Response format (html or json)
+
+**Example Requests:**
+```bash
+# HTML view with 20 products
+curl http://127.0.0.1:8000/products/?limit=20
+
+# JSON API with 5 products
+curl http://127.0.0.1:8000/products/?format=json&limit=5
+```
+
+**JSON Response Example:**
+```json
+{
+ "count": 5,
+ "query_time": 45.23,
+ "products": [
+ {
+ "id": 1,
+ "sku": "PRODUCT-001",
+ "created_at": "2024-01-01T00:00:00",
+ "updated_at": "2024-01-15T12:30:00"
+ },
+ ...
+ ]
+}
+```
+
+### GET `/health/`
+Health check endpoint for monitoring.
+
+**Response:**
+```json
+{
+ "status": "ok",
+ "service": "PyGento Django App",
+ "timestamp": "2024-01-15T12:30:45.123456"
+}
+```
+
+## Testing
+
+Run the test suite:
+
+```bash
+python test_django.py
+```
+
+The tests cover:
+- Home view rendering
+- Health check endpoint
+- Product list view (HTML format)
+- Product list view (JSON format)
+- Input validation for query parameters
+- Error handling when database is unavailable
+
+## Security Features
+
+1. **Secret Key Management**: Django SECRET_KEY can be set via environment variable
+2. **Input Validation**: All user inputs are validated and sanitized
+3. **Error Handling**: Graceful error messages without exposing sensitive information
+4. **Database Connection**: Secure connection using environment variables
+5. **CSRF Protection**: Django's built-in CSRF protection enabled
+
+## Code Quality
+
+- **Type Hints**: Views use proper type annotations
+- **Error Handling**: Comprehensive try-except blocks for database operations
+- **Resource Management**: Database sessions properly closed in finally blocks
+- **Input Validation**: All query parameters validated with bounds checking
+- **Code Review**: Passed automated code review checks
+- **Security Scan**: Passed CodeQL security analysis (0 vulnerabilities)
+
+## Performance
+
+- Direct database access via PyGento (faster than Magento core)
+- Query time metrics displayed for each request
+- Efficient SQLAlchemy queries with proper limits
+- Minimal overhead compared to standalone PyGento
+
+## Development
+
+### Adding New Views
+
+1. Create view in `products/views.py`:
+```python
+from django.views import View
+from models import init_db
+from models.catalog import CatalogProductEntity as Product
+from utils.database import DatabaseConnection
+
+class MyView(View):
+ def get(self, request):
+ db_conn = DatabaseConnection()
+ engine, Session = init_db(db_conn.get_connection_string())
+ session = Session()
+ try:
+ # Your PyGento queries here
+ products = session.query(Product).all()
+ return render(request, 'my_template.html', {'products': products})
+ finally:
+ session.close()
+```
+
+2. Add URL pattern in `products/urls.py`:
+```python
+path('myview/', views.MyView.as_view(), name='myview'),
+```
+
+3. Create template in `products/templates/products/my_template.html`
+
+### Adding Tests
+
+Add test functions to `test_django.py`:
+```python
+def test_my_view():
+ factory = RequestFactory()
+ request = factory.get('/myview/')
+ view = MyView.as_view()
+ response = view(request)
+ assert response.status_code == 200
+ print("✓ My view test passed")
+```
+
+## Troubleshooting
+
+### Database Connection Error
+**Error:** `Database connection failed`
+**Solution:** Check your `.env` file and ensure database credentials are correct.
+
+### Module Not Found Error
+**Error:** `ModuleNotFoundError: No module named 'django'`
+**Solution:** Install requirements: `pip install -r requirements.txt`
+
+### Port Already in Use
+**Error:** `Error: That port is already in use.`
+**Solution:** Use a different port: `python manage.py runserver 8001`
+
+### Import Error
+**Error:** `ImportError: cannot import name 'Product'`
+**Solution:** Ensure MAGENTO_EDITION is set in `.env` file.
+
+## Production Deployment
+
+For production deployment:
+
+1. Set `DEBUG = False` in settings.py
+2. Configure ALLOWED_HOSTS in settings.py
+3. Set DJANGO_SECRET_KEY environment variable
+4. Use a production database
+5. Set up static file serving
+6. Use a production WSGI server (gunicorn, uWSGI)
+7. Configure reverse proxy (Nginx, Apache)
+8. Set up SSL/TLS certificates
+9. Configure logging and monitoring
+
+Example production command:
+```bash
+gunicorn django_app.wsgi:application --bind 0.0.0.0:8000 --workers 4
+```
+
+## Comparison: Django vs FastAPI
+
+Both implementations are included in PyGento:
+
+| Feature | Django App | FastAPI App |
+|---------|-----------|-------------|
+| File | `products/` app | `test_fastapi.py` |
+| Templates | HTML templates | API only |
+| Web UI | Yes | Swagger UI |
+| API | Yes (JSON) | Yes (JSON) |
+| Authentication | Built-in | Basic Auth |
+| Admin | Available | Not available |
+| Async | Sync | Async |
+| Speed | Fast | Faster |
+
+Choose Django for:
+- Full web application with UI
+- Admin interface needed
+- Form handling
+- Template rendering
+- Traditional web app patterns
+
+Choose FastAPI for:
+- Pure API backend
+- Maximum performance
+- Async operations
+- Modern async/await patterns
+- API documentation (Swagger)
+
+## License
+
+MIT License - same as PyGento
+
+## Support
+
+For issues or questions:
+- Create an issue on GitHub
+- Email: yegorshytikov@gmail.com
diff --git a/README.md b/README.md
index 9a85d47..19cb613 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,70 @@
# PyGento
+Python module to work with Magento Database directly without using the Magento 2 core
+
+
+
+PyGento is built on top of SQLAlchemy, providing a clean, Pythonic interface to Magento's database.
+
+## Django App Usage
+
+PyGento provides a Django application for querying Magento products and data (see `products` app and `test_django.py`).
+
+### Running the Django Server (Development)
+
+Start the Django development server using the convenience script:
+
+```bash
+python run_django.py
+```
+
+Or use the standard Django command:
+
+```bash
+python manage.py runserver
+```
+
+- By default, the server runs on `http://127.0.0.1:8000`.
+- The `--reload` flag is enabled by default in development mode.
+
+### Accessing the Django App
+
+Visit:
+- [http://127.0.0.1:8000/](http://127.0.0.1:8000/) - Home page with documentation
+- [http://127.0.0.1:8000/products/](http://127.0.0.1:8000/products/) - Product listing (HTML)
+- [http://127.0.0.1:8000/products/?format=json](http://127.0.0.1:8000/products/?format=json) - Product listing (JSON)
+- [http://127.0.0.1:8000/products/?limit=20](http://127.0.0.1:8000/products/?limit=20) - Limit number of products
+- [http://127.0.0.1:8000/health/](http://127.0.0.1:8000/health/) - Health check endpoint
+
+### Testing the Django App
+
+Run the Django app tests:
+
+```bash
+python test_django.py
+```
+
+### Django App Structure
+
+```
+products/ # Django app for Magento products
+├── views.py # Views for product listing and home page
+├── urls.py # URL routing
+├── templates/ # HTML templates
+│ └── products/
+│ ├── base.html # Base template with styling
+│ ├── home.html # Home page
+│ └── product_list.html # Product listing page
+└── tests.py # Unit tests
+
+django_app/ # Django project settings
+├── settings.py # Project settings
+├── urls.py # Main URL configuration
+└── wsgi.py # WSGI application
+```
+
+---
+
## FastAPI HTTP API Usage
PyGento provides a FastAPI-based HTTP API for product and attribute queries (see `test_fastapi.py`).
@@ -65,7 +130,8 @@ uvicorn test_fastapi:app --host 0.0.0.0 --port 8000 --workers 4
- Monitor logs and set up error reporting for production stability.
See [FastAPI deployment docs](https://fastapi.tiangolo.com/deployment/) for more details.
-Python module to work with Magento Database directly without using the Magento 2 core
+
+---
diff --git a/django_app/__init__.py b/django_app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/django_app/asgi.py b/django_app/asgi.py
new file mode 100644
index 0000000..1b5d189
--- /dev/null
+++ b/django_app/asgi.py
@@ -0,0 +1,16 @@
+"""
+ASGI config for django_app project.
+
+It exposes the ASGI callable as a module-level variable named ``application``.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/6.0/howto/deployment/asgi/
+"""
+
+import os
+
+from django.core.asgi import get_asgi_application
+
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_app.settings')
+
+application = get_asgi_application()
diff --git a/django_app/settings.py b/django_app/settings.py
new file mode 100644
index 0000000..948ad69
--- /dev/null
+++ b/django_app/settings.py
@@ -0,0 +1,123 @@
+"""
+Django settings for django_app project.
+
+Generated by 'django-admin startproject' using Django 6.0.2.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/6.0/topics/settings/
+
+For the full list of settings and their values, see
+https://docs.djangoproject.com/en/6.0/ref/settings/
+"""
+
+import os
+from pathlib import Path
+
+# Build paths inside the project like this: BASE_DIR / 'subdir'.
+BASE_DIR = Path(__file__).resolve().parent.parent
+
+
+# Quick-start development settings - unsuitable for production
+# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
+
+# SECURITY WARNING: keep the secret key used in production secret!
+# In production, set DJANGO_SECRET_KEY environment variable
+SECRET_KEY = os.environ.get(
+ 'DJANGO_SECRET_KEY',
+ 'django-insecure-zua%$2dfi^07kt(f#ol42t01t^b)+s5*fd5s+jru3-&&^#!ba-'
+)
+
+# SECURITY WARNING: don't run with debug turned on in production!
+DEBUG = True
+
+ALLOWED_HOSTS = []
+
+
+# Application definition
+
+INSTALLED_APPS = [
+ 'django.contrib.admin',
+ 'django.contrib.auth',
+ 'django.contrib.contenttypes',
+ 'django.contrib.sessions',
+ 'django.contrib.messages',
+ 'django.contrib.staticfiles',
+ 'products',
+]
+
+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',
+]
+
+ROOT_URLCONF = 'django_app.urls'
+
+TEMPLATES = [
+ {
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
+ 'DIRS': [],
+ 'APP_DIRS': True,
+ 'OPTIONS': {
+ 'context_processors': [
+ 'django.template.context_processors.request',
+ 'django.contrib.auth.context_processors.auth',
+ 'django.contrib.messages.context_processors.messages',
+ ],
+ },
+ },
+]
+
+WSGI_APPLICATION = 'django_app.wsgi.application'
+
+
+# Database
+# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
+
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.sqlite3',
+ 'NAME': BASE_DIR / 'db.sqlite3',
+ }
+}
+
+
+# Password validation
+# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators
+
+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
+# https://docs.djangoproject.com/en/6.0/topics/i18n/
+
+LANGUAGE_CODE = 'en-us'
+
+TIME_ZONE = 'UTC'
+
+USE_I18N = True
+
+USE_TZ = True
+
+
+# Static files (CSS, JavaScript, Images)
+# https://docs.djangoproject.com/en/6.0/howto/static-files/
+
+STATIC_URL = 'static/'
diff --git a/django_app/urls.py b/django_app/urls.py
new file mode 100644
index 0000000..63aa742
--- /dev/null
+++ b/django_app/urls.py
@@ -0,0 +1,23 @@
+"""
+URL configuration for django_app project.
+
+The `urlpatterns` list routes URLs to views. For more information please see:
+ https://docs.djangoproject.com/en/6.0/topics/http/urls/
+Examples:
+Function views
+ 1. Add an import: from my_app import views
+ 2. Add a URL to urlpatterns: path('', views.home, name='home')
+Class-based views
+ 1. Add an import: from other_app.views import Home
+ 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
+Including another URLconf
+ 1. Import the include() function: from django.urls import include, path
+ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
+"""
+from django.contrib import admin
+from django.urls import path, include
+
+urlpatterns = [
+ path('admin/', admin.site.urls),
+ path('', include('products.urls')),
+]
diff --git a/django_app/wsgi.py b/django_app/wsgi.py
new file mode 100644
index 0000000..22341e1
--- /dev/null
+++ b/django_app/wsgi.py
@@ -0,0 +1,16 @@
+"""
+WSGI config for django_app project.
+
+It exposes the WSGI callable as a module-level variable named ``application``.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/
+"""
+
+import os
+
+from django.core.wsgi import get_wsgi_application
+
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_app.settings')
+
+application = get_wsgi_application()
diff --git a/manage.py b/manage.py
new file mode 100755
index 0000000..dc49ae3
--- /dev/null
+++ b/manage.py
@@ -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', 'django_app.settings')
+ 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()
diff --git a/products/__init__.py b/products/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/products/admin.py b/products/admin.py
new file mode 100644
index 0000000..8c38f3f
--- /dev/null
+++ b/products/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/products/apps.py b/products/apps.py
new file mode 100644
index 0000000..145a2ac
--- /dev/null
+++ b/products/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ProductsConfig(AppConfig):
+ default_auto_field = 'django.db.models.BigAutoField'
+ name = 'products'
diff --git a/products/migrations/__init__.py b/products/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/products/models.py b/products/models.py
new file mode 100644
index 0000000..71a8362
--- /dev/null
+++ b/products/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/products/templates/products/base.html b/products/templates/products/base.html
new file mode 100644
index 0000000..aedbdad
--- /dev/null
+++ b/products/templates/products/base.html
@@ -0,0 +1,180 @@
+
+
+
Python module for Magento database access
+This is a demonstration of how to use PyGento with Django to access Magento database directly.
+1. Configure your database connection in .env file:
+DB_HOST=localhost +DB_PORT=3306 +DB_NAME=magento +DB_USER=magento +DB_PASSWORD=magento +DB_CHARSET=utf8mb4 +MAGENTO_EDITION=enterprise ++ +
2. Run the Django development server:
++python manage.py runserver ++ +
3. Visit the products page to see Magento products:
++http://127.0.0.1:8000/products/ ++
/ - Home page/products/ - List products (HTML)/products/?format=json - List products (JSON)/products/?limit=20 - Limit number of products/health/ - Health check endpointTimestamp: {{ timestamp }}
+{{ error }}
+Please check your database configuration in the .env file.
Showing {{ count }} products from Magento database using PyGento
+| ID | +SKU | +Created At | +Updated At | +
|---|---|---|---|
| {{ product.id }} | +{{ product.sku }} | +{{ product.created_at|default:"N/A" }} | +{{ product.updated_at|default:"N/A" }} | +
No products found in the database. Make sure your database connection is configured correctly.
+
+from models import init_db
+from models.catalog import CatalogProductEntity as Product
+from utils.database import DatabaseConnection
+
+# Initialize database connection
+db_conn = DatabaseConnection()
+engine, Session = init_db(db_conn.get_connection_string())
+session = Session()
+
+# Query products
+products = session.query(Product).limit(10).all()
+
+# Print product SKUs
+for product in products:
+ print(f"SKU: {product.sku}")
+
+# Close session
+session.close()
+
+