import datetime
import random
from django import forms
from django.conf import settings
from django.contrib import messages
from django.utils.timezone import now
from django.views.generic import FormView
from shuup.testing.factories import (
create_random_company,
create_random_contact_group,
create_random_order,
create_random_person,
create_random_product_attribute,
)
from shuup.utils.django_compat import force_text
[docs]
class Mockers:
"""
Namespace object for mocker methods.
The docstrings for the callables are user-visible.
"""
[docs]
def mock_order(self, **kwargs):
"""Create a random order (randomly completed)."""
shop = kwargs.pop("shop")
try:
return create_random_order(completion_probability=0.6, shop=shop)
except Exception:
pass
[docs]
def mock_order_6h(self, **kwargs):
"""Create a random order for past 6h (20% chance for completion)."""
shop = kwargs.pop("shop")
try:
return create_random_order(completion_probability=0.2, shop=shop)
except Exception:
pass
[docs]
def mock_fully_paid_order(self, **kwargs):
"""Create a random order (complete and fully paid)."""
shop = kwargs.pop("shop")
try:
return create_random_order(completion_probability=1, shop=shop, create_payment_for_order_total=True)
except Exception:
pass
[docs]
def mock_fully_paid_order_6h(self, **kwargs):
"""Create a random order for past 6h (complete and fully paid)."""
shop = kwargs.pop("shop")
order_date = now() - datetime.timedelta(minutes=random.uniform(0, 360))
try:
return create_random_order(
completion_probability=1,
shop=shop,
create_payment_for_order_total=True,
order_date=order_date,
)
except Exception:
pass
[docs]
def mock_fully_paid_order_30d(self, **kwargs):
"""Create a random order for past 30 days (complete and fully paid)."""
shop = kwargs.pop("shop")
order_date = now() - datetime.timedelta(hours=random.uniform(0, 720))
try:
return create_random_order(
completion_probability=1,
shop=shop,
create_payment_for_order_total=True,
order_date=order_date,
)
except Exception:
pass
[docs]
def mock_person(self, **kwargs):
"""Create a random person."""
shop = kwargs.pop("shop")
return create_random_person(shop=shop)
[docs]
def mock_company(self, **kwargs):
"""Create a random company."""
shop = kwargs.pop("shop")
return create_random_company(shop=shop)
[docs]
def mock_customer_group(self, **kwargs):
"""Create a random contact group."""
return create_random_contact_group()
[docs]
def mock_product_attribute(self, **kwargs):
"""Create a random product attribute."""
return create_random_product_attribute()
[docs]
class MockerView(FormView):
form_class = MockerForm
template_name = "shuup_testing/mocker.jinja"
mockers = Mockers()
[docs]
def get_mockers(self):
return [
(
name,
force_text(getattr(self.mockers, name, None).__doc__ or name).strip(),
)
for name in dir(self.mockers)
if name.startswith("mock_")
]