#controllers/post.py import config, lib from bottle import route, template, request, redirect, response from models import postdb @route('/post/<id:int>') def post(id): config.kargs['blogTitle'] = "ទំព័រការផ្សាយ" config.kargs['post'] = postdb.select(1, id) config.kargs['posts'] = postdb.select(config.kargs['frontPagePostLimit']) config.kargs['thumbs'] = lib.getPostThumbs(config.kargs['posts']) return template('post', data=config.kargs) @route('/post/delete/<id:int>') def delete(id): author = request.get_cookie("logged-in", secret=config.kargs['secretKey']) if ((author != "Guest") and postdb.check(author)): postdb.delete(id) redirect('/login') @route('/post/edit/<id:int>') def edit(id): config.kargs['post'] = postdb.select(1, id) config.kargs['edit'] = True return template('dashboard/home', data=config.kargs)
#models/postdb.py import os import psycopg2 def createTable(): if 'DYNO' in os.environ: DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require') cursor = conn.cursor() else: conn = psycopg2.connect( database="postgres", user="postgres", password="sokhavuth", host="localhost", port="5432" ) cursor = conn.cursor() SQL = '''CREATE TABLE IF NOT EXISTS POST( ID TEXT, TITLE TEXT, AUTHOR TEXT, POSTDATE DATE, POSTTIME TIME, CATEGORY TEXT, CONTENT TEXT )''' cursor.execute(SQL) conn.commit() conn.close() def insert(*post): createTable() if 'DYNO' in os.environ: DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require') cursor = conn.cursor() else: conn = psycopg2.connect( database="postgres", user="postgres", password="sokhavuth", host="localhost", port="5432" ) cursor = conn.cursor() cursor.execute("INSERT INTO POST (ID, TITLE, AUTHOR, POSTDATE, POSTTIME, CATEGORY, CONTENT) VALUES %s ", (post,)) conn.commit() conn.close() def select(amount, id=None): createTable() if 'DYNO' in os.environ: DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require') cursor = conn.cursor() else: conn = psycopg2.connect( database="postgres", user="postgres", password="sokhavuth", host="localhost", port="5432" ) cursor = conn.cursor() if id and (amount == 1): cursor.execute("SELECT * FROM POST WHERE ID = '" + str(id) +"'") else: cursor.execute("SELECT * FROM POST ORDER BY CTID DESC LIMIT " + str(amount)) result = cursor.fetchall() return result def check(username): if 'DYNO' in os.environ: DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require') cursor = conn.cursor() else: conn = psycopg2.connect( database="postgres", user="postgres", password="sokhavuth", host="localhost", port="5432" ) cursor = conn.cursor() cursor.execute("SELECT USERNAME FROM USERS WHERE USERNAME = '"+ username + "' LIMIT 1") result = cursor.fetchone() if result: return True else: return False def delete(id): if 'DYNO' in os.environ: DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require') cursor = conn.cursor() else: conn = psycopg2.connect( database="postgres", user="postgres", password="sokhavuth", host="localhost", port="5432" ) cursor = conn.cursor() cursor.execute("DELETE FROM POST WHERE ID = '" + str(id) + "'") conn.commit() conn.close()
<!--views/dashboard/home.tpl--> %include('./dashboard/partials/header.tpl') <style> #content{ min-height: 350px; background: white; padding: 0; box-sizing: border-box; } #content .ck-editor__editable { min-height: 350px !important; } #content #post-title{ width: 100%; box-sizing: border-box; padding: 5px 10px; font: 16px/1.5 Koulen; } #content ::placeholder{ opacity: .4; } #content #bottombar{ background: #ebebeb; padding: 5px; border: 1px solid #bebbbb; } #content #bottombar .bottom-widget{ font: 14px/1.5 OdorMeanChey; height: 30px; } #content #bottombar input:hover{ cursor: pointer; } #content #bottombar #category{ min-width: 80px; } #content .post-time{ height: 24px !important; font:bold 14px/1.5 'Lucida Sans' !important; width: 100px; } </style> <div id='main' class='main region'> %include('./dashboard/partials/sidebar.tpl') <section id='content' class='content'> <form action="/posting" method="post"> %if 'edit' in data: <input id="post-title" name="fpost-title" value="{{data['post'][0][1]}}" type="text" placeholder="ចំណងជើង" required /> <textarea name="fcontent" id="editor">{{data['post'][0][6]}}</textarea> <div id="bottombar"> <input id="submit" class="bottom-widget" type="submit" value="ចុះផ្សាយ"> <select class="bottom-widget" id="category" name="fcategory"> <option>News</option> <option>Python</option> <option>Node.js</option> <option>PHP</option> </select> <script>$("#category").val("{{data['post'][0][5]}}").change();</script> <input id="post-date" value="{{data['post'][0][3].strftime('%d-%m-%Y')}}" class="bottom-widget post-time" type="text" name="fpost-date" /> <input id="post-time" value="{{data['post'][0][4].strftime('%H:%M:%S')}}" class="bottom-widget post-time" type="text" name="fpost-time" /> <input disabled style="background:white;text-align:center;" type='text' value="{{data['post'][0][2]}}" id="post-author" class="bottom-widget post-time" /> </div> % del data['edit'] %else: <input id="post-title" name="fpost-title" type="text" placeholder="ចំណងជើង" required /> <textarea name="fcontent" id="editor"></textarea> <div id="bottombar"> <input id="submit" class="bottom-widget" type="submit" value="ចុះផ្សាយ"> <select class="bottom-widget" id="category" name="fcategory"> <option>News</option> <option>Python</option> <option>Node.js</option> <option>PHP</option> </select> <input id="post-date" value="{{data['datetime'][0]}}" class="bottom-widget post-time" type="text" name="fpost-date" /> <input id="post-time" value="{{data['datetime'][1]}}" class="bottom-widget post-time" type="text" name="fpost-time" /> <input disabled style="background:white;text-align:center;" type='text' value="{{data['author']}}" id="post-author" class="bottom-widget post-time" /> </div> %end </form> <div style="text-align: center;">{{data["message"]}}</div> %data['message'] = "" <script src="/static/scripts/ckeditor/config.js"></script> </section><!--content--> </div><!--main--> %include('./dashboard/partials/footer.tpl')
GitHub: https://github.com/Sokhavuth/kwblog
Heroku: https://khmerweb-kwblog.herokuapp.com/