#routes/book.py
from bottle import Bottle, redirect
from controllers.login import checkLogged
import bottle
bottle.BaseRequest.MEMFILE_MAX = 1024 * 1024
app = Bottle()
@app.route('/')
def index():
if checkLogged.call():
from controllers.dashboard.book import get
return get.call()
else:
redirect('/')
@app.route('/', method='post')
def create():
if checkLogged.call():
from controllers.dashboard.book import create
return create.call()
else:
redirect('/')
@app.route('/edit/<id>')
def edit(id):
if checkLogged.call():
from controllers.dashboard.book import edit
return edit.call(id)
else:
redirect('/')
#controllers/dashboard/book/edit.py
import config
from bottle import template
from copy import deepcopy
from models.bookdb import getdb, editdb
def call(id):
kdict = deepcopy(config.kdict)
kdict['siteLogo'] = 'ទំព័រសៀវភៅ'
kdict['route'] = 'book'
kdict['edit'] = 'edit'
books, count = getdb.call(kdict['maxItemList'])
item = editdb.call(id)
kdict['items'] = books
kdict['count'] = count
kdict['item'] = item
return template('dashboard/index.tpl', data=kdict)
#models/bookdb/editdb.py
import setConnection
def call(id):
cursor, connection = setConnection.call()
cursor.execute("SELECT * FROM book WHERE id = ?", (id,))
item = cursor.fetchone()
cursor.close()
return item
<!--views/dashboard/book.tpl-->
<link rel='stylesheet' href='/static/styles/post.css' />
<script src="/static/scripts/ckeditor/ckeditor.js"></script>
<script src="/static/scripts/video.js"></script>
<style>
.Main .content form .wrapper{
margin-top:5px;
display: grid;
grid-template-columns: 20% 20% 20% auto 15%;
}
</style>
<section class='Main'>
<div class='content'>
<form action='/dashboard/book' method='post' >
%if 'edit' in data:
<input type='text' name='title' value='{{data["item"][0]}}' placeholder='ចំណងជើង' required />
<textarea name="content" id="editor" >{{data["item"][4]}}</textarea>
<div class='wrapper'>
<input type='text' name='book_title' value='{{data["item"][7]}}' required placeholder="ឈ្មោះសៀវភៅ" />
<input type='text' name='chapter' value='{{data["item"][5]}}' required placeholder="ជំពូក" />
<input type='text' name='thumb' value='{{data["item"][1]}}' required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type='datetime-local' value='{{data["item"][2]}}' name='datetime' required />
<input type='hidden' name='editid' value='{{data["item"][3]}}' />
<input type='submit' value='ចុះផ្សាយ' />
</div>
<input name='entries' value='{{!data["item"][6]}}' type='hidden' />
%else:
<input type='text' name='title' placeholder='ចំណងជើង' required />
<textarea name="content" id="editor" ></textarea>
<div class='wrapper'>
<input type='text' name='book_title' required placeholder="ឈ្មោះសៀវភៅ" />
<input type='text' name='chapter' required placeholder="ជំពូក" />
<input type='text' name='thumb' required placeholder="តំណរភ្ជាប់រូបតំណាង" />
<input type='datetime-local' value='' name='datetime' required />
<input type='submit' value='ចុះផ្សាយ' />
</div>
<input name='entries' value='' type='hidden' />
%end
</form>
<div class='form'>
<select name='type'>
<option>YouTube</option>
<option>YouTubePlaylist</option>
<option>Facebook</option>
<option>OK</option>
<option>Dailymotion</option>
<option>Vimeo</option>
</select>
<input name='id' type='text' placeholder="អត្តសញ្ញាណវីដេអូ" required />
<select name='ending'>
<option>ចប់ហើយ</option>
<option>មិនទាន់ចប់</option>
</select>
<input onclick='genJson()' type="button" value="បញ្ចូលវីដេអូ" />
</div>
<table class='viddata'></table>
%if 'edit' in data:
<script>
var entries = JSON.parse('{{!data["item"][6]}}')
</script>
%end
<script>
if(entries.length > 0){
let html = ``
for(let v in entries){
episode += 1
html += `<tr>`
html += `<td title="Delete" onClick="deleteRow(event)" class="episode">${episode}</td>`
html += `<td class="td${episode}">${entries[v].type}</td>`
html += `<td class="td${episode}">${entries[v].id}</td>`
html += `<td class="td${episode}">${entries[v].ending}</td>`
html += `</tr>`
}
if($('.viddata').html() === ''){
$('.viddata').append('<tr>')
$('.viddata').append('<th>ភាគ/លុប</th>')
$('.viddata').append('<th>ប្រភេទ</th>')
$('.viddata').append('<th>អត្តសញ្ញាណ</th>')
$('.viddata').append('<th>ចប់ឬនៅ?</th>')
$('.viddata').append('</tr>')
}
$('.viddata').append(`${html}`)
}
</script>
<script src="/static/scripts/ckeditor/config.js"></script>
</div>
</section>
#models/postdb/createdb.py
import setConnection
def call(title, thumb, datetime, id, edit, content, chapter, entries, bookTitle):
cursor, connection = setConnection.call()
if not edit:
sql = "INSERT INTO book VALUES(?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(sql, (title, thumb, datetime, id, content, chapter, entries, bookTitle))
else:
sql = """UPDATE book SET
title = ?,
thumb = ?,
datetime = ?,
content = ?,
chapter = ?,
entries = ?,
bookTitle = ?
WHERE id = ? """
cursor.execute(sql, (title, thumb, datetime, content, chapter, entries, bookTitle, id))
connection.commit()
cursor.close()
GitHub: https://github.com/Sokhavuth/REST-API
Vercel: https://rest-api-zeta.vercel.app