75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
"""Create disposable Bundle 0 browser fixtures in the isolated Django test database.
|
|
|
|
Run only with --settings=config.settings.test and BUNDLE0_TEST_PASSWORD set.
|
|
Never point TEST_DB_NAME at a shared or production database.
|
|
"""
|
|
|
|
import os
|
|
from datetime import timedelta
|
|
|
|
from django.conf import settings
|
|
from django.utils import timezone
|
|
|
|
from apps.blog.models import Post
|
|
from apps.events.models import Event
|
|
from apps.users.models import User
|
|
|
|
|
|
assert os.environ.get("DJANGO_SETTINGS_MODULE") == "config.settings.test"
|
|
assert settings.DATABASES["default"]["ENGINE"] == "django.db.backends.sqlite3"
|
|
password = os.environ["BUNDLE0_TEST_PASSWORD"]
|
|
now = timezone.now()
|
|
|
|
|
|
def fixture_user(username, mobile, *, staff=False):
|
|
user, _ = User.objects.get_or_create(
|
|
username=username,
|
|
defaults={"mobile": mobile, "email": f"{username}@example.invalid"},
|
|
)
|
|
user.mobile = mobile
|
|
user.email = f"{username}@example.invalid"
|
|
user.is_email_verified = True
|
|
user.is_mobile_verified = True
|
|
user.is_staff = staff
|
|
user.is_superuser = staff
|
|
user.set_password(password)
|
|
user.save()
|
|
return user
|
|
|
|
|
|
student = fixture_user("bundle0_student", "09198000111")
|
|
staff = fixture_user("bundle0_staff", "09198000112", staff=True)
|
|
|
|
for slug, title, price, capacity in (
|
|
("bundle0-free", "Bundle 0 Free Event", 0, 20),
|
|
("bundle0-full", "Bundle 0 Full Event", 0, 0),
|
|
("bundle0-paid", "Bundle 0 Paid Event", 50000, 20),
|
|
):
|
|
Event.objects.update_or_create(
|
|
slug=slug,
|
|
defaults={
|
|
"title": title,
|
|
"description": "Local baseline event for browser review.",
|
|
"start_time": now + timedelta(days=7),
|
|
"end_time": now + timedelta(days=7, hours=2),
|
|
"registration_start_date": now - timedelta(days=1),
|
|
"registration_end_date": now + timedelta(days=6),
|
|
"status": Event.StatusChoices.PUBLISHED,
|
|
"price": price,
|
|
"capacity": capacity,
|
|
},
|
|
)
|
|
|
|
Post.objects.update_or_create(
|
|
slug="bundle0-article",
|
|
defaults={
|
|
"title": "Bundle 0 Article",
|
|
"content": "# Local baseline article\n\nA sample article for the browser review.",
|
|
"author": staff,
|
|
"status": Post.StatusChoices.PUBLISHED,
|
|
"published_at": now,
|
|
},
|
|
)
|
|
|
|
print("Created isolated student, staff, free/full/paid events, and article fixtures.")
|