def test_exceptions(self): """Check ExceptionsHelper: exceptions types""" # Unprocessable Entity Exception with self.assertRaises(UnprocessableEntity): raise UnprocessableEntity( {'pointer': '/data/attributes/min-quantity'}, "min-quantity should be less than max-quantity") # Conflict Exception with self.assertRaises(ConflictException): raise ConflictException({'pointer': '/data/attributes/email'}, "Email already exists") # Authentication Required Exception with self.assertRaises(AuthenticationRequired): raise AuthenticationRequired({'source': ''}, "Authentication Required") # Forbidden Exception with self.assertRaises(ForbiddenException): raise ForbiddenException({'source': ''}, "Access Forbidden") # Method Not Allowed Exception with self.assertRaises(MethodNotAllowed): raise MethodNotAllowed({'source': ''}, "Method Not Allowed")
def before_post(self, args, kwargs, data): """ before post method to check for required relationship and proper permission :param args: :param kwargs: :param data: :return: """ require_relationship(['event'], data) if not has_access('is_coorganizer', event_id=data['event']): raise ObjectNotFound({'parameter': 'event_id'}, "Event: {} not found".format(data['event'])) if get_count( db.session.query(Ticket.id).filter_by(name=data['name'], event_id=int( data['event']), deleted_at=None)) > 0: raise ConflictException({'pointer': '/data/attributes/name'}, "Ticket already exists") if get_count( db.session.query(Event).filter_by( id=int(data['event']), is_ticketing_enabled=False)) > 0: raise MethodNotAllowed({'parameter': 'event_id'}, "Ticketing is disabled for this Event")
def before_post(self, args, kwargs, data): """ before post method to check for required relationship and proper permission :param args: :param kwargs: :param data: :return: """ require_relationship(['event'], data) if not has_access('is_coorganizer', event_id=data['event']): raise ForbiddenException({'source': ''}, 'Co-organizer access is required.') if get_count( db.session.query(Event).filter_by(id=int(data['event']), is_tax_enabled=False)) > 0: raise MethodNotAllowed({'parameter': 'event_id'}, "Tax is disabled for this Event")
def before_get(self, args, kwargs): if kwargs.get('ticket_id'): if has_access('is_coorganizer'): ticket = safe_query(db, Ticket, 'id', kwargs['ticket_id'], 'ticket_id') if ticket.discount_code_id: kwargs['id'] = ticket.discount_code_id else: kwargs['id'] = None else: raise UnprocessableEntity( {'source': ''}, "Please verify your permission. You must have coorganizer " "privileges to view ticket discount code details") if kwargs.get('event_id'): if has_access('is_admin'): event = safe_query(db, Event, 'id', kwargs['event_id'], 'event_id') if event.discount_code_id: kwargs['id'] = event.discount_code_id else: kwargs['id'] = None else: raise UnprocessableEntity({ 'source': '' }, "Please verify your permission. You must be admin to view event discount code details" ) if kwargs.get('event_identifier'): event = safe_query(db, Event, 'identifier', kwargs['event_identifier'], 'event_identifier') kwargs['event_id'] = event.id if kwargs.get('discount_event_identifier'): event = safe_query(db, Event, 'identifier', kwargs['discount_event_identifier'], 'event_identifier') kwargs['discount_event_id'] = event.id if kwargs.get('event_id') and has_access('is_admin'): event = safe_query(db, Event, 'id', kwargs['event_id'], 'event_id') if event.discount_code_id: kwargs['id'] = event.discount_code_id else: kwargs['id'] = None # Any registered user can fetch discount code details using the code. if kwargs.get('code'): # filter on deleted_at is required to catch the id of a # discount code which has not been deleted. discount = db.session.query(DiscountCode).filter_by( code=kwargs.get('code'), event_id=kwargs.get('discount_event_id'), deleted_at=None).first() if discount: kwargs['id'] = discount.id discount_tz = discount.valid_from.tzinfo current_time = datetime.now().replace(tzinfo=discount_tz) if not discount.is_active: raise MethodNotAllowed({'parameter': '{code}'}, "Discount Code is not active") elif current_time < discount.valid_from or current_time > discount.valid_till: raise MethodNotAllowed( {'parameter': '{code}'}, "Discount Code is not active in current time frame") else: raise ObjectNotFound({'parameter': '{code}'}, "DiscountCode: not found") self.schema = DiscountCodeSchemaTicket return if kwargs.get('id'): try: discount = db.session.query(DiscountCode).filter_by( id=kwargs.get('id')).one() except NoResultFound: raise ObjectNotFound({'parameter': '{id}'}, "DiscountCode: not found") # if discount.used_for == 'ticket' and has_access('is_coorganizer', event_id=discount.event_id): if discount.used_for == 'ticket': self.schema = DiscountCodeSchemaTicket # elif discount.used_for == 'event' and has_access('is_admin'): elif discount.used_for == 'event': self.schema = DiscountCodeSchemaEvent else: raise UnprocessableEntity({'source': ''}, "Please verify your permission")