Skip to content

Commit f33d8ba

Browse files
authored
Merge pull request #86 from golony6449/feature/golony/ticket
티켓-결제 관련 모듈 수정
2 parents bf142c4 + 070d955 commit f33d8ba

File tree

10 files changed

+208
-25
lines changed

10 files changed

+208
-25
lines changed

account/templates/account_mypage_payments.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
</tr>
1212
</thead>
1313
<tbody>
14-
{% for payment in payment_list %}
14+
{% for ticket in ticket_list %}
1515
<tr>
16-
<td>{{ payment.id }}</td>
17-
<td>{{ payment.user_id }}</td>
18-
<td>{{ payment.money }}</td>
19-
<td>{{ payment.create_at }}</td>
16+
<td>{{ ticket.id }}</td>
17+
<td>{{ ticket.payment.money }}</td>
18+
<td>{{ ticket.create_at }}</td>
2019
<td>
2120
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full">
2221
Button

account/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from rest_framework.views import APIView
1313
from rest_framework.decorators import api_view
1414

15-
import payment
15+
from ticket.models import Ticket
1616

1717
from allauth.account.views import LoginView, LogoutView
1818

@@ -39,9 +39,9 @@ class MyPage(APIView):
3939

4040
@login_required
4141
def mypage_payments(request):
42-
payment_list = payment.models.Payment.objects.filter(user_id=request.user)
42+
ticket_list = Ticket.objects.filter(user=request.user)
4343
return render(request, 'account_mypage_payments.html',
44-
context={'payment_list': payment_list})
44+
context={'ticket_list': ticket_list})
4545

4646

4747
@api_view(["POST"])
@@ -63,6 +63,7 @@ def login_api(request):
6363
}
6464
return Response(response_data)
6565

66+
6667
@api_view(["POST"])
6768
def logout_api(request):
6869

@@ -75,4 +76,3 @@ def logout_api(request):
7576
"msg": "ok"
7677
}
7778
return Response(response_data)
78-
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Generated by Django 4.1.5 on 2023-05-15 13:40
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("ticket", "0002_rename_conferencetickettype_tickettype"),
11+
("payment", "0001_initial"),
12+
]
13+
14+
operations = [
15+
migrations.RenameField(
16+
model_name="payment",
17+
old_name="user_id",
18+
new_name="user",
19+
),
20+
migrations.AddField(
21+
model_name="payment",
22+
name="status",
23+
field=models.IntegerField(
24+
choices=[
25+
(1, "결제 전"),
26+
(2, "결제 실패"),
27+
(3, "결제 성공"),
28+
(4, "환불 실패"),
29+
(5, "환불 완료"),
30+
],
31+
default=0,
32+
),
33+
preserve_default=False,
34+
),
35+
migrations.AddField(
36+
model_name="payment",
37+
name="ticket_type",
38+
field=models.ForeignKey(
39+
default=1,
40+
on_delete=django.db.models.deletion.PROTECT,
41+
to="ticket.tickettype",
42+
),
43+
preserve_default=False,
44+
),
45+
migrations.AlterField(
46+
model_name="paymenthistory",
47+
name="status",
48+
field=models.IntegerField(
49+
choices=[
50+
(1, "결제 전"),
51+
(2, "결제 실패"),
52+
(3, "결제 성공"),
53+
(4, "환불 실패"),
54+
(5, "환불 완료"),
55+
]
56+
),
57+
),
58+
]

payment/models.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
from django.db import models
22
from django.contrib.auth import get_user_model
33

4-
from ticket.models import TicketType
5-
64
User = get_user_model()
75

86

97
class Payment(models.Model):
108
payment_key = models.CharField(max_length=32) # TODO: uuid 처리
11-
user_id = models.ForeignKey(User, on_delete=models.PROTECT)
9+
user = models.ForeignKey(User, on_delete=models.PROTECT)
10+
ticket_type = models.ForeignKey("ticket.TicketType", on_delete=models.PROTECT)
1211
money = models.IntegerField()
12+
status = models.IntegerField(
13+
choices=(
14+
(1, "결제 전"),
15+
(2, "결제 실패"),
16+
(3, "결제 성공"),
17+
(4, "환불 실패"),
18+
(5, "환불 완료"),
19+
)
20+
)
1321
create_at = models.DateTimeField(auto_now_add=True)
1422
update_at = models.DateTimeField(auto_now=True)
1523

