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'))
def edit_api(self, key_id): form = APIKeyForm() if form.validate_on_submit(): api_key = current_user.api_keys.filter_by(key_id=key_id).first() if not api_key: flash('We could not find the API Key #{} in your account.'.format(key_id), 'danger') else: api_key.key_id = form.key_id.data api_key.vcode = form.vcode.data try: api_key.update_api_key() except AuthenticationException: flash('Could not authenticate with this API Key', 'error') 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: db.session.add(api_key) db.session.commit() current_user.update_keys() current_user.update_status() flash('API Key updated.', 'success') else: flash_errors(form) return redirect(url_for('AccountView:profile'))
def new_group(self): print('hello') new_group_form = GroupCreateForm() new_group_form.type.choices = [(element.name, element.value) for element in list(GroupType)] if new_group_form.validate_on_submit(): group = Group() new_group_form.populate_obj(group) group.type = GroupType[new_group_form.type.data].value membership = GroupMembership(user=current_user, group=group, is_admin=True, can_ping=True) db.session.add(group) db.session.add(membership) db.session.commit() flash("Group '{}' created with success.".format(group.name), 'success') else: flash_errors(new_group_form) return redirect(url_for('GroupsView:index'))