mirror of
https://github.com/ae-utbm/sith.git
synced 2025-11-22 12:46:58 +00:00
Compare commits
4 Commits
fix/produc
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a4d21611e | ||
|
|
992b6d6b79 | ||
|
|
710b4aa942 | ||
|
|
5fee2e4720 |
@@ -235,19 +235,6 @@ class ScheduledProductActionForm(forms.ModelForm):
|
|||||||
)
|
)
|
||||||
return super().clean()
|
return super().clean()
|
||||||
|
|
||||||
def set_product(self, product: Product):
|
|
||||||
"""Set the product to which this form's instance is linked.
|
|
||||||
|
|
||||||
When this form is linked to a ProductForm in the case of a product's creation,
|
|
||||||
the product doesn't exist yet, so saving this form as is will result
|
|
||||||
in having `{"product_id": null}` in the action kwargs.
|
|
||||||
For the creation to be useful, it may be needed to inject the newly created
|
|
||||||
product into this form, before saving the latter.
|
|
||||||
"""
|
|
||||||
self.product = product
|
|
||||||
kwargs = json.loads(self.instance.kwargs) | {"product_id": self.product.id}
|
|
||||||
self.instance.kwargs = json.dumps(kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class BaseScheduledProductActionFormSet(BaseModelFormSet):
|
class BaseScheduledProductActionFormSet(BaseModelFormSet):
|
||||||
def __init__(self, *args, product: Product, **kwargs):
|
def __init__(self, *args, product: Product, **kwargs):
|
||||||
@@ -334,19 +321,11 @@ class ProductForm(forms.ModelForm):
|
|||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
return super().is_valid() and self.action_formset.is_valid()
|
return super().is_valid() and self.action_formset.is_valid()
|
||||||
|
|
||||||
def save(self, *args, **kwargs) -> Product:
|
def save(self, *args, **kwargs):
|
||||||
product = super().save(*args, **kwargs)
|
ret = super().save(*args, **kwargs)
|
||||||
product.counters.set(self.cleaned_data["counters"])
|
self.instance.counters.set(self.cleaned_data["counters"])
|
||||||
for form in self.action_formset:
|
|
||||||
# if it's a creation, the product given in the formset
|
|
||||||
# wasn't a persisted instance.
|
|
||||||
# So if we tried to persist the scheduled actions in the current state,
|
|
||||||
# they would be linked to no product, thus be completely useless
|
|
||||||
# To make it work, we have to replace
|
|
||||||
# the initial product with a persisted one
|
|
||||||
form.set_product(product)
|
|
||||||
self.action_formset.save()
|
self.action_formset.save()
|
||||||
return product
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class ReturnableProductForm(forms.ModelForm):
|
class ReturnableProductForm(forms.ModelForm):
|
||||||
@@ -390,6 +369,7 @@ class EticketForm(forms.ModelForm):
|
|||||||
class CloseCustomerAccountForm(forms.Form):
|
class CloseCustomerAccountForm(forms.Form):
|
||||||
user = forms.ModelChoiceField(
|
user = forms.ModelChoiceField(
|
||||||
label=_("Refound this account"),
|
label=_("Refound this account"),
|
||||||
|
help_text=None,
|
||||||
required=True,
|
required=True,
|
||||||
widget=AutoCompleteSelectUser,
|
widget=AutoCompleteSelectUser,
|
||||||
queryset=User.objects.all(),
|
queryset=User.objects.all(),
|
||||||
|
|||||||
@@ -11,12 +11,8 @@ from model_bakery import baker
|
|||||||
|
|
||||||
from core.models import Group, User
|
from core.models import Group, User
|
||||||
from counter.baker_recipes import counter_recipe, product_recipe
|
from counter.baker_recipes import counter_recipe, product_recipe
|
||||||
from counter.forms import (
|
from counter.forms import ScheduledProductActionForm, ScheduledProductActionFormSet
|
||||||
ProductForm,
|
from counter.models import ScheduledProductAction
|
||||||
ScheduledProductActionForm,
|
|
||||||
ScheduledProductActionFormSet,
|
|
||||||
)
|
|
||||||
from counter.models import Product, ScheduledProductAction
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@@ -38,39 +34,6 @@ def test_edit_product(client: Client):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_create_actions_alongside_product():
|
|
||||||
"""The form should work when the product and the actions are created alongside."""
|
|
||||||
# non-persisted instance
|
|
||||||
product: Product = product_recipe.prepare(_save_related=True)
|
|
||||||
trigger_at = now() + timedelta(minutes=10)
|
|
||||||
form = ProductForm(
|
|
||||||
data={
|
|
||||||
"name": "foo",
|
|
||||||
"description": "bar",
|
|
||||||
"product_type": product.product_type_id,
|
|
||||||
"club": product.club_id,
|
|
||||||
"code": "FOO",
|
|
||||||
"purchase_price": 1.0,
|
|
||||||
"selling_price": 1.0,
|
|
||||||
"special_selling_price": 1.0,
|
|
||||||
"limit_age": 0,
|
|
||||||
"form-TOTAL_FORMS": "2",
|
|
||||||
"form-INITIAL_FORMS": "0",
|
|
||||||
"form-0-task": "counter.tasks.archive_product",
|
|
||||||
"form-0-trigger_at": trigger_at,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert form.is_valid()
|
|
||||||
product = form.save()
|
|
||||||
action = ScheduledProductAction.objects.last()
|
|
||||||
assert action.clocked.clocked_time == trigger_at
|
|
||||||
assert action.enabled is True
|
|
||||||
assert action.one_off is True
|
|
||||||
assert action.task == "counter.tasks.archive_product"
|
|
||||||
assert action.kwargs == json.dumps({"product_id": product.id})
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestProductActionForm:
|
class TestProductActionForm:
|
||||||
def test_single_form_archive(self):
|
def test_single_form_archive(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user