0

在 Flask 中,我想在会话超时时删除一些文件。我没有用户登录 - 任何人都可以上传图像并用它们做事。我希望图像在会话结束时消失。但是我如何检测到那是什么时候呢?

我相信将图像存储在 RAM 中是没有意义的,这就是为什么我想将它们保存在服务器上并再次删除它们的原因。

编辑:查看功能的代码和用于渲染 PDF 的代码,其中包含要在会话超时时删除的图像。

视图.py:

from flask import (render_template, request)
from flask_weasyprint import HTML, render_pdf


from .forms import toPDF
from .helpers import (custom_images_in_collection)



@user_blueprint.route("/collection", methods=["GET", "POST"])
def collection():

    form = toPDF()

    # 'collection' is a list of preexisting/permanent image objects selected by user and stored in session
    # custom_images is a list of extra image objects created by user on the fly. They're stored
    # and retreived from the database from their own table of temporary images with a time field.
    # I want the custom images to disappear when the session expires
    custom_images = custom_images_in_collection(collection)
    collection.append(custom_images)

    if request.method == "POST":
        if collection:
            if form.validate_on_submit():

                template = render_template("mypdf.html", collection=collection)
                html = HTML(string=template)
                # This bit of CSS is dynamically generated, the rest is hard coded in the template

                return render_pdf(html)

    return render_template("collection.html", collection=collection, form=form)

我的pdf.html:

{% if collection|length %}
<div class="innerbox">
    {% for word in collection %}
    <div class="imgcard">
        <div class="imgcontainer">
            <img class="card-img-top" src="{{ url_for('static', filename='images/' + word.image.name)}}"
                alt="image of '{{ word.word }}'">
        </div>
        <div class="card-body">
            <h2 class="card-title">{{ word.word }}</h2>
        </div>
    </div>
</div>

{% endfor %}

{% else %}
<div class="col-4">
    <p>You don't have a collection yet!</p>
</div>
{% endif %}
4

0 回答 0