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

app = Bottle()

@app.route('/')
def get():
    if checkLogged.call():
        from controllers.backend.posts import get
        return get.call()
    else:
        redirect('/login')

@app.route('/', method="post")
def create():
    if checkLogged.call():
        from controllers.backend.posts import create
        return create.call()
    else:
        redirect('/login')

@app.route('/edit/<id>')
def edit(id):
    if checkLogged.call():
        from controllers.backend.posts import edit
        return edit.call(id)
    else:
        redirect('/login')

@app.route('/delete/<id>')
def delete(id):
    if checkLogged.call():
        from controllers.backend.posts import delete
        return delete.call(id)
    else:
        redirect('/login')

 

#controllers/backend/posts/delete.py
from bottle import redirect
from models.postdb import deletedb

def call(id):
    deletedb.call(id)

    redirect('/admin/post')

 

#models/postdb/deletedb.py
import setConnection, config
from bottle import request

def call(id):
    mycol = setConnection.call("posts")

    userRole = request.get_cookie('userRole', secret=config.kdict['SECRET_KEY'])
    userID = request.get_cookie('userID', secret=config.kdict['SECRET_KEY'])
    post = mycol.find_one({"id": id})

    if(post["authorID"] == userID) or (userRole == "Admin"):
        myquery = { "id": id }
        mycol.delete_one(myquery)

 

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

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