Adding Login Functionality
#routes/index.py import config, copy from flask import render_template from flask_classful import FlaskView, route from controllers.login import Login class Index(FlaskView): def __init__(self): super().__init__() self.login = Login() @route('/') def index(self): vdict = copy.deepcopy(config.vdict) return render_template('index.html', data=vdict) @route('/login/', methods=['GET', 'POST']) def check_user(self): return self.login.check_user()
#controllers/login.py import config, copy, lib from flask import render_template, redirect, request, session from datetime import datetime from models.userdb import Userdb class Login(): def __init__(self): self.userdb = Userdb() def set_root(self): vlib = lib.Lib() vdatetime = vlib.get_timezone() date = datetime.strptime(vdatetime[0], "%d/%m/%Y") time = datetime.strptime(vdatetime[1], '%H:%M:%S') self.userdb.insert('root', 'etv@website.com', 'root', 'Admin', '', date, time, 'root') def check_user(self): vdict = copy.deepcopy(config.vdict) vdict['site_title'] = 'ទំព័រចុះឈ្មោះ' if (request.method == 'POST'): email = request.form['femail'] password = request.form['fpassword'] if(self.userdb.check_user(email, password)): session['logged-in'] = email return redirect('/dashboard/') else: if 'logged-in' in session: return redirect('/dashboard/') user = self.userdb.select(1) if not user: self.set_root() return render_template('login.html', data=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 USERS( ID SERIAL PRIMARY KEY, USERNAME VARCHAR(320), EMAIL VARCHAR(320), PASSWORD VARCHAR(320), ROLE TEXT, CONTENT TEXT, CDATE DATE, CTIME TIME, AUTHOR TEXT )''' self.cursor.execute(SQL) self.conn.commit() self.conn.close() def insert(self, *user): self.set_conection() self.cursor.execute("INSERT INTO USERS (USERNAME, EMAIL, PASSWORD, ROLE, CONTENT, CDATE, CTIME, AUTHOR) VALUES %s ", (user,)) self.conn.commit() self.conn.close() def select(self, amount=5, id='', page=0): self.set_conection() if id: SQL = "SELECT * FROM USERS WHERE ID=%s" self.cursor.execute(SQL, (id,)) result = self.cursor.fetchone() elif page: SQL = "SELECT * FROM USERS ORDER BY ID DESC OFFSET %s ROWS FETCH NEXT %s ROWS ONLY" self.cursor.execute(SQL, (amount*page, amount)) result = self.cursor.fetchall() else: SQL = "SELECT * FROM USERS ORDER BY ID DESC LIMIT %s" self.cursor.execute(SQL, (amount,)) result = self.cursor.fetchall() self.conn.close() return result def check_user(self, *user): self.set_conection() SQL = "SELECT EMAIL, PASSWORD FROM USERS WHERE EMAIL = %s AND PASSWORD = %s LIMIT 1" self.cursor.execute(SQL, (user)) result = self.cursor.fetchone() self.conn.close() return result
<!--templates/login.html--> {% extends 'base.html' %} {% block head %} {{ super() }} <link href="/static/styles/login.css" rel="stylesheet"> {% endblock %} {% block channel %} <form id="login" action="/login/" method="POST"> <a>Email:</a><input type="email" name="femail" required /> <a>ពាក្យសំងាត់ៈ</a><input type="password" name="fpassword" required /> <a></a><input type='submit' value="បញ្ជូន" /> </form> {% endblock %}
GitHub: "https://github.com/Sokhavuth/ETV
Heroku: https://khmerweb-etv.herokuapp.com/
Comments
Post a Comment