1624

1725
class PaymentHistory(models.Model):
1826
payment_key = models.CharField(max_length=32)
19-
status = models.IntegerField()
27+
status = models.IntegerField(
28+
choices=(
29+
(1, "결제 전"),
30+
(2, "결제 실패"),
31+
(3, "결제 성공"),
32+
(4, "환불 실패"),
33+
(5, "환불 완료"),
34+
)
35+
)
2036
create_at = models.DateTimeField(auto_now_add=True)
2137
update_at = models.DateTimeField(auto_now=True)

payment/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def generate_payment_key(user: User, ticket_type: TicketType):
1414
new_payment = Payment(
1515
payment_key=shortuuid.uuid(),
1616
user_id=user,
17-
# ticket_type=ticket_type, # TODO
17+
ticket_type=ticket_type,
1818
money=ticket_type.price
1919
)
2020

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 4.1.5 on 2023-05-15 13:40
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("sponsor", "0003_alter_sponsor_bank_book_file_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterModelOptions(
14+
name="sponsor",
15+
options={
16+
"ordering": ["paid_at", "id"],
17+
"verbose_name": "후원사",
18+
"verbose_name_plural": "후원사 목록",
19+
},
20+
),
21+
migrations.AlterModelOptions(
22+
name="sponsorlevel",
23+
options={"verbose_name": "후원사 등급", "verbose_name_plural": "후원사 등급"},
24+
),
25+
]

ticket/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib import admin
22

3-
from .models import ConferenceTicket, TicketType
3+
from .models import Ticket, TicketType
44

55

66
class ConferenceTicketAdmin(admin.ModelAdmin):
@@ -13,7 +13,7 @@ class ConferenceTicketAdmin(admin.ModelAdmin):
1313
list_filter = ("ticket_type",)
1414

1515

16-
admin.site.register(ConferenceTicket, ConferenceTicketAdmin)
16+
admin.site.register(Ticket, ConferenceTicketAdmin)
1717

1818

1919
class ConferenceTicketTypeAdmin(admin.ModelAdmin):
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Generated by Django 4.1.5 on 2023-05-15 13:40
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
import ticket.models
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
("payment", "0002_rename_user_id_payment_user_payment_status_and_more"),
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
("ticket", "0002_rename_conferencetickettype_tickettype"),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name="Ticket",
20+
fields=[
21+
(
22+
"id",
23+
models.BigAutoField(
24+
auto_created=True,
25+
primary_key=True,
26+
serialize=False,
27+
verbose_name="ID",
28+
),
29+
),
30+
("bought_at", models.DateTimeField()),
31+
(
32+
"ticket_code",
33+
models.CharField(
34+
db_index=True,
35+
default=ticket.models.make_ticket_code,
36+
max_length=25,
37+
unique=True,
38+
),
39+
),
40+
("is_refunded", models.BooleanField(default=False)),
41+
("refunded_at", models.DateTimeField(null=True)),
42+
("created_at", models.DateTimeField(auto_now_add=True)),
43+
("updated_at", models.DateTimeField(auto_now=True)),
44+
(
45+
"payment",
46+
models.ForeignKey(
47+
null=True,
48+
on_delete=django.db.models.deletion.PROTECT,
49+
to="payment.payment",
50+
),
51+
),
52+
],
53+
),
54+
migrations.RemoveField(
55+
model_name="tickettype",
56+
name="code",
57+
),
58+
migrations.AddField(
59+
model_name="tickettype",
60+
name="is_refundable",
61+
field=models.BooleanField(default=True),
62+
),
63+
migrations.DeleteModel(
64+
name="ConferenceTicket",
65+
),
66+
migrations.AddField(
67+
model_name="ticket",
68+
name="ticket_type",
69+
field=models.ForeignKey(
70+
on_delete=django.db.models.deletion.RESTRICT, to="ticket.tickettype"
71+
),
72+
),
73+
migrations.AddField(
74+
model_name="ticket",
75+
name="user",
76+
field=models.ForeignKey(
77+
on_delete=django.db.models.deletion.RESTRICT,
78+
to=settings.AUTH_USER_MODEL,
79+
),
80+
),
81+
]

