Hi everyone welcome back.
So in the previous video we saw that we did the whole structure for the project and now we're going
to re-implementing the after P1.
OK.
So the first thing is as usual we run out of important some of the important libraries and functions
and variables right from other modules.
So first of all read on our imports from class import the flask constructor.
We're also going to import J-Zone F-5s.
We use a lot.
When we return sounds into the user in Jason format and we're also going to import request.
OK.
Now if you notice you're this is a request without an ass.
The other libraries Call requests with an ass.
OK.
So just be careful that these two are not the same things they're different.
And many people actually confuse the two.
OK.
All right.
Now the second thing is we want flask restful so affronts us we're asked for we're going to import API.
Right.
So we want the API constructor and we also want to import resource which we inherit from.
The third thing we want to import is by Mungo's so from the by Mongo books by Mongo we want to import
the Mongo client that we use to connect to the Mongo D-B using the daquiri network.
OK.
Dr. componentry.
Now we also want to hash passwords so we're going to import it.
And then we're going to employ it secret right because that's what we used to do have the password and
then we're going to import this time we're going to important not buy import or actually let's just
leave it as as it is now.
OK so we will not import an Alpine denser flow.
We're going to leave that out.
We're going to see why are we going to leave it later.
And then we're also going to import requests so we send requests with an S is going to be used to get
the image from the or else that they use are sent.
OK.
And then we're also going to import subprocess.
And when I explain why we're going to import this one in there in the future of India.
And we're also going to import it Jason.
So we are.
But I also want to Jason what you on this side you don't need to download it is already in Python.
OK.
So there's this.
All right.
So you don't need to download it or install or anything so it's already in but or I said the first thing
we do is we do an app is equal to flask and then underscore in a square name underscore it underscored
or right.
So we initialize the flask with the name main and then we do Abiah as you go to API constructor.
So we initialize a new API.
Next we do client.
So we want to connect to the maladie So we do client you go to mung the client and then inside we're
going to say Mongo D.B and then semi-colon slash slash and then D-B to 7 0 1 7.
So that means we're connecting to the Mangione be and it's located at D-B right from the dock Nagar
compose your D.B and you're also connecting to the portal default for default port of Mungo D-B which
is 2 7 0 1 0.
OK.
Now after if connected to landlines we want to do a new database.
I want to say D-B is equal to client dot image.
So we're going to do a new database called Image recognition.
OK.
And then I'm going to say users is equal to D-B.
And then when I do a new collection inside the database called users right.
All right so we're now finished.
The main point.
OK.
So now we want to register it.
So do a class and then register.
All right.
And then you're going to inherit from resource.
All right.
And we said that this is going to be a post right if you remember from here we said all of them are
going to be published.
Right.
So we're going to come back here and then we're going to write a defined post self.
All right.
And then inside post we want to get.
First of all we always get to post the data.
So we say post a day that is equal to your request diet get your score.
Jason.
OK.
So you get the J on file at the post and then we get the user name and the password so say a user name
is equal to the today the user name.
OK so the users use your name and also the passwords so your password is equal to posted data and then
password.
Right.
All right so we have the user name and the password.
Well the first thing we did is for the error.
Well as you might have guessed 3:01 that hey there's a way to use it with this using the existing.
So choose another one.
So I want to say if user exists.
So when I write a helper function later on.
So if you use your existing username then what do I do.
Well I want to say you returned Jason is equal to status is three I want two hours in the air and then
a message is invalid usually dates are invalid username.
OK.
And I just returned just on a fly off this return Jason.
So are you going to return the Jaysh on if I ever return.
OK.
Now what happens if this is not the case.
Well we need to now have the password right.
So we're going to say hashed password this is standard is equal to me script.
And then I call the crib hash password function.
And then inside when I write the password.
But I'm going to encode it as UTF 8.
And don't forget about that one.
Or else you're going to it's not going to work.
So you could add two UTF 8 and then you generate assaults on Gaza a be grouped generate salt.
OK.
So generate salt and that's called off.
All right.
So now we have a hashed password.
OK.
Now the next thing is we're going to store the user and the password into our entire database.
I mean I say users got.
OK.
And then and then I inserted the user name as opposed to a user name.
Now we verified it doesn't exist.
I'm also going to do the password password and then.
So let's do that with a capital P.
So the password is going to mean the posted password that the user or hashed password actually not that
posted you know forgot about that.
So the hashed password not the plain password.
So you don't end up like most of these companies and then tokens is going to I'm going to give the user
just a few tokens in the store.
So I'm going to give them like five tokens right.
Or for say for tokens.
All right.
That should be enough.
And then if you want to you can refill later.
OK so once I've generated this user now I want to return a rope line.
So I want to say return Jason is equal to status is 200.
And the message is let's say you know you successfully signed up for this guy.
OK so you're successful they signed up for this.
OK.
All right.
And then once we are done that we just return.
Jason if I return Jason and that should be you.
We shouldn't have anything other than that.
But see we already used the user as this option but we didn't write it so we need to go back and rewrite
the user exists.
So I'm going to come here before the register class and then let's see if I can move it here.
Right then I'm going to write define user exist.
Ok so I'm going to write this user in this function and that is going to take the user name and is going
to tell me this from this user exists in the database or not.
So I'm going to say this.
So if that's so a little bit of inundations yours here too.
If users got signed then I want to find the user name with this user.
So a user name is you try to find some documents with this username that count is equal to zero.
Then I'm going to return false.
Why.
Because this user this user name does not exist in my collection right.
There's no user of this username so I'm allowing him to.
So this user does not exist to answer this question.
User exists with this user name.
No because the count is zero.
OK.
Else if he does exist that count is greater than zero.
So if his account is one for example then that means that the user does exists or return true.
OK.
So very simple on just checking the user with this user name exists then return false otherwise.
Which is true and that should be it for for the registration with just did it in a couple of lines if
it's OK.
So hopefully this is not too hard to see.
So the next thing we want to do is to implement the classify function but we're going to stop here and
we're going to take it from the next video and we're going to start with the classification and the
refilling.
OK.
So the next that is going to be quite long.
All right.
So we're going to stop here and we're going to wake up in the next few years until the next video.
Happy the.