Skip to content

Commit a04dbc3

Browse files
uds5501iamareebjamal
authored andcommitted
feat: Implementing is_active and timeframe checks for discount code (fossasia#5612)
* Implementing is_active and timeframe checks - Implementing is_active and time-frame checks for discount code application - Using `405` error code - Using `tz-aware` datetime elements for time frame checks * Modifying the tests to ensure code application * Remove print stub Co-Authored-By: uds5501 <singhuddeshyaofficial@gmail.com>
1 parent 97315ca commit a04dbc3

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

app/api/discount_codes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sqlalchemy.orm.exc import NoResultFound
44

55
from app.api.helpers.db import safe_query
6-
from app.api.helpers.exceptions import ConflictException, ForbiddenException, UnprocessableEntity
6+
from app.api.helpers.exceptions import ConflictException, ForbiddenException, UnprocessableEntity, MethodNotAllowed
77
from app.api.helpers.permission_manager import has_access
88
from app.api.helpers.permissions import jwt_required, current_identity
99
from app.api.helpers.utilities import require_relationship
@@ -15,6 +15,8 @@
1515
from app.models.ticket import Ticket
1616
from app.models.user import User
1717

18+
from datetime import datetime
19+
1820
class DiscountCodeListPost(ResourceList):
1921
"""
2022
Create Event and Ticket Discount code and Get Event Discount Codes
@@ -208,6 +210,13 @@ def before_get(self, args, kwargs):
208210
discount = db.session.query(DiscountCode).filter_by(code=kwargs.get('code'), deleted_at=None).first()
209211
if discount:
210212
kwargs['id'] = discount.id
213+
discount_tz = discount.valid_from.tzinfo
214+
current_time = datetime.now().replace(tzinfo=discount_tz)
215+
if not discount.is_active:
216+
raise MethodNotAllowed({'parameter': '{code}'}, "Discount Code is not active")
217+
elif current_time < discount.valid_from or current_time > discount.valid_till:
218+
raise MethodNotAllowed({'parameter': '{code}'},
219+
"Discount Code is not active in current time frame")
211220
else:
212221
raise ObjectNotFound({'parameter': '{code}'}, "DiscountCode: not found")
213222

app/factories/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
url_ = 'http://example.com'
99
imageUrl_ = 'https://www.w3schools.com/html/pic_mountain.jpg'
1010
date_ = '2016-12-13T23:59:59.123456+00:00'
11-
dateEnd_ = '2016-12-14T23:59:59.123456+00:00'
11+
dateEnd_ = '2020-12-14T23:59:59.123456+00:00'
1212
country_ = 'US'
1313
currency_ = 'USD'
1414
int_ = '1'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy