diff --git a/src/genlab_bestilling/models.py b/src/genlab_bestilling/models.py index 6d1c7fb..2c953c0 100644 --- a/src/genlab_bestilling/models.py +++ b/src/genlab_bestilling/models.py @@ -96,6 +96,9 @@ def get_absolute_url(self): class Order(PolymorphicModel): + class CannotConfirm(Exception): + pass + class OrderStatus(models.TextChoices): DRAFT = "draft", _("Draft") CONFIRMED = "confirmed", _("Confirmed") @@ -113,6 +116,10 @@ class OrderStatus(models.TextChoices): tags = TaggableManager(blank=True) + def confirm_order(self): + self.status = Order.OrderStatus.CONFIRMED + self.save() + class EquipmentType(models.Model): name = models.CharField(max_length=255, null=True, blank=True) @@ -141,6 +148,11 @@ def get_absolute_url(self): kwargs={"pk": self.pk, "project_id": self.project_id}, ) + def confirm_order(self): + if not EquimentOrderQuantity.objects.filter(order=self).exists(): + raise Order.CannotConfirm(_("No equipments found")) + return super().confirm_order() + class AnalysisOrder(Order): has_guid = models.BooleanField() # TODO: default? @@ -154,6 +166,11 @@ def get_absolute_url(self): kwargs={"pk": self.pk, "project_id": self.project_id}, ) + def confirm_order(self): + if not self.samples.all().exists(): + raise Order.CannotConfirm(_("No samples found")) + return super().confirm_order() + class Sample(models.Model): order = models.ForeignKey( diff --git a/src/genlab_bestilling/views.py b/src/genlab_bestilling/views.py index 5a425e9..231aaf1 100644 --- a/src/genlab_bestilling/views.py +++ b/src/genlab_bestilling/views.py @@ -1,10 +1,12 @@ from typing import Any +from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models.base import Model as Model from django.db.models.query import QuerySet from django.http import HttpResponse from django.urls import reverse +from django.utils.translation import gettext as _ from django.views.generic import CreateView, DetailView, FormView, UpdateView from django.views.generic.detail import SingleObjectMixin from django_tables2.views import SingleTableView @@ -164,9 +166,16 @@ def post(self, request, *args, **kwargs): return super().post(request, *args, **kwargs) def form_valid(self, form: Any) -> HttpResponse: - # TODO: check state transition - self.object.status = Order.OrderStatus.CONFIRMED - self.object.save() + i = self.object.get_real_instance() + try: + # TODO: check state transition + i.confirm_order() + messages.add_message( + self.request, messages.SUCCESS, _("Your order is confirmed") + ) + except Order.CannotConfirm as e: + messages.add_message(self.request, messages.ERROR, str(e)) + return super().form_valid(form) def get_success_url(self) -> str: diff --git a/src/theme/static_src/src/styles.css b/src/theme/static_src/src/styles.css index 5fad4c5..32c707f 100644 --- a/src/theme/static_src/src/styles.css +++ b/src/theme/static_src/src/styles.css @@ -16,7 +16,7 @@ @apply border-blue-500 bg-blue-300; } - .alert.alert-danger { + .alert.alert-error { @apply border-red-500 bg-red-300; }