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

import bottle
bottle.BaseRequest.MEMFILE_MAX = 1024 * 1024

app = Bottle()

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

 

#controllers/dashboard/post/create.py
import uuid
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

    createdb.call(title, thumb, datetime, id, edit, content, category, entries)

    return redirect('/dashboard')

 

#models/postdb/createdb.py
import setConnection

def call(title, thumb, datetime, id, edit, content, category, entries):
    cursor, connection = setConnection.call()

    if not edit:
        sql = "INSERT INTO post VALUES(?, ?, ?, ?, ?, ?, ?)"
        cursor.execute(sql, (title, thumb, datetime, id, content, category, entries))
    else:
        sql = """UPDATE post SET
            title = ?,
            thumb = ?,
            datetime = ?,
            content = ?,
            category = ?,
            entries = ?
            
            WHERE id = ? """

        cursor.execute(sql, (title, thumb, datetime, content, category, entries, id))

    connection.commit()
    cursor.close()

 

GitHub: https://github.com/Sokhavuth/REST-API

Vercel: https://rest-api-zeta.vercel.app