Skip to content

Commit

Permalink
prevent confirm empty order
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jun 21, 2024
1 parent 8436f4d commit 9eba3c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/genlab_bestilling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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?
Expand All @@ -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(
Expand Down
15 changes: 12 additions & 3 deletions src/genlab_bestilling/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/theme/static_src/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@apply border-blue-500 bg-blue-300;
}

.alert.alert-danger {
.alert.alert-error {
@apply border-red-500 bg-red-300;
}

Expand Down

0 comments on commit 9eba3c4

Please sign in to comment.