feat(라이브커머스): 달력 보기와 호스트 비용 계산 추가
2시간 칸 × 날짜 달력(카드 머리 = Tiktok Handle, SALES·COM·Fee), 끌어 옮기기· 맞바꾸기, Jump to Date, 날짜별 합계·광고비·ROAS 를 넣는다. 호스트 비용 = 시간 × RM 25 + 판매금액 10%, 표 보기에도 표시한다. 끝 시간을 필수로 바꾸고 광고비 표는 마이그레이션 002 로 추가한다. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+97
-6
@@ -82,13 +82,13 @@ class StoreTests(unittest.TestCase):
|
||||
store.parse_money(bad)
|
||||
|
||||
def test_clean_broadcast(self):
|
||||
got = store.clean_broadcast({"host_id": "3", "broadcast_date": "2026-11-04", "start_time": "20:00", "end_time": "",
|
||||
got = store.clean_broadcast({"host_id": "3", "broadcast_date": "2026-11-04", "start_time": "20:00", "end_time": "22:00",
|
||||
"venue": " Studio ", "sales_amount": "100"})
|
||||
self.assertEqual(got, {"host_id": 3, "broadcast_date": date(2026, 11, 4), "start_time": time(20, 0), "end_time": None,
|
||||
self.assertEqual(got, {"host_id": 3, "broadcast_date": date(2026, 11, 4), "start_time": time(20, 0), "end_time": time(22, 0),
|
||||
"venue": "Studio", "sales_amount": Decimal("100.00")})
|
||||
good = {"host_id": "3", "broadcast_date": "2026-11-04", "start_time": "20:00"}
|
||||
good = {"host_id": "3", "broadcast_date": "2026-11-04", "start_time": "20:00", "end_time": "22:00"}
|
||||
for over in ({"host_id": ""}, {"host_id": "x"}, {"broadcast_date": ""}, {"broadcast_date": "2026-13-01"}, {"start_time": ""},
|
||||
{"start_time": "25:00"}, {"end_time": "nope"}, {"venue": "x" * 201}, {"sales_amount": "-5"}):
|
||||
{"start_time": "25:00"}, {"end_time": ""}, {"end_time": "nope"}, {"end_time": "20:00"}, {"venue": "x" * 201}, {"sales_amount": "-5"}):
|
||||
with self.assertRaises(ValueError, msg=over):
|
||||
store.clean_broadcast({**good, **over})
|
||||
|
||||
@@ -109,6 +109,57 @@ class StoreTests(unittest.TestCase):
|
||||
self.assertEqual(store.fmt_money(Decimal("1234.5")), "1,234.50")
|
||||
self.assertEqual(store.fmt_money(None), "")
|
||||
|
||||
def test_session_pay(self):
|
||||
"""호스트 비용 = 시간 × RM 25 + 판매금액 10%. 받은 화면의 예: 2시간·판매 48 → 50 + 4.8."""
|
||||
p = store.session_pay(time(12, 0), time(14, 0), Decimal("48"))
|
||||
self.assertEqual((p["base"], p["commission"], p["fee"]), (Decimal("50.00"), Decimal("4.80"), Decimal("54.80")))
|
||||
self.assertEqual(store.session_pay(time(22, 0), time(0, 0), None)["fee"], Decimal("50.00")) # 자정까지
|
||||
self.assertEqual(store.session_pay(time(23, 0), time(1, 30), None)["base"], Decimal("62.50")) # 자정 넘김·30분
|
||||
self.assertEqual(store.session_pay(time(12, 0), None, Decimal("10"))["fee"], Decimal("1.00")) # 끝 시간 없음 = 기본 요금 없음
|
||||
self.assertEqual(store.session_pay(time(12, 0), time(14, 0), Decimal("0.05"))["commission"], Decimal("0.01")) # 반올림
|
||||
self.assertEqual((store.fmt_short(Decimal("48")), store.fmt_short(Decimal("4.80")), store.fmt_short(Decimal("1234.5"))), ("48", "4.80", "1,234.50"))
|
||||
|
||||
def test_moved_end(self):
|
||||
self.assertEqual(store.moved_end(time(12, 0), time(14, 30), time(20, 0)), time(22, 30))
|
||||
self.assertEqual(store.moved_end(time(12, 0), time(14, 0), time(22, 0)), time(0, 0)) # 자정으로 넘어감
|
||||
self.assertIsNone(store.moved_end(time(12, 0), None, time(20, 0)))
|
||||
self.assertEqual(store.parse_move("2026-11-04", "20:00"), (date(2026, 11, 4), time(20, 0)))
|
||||
for bad in (("", "20:00"), ("2026-11-04", ""), ("nope", "20:00")):
|
||||
with self.assertRaises(ValueError):
|
||||
store.parse_move(*bad)
|
||||
|
||||
def test_build_calendar(self):
|
||||
def b(id, day, start, end, sales=None, host=1, handle="yaya"):
|
||||
return {"id": id, "host_id": host, "host_name": "Yaya", "tiktok_handle": handle, "broadcast_date": day, "start_time": start,
|
||||
"end_time": end, "venue": "", "sales_amount": sales, "images": []}
|
||||
span = store.month_range("2026-11", date(2026, 11, 4))
|
||||
cal = store.build_calendar(span, [b(1, date(2026, 11, 4), time(12, 0), time(14, 0), Decimal("48")),
|
||||
b(2, date(2026, 11, 4), time(22, 0), time(0, 0), handle=""),
|
||||
b(3, date(2026, 11, 7), time(13, 30), time(15, 0))],
|
||||
{date(2026, 11, 4): Decimal("24")}, date(2026, 11, 4))
|
||||
self.assertEqual(len(cal["days"]), 30)
|
||||
self.assertEqual([s["label"] for s in cal["slots"]][0], "06:00 - 08:00")
|
||||
self.assertEqual([s["label"] for s in cal["slots"]][-1], "22:00 - 00:00")
|
||||
self.assertEqual(len(cal["slots"]), 9)
|
||||
self.assertEqual(cal["slot_note"], "2 Hours (RM50 Base)")
|
||||
d4 = cal["days"][3]
|
||||
self.assertEqual((d4["weekday"], d4["label"], d4["count"], d4["is_today"], d4["is_weekend"]), ("WEDNESDAY", "4 NOV", 2, True, False))
|
||||
self.assertEqual((d4["sales_text"], d4["spend_text"], d4["fee_text"], d4["roas"]), ("48", "24", "104.80", "2.00"))
|
||||
self.assertEqual(cal["days"][6]["is_weekend"], True) # 11-07 토
|
||||
self.assertEqual(cal["days"][0]["roas"], "-") # 광고비 없음
|
||||
noon = next(s for s in cal["slots"] if s["start"] == "12:00")
|
||||
self.assertEqual(noon["cells"][3]["cards"][0]["label"], "yaya") # 카드 머리 = Tiktok Handle
|
||||
self.assertEqual((noon["cells"][3]["cards"][0]["card_sales"], noon["cells"][3]["cards"][0]["card_commission"], noon["cells"][3]["cards"][0]["card_fee"]),
|
||||
("48", "4.80", "54.80"))
|
||||
late = next(s for s in cal["slots"] if s["start"] == "22:00")
|
||||
self.assertEqual((late["end"], late["cells"][3]["cards"][0]["label"]), ("00:00", "Yaya")) # 핸들 없으면 이름
|
||||
self.assertEqual(next(s for s in cal["slots"] if s["start"] == "12:00")["cells"][6]["cards"][0]["id"], 3) # 13:30 = 12~14 칸
|
||||
self.assertEqual(cal["totals"]["sales"], "48")
|
||||
# 06시보다 이른 칸은 방송이 있을 때만
|
||||
early = store.build_calendar(span, [b(9, date(2026, 11, 1), time(3, 0), time(5, 0))], {}, date(2026, 11, 4))
|
||||
self.assertEqual(early["slots"][0]["label"], "02:00 - 04:00")
|
||||
self.assertEqual(len(early["slots"]), 10)
|
||||
|
||||
def test_thumbnail(self):
|
||||
for mode, fmt in (("RGB", "JPEG"), ("RGBA", "PNG")):
|
||||
raw = io.BytesIO()
|
||||
@@ -194,6 +245,7 @@ def body(c):
|
||||
assert r.json()["ok"], r.text
|
||||
page = c.get("/live/?month=2031-03").text
|
||||
assert "2031.03.05(수)" in page and "20:00 ~ 22:30" in page and "KL Studio" in page and "1,234.50" in page
|
||||
assert ">123.45</td>" in page and ">185.95</td>" in page # 커미션 10%·호스트 비용(2.5시간 × 25 + 123.45)
|
||||
assert "KL Studio" not in c.get("/live/?month=2031-04").text
|
||||
row = st.list_broadcasts(__import__("datetime").date(2031, 3, 1), __import__("datetime").date(2031, 4, 1))
|
||||
row = next(b for b in row if b["host_id"] == hid)
|
||||
@@ -213,10 +265,49 @@ def body(c):
|
||||
who["u"] = staff
|
||||
|
||||
# ── 고치기: 값 바뀜, 이미지 유지 + 더하기, 판매금액 지우기, 10장 제한 ──
|
||||
r = c.post("/live/broadcasts", data={**show, "id": bid, "venue": "PJ Studio", "sales_amount": "", "end_time": ""})
|
||||
r = c.post("/live/broadcasts", data={**show, "id": bid, "venue": "PJ Studio", "sales_amount": "", "end_time": "21:00"})
|
||||
assert r.json()["ok"], r.text
|
||||
page = c.get("/live/?month=2031-03").text
|
||||
assert "PJ Studio" in page and "1,234.50" not in page and "20:00 ~" not in page
|
||||
assert "PJ Studio" in page and "1,234.50" not in page and "20:00 ~ 21:00" in page
|
||||
assert c.post("/live/broadcasts", data={**show, "end_time": ""}).status_code == 400 # 끝 시간 필수
|
||||
|
||||
# ── 달력: 카드 머리 = Tiktok Handle, Fee, 옮기기(길이 유지)·맞바꾸기, 광고비·ROAS, 권한 ──
|
||||
c.post("/live/broadcasts", data={**show, "id": bid, "sales_amount": "48", "start_time": "12:00", "end_time": "14:00", "broadcast_date": "2031-03-05"})
|
||||
cal = c.get("/live/calendar?month=2031-03").text
|
||||
assert "Jump to Date" in cal and "SATURDAY" in cal and "12:00 - 14:00" in cal and "2 Hours (RM50 Base)" in cal
|
||||
assert '<span class="lv-card-name">siti.%s</span>' % tag in cal and "RM54.80" in cal and ">4.80</b>" in cal
|
||||
assert ACCOUNT not in cal and "Maybank" not in cal
|
||||
day = __import__("datetime").date
|
||||
def one(i):
|
||||
return next(b for b in st.list_broadcasts(day(2031, 3, 1), day(2031, 4, 1)) if b["id"] == i)
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"broadcast_date": "2031-03-07", "start_time": "22:00"}).json()["ok"]
|
||||
got = one(bid)
|
||||
assert (got["broadcast_date"], str(got["start_time"]), str(got["end_time"])) == (day(2031, 3, 7), "22:00:00", "00:00:00")
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"broadcast_date": "bad", "start_time": "22:00"}).status_code == 400
|
||||
assert c.post("/live/broadcasts/99999999/move", data={"broadcast_date": "2031-03-07", "start_time": "22:00"}).status_code == 400
|
||||
c.post("/live/broadcasts", data={**show, "broadcast_date": "2031-03-09", "start_time": "08:00", "end_time": "09:00", "venue": "Second"})
|
||||
other = next(b["id"] for b in st.list_broadcasts(day(2031, 3, 9), day(2031, 3, 10)) if b["venue"] == "Second")
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"swap_with": other}).json()["ok"]
|
||||
a, b2 = one(bid), one(other)
|
||||
assert (a["broadcast_date"], str(a["start_time"]), str(a["end_time"])) == (day(2031, 3, 9), "08:00:00", "09:00:00")
|
||||
assert (b2["broadcast_date"], str(b2["start_time"]), str(b2["end_time"])) == (day(2031, 3, 7), "22:00:00", "00:00:00")
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"swap_with": 99999999}).status_code == 400
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"swap_with": bid}).json()["ok"]
|
||||
assert c.post("/live/ad-spend", data={"day": "2031-03-09", "amount": "24"}).json()["ok"]
|
||||
assert c.post("/live/ad-spend", data={"day": "2031-03-09", "amount": "12"}).json()["ok"] # 덮어쓰기
|
||||
assert st.ad_spends(day(2031, 3, 1), day(2031, 4, 1)) == {day(2031, 3, 9): __import__("decimal").Decimal("12.00")}
|
||||
assert c.post("/live/ad-spend", data={"day": "2031-03-09", "amount": "-1"}).status_code == 400
|
||||
assert c.post("/live/ad-spend", data={"day": "bad", "amount": "1"}).status_code == 400
|
||||
assert "4.00" in c.get("/live/calendar?month=2031-03").text # ROAS 48 ÷ 12
|
||||
assert c.post("/live/ad-spend", data={"day": "2031-03-09", "amount": ""}).json()["ok"] # 빈칸 = 지우기
|
||||
assert st.ad_spends(day(2031, 3, 1), day(2031, 4, 1)) == {}
|
||||
assert c.post("/live/broadcasts/%d/delete" % other).json()["ok"]
|
||||
c.post("/live/broadcasts", data={**show, "id": bid}) # 아래 시험을 위해 원래 값으로
|
||||
who["u"] = outsider
|
||||
assert c.get("/live/calendar").status_code == 403
|
||||
assert c.post("/live/broadcasts/%d/move" % bid, data={"swap_with": bid}).status_code == 403
|
||||
assert c.post("/live/ad-spend", data={"day": "2031-03-09", "amount": "1"}).status_code == 403
|
||||
who["u"] = staff
|
||||
many = [("images", ("%d.jpg" % i, jpeg(), "image/jpeg")) for i in range(10)]
|
||||
r = c.post("/live/broadcasts", data={**show, "id": bid}, files=many)
|
||||
assert r.status_code == 400 and "10장" in r.json()["message"], r.text # 이미 1장 + 10장
|
||||
|
||||
Reference in New Issue
Block a user