def create_event_date(): data = request.get_json() validate(data, post_create_event_date_schema) check_event_id_and_datetime(data['event_id'], data['event_datetime']) if 'venue_id' not in data: venue = dao_get_default_venue() data['venue_id'] = venue.id event_date = EventDate(**data) dao_create_event_date(event_date) return jsonify(event_date.serialize()), 201
def add_event_date(event_datetime): event_date = EventDate( event_datetime=event_datetime, duration=item['Duration'], fee=item['Fee'], conc_fee=item['ConcFee'], multi_day_fee=item['MultiDayFee'], multi_day_conc_fee=item['MultiDayConcFee'], venue_id=venue.id) dao_create_event_date(event_date, speakers) event.event_dates.append(event_date)
def create_event_date( event_id=None, event_datetime='2018-01-01 19:00', duration=90, soldout=False, repeat=3, repeat_interval=7, fee=5, conc_fee=3, multi_day_fee=12, multi_day_conc_fee=10, speakers=None, ): venue = create_venue() data = { 'event_id': event_id, 'event_datetime': event_datetime, 'duration': duration, 'soldout': soldout, 'repeat': repeat, 'repeat_interval': repeat_interval, 'fee': fee, 'conc_fee': conc_fee, 'multi_day_fee': multi_day_fee, 'multi_day_conc_fee': multi_day_conc_fee, 'venue_id': venue.id, } event_date = EventDate(**data) if speakers: for s in speakers: event_date.speakers.append(s) dao_create_event_date(event_date) return event_date
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), remote_access=data.get('remote_access'), remote_pw=data.get('remote_pw'), ) 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=None if event_date.get('end_time') == '' else event_date.get('end_time'), speakers=speakers) dao_create_event_date(e) event.event_dates.append(e) dao_create_event(event) 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, base64.b64decode(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) if event.fee and event.fee > 0 and event.event_state in [READY, APPROVED]: paypal_tasks.create_update_paypal_button_task.apply_async( (str(event.id), )) 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 == []: raise InvalidRequest('{} needs an event date'.format(event_id), 400) if event_data.get('fee') and event_data.get('fee') > 0: 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: if data.get('event_state') in [READY, APPROVED]: paypal_tasks.create_update_paypal_button_task.apply_async( (str(event_id), )) res = dao_update_event(event_id, **event_data) if res: image_data = data.get('image_data') image_filename = data.get('image_filename') if current_app.config['STORAGE'].startswith('None'): current_app.logger.warn('Storage not setup') else: 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)) if data.get('event_state') != APPROVED: target_image_filename += '-temp' storage.upload_blob_from_base64string( image_filename, target_image_filename, base64.b64decode(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) if image_filename: if data.get('event_state') == APPROVED: if '-temp' in image_filename: q_pos = image_filename.index('-temp?') image_filename = image_filename[0:q_pos] storage.rename_image(image_filename + '-temp', image_filename) else: current_app.logger.warn( f"No temp file to rename: {image_filename}") event.image_filename = image_filename dao_update_event(event.id, image_filename=image_filename) json_event = event.serialize() 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) status_code = send_smtp_email( emails_to, '{} is ready for review'.format(event.title), message) if status_code != 200: errs.append(f"Problem sending admin email {status_code}") 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>' status_code = send_smtp_email( emails_to, '{} event needs to be corrected'.format(event.title), message) if status_code != 200: errs.append(f"Problem sending smtp emails: {status_code}") json_event['errors'] = errs return jsonify(json_event), 200 raise InvalidRequest('{} did not update event'.format(event_id), 400)