def it_logs_args_if_development_and_no_google_config_when_upload_file(self, app, mocker): mocker.patch.dict('app.storage.utils.current_app.config', { 'ENVIRONMENT': 'development', 'GOOGLE_APPLICATION_CREDENTIALS': '', }) mock_logger = mocker.patch("app.storage.utils.current_app.logger.info") store = Storage('test-store') store.upload_blob('source', 'destination') assert mock_logger.called
def it_uploads_a_file(self, app, mocker): mocker.patch.dict('os.environ', { 'GOOGLE_APPLICATION_CREDENTIALS': 'path/to/creds' }) mocker.patch("google.cloud.storage.Client", MockStorageClient) mocker.patch("google.auth.compute_engine.Credentials") store = Storage('test-store') store.upload_blob('source', 'destination') assert store.bucket.destination_filename == 'destination' assert store.bucket.blob.source_filename == 'source' assert store.bucket.blob.public
def import_events(): data = request.get_json(force=True) validate(data, post_import_events_schema) errors = [] events = [] for item in data: err = '' event = Event.query.filter(Event.old_id == item['id']).first() if not event: speakers = [] event_type = dao_get_event_type_by_old_id(item['Type']) if not event_type: err = '{} event type not found: {}'.format( item['id'], item['Type']) current_app.logger.info(err) errors.append(err) if item['Speaker']: for s in re.split(r' and | & ', item['Speaker']): speaker = dao_get_speaker_by_name(s) if not speaker: err = '{} speaker not found: {}'.format( item['id'], item['Speaker']) current_app.logger.info(err) errors.append(err) else: speakers.append(speaker) venue = dao_get_venue_by_old_id(item['venue']) if not venue: err = '{} venue not found: {}'.format(item['id'], item['venue']) current_app.logger.info(err) errors.append(err) if err: continue event = Event(old_id=item['id'], event_type_id=event_type.id, title=item['Title'], sub_title=item['SubTitle'], description=item['Description'], booking_code=item['BookingCode'], image_filename=item['ImageFilename'], fee=item['Fee'], conc_fee=item['ConcFee'], multi_day_fee=item['MultiDayFee'], multi_day_conc_fee=item['MultiDayConcFee'], duration=item['Duration'], venue_id=venue.id, event_state=APPROVED) 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) add_event_date(item['StartDate']) for i in range(2, 5): if item['StartDate{}'.format(i)] > '0000-00-00 00:00:00': add_event_date(item['StartDate{}'.format(i)]) events.append(event) dao_create_event(event) current_app.logger.info(u'added event {} - {}'.format( event.old_id, event.title)) else: err = u'event already exists: {} - {}'.format( event.old_id, event.title) current_app.logger.info(err) errors.append(err) if is_running_locally() and item[ 'ImageFilename'] and item['ImageFilename'] != '../spacer.gif': storage = Storage(current_app.config['STORAGE']) if not storage.blob_exists(item['ImageFilename']): fname = "./data/events/{}".format(item['ImageFilename']) if os.path.isfile(fname): storage.upload_blob(fname, item['ImageFilename']) else: err = '{} not found for {}'.format(fname, event.old_id) current_app.logger.info(err) errors.append(err) else: current_app.logger.info('{} found'.format( item['ImageFilename'])) res = {"events": [e.serialize() for e in events]} if errors: res['errors'] = errors return jsonify(res), 201 if events else 400 if errors else 200