Creating Teacher Database
#main.py
import os
from flask import Flask
from controllers.index import Index

app = Flask(__name__)

Index.register(app, route_base='/')

if __name__ == '__main__':
  app.run(debug=True)
#controllers/index.py
import config, copy
from flask import render_template, request
from flask_classful import FlaskView, route
from models.userdb import Userdb

class Index(FlaskView):
    def __init__(self):
        self.vdict = copy.deepcopy(config.vdict)
        self.userdb = Userdb()

    @route('/')
    def index(self):
        return render_template('index.html', data=self.vdict)

    @route('/login/', methods=['GET', 'POST'])
    def login(self):
        if request.method == 'POST':
            email = request.form['femail']
            #if(self.userdb.check_username(username)):
            return render_template('dashboard.html', data=self.vdict)
        
        return render_template('login.html', data=self.vdict)
#models/userdb.py
import os, psycopg2

class Userdb():
  def __init__(self):
    self.create_table()

  def set_conection(self):
    if 'DYNO' in os.environ:
      DATABASE_URL = os.environ['DATABASE_URL']
      self.conn = psycopg2.connect(DATABASE_URL, sslmode='require')
      self.cursor = self.conn.cursor()
    else: 
      self.conn = psycopg2.connect(
        database="postgres", 
        user="postgres", 
        password="sokhavuth", 
        host="localhost", 
        port="5432"
      )

      self.cursor = self.conn.cursor()

  def create_table(self):
    self.set_conection()
    
    SQL = '''CREATE TABLE IF NOT EXISTS TEACHERS(
      ID SERIAL PRIMARY KEY,
      EMAIL VARCHAR(320),
      PASSWORD VARCHAR(320),
      ROLE TEXT
    )'''

    self.cursor.execute(SQL)
    self.conn.commit()
    self.conn.close() 

  def insert(self, *user):
    self.set_conection()

    self.cursor.execute("INSERT INTO TEACHERS (EMAIL, PASSWORD, ROLE) VALUES %s ", (user,))
  
    self.conn.commit()
    self.conn.close()

GitHub: "https://github.com/Sokhavuth/E-Learning
Heroku: https://khmerweb-elearning.herokuapp.com/