Moved code from views to instructions.py
This commit is contained in:
parent
4a785df532
commit
6320629b07
@ -138,6 +138,18 @@ class BrickInstructions(object):
|
|||||||
if img_tag and "LEGO Building Instructions" in img_tag['alt']:
|
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
|
found_tags.append((img_tag['alt'].replace('LEGO Building Instructions for ', ''), a_tag['href'])) # Save alt and href
|
||||||
return found_tags
|
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:
|
def download(self, href: str, /) -> None:
|
||||||
target = self.path(secure_filename(self.filename))
|
target = self.path(secure_filename(self.filename))
|
||||||
|
@ -147,6 +147,7 @@ def download() -> str:
|
|||||||
def do_download() -> Response:
|
def do_download() -> Response:
|
||||||
set_id: str = request.form.get('add-set', '')
|
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)
|
found_tags = BrickInstructions(set_id).find_instructions(set_id)
|
||||||
|
|
||||||
return render_template('instructions.html', download=True, found_tags=found_tags)
|
return render_template('instructions.html', download=True, found_tags=found_tags)
|
||||||
@ -155,20 +156,15 @@ def do_download() -> Response:
|
|||||||
@login_required
|
@login_required
|
||||||
@exception_handler(__file__, post_redirect='instructions.download')
|
@exception_handler(__file__, post_redirect='instructions.download')
|
||||||
def confirm_download() -> Response:
|
def confirm_download() -> Response:
|
||||||
selected_instructions = []
|
|
||||||
for key in request.form:
|
# BrickInstructions require an argument. Not sure which makes sense here.
|
||||||
if key.startswith('instruction-') and request.form.get(key) == 'on': # Checkbox is checked
|
selected_instructions = BrickInstructions("").get_list(request.form)
|
||||||
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))
|
|
||||||
|
|
||||||
if not selected_instructions:
|
if not selected_instructions:
|
||||||
flash("No instructions selected", 'danger')
|
# No instructions selected
|
||||||
return redirect(url_for('instructions.download'))
|
return redirect(url_for('instructions.download'))
|
||||||
|
|
||||||
for href, filename in selected_instructions:
|
for href, filename in selected_instructions:
|
||||||
print(f"Downloading {filename} from {href}")
|
|
||||||
BrickInstructions(f"{filename}.pdf").download(href)
|
BrickInstructions(f"{filename}.pdf").download(href)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user