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

def call():
    kdict = deepcopy(config.kdict)
    kdict['pageTitle'] = 'ទំព័រ​ការផ្សាយ'
    kdict['route'] = 'post'
    posts, count, categories = getdb.call(kdict['maxItemList'])
    kdict['items'] = posts
    kdict['count'] = count
    kdict['categories'] = categories

    return template('backend/admin', data=kdict)

 

#models/postdb/getdb.py
import setConnection

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

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

    mycol = setConnection.call("categories")
    categories = mycol.find().sort('title', 1)

    return posts, count, categories

 

#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, categories = editdb.call(id, kdict['maxItemList'])

    kdict['items'] = posts
    kdict['count'] = count
    kdict['item'] = post
    kdict['categories'] = categories
    
    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})

    mycol = setConnection.call("categories")
    categories = mycol.find().sort('title', 1)

    return posts, count, post, categories

 

<!--views/dashboard/post.tpl-->
<link rel='stylesheet' href='/static/styles/post.css' />
<script src="/static/scripts/ckeditor/ckeditor.js"></script>
<script src="/static/scripts/video.js"></script>

<section class='Main'>
    <div class='content'>
        <form action='/admin/post' method='post' >
            %if 'edit' in data:
            <input type='text' name='title' value='{{data["item"]["title"]}}' placeholder='ចំណងជើង' required />
            <textarea name="content" id="editor" >{{data["item"]["content"]}}</textarea>
            <div class='wrapper'>
                <select name='category' class='category' >
                    %if 'categories' in data:
                    %for category in data['categories']:
                        <option>{{ category['title'] }}</option>
                    %end
                    %end
                </select>
                <script>$(".category").val("{{data['item']['category']}}").change();</script>
                <input type='text' name='thumb' value='{{data["item"]["thumb"]}}' required placeholder="តំណរ​ភ្ជាប់​រូប​តំណាង" />
                <input type='datetime-local' value='{{data["item"]["datetime"]}}' name='datetime' required />
                <input type='hidden' name='editid' value='{{data["item"]["id"]}}' />
                <input type='submit' value='ចុះ​ផ្សាយ' />
            </div>
            <input name='entries' value='{{!data["item"]["entries"]}}' type='hidden' />
            %else:
            <input type='text' name='title' placeholder='ចំណងជើង' required />
            <textarea name="content" id="editor" ></textarea>
            <div class='wrapper'>
                <select name='category'>
                    %if 'categories' in data:
                    %for category in data['categories']:
                        <option>{{ category['title'] }}</option>
                    %end
                    %end
                </select>
                <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"]["entries"]}}')
            </script>
        %else:
            <script>
                var entries = []
            </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>

 

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

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