#controllers/posting.py import config import datetime, uuid from bottle import route, template, request, redirect, response from models import postdb @route('/posting', method="POST") def posting(): author = request.get_cookie("logged-in", secret=config.kargs['secretKey']) if ((author != "Guest") and postdb.check(author)): title = request.forms.getunicode('fpost-title') if title == "": title = "untitled" postdate = request.forms.getunicode('fpost-date') posttime = request.forms.getunicode('fpost-time') category = request.forms.getunicode('fcategory') content = request.forms.getunicode('fcontent') try: postdate = datetime.datetime.strptime(postdate, "%d-%m-%Y") except ValueError: config.kargs['message'] = 'ទំរង់កាលបរិច្ឆេទមិនត្រឹមត្រូវ!' return template('dashboard/home', data=config.kargs) try: datetime.datetime.strptime(posttime, "%H:%M:%S") except ValueError: config.kargs['message'] = 'ទំរង់ពេលវេលាមិនត្រឹមត្រូវ!' return template('dashboard/home', data=config.kargs) postdb.insert(str(uuid.uuid4().int), title, author, postdate, posttime, category, content) redirect('/login')
#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): 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("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/dashboard/footer.tpl--> <style> .footer{ min-height: 150px; background: lavender; padding: 20px; box-sizing: border-box; margin: 15px auto; } .post-panel{ padding: 10px; background: rgb(247, 247, 248); font: 16px/1.5 Limonf3, Oswald; display: grid; grid-template-columns: 150px auto 15%; grid-gap: 10px; margin-bottom: 15px; } .post-panel:last-child{ margin-bottom: 0; } .post-panel .post-thumb img{ max-width: 100%; float: left; } .post-panel .post-title{ text-align: left; display: block; } .post-panel .post-author{ text-align: right; } .post-panel .post-date{ font: bold 14px/1.5 'Lucida Sans'; } </style> <footer class="footer region"> %if data['posts']: %for post in data['posts']: <div class="post-panel"> <a class="post-thumb" href="/post/{{post[0]}}"><img src="/static/images/nopicture.jpg" /></a> <div class="title-wrapper"> <a class="post-title" href="/post/{{post[0]}}">{{post[1]}}</a> %postdate = post[3].strftime("%d-%m-%Y") <div class="post-date">{{postdate}}</div> <a class="post-category">{{post[5]}}</a> </div> <a class="post-author" href="/author/{{post[2]}}">{{post[2]}}</a> </div> %end %end </footer> </div><!--site--> </body> </html>
GitHub: https://github.com/Sokhavuth/kwblog
Heroku: https://khmerweb-kwblog.herokuapp.com/