Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help3403


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
 
Options:
RSS cloud
Jump to:

Flask REST API and Live Feedback

3 of 419 articles shown, currently no other people reading this forum.
photo
From: Matthew C.
Date: Wed 20th May 2020, 10:43am
Actions: 
        Login-to-reply

 

Hi Tim, Tom and Haolin,

As per my last post https://secure.csse.uwa.edu.au/run/help3403?p=np&a=239&all=y3

I have been looking into added live feedback (and other live things) into our project. 
For context, once a user completes a quiz they see a "View Attempt Page" for that 
quiz, which contains all the quiz questions, the user's submitted answers, the marks 
and the feedback. The admin can provide feedback from flask admin (will likely 
implement a marking page for this) on each attempt of each question. The admin changes 
an entry in our database when they do this. 

To allow for live feedback to be added to this page, I have implemented a REST API for 
flask with limited functionality, that can extract the database entries for question 
attempts (which is where feedback is added) using an ajax call with xmlhttprequest. 
Such that this extracted json from the api has the new feedback from the admin and if 
this feedback does not correspond with the feedback on the page the user is looking 
at, it is changed without having to refresh the page.  

I have made this work without the use of token's in the api. However, when I try to 
add token functionality to the API, the request will work but I can not use the 
"setInterval" function to repeatedly call the function that retrieves the new json.

This is my pastebin: https://pastebin.com/TRK44dqa

My question is how do I allow this script to repeated call the API with the acquired 
token such that the old feedback can be replaced.

I am also interested in what sort of error handling I need to do for ajax, and how to 
do it as well as whether or not I need to delete this token for the user when the page 
isn't being used, though I also don't know how to do this.

Sincerely,
Matthew Carter

Flask REST API and Live Feedback

photo
From: Matthew C.  O.P.
Date: Wed 20th May 2020, 3:33pm
Actions: 
        Login-to-reply

 

I have fixed the function call, it seems setInterval and setTimeout both have trouble 
accepting parameters. You can get around this by using "Function.prototype.bind()". eg the 
last line of the pastebin is now:

setInterval(retrieveNewAttempts.bind(null, token), 5000)

However, I am still interested in what kind error handling needs to be done for ajax, and how 
to do it? Happy to be pointed in the right direction and not spoon-fed as I'm sure there is a 
bit to it.

In addition, I do not know when/how I should revoke a token from a user?

Sincerely,
Matthew Carter

Flask REST API and Live Feedback

photo
From: Timothy F.
Date: Thu 21st May 2020, 11:24am
Actions: 
        Login-to-reply

 

"Matthew Carter" <22*6*8*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> I have fixed the function call, it seems setInterval and setTimeout both have trouble 
> accepting parameters. You can get around this by using "Function.prototype.bind()". eg the 
> last line of the pastebin is now:
> 
> setInterval(retrieveNewAttempts.bind(null, token), 5000)
> 
> However, I am still interested in what kind error handling needs to be done for ajax, and how 
> to do it? Happy to be pointed in the right direction and not spoon-fed as I'm sure there is a 
> bit to it.
> 
> In addition, I do not know when/how I should revoke a token from a user?
> 
> Sincerely,
> Matthew Carter

This was covered in yesterdays lecture. For a REST API you should revoke a token when a user 
requests it (a DELETE request is sent to /api/token/) or the token expires (so a token expiry 
date is stred with the token).
This Page


Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020