def get_opinions(): if request.method == "POST": product_code = request.form['product_code'] if product_code and product_code.isdecimal(): if product_list.find_one({"code": product_code}): return redirect('/product/' + product_code) elif Product.has_opinions(product_code): product_list.insert_one(Product.dict(product_code)) return redirect('/product/' + product_code) else: feedback = 'Produkt nie posiada opinii' else: feedback = 'Nieprawidłowy kod produktu.' return render_template('extraction.html', feedback=feedback) return render_template('extraction.html')
def download_opinions(product_code, file_extension): file_name = f'{product_code}-opinie.{file_extension}' file_path = f'./opinions/{product_code}' opinions = Product.dict(product_code)['opinions'] temp_file_name = ''.join( random.choices(string.ascii_letters + string.digits, k=16)) with open(temp_file_name, 'w', encoding='utf-8') as file: dict_list_to_file(opinions, file, file_extension) with open(temp_file_name, 'rb') as file: try: threading.Thread( target=delete_file_after, args=[temp_file_name, opinions_file_deletion_delay]).start() return send_file(file.name, as_attachment=True, attachment_filename=file_name) except Exception as e: return str(e)