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

app = Bottle()

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

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

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

 

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

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

    categories, count, category = editdb.call(id, kdict['maxItemList'])

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

 

#models/postdb/editdb.py
import setConnection

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

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

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

    return categories, count, category

 

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

def call(name, link, datetime, id, edit):
    mycol = setConnection.call("categories")

    userRole = request.get_cookie('userRole', secret=config.kdict['SECRET_KEY'])

    if (not edit) and (userRole == "Admin"):
        category = {
            "id":id,
            "title":name,
            "thumb":link,
            "datetime":datetime
        }
        
        mycol.insert_one(category)
    else:
        if userRole == "Admin":
            myquery = {"id": edit}
            newvalues = {"$set": {
                    "title":name,
                    "thumb":link,
                    "datetime":datetime
                }
            }

            mycol.update_one(myquery, newvalues)

 

<!--views/backend/category-->
<link href='/static/styles/category.css' rel='stylesheet' />

<section class='Category'>
    %if 'edit' in data:
    <form action='/admin/category' method='post'>
        <a>ឈ្មោះ​ជំពូកៈ</a><input type='text' value="{{data['item']['title']}}" name="name" required />
        <a>តំណរភ្ជាប់​រូបៈ</a><input type='text' value="{{data['item']['thumb']}}" name='link' required />
        <a>ពេល​បង្កើតៈ</a><input type='datetime-local' value="{{data['item']['datetime']}}" name='datetime' required />
        <a></a><input type='submit' value='បញ្ជូន' />
        <a></a><input type='hidden' value="{{data['item']['id']}}" name="editid" />
    </form>
    %else:
    <form action='/admin/category' method='post'>
        <a>ឈ្មោះ​ជំពូកៈ</a><input type='text' name="name" required />
        <a>តំណរភ្ជាប់​រូបៈ</a><input type='text' name='link' required />
        <a>ពេល​បង្កើតៈ</a><input type='datetime-local' name='datetime' required />
        <a></a><input type='submit' value='បញ្ជូន' />
    </form>
    %end
</section>

 

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

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