Home > database >  Python Flask - Need to insert timestamp when a csv file is uploaded and posted to database
Python Flask - Need to insert timestamp when a csv file is uploaded and posted to database

Time:02-23

I'm creating a form that allows you to upload a .csv file and then on submit, adds the uploaded file to the database in a table called "Files".

What i'm trying to figure out is how to insert a timestamp for when the file is uploaded, so that on the HTML side, it shows the uploaded file, along with the date and time it was uploaded/posted.

What I tried before was giving me the local time, and it would keep changing with refresh, but I want the exact date and time the file is uploaded. In essence, something like the "Created" timestamp when you click on a file's properties in File Explorer.

I realize I may need to add another attribute for DateTime in my Files class/table so that it can be posted to the database, and then rendered onto the HTML.

Here is my class "Files":

class Files(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    data = db.Column(db.LargeBinary)
    f_source_id = db.Column(db.Integer, db.ForeignKey('funding_source.id'), nullable=True)

Here is where i'm posting the uploaded file to the database:

@main.route("/upload_file", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST' and 'csvfile' in request.files:
        f = request.files['csvfile']

        # pressing submit without uploading a file
        if f.filename == "":
            flash("No selected file. Please attach CSV file", "danger")
            return redirect(url_for('main.alloc_summ'))
        # only allowing .csv extension files
        if f.filename and not allowed_file(f.filename):
            flash("Only CSV Files Allowed", "danger")
            return redirect(url_for('main.alloc_summ'))
        # opening file, reading it in, and inserting values to database
        if f.filename != '' and allowed_file(f.filename):
            with open('<hidden path url>', 'r') as csv_file:
                csv_reader = csv.reader(csv_file, delimiter=',')
                new_file = Files(name=f.filename, data=f.read())
                db.session.add(new_file)
                db.session.commit()
                flash("File Uploaded", "success")
                return redirect(url_for('main.alloc_summ'))

I would really appreciate it if anyone could guide me on how to get the timestamp for when a file is uploaded/posted to the database so that I can query the Files table to show the file and timestamp on the HTML side. TIA!!

CodePudding user response:

I think it should be sufficient if you add another column to the database model that holds a timestamp. This is automatically filled with a value as soon as the database entry is created. So your requirements should be met.

from datetime import datetime

class Files(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime,
        nullable=False, unique=False, index=False,
        default=datetime.utcnow)
    # ...

You can use Flask-Moment to show the timestamp. It uses Moment.js to display.

  • Related