#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')

 

#controllers/backend/posts/create.py
import uuid, config
from bottle import request, redirect
from models.postdb import createdb

def call():
    title = request.forms.getunicode('title')
    thumb = request.forms.getunicode('thumb')
    datetime = request.forms.getunicode('datetime')
    edit = request.forms.getunicode('editid')
    content = request.forms.getunicode('content')
    category = request.forms.getunicode('category')
    entries = request.forms.getunicode('entries')

    if not edit:
        id = uuid.uuid4().hex
    else:
        id = edit

    userRole = request.get_cookie('userRole', secret=config.kdict['SECRET_KEY'])
    if userRole != 'visitor':
        createdb.call(title, thumb, datetime, id, edit, content, category, entries)

    return redirect('/admin/post')

 

#models/postdb/createdb.py
import setConnection

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

    if not edit:
        post = {
            "id":id,
            "title":title,
            "thumb":thumb,
            "datetime":datetime,
            "content":content,
            "category":category,
            "entries":entries
        }
        
        mycol.insert_one(post)
        
    else:
        pass

 

#setConnection.py
import config, pymongo

def call(collection):
    myclient = pymongo.MongoClient(config.kdict['MONGODB_URI'])
    mongodb = myclient["multimedia"]
    mongocol = mongodb[collection]

    return mongocol

 

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

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