From 6320629b07fc07234781dbd9a069c1d5a8ea52d8 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 24 Jan 2025 17:20:53 +0100 Subject: [PATCH] Moved code from views to instructions.py --- bricktracker/instructions.py | 12 ++++++++++++ bricktracker/views/instructions.py | 14 +++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bricktracker/instructions.py b/bricktracker/instructions.py index 1c4f753..d776db5 100644 --- a/bricktracker/instructions.py +++ b/bricktracker/instructions.py @@ -138,6 +138,18 @@ class BrickInstructions(object): if img_tag and "LEGO Building Instructions" in img_tag['alt']: found_tags.append((img_tag['alt'].replace('LEGO Building Instructions for ', ''), a_tag['href'])) # Save alt and href return found_tags + + def get_list(self, request_form, /) -> list: + selected_instructions = [] + # Get the list of instructions + for key in request_form: + if key.startswith('instruction-') and request_form.get(key) == 'on': # Checkbox is checked + index = key.split('-')[-1] + alt_text = request_form.get(f'instruction-alt-text-{index}') + href_text = request_form.get(f'instruction-href-text-{index}') + selected_instructions.append((href_text,alt_text)) + + return selected_instructions def download(self, href: str, /) -> None: target = self.path(secure_filename(self.filename)) diff --git a/bricktracker/views/instructions.py b/bricktracker/views/instructions.py index c11a407..de2c3b9 100644 --- a/bricktracker/views/instructions.py +++ b/bricktracker/views/instructions.py @@ -147,6 +147,7 @@ def download() -> str: def do_download() -> Response: set_id: str = request.form.get('add-set', '') + # BrickInstructions require an argument. Not sure which makes sense here. found_tags = BrickInstructions(set_id).find_instructions(set_id) return render_template('instructions.html', download=True, found_tags=found_tags) @@ -155,20 +156,15 @@ def do_download() -> Response: @login_required @exception_handler(__file__, post_redirect='instructions.download') def confirm_download() -> Response: - selected_instructions = [] - for key in request.form: - if key.startswith('instruction-') and request.form.get(key) == 'on': # Checkbox is checked - index = key.split('-')[-1] - alt_text = request.form.get(f'instruction-alt-text-{index}') - href_text = request.form.get(f'instruction-href-text-{index}') - selected_instructions.append((href_text,alt_text)) + + # BrickInstructions require an argument. Not sure which makes sense here. + selected_instructions = BrickInstructions("").get_list(request.form) if not selected_instructions: - flash("No instructions selected", 'danger') + # No instructions selected return redirect(url_for('instructions.download')) for href, filename in selected_instructions: - print(f"Downloading {filename} from {href}") BrickInstructions(f"{filename}.pdf").download(href)