Skip to content

Instantly share code, notes, and snippets.

@gsidebo
Created April 24, 2019 16:03
Show Gist options
  • Save gsidebo/a3d836e3a4379f7e48ccfebf0a272fca to your computer and use it in GitHub Desktop.
Save gsidebo/a3d836e3a4379f7e48ccfebf0a272fca to your computer and use it in GitHub Desktop.
from types import SimpleNamespace
import random
from django.contrib.contenttypes.models import ContentType
from courses.models import Program, Course, CourseRun
from ecommerce.models import (
Product, ProductVersion,
Coupon, CouponVersion, CouponPayment, CouponPaymentVersion, CouponEligibility,
Basket, BasketItem,
Company
)
PROGRAM_PRICE_RANGE = (1000, 2000)
COURSE_PRICE_RANGE = (300, 500)
COURSE_RUN_PRICE_RANGE = (300, 500)
COUPON_CONFIGS = [
SimpleNamespace(name="50% off Coupon 1", tag="[50% off]", code="50-pct-off", amount=0.5,),
SimpleNamespace(name="25% off Coupon 1", tag="[25% off]", code="25-pct-off", amount=0.25,),
SimpleNamespace(name="10% off Coupon 1", tag="[10% off]", code="10-pct-off", amount=0.1,),
SimpleNamespace(name="100% off Coupon 1", tag="[100% off]", code="100-pct-off", amount=1,),
SimpleNamespace(name="100% off Coupon 2", tag="[100% off!]", code="100-pct-off-2", amount=1,),
]
COURSE_COUPON_MAP = {
"SEED edX Demonstration Course": "50-pct-off",
"SEED edX Demonstration Course": "25-pct-off",
"SEED edX Demonstration Course": "10-pct-off",
"SEED Digital Learning 100": "50-pct-off",
"SEED Digital Learning 300": "100-pct-off",
"SEED Digital Learning 300": "100-pct-off-2",
}
PROGRAM_COUPON_MAP = {
"SEED Digital Learning": "50-pct-off",
"SEED Digital Learning": "25-pct-off",
"SEED Digital Learning": "10-pct-off",
"SEED Digital Learning": "100-pct-off-2",
"SEED Systems Engineering": "50-pct-off",
"SEED Systems Engineering": "100-pct-off",
"SEED Analog Learning": "100-pct-off",
}
COURSE_RUN_COUPON_MAP = {
"SEED Systems Engineering - Fall 2020": "100-pct-off",
"SEED Digital Learning 100 - August 2021": "100-pct-off",
"SEED Digital Learning 100 - August 2021": "100-pct-off-2",
}
def model_object_counts(*model_classes):
return {
model_cls.__name__: model_cls.objects.count()
for model_cls in model_classes
}
def ensure_product_and_version(courseware_obj, price):
ct = ContentType.objects.get_for_model(courseware_obj.__class__)
product, _ = Product.objects.get_or_create(
content_type=ct,
object_id=courseware_obj.id
)
product_version, _ = ProductVersion.objects.get_or_create(
product=product,
defaults=dict(
price=price,
description="{} for sale".format(courseware_obj.__class__.__name__)
)
)
return product, product_version
def ensure_products_and_versions():
for courserun in CourseRun.objects.all():
ensure_product_and_version(courserun, random.randint(*COURSE_RUN_PRICE_RANGE))
for course in Course.objects.all():
ensure_product_and_version(course, random.randint(*COURSE_PRICE_RANGE))
for program in Program.objects.all():
ensure_product_and_version(program, random.randint(*PROGRAM_PRICE_RANGE))
return model_object_counts(Product, ProductVersion)
def ensure_coupons():
for coupon_config in COUPON_CONFIGS:
coupon_payment, _ = CouponPayment.objects.get_or_create(name=coupon_config.name)
coupon_payment_version, _ = CouponPaymentVersion.objects.get_or_create(
payment=coupon_payment,
tag=coupon_config.tag,
defaults=dict(
coupon_type="credit_card",
num_coupon_codes=1,
max_redemptions=1,
max_redemptions_per_user=1,
amount=coupon_config.amount
)
)
coupon, _ = Coupon.objects.get_or_create(
payment=coupon_payment,
coupon_code=coupon_config.code,
defaults=dict(
enabled=True
)
)
CouponVersion.objects.get_or_create(
coupon=coupon,
payment_version=coupon_payment_version
)
return model_object_counts(CouponPayment, CouponPaymentVersion, Coupon, CouponVersion)
def ensure_coupon_eligibility(model_cls, title, coupon_code):
content_type = ContentType.objects.get_for_model(model=model_cls)
obj = model_cls.objects.get(title=title)
CouponEligibility.objects.get_or_create(
coupon=Coupon.objects.get(coupon_code=coupon_code),
product=Product.objects.get(
object_id=obj.id,
content_type=content_type
)
)
def ensure_coupon_eligibilities():
for title, coupon_code in COURSE_COUPON_MAP.items():
ensure_coupon_eligibility(Course, title, coupon_code)
for title, coupon_code in PROGRAM_COUPON_MAP.items():
ensure_coupon_eligibility(Program, title, coupon_code)
for title, coupon_code in COURSE_RUN_COUPON_MAP.items():
ensure_coupon_eligibility(CourseRun, title, coupon_code)
return model_object_counts(CouponEligibility)
def seed_ecommerce():
return [
ensure_products_and_versions(),
ensure_coupons(),
ensure_coupon_eligibilities(),
]
seed_ecommerce()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment