diff --git a/src/shop/migrations/0005_shoppurchaselog_purchased_price.py b/src/shop/migrations/0005_shoppurchaselog_purchased_price.py new file mode 100644 index 0000000..6968b7c --- /dev/null +++ b/src/shop/migrations/0005_shoppurchaselog_purchased_price.py @@ -0,0 +1,36 @@ +# Generated by Django 3.2.12 on 2024-03-03 17:33 +# Edited manually by f2koi@plus on the same date + +from django.apps.registry import Apps +from django.db import migrations, models +from django.db.backends.base.schema import BaseDatabaseSchemaEditor + +import shop.models as shop_models + + +def forwards_func(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): + ShopPurchaseLog: shop_models.ShopPurchaseLog = apps.get_model( + "shop", "ShopPurchaseLog" + ) + for log in ShopPurchaseLog.objects.all(): + log.purchased_price = log.item.price + log.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("shop", "0004_shopitem_author"), + ] + + operations = [ + migrations.AddField( + model_name="shoppurchaselog", + name="purchased_price", + field=models.PositiveIntegerField(default=0), + ), + # https://docs.djangoproject.com/en/3.2/ref/migration-operations/#runpython + migrations.RunPython( + code=forwards_func, reverse_code=migrations.RunPython.noop + ), + ] diff --git a/src/shop/models.py b/src/shop/models.py index 04a212a..846521c 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -1,13 +1,12 @@ import os -from django.db import models from django.conf import settings -from django.core.files.storage import FileSystemStorage from django.contrib.auth import get_user_model +from django.core.files.storage import FileSystemStorage +from django.db import models from problem.models import ProblemList - User = get_user_model() @@ -53,6 +52,7 @@ class ShopPurchaseLog(models.Model): succeed = models.BooleanField() retrieved = models.BooleanField() purchase_time = models.DateTimeField(auto_now_add=True) + purchased_price = models.PositiveIntegerField() class Meta: verbose_name = "상점 구매 로그" diff --git a/src/shop/views.py b/src/shop/views.py index 4d67ba1..116bca1 100644 --- a/src/shop/views.py +++ b/src/shop/views.py @@ -1,16 +1,17 @@ from decimal import Decimal -from random import SystemRandom from functools import reduce +from random import SystemRandom from django import forms -from django.db import IntegrityError from django.core.exceptions import ObjectDoesNotExist +from django.db import IntegrityError from django.http import Http404, HttpResponseBadRequest, JsonResponse from django.shortcuts import render from django.views import View -from website.views import PlusMemberCheck from problem.helpers.problem_info import get_problem_list_user_info +from website.views import PlusMemberCheck + from .models import Shop, ShopItem, ShopPurchaseLog @@ -36,7 +37,10 @@ def get(self, request, pk=None): shop_items = shop.shop_items.filter(hidden=False) _, user_money = get_problem_list_user_info(shop.problem_list, request.user) purchase_log = list( - map(lambda x: x.item.price, ShopPurchaseLog.objects.filter(user=request.user, shop=shop)) + map( + lambda x: x.purchased_price, + ShopPurchaseLog.objects.filter(user=request.user, shop=shop), + ) ) if purchase_log: user_money -= reduce(lambda x, y: x + y, purchase_log) @@ -75,11 +79,14 @@ def post(self, request, pk): required_luck = Decimal(100) - item_to_buy.chance _, user_money = get_problem_list_user_info(shop_point_source, request.user) - purchase_log = list( - map(lambda x: x.item.price, ShopPurchaseLog.objects.filter(user=request.user, shop=shop_to_visit)) + purchased_prices = list( + map( + lambda x: x.purchased_price, + ShopPurchaseLog.objects.filter(user=request.user, shop=shop_to_visit), + ) ) - if purchase_log: - user_money -= reduce(lambda x, y: x + y, purchase_log) + if purchased_prices: + user_money -= reduce(lambda x, y: x + y, purchased_prices) enough_point = user_money >= required_point enough_luck = SystemRandom().uniform(0, 100) > required_luck @@ -95,6 +102,7 @@ def post(self, request, pk): item=item_to_buy, succeed=succeed_purchase, retrieved=False, + purchased_price=required_point, ) response["result"] = succeed_purchase