Skip to content

Commit 8517224

Browse files
committed
Moved common check to function
1 parent ab8c818 commit 8517224

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

app/api/tickets.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
from app.api.helpers.db import get_count
2222

2323

24-
def validate_ticket_price(ticket_type, price):
25-
if ticket_type != 'free' and int(price) <= 0:
24+
def validate_ticket_price(data):
25+
if not data.get('price') and data.get('type'):
26+
raise UnprocessableEntity({}, "Type/price of ticket is missing")
27+
if data.get('type') != 'free' and int(data.get('price')) <= 0:
2628
raise UnprocessableEntity(
27-
{'price': price}, "Price of a paid/donation ticket must be greater than zero")
29+
{'price': data.get('price')}, "Price of a paid/donation ticket must be greater than zero")
2830

2931

3032
class TicketListPost(ResourceList):
@@ -66,7 +68,7 @@ def before_create_object(self, data, view_kwargs):
6668
if not event.is_payment_enabled():
6769
raise UnprocessableEntity(
6870
{'event_id': data['event']}, "Event having paid ticket must have a payment method")
69-
validate_ticket_price(data.get('type'), data.get('price'))
71+
validate_ticket_price(data)
7072

7173
schema = TicketSchema
7274
methods = ['POST', ]
@@ -189,8 +191,7 @@ def before_update_object(self, ticket, data, view_kwargs):
189191
if not event.is_payment_enabled():
190192
raise UnprocessableEntity(
191193
{'event_id': ticket.event.id}, "Event having paid ticket must have a payment method")
192-
if data.get('price') and data.get('type'):
193-
validate_ticket_price(data.get('type'), data.get('price'))
194+
validate_ticket_price(data)
194195

195196
decorators = (api.has_permission('is_coorganizer', fetch='event_id',
196197
fetch_as="event_id", model=Ticket, methods="PATCH,DELETE"),)

0 commit comments

Comments
 (0)