Adding Edit/Delete Functionalities to Dashboard
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
#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)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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()
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
<!--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/