ticket/models.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010

1111
class TicketType(models.Model):
12-
code = models.CharField(max_length=50)
1312
name = models.CharField(max_length=100)
1413
price = models.IntegerField()
1514
min_price = models.IntegerField(null=True, blank=True)
@@ -22,17 +21,19 @@ class TicketType(models.Model):
2221
("WEEKEND", "토/일요일"),
2322
),
2423
)
24+
# program = models.ForeignKey() # TODO
25+
is_refundable = models.BooleanField(default=True)
2526

2627
def __str__(self):
2728
return self.name
2829

2930
@property
3031
def buyable(self) -> bool:
3132
"""잔여 수량이 있는지"""
32-
sat_ticket_count = ConferenceTicket.objects.filter(
33+
sat_ticket_count = Ticket.objects.filter(
3334
models.Q(ticket_type__day="SAT") & models.Q(ticket_type__day="WEEKEND")
3435
).count()
35-
sun_ticket_count = ConferenceTicket.objects.filter(
36+
sun_ticket_count = Ticket.objects.filter(
3637
models.Q(ticket_type__day="SUN") & models.Q(ticket_type__day="WEEKEND")
3738
).count()
3839

@@ -61,7 +62,7 @@ def make_ticket_code() -> str:
6162
return shortuuid.uuid()
6263

6364

64-
class ConferenceTicket(models.Model):
65+
class Ticket(models.Model):
6566
# 구분
6667
ticket_type = models.ForeignKey(
6768
TicketType, on_delete=models.RESTRICT, db_index=True
@@ -74,7 +75,10 @@ class ConferenceTicket(models.Model):
7475
ticket_code = models.CharField(
7576
max_length=25, default=make_ticket_code, unique=True, db_index=True
7677
)
77-
78+
# 결제 정보
79+
payment = models.ForeignKey("payment.Payment", on_delete=models.PROTECT, null=True)
80+
is_refunded = models.BooleanField(default=False)
81+
refunded_at = models.DateTimeField(null=True)
7882
created_at = models.DateTimeField(auto_now_add=True)
7983
updated_at = models.DateTimeField(auto_now=True)
8084

ticket/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.views.decorators.csrf import csrf_exempt
1010
from django.views import View
1111

12-
from .models import ConferenceTicket, TicketType
12+
from .models import Ticket, TicketType
1313
from .requests import (
1414
AddConferenceTicketRequest,
1515
CheckConferenceTicketTypeBuyableRequest,
@@ -94,7 +94,7 @@ def get__check_conference_ticket_type_buyable(
9494
except User.DoesNotExist:
9595
return HttpResponse(json.dumps(ticket_type.buyable))
9696

97-
bought_tickets = ConferenceTicket.objects.filter(user=user)
97+
bought_tickets = Ticket.objects.filter(user=user)
9898

9999
return HttpResponse(
100100
json.dumps(
@@ -141,7 +141,7 @@ def post__add_conference_ticket(request: HttpRequest, **kwargs) -> HttpResponse:
141141
except User.DoesNotExist:
142142
return HttpResponse("Cannot find user with user_id", status=400)
143143

144-
bought_tickets = ConferenceTicket.objects.filter(user=user)
144+
bought_tickets = Ticket.objects.filter(user=user)
145145
if any(
146146
(
147147
not bought_ticket.ticket_type.can_coexist(ticket_type)
@@ -150,7 +150,7 @@ def post__add_conference_ticket(request: HttpRequest, **kwargs) -> HttpResponse:
150150
):
151151
return HttpResponse("Duplicate day", status=400)
152152

153-
ticket = ConferenceTicket.objects.create(
153+
ticket = Ticket.objects.create(
154154
ticket_type=ticket_type,
155155
bought_at=bought_at,
156156
user=user,

0 commit comments

Comments
 (0)