Skip to content

Commit

Permalink
add confirm order
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jun 20, 2024
1 parent 50e0264 commit 0912292
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/genlab_bestilling/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from collections.abc import Mapping
from typing import Any

from django import forms
from django.forms.renderers import BaseRenderer
from django.forms.utils import ErrorList
from formset.renderers.tailwind import FormRenderer
from formset.utils import FormMixin
from formset.widgets import DateInput, DualSortableSelector, Selectize
Expand Down Expand Up @@ -236,3 +241,36 @@ def retrieve_instance(self, data):
location_id=data.get("location"),
volume=data.get("volume"),
)


class ActionForm(forms.Form):
hidden = forms.CharField(required=False, widget=forms.widgets.HiddenInput())

def __init__(
self,
data: Mapping[str, Any] | None,
files: Mapping[str, Any] | None,
auto_id: bool | str | None = None,
prefix: str | None = None,
initial: Mapping[str, Any] | None = None,
error_class: type[ErrorList] = None,
label_suffix: str | None = None,
empty_permitted: bool = None,
field_order: list[str] | None = None,
use_required_attribute: bool | None = None,
renderer: type[BaseRenderer] | None = None,
**kwargs,
) -> None:
super().__init__(
data,
files,
auto_id,
prefix,
initial,
error_class,
label_suffix,
empty_permitted,
field_order,
use_required_attribute,
renderer,
)
2 changes: 1 addition & 1 deletion src/genlab_bestilling/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class OrderTable(tables.Table):

class Meta:
model = Order
fields = ("name", "species", "sample_types", "polymorphic_ctype")
fields = ("name", "status", "polymorphic_ctype", "species", "sample_types")

empty_text = "No Orders"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ <h5 class="text-2xl my-5">Samples</h5>


<div class="flex gap-5 my-5">
{% if object.status == 'draft' %}
<a class="btn bg-primary" href="{% url 'project-analysis-update' project_id=object.project_id pk=object.id %}">Edit</a>
<a class="btn bg-primary" href="{% url 'project-analysis-samples' project_id=object.project_id pk=object.id %}">Edit Samples</a>
<button class="btn bg-secondary">Confirm Order</button>
{% url 'project-order-confirm' project_id=object.project_id pk=object.id as confirm_order_url %}
{% action-button action=confirm_order_url class="bg-secondary" submit_text="Confirm Order" csrf_token=csrf_token %}
{% endif %}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ <h5 class="text-2xl my-5">Requested Equipment</h5>
{% /table %}

<div class="flex gap-5 my-5">
{% if object.status == 'draft' %}
<a class="btn bg-primary" href="{% url 'project-equipment-update' project_id=object.project_id pk=object.id %}">Edit</a>
<a class="btn bg-secondary" href="{% url 'project-equipment-quantity-update' project_id=object.project_id pk=object.id %}">Edit requested equipment</a>
<button class="btn bg-secondary">Confirm Order</button>
{% url 'project-order-confirm' project_id=object.project_id pk=object.id as confirm_order_url %}
{% action-button action=confirm_order_url class="bg-secondary" submit_text="Confirm Order" csrf_token=csrf_token %}
{% endif %}
</div>
{% endblock %}
7 changes: 7 additions & 0 deletions src/genlab_bestilling/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
AnalysisOrderCreateView,
AnalysisOrderDetailView,
AnalysisOrderEditView,
ConfirmOrderActionView,
EquipmentOrderCreateView,
EquipmentOrderDetailView,
EquipmentOrderEditView,
Expand Down Expand Up @@ -83,4 +84,10 @@
SamplesUpdateView.as_view(),
name="project-analysis-samples",
),
# Actions
path(
"projects/<int:project_id>/orders/<int:pk>/confirm/",
ConfirmOrderActionView.as_view(),
name="project-order-confirm",
),
]
37 changes: 34 additions & 3 deletions src/genlab_bestilling/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
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.views.generic import CreateView, DetailView, UpdateView
from django.views.generic import CreateView, DetailView, FormView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from django_tables2.views import SingleTableView
from formset.views import (
BulkEditCollectionView,
Expand All @@ -14,6 +16,7 @@
from neapolitan.views import CRUDView

from .forms import (
ActionForm,
AnalysisOrderForm,
EquipmentOrderForm,
EquipmentQuantityCollection,
Expand All @@ -31,6 +34,16 @@
from .tables import OrderTable, ProjectTable


class ActionView(FormView):
form_class = ActionForm

def get(self, request, *args, **kwargs):
"""
Action forms should be used just to modify the system
"""
self.http_method_not_allowed(self, request, *args, **kwargs)


class FormsetCreateView(
IncompleteSelectResponseMixin,
FormViewMixin,
Expand Down Expand Up @@ -130,6 +143,24 @@ class AnalysisOrderDetailView(ProjectNestedMixin, DetailView):
model = AnalysisOrder


class ConfirmOrderActionView(ProjectNestedMixin, SingleObjectMixin, ActionView):
model = Order

def post(self, request, *args, **kwargs):
self.project = self.get_project()
self.object = self.get_object()
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()
return super().form_valid(form)

def get_success_url(self) -> str:
return self.object.get_absolute_url()


class EquipmentOrderEditView(
ProjectNestedMixin,
FormsetUpdateView,
Expand Down Expand Up @@ -202,7 +233,7 @@ def get_queryset(self) -> QuerySet[Any]:
return (
super()
.get_queryset()
.filter(order_id=self.kwargs["pk"], status=Order.OrderStatus.DRAFT)
.filter(order_id=self.kwargs["pk"], order__status=Order.OrderStatus.DRAFT)
)

def get_collection_kwargs(self):
Expand Down Expand Up @@ -235,7 +266,7 @@ def get_queryset(self) -> QuerySet[Any]:
return (
super()
.get_queryset()
.filter(order_id=self.kwargs["pk"], status=Order.OrderStatus.DRAFT)
.filter(order_id=self.kwargs["pk"], order__status=Order.OrderStatus.DRAFT)
)

def get_collection_kwargs(self):
Expand Down
1 change: 1 addition & 0 deletions src/templates/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ components:
table-cell: "components/table-cell.html"
formset: "components/formset.html"
object-detail: "components/object-detail.html"
action-button: "components/action-button.html"
10 changes: 10 additions & 0 deletions src/templates/components/action-button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% load static %}

{% var submit_text=submit_text|default:"Submit" %}
{% var class=class|default:"bg-secondary text-white" %}

<form method="post" action="{{ action }}">
{% csrf_token %}
{% if form %}{{ form }}{% endif %}
<button class="btn {{ class }}" type="submit">{{ submit_text }}</button>
</form>

0 comments on commit 0912292

Please sign in to comment.