Giter Site home page Giter Site logo

elite-lab-models-1's Introduction

Elite Lab 6: Sessions

Intro

In this lab, we will be improving our chatrooms by creating persistent sessions. This concept is what drives a lot of our online interaction. Any time you log in, you are creating a session. This is what allows you to log in to reddit/facebook/gmail, and stay logged in (even if you close your browser).

We will be exploring how the server Python code and client Javascript code interact. You will be editing parts of both.

This will also be your first time working with Javascript in this course. I've added a lot more print statements this time, and a whole lot more comments. Be sure to look closely at session.js as it'll be a good starter on how to write javascript code.

I've prepped some quick reference links down below to help you out:

Remember to always use Google and StackOverflow as a resource if you are not sure how to implement something. Feel free to reach out to me as well.

Objective

Task

Your task is to create the APIs that the web page can use to create session tokens and retrieve them. You will also be editing your webpage's javascript code to add the correct URLs.

Lab is complete when you are able to succesfully:

  • Enter a chatroom with a username
  • Open up the chatroom in a separate browser and still be logged in as the same user

You will be editing these files:

  • app/static/js/session.js
  • app/controllers.py

You don't need to modify anything else.

If you look closely into the session.js file, it will make a single Ajax request to create a session. You will need to fill out:

  • Where to send this request (the url)
  • What to put into the request (the body)

If you look into controllers.py, you will need to implement the API to create a session. You will need to create a single API endpoint that will handle creating sessions. It will consist of:

  • The URL name (and supported request type)
  • Flask function that will handle
    • How to get the username from the request body
    • How to create the session
    • How to return the token in the response

Context

We have provided you with starter and demo code.

We have also provided scripts/util.py that you can run in the command line. This script can help insert test messages into your database and also delete all messages (so you can start fresh if need be). This is to help you out, but not needed for completion.

Set Up

  • Fork and clone the repository to your local dev environment

  • Activate your virtual environment

python3 -m venv venv
source venv/bin/activate
  • Install the dependencies to your virtual environment
pip3 install -r requirements.txt
  • Start up your SQLite database with:
FLASK_APP=lab-app.py python3 -m flask db init
FLASK_APP=lab-app.py python3 -m flask db migrate -m "my first migration"
FLASK_APP=lab-app.py python3 -m flask db upgrade
  • Spin up the local web server with:
FLASK_APP=lab-app.py FLASK_ENV=development python3 -m flask run

Lab Steps

  • Look through the app/static/js/session.js file and read the comments to learn about what it does.

  • Look at the Ajax request in this file and notice what it expects in the response.

  • Look through app/models.py file and notice the manager function SessionManager.create_session. Notice what it takes in as an argument and what it returns.

  • (Optional) Run the scripts/util.py script to seed your database with test messages

  • Name your API and implement its handler function

  • Complete the javascript code to call that API correctly

  • Enter the chatroom and validate that you can start a session

  • Send some messages as yourself in this session

  • Open the same webpage in a different browser and check if you are still logged in as the same person

  • You're done!

Lab Advice

  • Read the comments in app/static/js/session.js. We go in-depth on what the javascript code is doing. Feel free to ask me further questions about callbacks.

  • Look at the app/models.py specifically the SessionManager.create_session function. Notice what kind of argument it takes in. Notice what it returns. This will determine what you need to do with the request and response in app/controllers.py

  • Open the webpage console (right click on the webpage and click inspect) to see the Javascript print statements. It will be very useful for debugging.

  • You should not need to modify the schema, but if your database goes out of sync with your schema, you can run these commands:

FLASK_APP=lab-app.py python3 -m flask db stamp head
FLASK_APP=lab-app.py python3 -m flask db migrate
FLASK_APP=lab-app.py python3 -m flask db upgrade

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.