def it_raises_an_error_if_no_item_found(self, app, mocker, sample_uuid): mocker.patch('app.payments.paypal.requests', MockRequests()) p = PayPal() with pytest.raises(expected_exception=PaypalException): p.create_update_paypal_button(sample_uuid, 'test title', booking_code='test booking code')
def it_raises_an_error_on_paypal_error(self, app, mocker): mocker.patch( 'app.payments.paypal.requests', MockRequests(ack='Error', err_msg='&L_LONGMESSAGE0=Error')) p = PayPal() with pytest.raises(expected_exception=PaypalException): p.create_update_paypal_button('152', 'test title')
def create_test_paypal(item_id): if current_app.config['ENVIRONMENT'] == 'live': return 'Cannot test paypal on live environment' p = PayPal() button_id = p.create_update_paypal_button(item_id, 'test paypal') return button_id
def it_calls_paypal_apis_to_create_button(self, app, mocker, sample_uuid): mocker.patch('app.payments.paypal.requests', MockRequests()) p = PayPal() button_id = p.create_update_paypal_button(sample_uuid, 'test title', all_fee=20, all_conc_fee=15, members_free=True) assert button_id == mock_create_button_id
def it_creates_a_paypal_button_for_search_without_L_BUTTONVAR6( self, app, mocker, sample_uuid): mocker.patch('app.payments.paypal.requests', MockRequests(has_L_BUTTONVAR6=False)) p = PayPal() button_id = p.create_update_paypal_button(sample_uuid, 'test title', all_fee=20, all_conc_fee=15, members_free=True) assert button_id == mock_create_button_id
def create_update_paypal_button_task(self, event_id): current_app.logger.info('Task create_paypal_button received for event %s', str(event_id)) try: event = dao_get_event_by_id(event_id) if not event.booking_code: dao_update_event(event_id, booking_code=f'pending: {self.request.id}') booking_code = event.booking_code if event.booking_code.startswith( 'pending:') or event.booking_code.startswith('error:'): booking_code = None p = PayPal() booking_code = p.create_update_paypal_button( event_id, event.title, event.fee, event.conc_fee, event.multi_day_fee, event.multi_day_conc_fee, True if event.event_type == 'Talk' else False, booking_code=booking_code) dao_update_event(event_id, booking_code=booking_code) except NoResultFound as e: current_app.logger.error( f'No result error trying to create_update_paypal_button {e} {event_id}' ) except PaypalException as e: dao_update_event(event_id, booking_code=f'error: {str(e)[:40]}') current_app.logger.error( f'Paypal error trying to create_update_paypal_button {e} {event_id}' )
def it_returns_a_mock_button_id_if_paypal_not_configured( self, app, mocker, sample_uuid): self.mock_config = { 'PAYPAL_URL': None, 'PAYPAL_USER': None, 'PAYPAL_PASSWORD': None, 'PAYPAL_SIG': None } mocker.patch.dict('app.payments.paypal.current_app.config', self.mock_config) mock_logger = mocker.patch( 'app.payments.paypal.current_app.logger.info') p = PayPal() button_id = p.create_update_paypal_button(sample_uuid, 'test title', all_fee=20, all_conc_fee=15, members_free=True) assert mock_logger.call_args == call( 'Paypal not configured, returning MOCK_BUTTON_ID') assert button_id == 'MOCK_BUTTON_ID'
def create_event(): data = request.get_json(force=True) event_year = None validate(data, post_create_event_schema) try: dao_get_event_type_by_id(data['event_type_id']) except NoResultFound: raise InvalidRequest( 'event type not found: {}'.format(data['event_type_id']), 400) try: dao_get_venue_by_id(data['venue_id']) except NoResultFound: raise InvalidRequest('venue not found: {}'.format(data['venue_id']), 400) event = Event(event_type_id=data['event_type_id'], title=data['title'], sub_title=data.get('sub_title'), description=data['description'], booking_code='', fee=data.get('fee'), conc_fee=data.get('conc_fee'), multi_day_fee=data.get('multi_day_fee'), multi_day_conc_fee=data.get('multi_day_conc_fee'), venue_id=data.get('venue_id'), event_state=data.get('event_state', DRAFT)) for event_date in data.get('event_dates'): if not event_year: event_year = event_date['event_date'].split('-')[0] speakers = [] for s in event_date.get('speakers', []): speaker = dao_get_speaker_by_id(s['speaker_id']) speakers.append(speaker) e = EventDate(event_datetime=event_date['event_date'], end_time=event_date.get('end_time'), speakers=speakers) dao_create_event_date(e) event.event_dates.append(e) dao_create_event(event) if event.fee: event_type = dao_get_event_type_by_id(event.event_type_id) p = PayPal() booking_code = p.create_update_paypal_button( str(event.id), event.title, event.fee, event.conc_fee, event.multi_day_fee, event.multi_day_conc_fee, True if event_type.event_type == 'Talk' else False) dao_update_event(event.id, booking_code=booking_code) image_filename = data.get('image_filename') image_data = data.get('image_data') storage = Storage(current_app.config['STORAGE']) if image_data: target_image_filename = '{}/{}'.format(event_year, str(event.id)) storage.upload_blob_from_base64string(image_filename, target_image_filename, image_data) image_filename = target_image_filename elif image_filename: if not storage.blob_exists(image_filename): raise InvalidRequest('{} does not exist'.format(image_filename), 400) event.image_filename = image_filename dao_update_event(event.id, image_filename=image_filename) return jsonify(event.serialize()), 201
def update_event(event_id): data = request.get_json(force=True) current_app.logger.info('Update event: {}'.format(data)) validate(data, post_update_event_schema) try: event = dao_get_event_by_id(event_id) except NoResultFound: raise InvalidRequest('event not found: {}'.format(event_id), 400) errs = [] event_dates = [] event_data = {} if data.get('event_state') == REJECTED: new_rejects = [ r for r in data.get('reject_reasons') if not r.get('id') ] if not new_rejects: raise InvalidRequest('rejected event requires new reject reason', 400) elif data.get('event_state') == APPROVED: if data.get('reject_reasons'): rejects = [ r for r in data.get('reject_reasons') if not r.get('resolved') ] if rejects: raise InvalidRequest( 'approved event should not have any reject reasons', 400) data_event_dates = data.get('event_dates') if data_event_dates: serialized_event_dates = event.serialize_event_dates() data_event_dates__dates = [e['event_date'] for e in data_event_dates] serialized_event_dates__dates = [ e['event_datetime'] for e in serialized_event_dates ] diff_add = set(data_event_dates__dates).difference( serialized_event_dates__dates) intersect = set(data_event_dates__dates).intersection( serialized_event_dates__dates) dates_to_add = [ e for e in data_event_dates if e['event_date'] in diff_add ] dates_to_update = [ e for e in data_event_dates if e['event_date'] in intersect ] for _date in dates_to_add: speakers = [] for s in _date.get('speakers', []): speaker = dao_get_speaker_by_id(s['speaker_id']) speakers.append(speaker) e = EventDate(event_id=event_id, event_datetime=_date['event_date'], end_time=_date.get('end_time'), speakers=speakers) current_app.logger.info('Adding event date: {}'.format( _date['event_date'])) dao_create_event_date(e) if _date['event_date'] not in [ _e.event_datetime for _e in event_dates ]: event_dates.append(e) for _date in sorted(dates_to_update, key=lambda k: k['event_date']): speakers = [] for s in _date['speakers']: speaker = dao_get_speaker_by_id(s['speaker_id']) speakers.append(speaker) db_event_date = [ e for e in event.event_dates if e.event_datetime.strftime( '%Y-%m-%d %H:%M') == _date['event_date'] ][0] db_event_date.speakers = speakers if _date['event_date'] not in [ _e.event_datetime for _e in event_dates ]: event_dates.append(db_event_date) if data.get('reject_reasons'): for reject_reason in data.get('reject_reasons'): if reject_reason.get('id'): reject_data = { 'reason': reject_reason['reason'], 'resolved': reject_reason.get('resolved') or False } dao_update_reject_reason(reject_reason.get('id'), **reject_data) else: rr = RejectReason(event_id=event_id, reason=reject_reason['reason'], resolved=reject_reason.get('resolved') or False, created_by=reject_reason.get('created_by')) dao_create_reject_reason(rr) event_data = {} for k in data.keys(): if hasattr(Event, k) and k not in ['reject_reasons']: event_data[k] = data[k] if event_dates: event_data['event_dates'] = event_dates elif data_event_dates == []: error = 'event needs to have a date' raise InvalidRequest('{} needs an event date'.format(event_id), 400) if event_data.get('fee'): update_data = { 'fee': event_data.get('fee'), 'conc_fee': event_data.get('conc_fee'), 'multi_day_fee': event_data.get('multi_day_fee') or 0, 'multi_day_conc_fee': event_data.get('multi_day_conc_fee') or 0, 'event_type_id': event_data.get('event_type_id'), } db_data = { 'fee': event.fee, 'conc_fee': event.conc_fee, 'multi_day_fee': event.multi_day_fee, 'multi_day_conc_fee': event.multi_day_conc_fee, 'event_type_id': str(event.event_type.id), } if update_data != db_data: event_type = dao_get_event_type_by_id( event_data.get('event_type_id')) p = PayPal() try: event_data['booking_code'] = p.create_update_paypal_button( event_id, event_data.get('title'), event_data.get('fee'), event_data.get('conc_fee'), event_data.get('multi_day_fee'), event_data.get('multi_day_conc_fee'), True if event_type.event_type == 'Talk' else False, booking_code=event_data.get('booking_code')) except PaypalException as e: current_app.logger.error(e) errs.append(str(e)) res = dao_update_event(event_id, **event_data) if res: image_data = data.get('image_data') image_filename = data.get('image_filename') storage = Storage(current_app.config['STORAGE']) if image_data: event_year = str(event.event_dates[0].event_datetime).split('-')[0] target_image_filename = '{}/{}'.format(event_year, str(event_id)) storage.upload_blob_from_base64string(image_filename, target_image_filename, image_data) unix_time = time.time() image_filename = '{}?{}'.format(target_image_filename, unix_time) elif image_filename: image_filename_without_cache_buster = image_filename.split('?')[0] if not storage.blob_exists(image_filename_without_cache_buster): raise InvalidRequest( '{} does not exist'.format( image_filename_without_cache_buster), 400) event.image_filename = image_filename dao_update_event(event.id, image_filename=image_filename) json_event = event.serialize() json_event['errors'] = errs if data.get('event_state') == READY: emails_to = [admin.email for admin in dao_get_admin_users()] message = 'Please review this event for publishing <a href="{}">{}</a>'.format( '{}/events/{}'.format(current_app.config['FRONTEND_ADMIN_URL'], event_id), event.title) send_email(emails_to, '{} is ready for review'.format(event.title), message) elif data.get('event_state') == REJECTED: emails_to = [user.email for user in dao_get_users()] message = '<div>Please correct this event <a href="{}">{}</a></div>'.format( '{}/events/{}'.format(current_app.config['FRONTEND_ADMIN_URL'], event_id), event.title) message += '<ol>' for reject_reason in [ rr for rr in json_event.get('reject_reasons') if not rr.get('resolved') ]: message += '<li>{}</li>'.format(reject_reason['reason']) message += '</ol>' send_email(emails_to, '{} event needs to be corrected'.format(event.title), message) return jsonify(json_event), 200 raise InvalidRequest('{} did not update event'.format(event_id), 400)
def it_calls_paypal_apis_to_update_button(self, app, mocker): mocker.patch('app.payments.paypal.requests', MockRequests()) p = PayPal() button_id = p.create_update_paypal_button(mock_item_id, 'test title') assert button_id == mock_update_button_id