#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 create(id):
    if checkLogged.call():
        from controllers.backend.posts import edit
        return edit.call(id)
    else:
        redirect('/login')

 

#controllers/backend/posts/edit.py
import config
from bottle import template
from copy import deepcopy
from models.postdb import editdb

def call(id):
    kdict = deepcopy(config.kdict)
    kdict['pageTitle'] = 'ទំព័រ​កែប្រែ'
    kdict['route'] = 'post'
    kdict['edit'] = True

    posts, count, post = editdb.call(id, kdict['maxItemList'])

    kdict['items'] = posts
    kdict['count'] = count
    kdict['item'] = post
    
    return template('backend/admin.tpl', data=kdict)

 

#models/postdb/editdb.py
import setConnection

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

    posts = mycol.find().sort([("datetime", -1), ("_id", -1)]).limit(amount)
    count = mycol.count_documents({})

    post = mycol.find_one({"id": id})

    return posts, count, post

 

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

def call(title, thumb, datetime, id, edit, content, category, entries, authorID):
    
    mycol = setConnection.call("posts")

    if not edit:
        post = {
            "id":id,
            "title":title,
            "thumb":thumb,
            "datetime":datetime,
            "content":content,
            "category":category,
            "entries":entries,
            "authorID":authorID
        }
        
        mycol.insert_one(post)
        
    else:
        post = mycol.find_one({"id":edit})
        userRole = request.get_cookie('userRole', secret=config.kdict['SECRET_KEY'])

        if(post["authorID"] == authorID) or (userRole == "Admin"):
            myquery = {"id": edit}
            newvalues = {"$set": {
                    "title":title,
                    "thumb":thumb,
                    "datetime":datetime,
                    "content":content,
                    "category":category,
                    "entries":entries,
                    "authorID":authorID
                }
            }

            mycol.update_one(myquery, newvalues)

 

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

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