Adding Setting Functionality 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
#config.py
from models import settingdb
 
setting = settingdb.createTable()
 
kargs = {}
kargs['message'] = ''
kargs['page'] = 1
 
def reset(setting):
  kargs['blogTitle'] = setting[0]
  kargs['secretKey'] = setting[1]
  kargs['dashboardPostLimit'] = setting[2]
  kargs['frontPagePostLimit'] = setting[3]
  kargs['homePagePostLimit'] = setting[4]
  kargs['authorPagePostLimit'] = setting[5]
  kargs['blogDescription'] = setting[6]
 
if not setting:
  settingdb.insert('គេហទំព័រ​ខ្មែរអង្គរ', 'h4!#au%8tb_9@oe+c0te=g=u%cfxb8t8fy%7+(gx2+51!t*b+s', 5, 8, 12, 16, 'Description' )
  setting = settingdb.select()
   
reset(setting)
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
#models/settingdb.py
import os
import psycopg2
 
def connect():
  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()
 
  return (cursor, conn)
 
def createTable():
  cursor, conn = connect()
   
  SQL = '''CREATE TABLE IF NOT EXISTS SETTING(
  BLOGTITLE TEXT,
  SECRETKEY TEXT,
  DASHBOARDPOSTLIMIT INT,
  FRONTPAGEPOSTLIMIT INT,
  HOMEPAGEPOSTLIMIT INT,
  AUTHORPAGEPOSTLIMIT INT,
  SITEDESCRIPTION TEXT
  )'''
 
  cursor.execute(SQL)
   
  cursor.execute("SELECT * FROM SETTING LIMIT 1")
  result = cursor.fetchone()
  conn.commit()
  conn.close()
  return result
 
def insert(*setting):
  cursor, conn = connect()
 
  SQL = "INSERT INTO SETTING VALUES %s"
  cursor.execute(SQL, (setting,))
   
  conn.commit()
  conn.close()
 
def select():
  createTable()
  cursor, conn = connect()
 
  cursor.execute("SELECT * FROM SETTING LIMIT 1")
  result = cursor.fetchone()
 
  conn.commit()
  conn.close()
  return result
 
def update(*args):
  cursor, conn = connect()
 
  sql = '''UPDATE SETTING SET
  BLOGTITLE = %s,
  SECRETKEY = %s,
  DASHBOARDPOSTLIMIT = %s,
  FRONTPAGEPOSTLIMIT = %s,
  HOMEPAGEPOSTLIMIT = %s,
  AUTHORPAGEPOSTLIMIT = %s,
  SITEDESCRIPTION = %s
   '''
   
  cursor.execute(sql, args)
   
  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
#controllers/setting.py
import config, lib
from bottle import route, template, request, redirect, response
from models import settingdb, userdb, postdb
 
@route('/setting', method="POST")
def posting():
  author = request.get_cookie("logged-in", secret=config.kargs['secretKey'])
  if ((author != "Guest") and userdb.checkAdmin(author)):
    blogTitle = request.forms.getunicode('fblog-title')
    secretKey = request.forms.getunicode('fsecret-key')
    dpostLimit = request.forms.getunicode('fdpost-limit')
    fpostLimit = request.forms.getunicode('ffpost-limit')
    hpostLimit = request.forms.getunicode('fhpost-limit')
    apostLimit = request.forms.getunicode('fapost-limit')
    blogDescription = request.forms.getunicode('fblog-description')
 
    settingdb.update(blogTitle, secretKey, dpostLimit, fpostLimit, hpostLimit, apostLimit, blogDescription)
 
  redirect('/login')
 
@route('/setting')
def edit():
  author = request.get_cookie("logged-in", secret=config.kargs['secretKey'])
  if ((author != "Guest") and userdb.checkAdmin(author)):
    config.kargs['blogTitle'] = "ទំព័រ​កែ​តំរូវ"
    config.kargs['posts'] = postdb.select(config.kargs['dashboardPostLimit'])
    config.kargs['thumbs'] = lib.getPostThumbs(config.kargs['posts'])
    config.kargs['post'] = settingdb.select()
    config.kargs['page'] = 1
    return template('dashboard/setting', data=config.kargs)
   
  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
<!--views/dashboard/setting.tpl-->
%include('./dashboard/partials/header.tpl')
 
<style>
  #content{
    min-height: 350px;
    background: lavender;
    padding: 20px;
    box-sizing: border-box;
  }
  #content #setting{
    display: grid;
    grid-template-columns: 17% 50%;
    grid-gap: 5px;
    align-items: center;
  }
  #content #setting span{
    text-align: right;
    font: 14px/1.5 Oswald, Bayon;
  }
  #content #setting input{
    padding: 5px 10px;
    font: 14px/1.5 Arial, OdorMeanChey;
  }
</style>
 
<div  id='main' class='main region'>
 
  %include('./dashboard/partials/sidebar.tpl')
 
  <section id='content' class='content'>
     
    <form id="setting" action="/setting" method="post">
      <span>Blog Title:</span><input type="text" name="fblog-title" value="{{data['post'][0]}}" required />
      <span>Secret Key:</span><input type="text" name="fsecret-key" value="{{data['post'][1]}}" required/>
      <span>Dashboard Post Limit:</span><input type="text" name="fdpost-limit" value="{{data['post'][2]}}" required />
      <span>Frontend Post Limit:</span><input type="text" name="ffpost-limit" value="{{data['post'][3]}}" required />
      <span>Home Post Limit:</span><input type="text" name="fhpost-limit" value="{{data['post'][4]}}" required />
      <span>Author Post Limit:</span><input type="text" name="fapost-limit" value="{{data['post'][5]}}" required />
      <span>Blog Description:</span><input type="text" name="fblog-description" value="{{data['post'][6]}}" required />
      <a></a><input type="submit" value="បញ្ចូល​ទិន្នន័យ" />
    </form>
    <div style="text-align: center;">{{data["message"]}}</div>
    %data['message'] = ""
 
  </section><!--content-->
</div><!--main-->
 
%include('./dashboard/partials/footer.tpl')

GitHub: https://github.com/Sokhavuth/kwblog
Heroku: https://khmerweb-kwblog.herokuapp.com/