Instructor: Hi guys, and welcome back,
in this video we're going to be adding
the vote date to our votes in the polling app.
So let's get started.
We're gonna start off by opening database.py
and we want to add a new column into the votes table.
So, oh by the way, I've got an old import there,
so we're gonna do that, in create votes,
we have the username, the option ID,
an now we're also gonna add the vote timestamp,
which is going to be a integer.
Make sure that because we're changing the table,
we have to go into ElephantSQL,
or your server of choice,
and delete the votes table from there.
Otherwise, whenever you try to insert a new vote,
into this table, your gonna find that the existing table
doesn't have enough columns.
So something else that we have to do here of course,
is go ahead and change the insert vote here
as well as the username and option ID,
we have to enter the vote timestamp
and the values do require the third parameter there.
Now let's go over to add poll vote down here.
And we're just gonna make sure that,
as well as the username and the option ID,
we get the timestamp, I'm gonna put it down here
in the middle, so I'm gonna do vote timestamp,
it's gonna be a float, and we're gonna pass it in there.
Notice that we're putting an integer into the database
as opposed to a float, that's just gonna lose everything
after the decimal place and that's okay,
we don't really care about which microsecond
somebody voted in, maybe we would,
if we were doing something a bit more like politics,
voting, or something like that.
But for here, we can covert from floats to integers
and lose everything after the decimal point
and that's okay and then we're going to add this in,
but I do realise that
we need to put that in there
and just because the username, option ID,
and vote timestamp have to match
the order of the columns that we're putting in here.
All right so this is how we insert a vote.
Now, whenever we're selecting a vote, from here,
get votes for option, this is gonna return everything,
so that means we're gonna get the timestamp in here as well.
We're gonna deal with that in a moment.
The first thing though is gonna go to option.py
and here, when we're voting, we need to change this slightly
because now we have to pass in the timestamp.
And we know that we have to pass in the UTC timestamp,
not the local time.
So let's do it, using what we learned in the last lecture.
We can do current datetime as UTC and in the last video,
we learned a way to get the current datetime as a UTC,
which was getting the UTC timezone from pytz
and then using that to localise the naive datetime,
but we can do it with datetime.datetime.now
and passing the timezone here directly,
this is an alternative.
So of course, we do have to import the datetime module,
and the pytz module.
These are just going up to the top and we're doing import
pytz and import datetime.
So there we have the current datetime in UTC,
let's get the current timestamp in UTC,
and that's current datetime UTC dot timestamp.
And then we're gonna put that into the database.
The reason I opted for putting the timestamp
before the option ID,
is just because I like seeing self.id there at the end.
It seems to be not something we're doing everywhere,
so it just makes sense to put it there.
But it's up to you where you wanna put your parameters.
Finally, we can display the vote dates in app.py.
So what I'm gonna do is go down here
to show all votes,
and I'm gonna add a couple of lines to it.
I'm gonna ask whether the user
wants to view the vote log,
so who's voted for what.
I'm gonna say vote log equal
input, something like would you want to see the vote log?
And we're gonna default to no.
Like that.
And then, if the vote log
is equal to yes,
we're gonna print vote for options,
and we're gonna pass in the options in here.
Now we do have to create this function, so let's do that.
I'm gonna do
print votes for options like that.
It's list and options list there.
And this is gonna be a list of option.
And what this is gonna do is go through the options
and print out each vote
and information about each vote.
Basically if you wanna see who voted for what
and when they did it,
you print this out.
So we'll do for option in options,
and then I'm gonna print out the option text.
So we'll do option.text like that.
And then we'll do for vote
in options., or rather options.vote.
Then we're gonna grab the date
and print the vote information.
Oh by the way, this should be option.votes there
otherwise we're trying to call the function.
So we're gonna do naive daytime
is what's coming from the database.
So we'll do daytime.daytime
UTC from timestamp
and we'll do vote two.
We do have to import the daytime module,
so we'll do import this name,
import the daytime module in there.
Then we get the UTC date,
which is pytz.utc.localize
and put in the naive data.
We do have to import the pytz module,
so we'll do that as well.
Now, we grab the local date,
and that's UTC date astimezone
and we're gonna do pytz.timezone
Europe slash London.
And then we're gonna do local date as a string
is gonna be local date.strftime
and we're gonna pass in here something like
percent y, percent m, percent d,
percent h, percent n.
I'm actually okay with just calculating that
to the end of there
so that we get the local date as a string.
So I'm just gonna do that instead,
but it's up to you how far you wanna subdivide it.
And finally at the end,
we're gonna do printout,
backslash d for a tab character,
which gives us a few spaces,
and then we'll do vote zero
on local date.
Something that is hard coded in here
is of course the time zone information.
What I would recommend when you're getting into
this level of, you know, functionality,
is probably to create a model for users,
store them in their own table,
store the timezone information alongside their data,
and, you know, that way you can start to build out
more functionality in this application.
But for now, we're gonna go ahead and right click
app.py and run it.
I'm gonna load this from the dot m file,
we're gonna create a poll.
It's gonna be flash versus jango.
Let's say poll owner is gonna be me.
Flask and jango and it depends
are gonna be our three options.
And then we're gonna list the open ports,
make sure this is working.
You can see that it's there.
We're gonna vote on a poll.
That's the ID one,
and obviously we're gonna vote for flask.
And we're gonna vote as rolf.
Now we're gonna show the poll votes.
So we'll do show poll votes,
we're gonna see poll one,
and you can see that flask got one vote,
and that's 100 percent of the total.
And now we wanna see the vote log.
So we'll press on the y here.
And you can see that each option gets their sub menu there.
And then here we see rolf on 2020-05-16.
So that's today
at 5:06.
So two things that we've learned in this video, really.
The first one is how to use the data and times
from your database here in the polling application,
which is great.
But really the more important thing
is how to change something in your database
and propagate that change throughout your application.
Usually the flow for a change like this
is gonna start with the data you want to handle,
or rather how you want the user to interact with it.
So figure out the data you want to store
and the data the users need,
and then work backwards from the database
to add the necessary flow.
Change the database interaction function
and the query.
Change the model that's using that,
and then change the user facing code that's using that.
That way, you're always in a working state.
But that's about it.
Thank you for joining me in this section,
I hope you have enjoyed it,
I hope you've enjoyed learning about data and times,
how to simplify our code, how to use models,
and how we can create these larger python applications
that use databases.
Thanks guys for joining me,
thank you for watching,
and I'll see you in the next one.