#index.py
import sys
from routes.frontend import index
from routes.frontend import login
from routes.backend import admin

app = index.app
app.mount('/login', login.app)
app.mount('/admin', admin.app)

if sys.platform == 'win32':
    app.run(host='localhost', port=7000, debug=True, reloader=True)

 

#routes/frontend/login.py
from bottle import Bottle, redirect
from controllers.frontend.login import checkLogged

app = Bottle()

@app.route('/')
def index():
    if checkLogged.call():
        redirect('/admin/post')
    else:
        from controllers.frontend.login import get
        return get.call()

@app.route('/', method="post")
def checkUser():
    from controllers.frontend.login import checkUser
    return checkUser.call()

@app.route('/logout')
def logout():
    from controllers.frontend.login import logout
    return logout.call()

 

#controllers/frontend/login/checkUser.py
import config, bcrypt
from copy import deepcopy
from bottle import template, request, response, redirect
from models.userdb import checkUserDB

def call():
    kdict = deepcopy(config.kdict)
    kdict['pageTitle'] = 'ទំព័រ​ចុះ​ឈ្មោះ'

    password = request.forms.getunicode('password')
    email = request.forms.getunicode('email')
    
    user = checkUserDB.call(email)
    
    if user:
        if(bcrypt.checkpw(password.encode('utf-8'), user['password'])):
            kdict["siteLogo"] = 'ទំព័រ​ការផ្សាយ'
            response.set_cookie('userID', user['userID'], path='/', secret=kdict['SECRET_KEY'])
            response.set_cookie('userRole', user['role'], path='/', secret=kdict['SECRET_KEY'])
            return redirect('/admin/post')
        else:
            kdict['message'] = 'ពាក្យ​សំងាត់របស់អ្នក​​មិន​ត្រឹមត្រូវ​ទេ!'
            return template('frontend/login', data=kdict)
    else:
        kdict['message'] = 'Email របស់​អ្នក​មិន​ត្រឹមត្រូវ​ទេ!'
        return template('frontend/login', data=kdict)

 

#models/userdb/checkUserDB.py
import setConnection, pymongo, config

def call(email):
    myclient = pymongo.MongoClient(config.kdict['MONGODB_URI'])
    mydb = myclient["multimedia"]
    mycol = mydb["users"]

    myquery = {'email':email}
    user = mycol.find_one(myquery)
    
    return user

 

GitHub: https://github.com/Sokhavuth/khmerweb-multimedia

Vercel: https://khmerweb-multimedia.vercel.app/