Timestamps

In order to also save the date when the talk was created, we can also create and save a date. Handling dates and times in software development is often a challenging task, given things like timezones, different formats for expressing date/time in different countries, etc, but there are libraries available in most programming languages that help with these things.

Python comes bundled with a number of libraries by default when you install it. These are referred to as the Python standard library. Other libraries need to be installed separately - you might remember that we installed a few of these, such as tweepy and peewee, during the install-fest portion before the Jumpstart program week.

The datetime library that we’ll use is part of the standard library. Let’s import it using the following in app.py.

import datetime

Then we can create a new datetime that contains the current date and time like this in app.py:

talk_timestamp = datetime.datetime.now()

We can then add this to our Talk object in the “created_at” field so that this information is also stored in the database along with the talk title and description text. The ORM will know how to map this to a format that SQLite will understand. We will also want to change what we’re passing to our template to pass this additional information about each talk.

You can see the full code here:

# app.py

from flask import Flask, render_template, redirect, url_for, jsonify, request
from talk_mashup_bot import talk_mashup_bot
from peewee import *
import datetime

app = Flask(__name__)
db = SqliteDatabase('talks.db')

@app.route('/')
def hello():
    return "Hello world!"

@app.route('/talk/new')
def new_talk():
    title = talk_mashup_bot.generateTitle()
    return render_template('new_talk.html', title=title)

@app.route('/talk', methods=['POST', 'GET'])
def talk():
    if request.method == 'GET':
        return redirect(url_for('new_talk'))

    talk_title = request.form['form-title']
    talk_description = request.form['form-description']
    talk_timestamp = datetime.datetime.now()

    new_talk = Talk(title=talk_title, description=talk_description, created_at=talk_timestamp)
    new_talk.save()

    return render_template('talk.html', title=talk_title, description=talk_description, timestamp=talk_timestamp)

class Talk(Model):
    title = CharField()
    description = CharField()
    created_at = DateField()

    class Meta:
        database = db

if __name__ == '__main__':
    app.run(debug=True)

Also be sure to update your templates/talk.html template to display some of this new information:

<h1>Your submitted talk:</h1>

<h2>{{ title }}</h2>
<h3>Submitted on {{ timestamp }}</h3>
<p>{{ description }}</p>

Re-submit the form a number of times to create and save a bunch of new talks.

We’ve now added a complete new feature to save talks to the database, so now would normally be a good time to git add, commit, and push our changes. We’ll only do this once at the end of this section for this workshop.