#controllers/post.py import config import datetime from bottle import route, template, request, redirect, response from models import postdb @route('/post/<id:int>') def post(id): singlePost = postdb.select(1, id) config.kargs['blogTitle'] = "ទំព័រការផ្សាយ" config.kargs['post'] = singlePost return template('post', 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: cursor.execute("SELECT * FROM POST WHERE ID = '" + str(id) +"'") else: cursor.execute("SELECT * FROM POST ORDER BY POSTDATE, POSTTIME 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
<!--views/post.tpl--> %include('./partials/header.tpl') <style> #content{ min-height: 350px; background: lavender; box-sizing: border-box; } #content article{ background: rgb(247, 247, 248); min-height: 350px; padding: 20px; } #content .post-title{ font: 20px/1.5 Limonf3, Oswald; } #content .post-author{ font: bold 14px/1.5 "Lucida Sans"; margin: 10px 0; } #content .post-date{ float: right; font: bold 18px/1.5 'Lucida Sans'; } #content .post-body{ line-height: 1.75; } #content .post-body .image{ padding: 0; margin: 0; } #content .post-body img{ width: 100%; } </style> <div id='main' class='main region'> %include('./partials/sidebar.tpl') <section id='content' class='content'> <article class="post"> %if data['post']: <div class='post-header'> <span class="post-title">{{data['post'][0][1]}}</span> <span class="post-date">{{data['post'][0][3].strftime("%d-%m-%Y")}}</span> </div> <div class="post-author">{{data['post'][0][2]}}</div> <div class="post-body">{{!data['post'][0][6]}}</div> %end <div id="disqus_thread"></div> <script> (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://khmer-js.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })(); </script> <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> </article> </section><!--content--> </div><!--main--> %include('./partials/footer.tpl')
GitHub: https://github.com/Sokhavuth/kwblog
Heroku: https://khmerweb-kwblog.herokuapp.com/