From c5f1b4e1310f6e4a7dcd2250a09709246f9daa5d Mon Sep 17 00:00:00 2001 From: juanjo <130799031+juanminguezsanz2023@users.noreply.github.com> Date: Sat, 11 Apr 2026 13:00:25 +0200 Subject: [PATCH] refactor codigo --- .gitignore | 1 + apps/common/apps.py | 5 ++ apps/promociones/actions.py | 6 +- apps/promociones/apps.py | 5 ++ core/settings.py | 107 ++++++++++++++++++++++++++++++++---- core/urls.py | 4 +- db.sqlite3 | Bin 0 -> 16384 bytes init_db.py | 32 +++++++++++ 8 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 apps/common/apps.py create mode 100644 apps/promociones/apps.py create mode 100644 db.sqlite3 create mode 100644 init_db.py 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 0000000000000000000000000000000000000000..933e1e8df4fc3fa595d10548dfb9355884a67a2f GIT binary patch literal 16384 zcmeI(zi!$<90%|_8x;{L&`vBJeijQTNd>fN*E(=bQ3z1ismg?mxulkYO>8H7+NqDy zm&rTy0U~A3(z6q=!_cWik-k6KzO&C~{qE!G{O9whz86b64Z^t-(`)vean9ZoF~$my zt18ma)HXS`48E-Vr7W;lS3kGa33~Y@E8mxYsR9832tWV=5P$##AOHafKmY>&nZWgy zUaT4hzn;X-^jx}5EYE__bE2$P=vczE1=;37Ur=_O>Ra>}-i&$!TO10DMpp0Gw8nHK z#$=w@!`?u3KNbUfcXhTO%%`EGFQ(P`Xj=95{(d8EYmBsK)twzaPE(04&eg1S63L58 z>AP}mSAUY!T4UXJ=5kl{Y<$;vv0AI~A4lmm7hy0DTrcotl&fk_u9a)}mm|!Ee=6N? z&cyRQl?Zi}Y}PcBksEr8Btx>r*PNN-#@sH59UeVrdHN>bz>&k-FRS4 z=H|R|!_