115 lines
4.0 KiB
Python
115 lines
4.0 KiB
Python
"""
|
|
کارخانههای ساخت داده آزمایشی برای تستهای پروژه.
|
|
|
|
این توابع فقط روی دیتابیس تستی Django اجرا میشوند و هیچگاه به
|
|
`data9/kt.sqlite3` (دیتابیس تولیدی) دست نمیزنند.
|
|
"""
|
|
import itertools
|
|
|
|
from django.contrib.auth.models import Permission, User
|
|
|
|
from admin_panel.models import (Customer, PhoneNumber, Product, ProductCategory,
|
|
ProductInStore, StoreRoom, StoreRoomOwner)
|
|
|
|
_counter = itertools.count(1)
|
|
|
|
|
|
def _next(prefix):
|
|
return f"{prefix}{next(_counter)}"
|
|
|
|
|
|
def make_user(username=None, *, password='TestPass!2345', superuser=False,
|
|
first_name='', last_name='', perms=()):
|
|
"""
|
|
ساخت کاربر. با `superuser=True` تمام بررسیهای دسترسی عبور داده میشوند.
|
|
`perms` لیستی از کدنام پرمیشنهاست (مثلاً ['OnlyAnbar', 'ViewStore']).
|
|
"""
|
|
username = username or _next('user')
|
|
if superuser:
|
|
user = User.objects.create_superuser(username=username, email='', password=password)
|
|
else:
|
|
user = User.objects.create_user(username=username, password=password)
|
|
|
|
if first_name or last_name:
|
|
user.first_name = first_name
|
|
user.last_name = last_name
|
|
user.save(update_fields=['first_name', 'last_name'])
|
|
|
|
if perms:
|
|
user.user_permissions.add(*Permission.objects.filter(codename__in=list(perms)))
|
|
# رفرش کش پرمیشنها تا بلافاصله در دسترس باشند
|
|
user = User.objects.get(pk=user.pk)
|
|
|
|
return user
|
|
|
|
|
|
def login_superuser(client, username):
|
|
"""ساخت کاربر سوپریوزر و ورود آن به کلاینت تست؛ کاربر را برمیگرداند."""
|
|
user = make_user(username, superuser=True)
|
|
client.force_login(user)
|
|
return user
|
|
|
|
|
|
def make_store(name=None, *, viewers=(), owner=None):
|
|
"""
|
|
ساخت انبار.
|
|
- `viewers` به M2M `user` اضافه میشوند (دسترسی مشاهده/تایید انتقال).
|
|
- `owner` به M2M `owners` (از طریق StoreRoomOwner) اضافه میشود
|
|
(همان چیزی که `StoreRoom.objects.get(owners=user)` انتظار دارد).
|
|
"""
|
|
store = StoreRoom.objects.create(store_name=name or _next('انبار'))
|
|
|
|
for v in viewers:
|
|
store.user.add(v)
|
|
|
|
if owner is not None:
|
|
StoreRoomOwner.objects.create(storeroom=store, user=owner)
|
|
store.user.add(owner)
|
|
|
|
return store
|
|
|
|
|
|
def make_category(name=None):
|
|
return ProductCategory.objects.create(category_name=name or _next('دسته'))
|
|
|
|
|
|
def make_product(name=None, *, price=1000, category=None, year=1400):
|
|
return Product.objects.create(
|
|
product_name=name or _next('کالا'),
|
|
product_price=price,
|
|
ProductCategory=category or make_category(),
|
|
year=year,
|
|
)
|
|
|
|
|
|
def stock_in(store, product, quantity, *, year=1400):
|
|
"""ثبت ورود موجودی اولیه به انبار (از مسیر امن ProductInStore.save)."""
|
|
return ProductInStore.objects.create(
|
|
StoreRoom=store,
|
|
Product=product,
|
|
s_in=quantity,
|
|
s_out=0,
|
|
year=year,
|
|
desc=f"موجودی اولیه: {quantity}",
|
|
movement_type=ProductInStore.MovementType.INITIAL,
|
|
)
|
|
|
|
|
|
def current_stock(store, product, *, year=1400):
|
|
"""موجودی لحظهای کالا در انبار (s_in - s_out)."""
|
|
total = 0
|
|
for row in ProductInStore.objects.filter(StoreRoom=store, Product=product, year=year):
|
|
total += (row.s_in or 0) - (row.s_out or 0)
|
|
return total
|
|
|
|
|
|
def make_customer(name=None, *, address='آدرس تست', phones=(), is_active=True):
|
|
customer = Customer.objects.create(
|
|
name_family=name or _next('مشتری'),
|
|
address=address,
|
|
is_active=is_active,
|
|
)
|
|
for i, phone in enumerate(phones):
|
|
PhoneNumber.objects.create(customer=customer, title=f'تلفن {i + 1}', phone_number=phone)
|
|
return customer
|