def new_api(self): form = APIKeyForm() if form.validate_on_submit(): api_key = APIKey() form.populate_obj(api_key) try: api_key.update_api_key() except AuthenticationException: flash('Could not authenticate with this API Key', 'danger') return redirect(url_for('AccountView:profile')) except Exception as e: flash('Error updating API Key: {}'.format(e.message), 'danger') return redirect(url_for('AccountView:profile')) else: current_user.api_keys.append(api_key) db.session.add(current_user) try: db.session.commit() except Exception as e: flash('We could not save your API Key in the database.', 'danger') return redirect(url_for('AccountView:profile')) else: current_user.update_keys() current_user.update_status() db.session.commit() flash('API Key added with success.', 'success') else: flash_errors(form) return redirect(url_for('AccountView:profile'))