Handle empty input folders

This commit is contained in:
mandlm 2018-11-10 18:33:27 +01:00
parent 6deefc7ea8
commit d5f6b4bfd7

View File

@ -26,15 +26,18 @@ def random():
@app.route("/random_image/") @app.route("/random_image/")
def random_image(): def random_image():
last_modified_time, last_modified_file = max( try:
(f.stat().st_mtime, f) for f in imgdir.glob(img_glob) last_modified_time, last_modified_file = max(
) (f.stat().st_mtime, f) for f in imgdir.glob(img_glob)
)
if time() - last_modified_time <= 60: if time() - last_modified_time <= 60:
selected_image = last_modified_file.relative_to(imgdir) selected_image = last_modified_file.relative_to(imgdir)
else: else:
images = list(imgdir.glob(img_glob)) images = list(imgdir.glob(img_glob))
selected_image = choice(images).relative_to(imgdir) selected_image = choice(images).relative_to(imgdir)
except ValueError:
return redirect(url_for("static", filename="clear.gif"))
return redirect( return redirect(
url_for("image", filename=selected_image) url_for("image", filename=selected_image)