1 2 3 4 5 6 7 8 9 10 11 | #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 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #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) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #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/