Creating Post Template
1
2
3
4
5
6
7
8
9
10
11
12
#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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!--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/