Creating Post Database
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
#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')
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
#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
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
<!--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/