From 17644e584e47ca2cbc83874805b930d5c4275407 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 11 Dec 2024 16:33:43 +0100 Subject: views: cleanup action_setup_modify() Check form.validate() for errors at the begining of the view handler for an early return in case of error. --- ogcp/views.py | 115 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/ogcp/views.py b/ogcp/views.py index f298213..e449463 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -830,69 +830,72 @@ def action_setup_show(): def action_setup_modify(): form = SetupForm(request.form) MIN_EFI_SIZE = 500 - if form.validate(): - cache_count = 0 - for partition in form.partitions: - if partition.part_type.data == 'CACHE': - cache_count += 1 - - if partition.part_type.data == 'EFI' and partition.fs.data != 'FAT32': - flash(_('The EFI partition requires a FAT32 filesystem'), category='error') - return redirect(url_for('commands')) + if not form.validate(): + flash(_(f'Invalid setup form'), category='error') + return redirect(url_for('commands')) - if partition.part_type.data == 'EFI' and partition.size.data < MIN_EFI_SIZE: - flash(_(f'The EFI partition requires a size of {MIN_EFI_SIZE}MiB or higher'), category='error') - return redirect(url_for('commands')) + cache_count = 0 + for partition in form.partitions: + if partition.part_type.data == 'CACHE': + cache_count += 1 - if partition.size.data <= 0: - flash(_('Partitions can\'t have a size of zero or lower'), category='error') - return redirect(url_for('commands')) + if partition.part_type.data == 'EFI' and partition.fs.data != 'FAT32': + flash(_('The EFI partition requires a FAT32 filesystem'), category='error') + return redirect(url_for('commands')) - if cache_count > 1: - flash(_(f'More than one cache partition is not supported'), category='error') + if partition.part_type.data == 'EFI' and partition.size.data < MIN_EFI_SIZE: + flash(_(f'The EFI partition requires a size of {MIN_EFI_SIZE}MiB or higher'), category='error') return redirect(url_for('commands')) - ips = form.ips.data.split(' ') + if partition.size.data <= 0: + flash(_('Partitions can\'t have a size of zero or lower'), category='error') + return redirect(url_for('commands')) - payload = {'clients': ips, - 'disk': str(form.disk.data), - 'type': str(form.disk_type.data), - 'cache': str(0), - 'cache_size': str(0), - 'partition_setup': []} - - partition_index = 0 - - for partition in form.partitions: - partition_index += 1 - partition_setup = {'partition': str(partition_index), - 'code': str(partition.part_type.data), - 'filesystem': str(partition.fs.data), - 'size': str(partition.size.data * 1024), - 'format': '1'} - payload['partition_setup'].append(partition_setup) - - if partition.part_type.data == 'CACHE': - payload['cache'] = '1' - payload['cache_size'] = str(partition.size.data * 1024) - - for partition_index in range(len(form.partitions) + 1, 5): - empty_part = { - 'partition': str(partition_index), - 'code': 'EMPTY', - 'filesystem': 'EMPTY', - 'size': '0', - 'format': '1', - } - payload['partition_setup'].append(empty_part) + if cache_count > 1: + flash(_(f'More than one cache partition is not supported'), category='error') + return redirect(url_for('commands')) - server = get_server_from_clients(list(ips)) - r = server.post('/setup', payload=payload) - if not r: - return ogserver_down('commands') - if r.status_code == requests.codes.ok: - return redirect(url_for('commands')) - flash(_(f'Invalid setup form'), category='error') + ips = form.ips.data.split(' ') + + payload = {'clients': ips, + 'disk': str(form.disk.data), + 'type': str(form.disk_type.data), + 'cache': str(0), + 'cache_size': str(0), + 'partition_setup': []} + + partition_index = 0 + + for partition in form.partitions: + partition_index += 1 + partition_setup = {'partition': str(partition_index), + 'code': str(partition.part_type.data), + 'filesystem': str(partition.fs.data), + 'size': str(partition.size.data * 1024), + 'format': '1'} + payload['partition_setup'].append(partition_setup) + + if partition.part_type.data == 'CACHE': + payload['cache'] = '1' + payload['cache_size'] = str(partition.size.data * 1024) + + for partition_index in range(len(form.partitions) + 1, 5): + empty_part = { + 'partition': str(partition_index), + 'code': 'EMPTY', + 'filesystem': 'EMPTY', + 'size': '0', + 'format': '1', + } + payload['partition_setup'].append(empty_part) + + server = get_server_from_clients(list(ips)) + r = server.post('/setup', payload=payload) + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error(r, 'commands') + flash(_(f'Setup command sent sucessfully'), category='info') return redirect(url_for('commands')) def search_image(images_list, image_id): -- cgit v1.2.3-18-g5258