#controllers/login.py import os, config, lib, uuid from pytz import timezone from datetime import datetime from bottle import route, template, request, response, redirect from models import userdb, postdb, categorydb def checkLogin(username, password): if (username == 'Guest') and (password == 'password'): return True elif userdb.check(username,password): return True else: return False def getTimeZone(): khtz = timezone('Asia/Phnom_Penh') date = datetime.now().astimezone(tz=khtz).strftime('%d-%m-%Y') time = datetime.now().astimezone(tz=khtz).strftime('%H:%M:%S') return (date, time) @route('/signup', method="POST") def signup(): username = request.forms.get('fusername') password = request.forms.get('fpassword') rights = request.forms.get('frights') email = request.forms.get('femail') userdb.insert(username, password, rights, email) redirect('/login') @route('/upload') def upload(): return template('dashboard/upload', data=config.kargs) @route('/upload', method='POST') def saveFile(): upload = request.files.get('fupload') name, ext = os.path.splitext(upload.filename) if ext not in ('.png','.jpg','.jpeg'): return 'File extension not allowed.' upload.filename = str(uuid.uuid4().int) + ext ROOT_DIR = os.path.dirname(os.path.abspath("config.py")) savePath = ROOT_DIR + "/public/images/uploads/" config.kargs['uploadUrl'] = "/static/images/uploads/" + upload.filename upload.save(savePath) return template('dashboard/uploadurl', data=config.kargs) @route('/login', method="POST") def user(): username = request.forms.get('fusername') password = request.forms.get('fpassword') if checkLogin(username, password): response.set_cookie("logged-in", username, secret=config.kargs['secretKey']) redirect('/login') @route('/login') def login(): user = userdb.createTable() username = request.get_cookie("logged-in", secret=config.kargs['secretKey']) if not user: return template('dashboard/signup', data=config.kargs) elif username: config.kargs['author'] = username config.kargs['blogTitle'] = "ទំព័រគ្រប់គ្រង" config.kargs['datetime'] = getTimeZone() config.kargs['posts'] = postdb.select(config.kargs['dashboardPostLimit']) config.kargs['categories'] = categorydb.select(amount="all") config.kargs['thumbs'] = lib.getPostThumbs(config.kargs['posts']) config.kargs['page'] = 1 return template('dashboard/home', data=config.kargs) else: return template('login', data=config.kargs)
<!--views/dashboard/upload.tpl--> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>{{data['blogTitle']}}</title> <link href="/static/styles/upload.css" rel="stylesheet"></link> <link href="/static/images/site_logo.png" rel="icon" ></link> <link href="/static/fonts/setup.css" rel="stylesheet"></link> </head> <body> <form id='upload' action="/upload" method="POST" enctype="multipart/form-data"> <a>ជ្រើសរើសឯកសារៈ</a><input type="file" name="fupload" /> <a></a><input type='submit' value="ចំលងទុកក្នុងគេហទំព័រ" /> </form> </body> </html>
<!--views/dashboard/uploadurl.tpl--> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>{{data['blogTitle']}}</title> <script src="/static/scripts/jQuery.js"></script> <link href="/static/styles/upload.css" rel="stylesheet"></link> <link href="/static/images/site_logo.png" rel="icon" ></link> <link href="/static/fonts/setup.css" rel="stylesheet"></link> </head> <body> <div id="upload"> <input id="copy" type="button" value="ចំលងតំណរភ្ជាប់" /><input class="url" type="text" value="{{data['uploadUrl']}}" /> <script> $('#copy').click(function(){ $(this).siblings('input.url').select(); document.execCommand("copy"); window.close(); }); </script> </div> </body> </html>
/*public/css/login.css*/ body{ background: lightgrey; position: relative; } #upload{ width: 500px; background: lavender; margin:30px auto; padding: 10px; display: grid; grid-template-columns: 140px auto; grid-gap: 10px; font: 14px/1.5 OdorMeanChey; position: absolute; left: 50%; transform: translate(-50%,0); } #upload input{ padding: 10px; } #upload input[type=file]{ border: 1px solid black; } #upload input[type=button], #upload input[type=submit]{ font: 14px/1.5 OdorMeanChey; } #upload a{ text-align: right; padding: 10px 0; } @media only screen and (max-width: 600px){ #upload{ grid-template-columns: 100%; width: 100%; } #upload a{ text-align: left; padding: 0; } }
GitHub: https://github.com/Sokhavuth/kwblog
Heroku: https://khmerweb-kwblog.herokuapp.com/