Skip to content

Commit

Permalink
kahluaband#1 Feat: calculate price in general ticket (kahluaband#14)
Browse files Browse the repository at this point in the history
* kahluaband#2 feat: add portone api

* kahluaband#12 feat: add swagger

* kahluaband#2 feat: modify ticket model and add general ticket view

* kahluaband#1 feat: add ticket checkout and validation function

* kahluaband#1 feat: add requirements.txt 8/1

* kahluaband#4 feat: add applyform create view

* kahluaband#1 Fix: remove ordercompleteserializer in ticket.views

* kahluaband#12 Fix: replace to empty db

* kahluaband#1 Feat: calculate price in general_ticket

* kahluaband#1 Feat: make reservation_id in general ticket

* kahluaband#1 Feat: modify swagger in generalticketorderview
  • Loading branch information
kjiyun authored Aug 9, 2023
1 parent 74870f5 commit 83586b8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
18 changes: 18 additions & 0 deletions tickets/migrations/0003_generalticket_reservation_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0 on 2023-08-06 10:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tickets', '0002_ordertransaction'),
]

operations = [
migrations.AddField(
model_name='generalticket',
name='reservation_id',
field=models.CharField(max_length=10, null=True, unique=True),
),
]
1 change: 1 addition & 0 deletions tickets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GeneralTicket(TimeStampedModel): #일반 티켓
phone_num = models.CharField(max_length=20, unique=True) # 핸드폰 번호
member = models.PositiveIntegerField(default=1) # 예매 인원
price = models.PositiveIntegerField(default=0)
reservation_id = models.CharField(max_length=10, unique=True, null=True) # 주문번호(예약번호)

def __str__(self):
return 'Order {}/{}'.format(self.buyer, self.id)
Expand Down
18 changes: 17 additions & 1 deletion tickets/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import random
import string

from rest_framework import serializers

from .models import GeneralTicket, FreshmanTicket

class GeneralTicketDetailSerializer(serializers.ModelSerializer):
# price = serializers.SerializerMethodField()

class Meta:
model = GeneralTicket
Expand All @@ -15,9 +16,24 @@ class Meta:
'phone_num',
'member',
'price',
'reservation_id'
]

def create(self, validated_data):
member = validated_data.get('member', 1)
price = member * int(5000)
validated_data['price'] = price

# 예약번호 랜덤 생성 (최대 10번 시도)
for _ in range(10):
reservation_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
if not GeneralTicket.objects.filter(reservation_id=reservation_id).exists():
validated_data['reservation_id'] = reservation_id
break

if not reservation_id:
raise ValueError("유효한 예약번호를 생성하지 못했습니다.")

ticket = GeneralTicket.objects.create(**validated_data)

return ticket
Expand Down
2 changes: 1 addition & 1 deletion tickets/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import path
from .views import FreshmanTicketOrderView, GeneralTicketOrderView, OrderCheckoutView, OrderValidationView
from .views.views import FreshmanTicketOrderView, GeneralTicketOrderView, OrderCheckoutView, OrderValidationView

app_name = 'tickets'

Expand Down
50 changes: 32 additions & 18 deletions tickets/views.py → tickets/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

from .models import GeneralTicket, FreshmanTicket, Participant, OrderTransaction
from .serializers import GeneralTicketDetailSerializer, FreshmanTicketDetailSerializer
from ..models import GeneralTicket, FreshmanTicket, Participant, OrderTransaction
from ..serializers import GeneralTicketDetailSerializer, FreshmanTicketDetailSerializer

# import traceback

Expand Down Expand Up @@ -67,7 +67,7 @@ def create(self, request, *args, **kwargs):
return Response({
'status': 'success',
'data': serializer.data,
}, status=status.HTTP_201_CREATED)
}, status=status.HTTP_200_OK)

return Response({
'status':'error',
Expand Down Expand Up @@ -107,7 +107,13 @@ class Meta:
examples={
"application/json": {
"status": "success",
"data": {'id': 1}
"data": {'id': 1,
'buyer':'깔루아1',
'phone_num':'010-1234-5678',
'member':'3',
'price':'15000',
'reservation_id': 'ABCDE12345',
},
}
}
),
Expand All @@ -122,28 +128,30 @@ def create(self, request, *args, **kwargs):
phone_list = order_info.getlist('phone')

serializer = self.get_serializer(data=order_info)
if serializer.is_valid():
if serializer.is_valid(raise_exception=True):
new_order = serializer.save()

mem = dict(zip(name_list, phone_list))

for key, value in mem.items():
mem = Participant(name=key, phone_num=value, general_ticket=new_order)
mem.save()

return Response({
'status': 'success',
'data': new_order.id,
}, status=status.HTTP_201_CREATED)
'data': serializer.data,
}, status=status.HTTP_200_OK)

return Response({
'status':'error',
}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
operation_id='주문이 완료된 후 표시하는 View',
operation_id='주문이 완료된 후 표시 & 예매 티켓 조회',
operation_description='''
전달된 쿼리 파라미터에 해당하는 예매 정보를 반환합니다.<br/>
결제를 하고 나서 주문이 완료되었다는 화면을 표시할 때 사용됩니다.<br/>
또는 예매 티켓을 조회하는 경우 예매번호를 입력하여 예매 내역을 확인합니다.<br/>
주문 번호에 해당하는 결제 완료 화면을 보여줍니다.<br/>
''',
responses={
Expand All @@ -156,7 +164,8 @@ def create(self, request, *args, **kwargs):
'buyer':'깔루아1',
'phone_num':'010-1234-5678',
'member':'3',
'price':'15000'
'price':'15000',
'reservation_id': 'ABCDE12345',
}
}
}
Expand All @@ -167,14 +176,19 @@ def create(self, request, *args, **kwargs):
}
)
def get(self, request):
order_id = request.query_params.get('order_id')
order = GeneralTicket.objects.get(id=order_id)
serializer = self.get_serializer(order)

return Response({
'status': 'success',
'data': serializer.data
}, status=status.HTTP_200_OK)
try:
request_id = request.query_params.get('reservation_id')
order = GeneralTicket.objects.get(reservation_id=request_id)
serializer = self.get_serializer(order)

return Response({
'status': 'success',
'data': serializer.data
}, status=status.HTTP_200_OK)
except:
return Response({
'status': 'error',
}, status=status.HTTP_400_BAD_REQUEST)


class OrderCheckoutView(viewsets.ModelViewSet):
Expand Down

0 comments on commit 83586b8

Please sign in to comment.