Program purpose and function

The purpose make users sign in to their own account to use our product. The program I was working on is the signup and login feature.

Data abstraction

  • For the frontend data abstraction, I only provide two signup input boxes which are the username and password, user does not need to provide any personal information.
  • For the backend data abstraction, I use a database to hold all the login credentials for the users and define functions like checking the account info and insert the account info.
    I write all the code in main.py in order for it to work when the server run
from flask import Flask, render_template, request, redirect, session  # import render_template from "public" flask libraries
# import "packages" from "this" project
from __init__ import app,db  # Definitions initialization
from api import app_api # Blueprint import api definition
from bp_projects.projects import app_projects # Blueprint directory import projects definition
import sqlite3 #import sqlite library for database
app.register_blueprint(app_api) # register api routes
app.register_blueprint(app_projects) # register api routes

def register_user(username, password):
    conn = sqlite3.connect('GroupA/user.db')
    c = conn.cursor()
    c.execute('INSERT INTO users(username, password) values (?, ?)', (username, password))
    conn.commit()
    conn.close()

def check_user(username, password):
    conn = sqlite3.connect('GroupA/user.db')
    c = conn.cursor()
    c.execute('Select username, password FROM users WHERE username=? and password=?', (username, password))

    result = c.fetchone()
    if result:
        return True
    else:
        return False

app.secret_key = "123456"

@app.errorhandler(404)  # catch for URL not found
def page_not_found(e):
    # note that we set the 404 status explicitly
    return render_template('404.html'), 404

@app.route('/')  # connects default URL to index() function
def index():
    return render_template("home.html")

@app.route('/stub/')  # connects /stub/ URL to stub() function
def stub():
    return render_template("stub.html")

@app.route('/registeri/')
def registeri():
    return render_template("register.html")

@app.route('/register', methods=["POST", "GET"])
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if check_user(username, password):
            return "user already exist"
        else:
            register_user(username, password)
            return redirect('/')
    
@app.route('/login', methods=["POST", "GET"])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        print(check_user(username, password))
        if check_user(username, password):
            session['username']=username

            return redirect('/home')
        else:
            return "Wrong Username or Password"
    
@app.route('/home', methods=["POST","GET"])
def home():
    if 'username' in session:
        return render_template("index.html", username=session['username'])
    else:
        return "Wrong Credentials"
    
@app.route('/logout')
def logout():
    session.clear()
    return redirect('/')   

# this runs the application on the development server
if __name__ == "__main__":
    app.run(debug=True)

Managing complexity

I manage complexity by defining functions and app.route as action to do jobs

Procedural abstraction

This part calls back the function from before to use the checking user in database function to check if there were same account that was already registered.

@app.route('/register', methods=["POST", "GET"])
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if check_user(username, password):
            return "user already exist"
        else:
            register_user(username, password)
            return redirect('/')

Algorithm Implementation

This include the action that is defined in the python file to complete the task of check user information and login.

<div class="container">
      <form action="/login" method="post">
      <div class="mb-3">
        <label for="Username" class="form-label">Username</label>
        <input type="text" class="form-control" id="Username" name="username">
      </div>
      <div class="mb-3">
        <label for="Password" class="form-label">Password</label>
        <input type="password" class="form-control" id="Password" name="password">
      </div>
      <button type="submit" class="btn btn-primary">Login</button>
    </form>