mirror of
https://github.com/adrigongv23/G26---Telemetry-Software.git
synced 2026-08-25 11:33:17 +02:00
Inicio del Django y base de datos
This commit is contained in:
parent
57494f02e5
commit
0e0c8d7d1b
66 changed files with 647 additions and 69 deletions
0
Plataform_Web/documentos/__init__.py
Normal file
0
Plataform_Web/documentos/__init__.py
Normal file
BIN
Plataform_Web/documentos/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
Plataform_Web/documentos/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Plataform_Web/documentos/__pycache__/admin.cpython-314.pyc
Normal file
BIN
Plataform_Web/documentos/__pycache__/admin.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Plataform_Web/documentos/__pycache__/apps.cpython-314.pyc
Normal file
BIN
Plataform_Web/documentos/__pycache__/apps.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Plataform_Web/documentos/__pycache__/models.cpython-314.pyc
Normal file
BIN
Plataform_Web/documentos/__pycache__/models.cpython-314.pyc
Normal file
Binary file not shown.
14
Plataform_Web/documentos/admin.py
Normal file
14
Plataform_Web/documentos/admin.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from django.contrib import admin
|
||||
from .models import Documento
|
||||
|
||||
@admin.register(Documento)
|
||||
class DocumentoAdmin(admin.ModelAdmin):
|
||||
list_display = ('titulo', 'categoria', 'tipo', 'temporada', 'subido_por', 'fecha_subida')
|
||||
list_filter = ('temporada', 'categoria', 'tipo') # ¡Filtros laterales muy útiles!
|
||||
search_fields = ('titulo', 'descripcion')
|
||||
|
||||
# Esto hace que el campo "subido_por" se rellene solo con tu usuario al crear un doc
|
||||
def save_model(self, request, obj, form, change):
|
||||
if not obj.subido_por:
|
||||
obj.subido_por = request.user
|
||||
super().save_model(request, obj, form, change)
|
||||
5
Plataform_Web/documentos/apps.py
Normal file
5
Plataform_Web/documentos/apps.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DocumentosConfig(AppConfig):
|
||||
name = 'documentos'
|
||||
37
Plataform_Web/documentos/migrations/0001_initial.py
Normal file
37
Plataform_Web/documentos/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# Generated by Django 6.0.2 on 2026-02-17 11:36
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('temporadas', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Documento',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('titulo', models.CharField(max_length=100, verbose_name='Título del documento')),
|
||||
('archivo', models.FileField(upload_to='ingenieria_docs/')),
|
||||
('categoria', models.CharField(choices=[('aerodinamica', 'Aerodinámica'), ('chasis', 'Chasis'), ('suspension', 'Suspensión'), ('motor', 'Motor/Powertrain'), ('electronica', 'Electrónica'), ('general', 'General / Normativa')], max_length=20)),
|
||||
('tipo', models.CharField(choices=[('diseno', 'Diseño / CAD'), ('simulacion', 'Simulación'), ('informe', 'Informe Técnico'), ('factura', 'Factura / Presupuesto'), ('otro', 'Otro')], default='informe', max_length=20)),
|
||||
('descripcion', models.TextField(blank=True, null=True, verbose_name='Descripción o notas')),
|
||||
('fecha_subida', models.DateTimeField(auto_now_add=True)),
|
||||
('subido_por', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='documentos_subidos', to=settings.AUTH_USER_MODEL)),
|
||||
('temporada', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='temporadas.temporada')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Documento Técnico',
|
||||
'verbose_name_plural': 'Documentos de Ingeniería',
|
||||
'ordering': ['-fecha_subida'],
|
||||
},
|
||||
),
|
||||
]
|
||||
0
Plataform_Web/documentos/migrations/__init__.py
Normal file
0
Plataform_Web/documentos/migrations/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
59
Plataform_Web/documentos/models.py
Normal file
59
Plataform_Web/documentos/models.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
from django.db import models
|
||||
from users.models import CustomUser
|
||||
from temporadas.models import Temporada
|
||||
import os
|
||||
|
||||
class Documento(models.Model):
|
||||
# Opciones de categorías (puedes añadir más)
|
||||
CATEGORIAS = (
|
||||
('aerodinamica', 'Aerodinámica'),
|
||||
('chasis', 'Chasis'),
|
||||
('suspension', 'Suspensión'),
|
||||
('motor', 'Motor/Powertrain'),
|
||||
('electronica', 'Electrónica'),
|
||||
('general', 'General / Normativa'),
|
||||
('software', 'Software')
|
||||
)
|
||||
|
||||
TIPO_DOC = (
|
||||
('diseno', 'Diseño / CAD'),
|
||||
('simulacion', 'Simulación'),
|
||||
('informe', 'Informe Técnico'),
|
||||
('factura', 'Factura / Presupuesto'),
|
||||
('otro', 'Otro'),
|
||||
)
|
||||
|
||||
titulo = models.CharField(max_length=100, verbose_name="Título del documento")
|
||||
|
||||
# Aquí definimos dónde se guardan los archivos.
|
||||
# upload_to='ingenieria/' creará esa carpeta automáticamente.
|
||||
archivo = models.FileField(upload_to='ingenieria_docs/')
|
||||
|
||||
categoria = models.CharField(max_length=20, choices=CATEGORIAS)
|
||||
tipo = models.CharField(max_length=20, choices=TIPO_DOC, default='informe')
|
||||
|
||||
descripcion = models.TextField(blank=True, null=True, verbose_name="Descripción o notas")
|
||||
|
||||
# RELACIONES (La parte potente)
|
||||
# 1. Si borras una temporada, ¿borramos sus documentos? -> models.CASCADE (Sí)
|
||||
temporada = models.ForeignKey(Temporada, on_delete=models.CASCADE)
|
||||
|
||||
# 2. Si borras un usuario, ¿borramos sus docs? -> models.SET_NULL (No, mejor mantenemos el doc y ponemos usuario a null)
|
||||
subido_por = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, related_name="documentos_subidos")
|
||||
|
||||
fecha_subida = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Documento Técnico"
|
||||
verbose_name_plural = "Documentos de Ingeniería"
|
||||
ordering = ['-fecha_subida']
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.titulo} ({self.temporada})"
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
# Esto borra el archivo físico del disco duro cuando borras la entrada en la base de datos
|
||||
if self.archivo:
|
||||
if os.path.isfile(self.archivo.path):
|
||||
os.remove(self.archivo.path)
|
||||
super().delete(*args, **kwargs)
|
||||
3
Plataform_Web/documentos/tests.py
Normal file
3
Plataform_Web/documentos/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
Plataform_Web/documentos/views.py
Normal file
3
Plataform_Web/documentos/views.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Loading…
Add table
Add a link
Reference in a new issue