diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/apps/common/apps.py b/apps/common/apps.py new file mode 100644 index 0000000..0c3ab12 --- /dev/null +++ b/apps/common/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + +class CommonConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'apps.common' \ No newline at end of file diff --git a/apps/promociones/actions.py b/apps/promociones/actions.py index 8f8d48e..fab8da2 100644 --- a/apps/promociones/actions.py +++ b/apps/promociones/actions.py @@ -1,5 +1,5 @@ from django.db import connection -from common.utils import clean_sql_string, clean_sql_int +from apps.common.utils import clean_sql_string, clean_sql_int def getData(params): """ @@ -39,9 +39,9 @@ def setData(params): WHERE p.categoria_id = c.id AND p.id = %s """ - # Fecha en formato Año-Día-Mes (Y-d-m) + # Fecha en formato YYYY-MM-DD para SQL import datetime - fecha_hoy = datetime.datetime.now().strftime('%Y-%d-%m') + fecha_hoy = datetime.datetime.now().strftime('%Y-%m-%d') parameter_dict = [ clean_sql_string(params.get('nombre')), diff --git a/apps/promociones/apps.py b/apps/promociones/apps.py new file mode 100644 index 0000000..24d8bb9 --- /dev/null +++ b/apps/promociones/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + +class PromocionesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'apps.promociones' \ No newline at end of file diff --git a/core/settings.py b/core/settings.py index 36c1ad3..7bd15e9 100644 --- a/core/settings.py +++ b/core/settings.py @@ -1,20 +1,105 @@ from dotenv import load_dotenv import os +from pathlib import Path # Cargar variables desde el archivo .env load_dotenv() -SECRET_KEY = os.getenv('SECRET_KEY') -DEBUG = os.getenv('DEBUG') == 'True' +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent -# Configuración de base de datos usando las variables -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.getenv('DB_NAME'), - 'USER': os.getenv('DB_USER'), - 'PASSWORD': os.getenv('DB_PASSWORD'), - 'HOST': os.getenv('DB_HOST'), - 'PORT': os.getenv('DB_PORT'), +SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-change-me-for-production') +DEBUG = os.getenv('DEBUG', 'True').lower() == 'true' + +ALLOWED_HOSTS = ['localhost', '127.0.0.1'] + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'apps.promociones', + 'apps.common', +] + +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 = 'core.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + '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' + +# Para desarrollo local: SQLite +if DEBUG: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } } +else: + # Producción: PostgreSQL desde .env + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.getenv('DB_NAME'), + 'USER': os.getenv('DB_USER'), + 'PASSWORD': os.getenv('DB_PASSWORD'), + 'HOST': os.getenv('DB_HOST'), + 'PORT': os.getenv('DB_PORT'), + } + } + +# Internationalization +LANGUAGE_CODE = 'es-es' +TIME_ZONE = 'Europe/Madrid' +USE_I18N = True +USE_TZ = True + +# Static files +STATIC_URL = '/static/' +STATIC_ROOT = BASE_DIR / 'staticfiles' + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +# Logging básico +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'INFO', + }, + }, } \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index ddb5a6d..eaae147 100644 --- a/core/urls.py +++ b/core/urls.py @@ -2,5 +2,5 @@ from django.urls import path, include urlpatterns = [ # Redirigimos todas las peticiones de /api/promociones/ a nuestra app - path('api/promociones/', include('promociones.urls')), -] \ No newline at end of file + path('api/promociones/', include('apps.promociones.urls')), +] diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..933e1e8 Binary files /dev/null and b/db.sqlite3 differ diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..3738b4d --- /dev/null +++ b/init_db.py @@ -0,0 +1,32 @@ +import os +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') +import django +django.setup() +from django.db import connection +with connection.cursor() as cursor: + cursor.execute(""" + CREATE TABLE IF NOT EXISTS promociones ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + nombre VARCHAR(255), + fecha_inicio DATE, + descripcion TEXT, + activo BOOLEAN DEFAULT 1, + categoria_id INTEGER + ) + """) + cursor.execute(""" + INSERT OR IGNORE INTO promociones (id, nombre, fecha_inicio, descripcion, activo, categoria_id) + VALUES (1, 'Promo Test', '2026-04-11', 'Descripcion de prueba', 1, 1) + """) + cursor.execute(""" + CREATE TABLE IF NOT EXISTS categorias ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + nombre VARCHAR(255) + ) + """) + cursor.execute(""" + INSERT OR IGNORE INTO categorias (id, nombre) + VALUES (1, 'Categoria Test') + """) +connection.commit() +print('Tablas y datos de prueba creados exitosamente en db.sqlite3') \ No newline at end of file