diff --git a/dhub/urls.py b/dhub/urls.py index 9bd6fb4..6305b93 100644 --- a/dhub/urls.py +++ b/dhub/urls.py @@ -22,4 +22,5 @@ urlpatterns = [ path("", include("login.urls")), path("dashboard/", include("dashboard.urls")), path("device/", include("device.urls")), + path("lot/", include("lot.urls")), ] diff --git a/lot/migrations/0001_initial.py b/lot/migrations/0001_initial.py new file mode 100644 index 0000000..35ca297 --- /dev/null +++ b/lot/migrations/0001_initial.py @@ -0,0 +1,58 @@ +# Generated by Django 5.0.6 on 2024-07-08 13:55 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("device", "0003_remove_component_type_remove_computer_type_and_more"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Lot", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created", models.DateTimeField(auto_now_add=True)), + ("updated", models.DateTimeField(auto_now=True)), + ( + "type", + models.CharField( + choices=[ + ("Incoming", "Incoming"), + ("Outgoing", "Outgoing"), + ("Temporal", "Temporal"), + ], + default="Temporal", + max_length=32, + ), + ), + ("name", models.CharField(blank=True, max_length=64, null=True)), + ("code", models.CharField(blank=True, max_length=64, null=True)), + ("description", models.CharField(blank=True, max_length=64, null=True)), + ("closed", models.BooleanField(default=True)), + ("devices", models.ManyToManyField(to="device.device")), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/lot/models.py b/lot/models.py index 71a8362..9f33e57 100644 --- a/lot/models.py +++ b/lot/models.py @@ -1,3 +1,23 @@ from django.db import models +from django.utils.translation import gettext_lazy as _ +from utils.constants import STR_SM_SIZE, STR_SIZE -# Create your models here. +from user.models import User +from device.models import Device + + +class Lot(models.Model): + class Types(models.TextChoices): + INCOMING = "Incoming" + OUTGOING = "Outgoing" + TEMPORAL = "Temporal" + + created = models.DateTimeField(auto_now_add=True) + updated = models.DateTimeField(auto_now=True) + type = models.CharField(max_length=STR_SM_SIZE, choices=Types, default=Types.TEMPORAL) + name = models.CharField(max_length=STR_SIZE, blank=True, null=True) + code = models.CharField(max_length=STR_SIZE, blank=True, null=True) + description = models.CharField(max_length=STR_SIZE, blank=True, null=True) + closed = models.BooleanField(default=True) + owner = models.ForeignKey(User, on_delete=models.CASCADE) + devices = models.ManyToManyField(Device) diff --git a/lot/templates/new_lot.html b/lot/templates/new_lot.html new file mode 100644 index 0000000..8f2df37 --- /dev/null +++ b/lot/templates/new_lot.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} +
+ {% translate "Cancel" %} + +
+ +
+{% endblock %} diff --git a/lot/urls.py b/lot/urls.py new file mode 100644 index 0000000..313a23d --- /dev/null +++ b/lot/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from lot import views + +app_name = 'lot' + +urlpatterns = [ + path("add/", views.NewLotView.as_view(), name="add"), + path("edit//", views.EditLotView.as_view(), name="edit"), +] diff --git a/lot/views.py b/lot/views.py index 91ea44a..12e7872 100644 --- a/lot/views.py +++ b/lot/views.py @@ -1,3 +1,51 @@ -from django.shortcuts import render +from django.urls import reverse_lazy +from django.shortcuts import get_object_or_404 +from django.utils.translation import gettext_lazy as _ +from django.views.generic.edit import ( + CreateView, + UpdateView, +) +from dashboard.mixins import DashboardView, DetailsMixin +from lot.models import Lot -# Create your views here. + +class NewLotView(DashboardView, CreateView): + template_name = "new_lot.html" + title = _("New lot") + breadcrumb = "lot / New lot" + success_url = reverse_lazy('dashboard:unassigned_devices') + model = Lot + fields = ( + "type", + "name", + "code", + "description", + "closed", + ) + + def form_valid(self, form): + form.instance.owner = self.request.user + response = super().form_valid(form) + return response + + +class EditLotView(DashboardView, UpdateView): + template_name = "new_lot.html" + title = _("Update lot") + breadcrumb = "Lot / Update lot" + success_url = reverse_lazy('dashboard:unassigned_devices') + model = Lot + fields = ( + "type", + "name", + "code", + "description", + "closed", + ) + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + self.object = get_object_or_404(self.model, pk=pk) + # self.success_url = reverse_lazy('dashbiard:lot', args=[pk]) + kwargs = super().get_form_kwargs() + return kwargs