implemented the enable button

This commit is contained in:
b3yond 2018-03-16 12:41:34 +01:00
parent 79f301d823
commit 26d1282413
4 changed files with 81 additions and 9 deletions

View file

@ -111,6 +111,7 @@ def confirmaccount(encoded_jwt):
db.cur.execute("INSERT INTO user(email, pass_hashed, enabled) VALUES(?, ?, ?);", (uname, pass_hashed, 1)) db.cur.execute("INSERT INTO user(email, pass_hashed, enabled) VALUES(?, ?, ?);", (uname, pass_hashed, 1))
db.conn.commit() db.conn.commit()
bottle.response.set_cookie("account", uname, secret) bottle.response.set_cookie("account", uname, secret)
bottle.response.set_cookie("enabled", "True")
return bottle.redirect("/settings") return bottle.redirect("/settings")
@ -122,17 +123,43 @@ def manage_bot():
""" """
uname = bottle.request.get_cookie("account", secret=secret) uname = bottle.request.get_cookie("account", secret=secret)
if uname is not None: if uname is not None:
db.cur.execute("SELECT enabled FROM user WHERE email=?;", (uname,))
try:
enabled = db.cur.fetchone()[0]
except TypeError:
return "Wrong Credentials." # no user with this email
# Set Enable Status with a Cookie
if enabled:
bottle.response.set_cookie("enabled", "True")
else:
bottle.response.set_cookie("enabled", "False")
return bottle.static_file("../static/bot.html", root='../static') return bottle.static_file("../static/bot.html", root='../static')
else: else:
bottle.abort(401, "Sorry, access denied.") bottle.abort(401, "Sorry, access denied.")
@app.route('/enable') @app.route('/enable', method="POST")
def enable(): def enable():
"""
Enable the bot. Called by the Enable button in bot.html
:return: redirect to settings page
"""
email = bottle.request.get_cookie("account", secret=secret) email = bottle.request.get_cookie("account", secret=secret)
db.cur.execute("MODIFY user.enabled = 1 WHERE email=?;", (email)) # :todo is this correct SQL? db.cur.execute("UPDATE user SET enabled = 1 WHERE email=?;", (email,)) # :todo is this correct SQL?
db.conn.commit() db.conn.commit()
return bottle.static_file("../static/bot.html", root='../static') bottle.response.set_cookie("enabled", "True")
return bottle.redirect("/settings")
@app.route('/disable', method="POST")
def disable():
"""
Disable the bot. Called by the Disable button in bot.html
:return: redirect to settings page
"""
email = bottle.request.get_cookie("account", secret=secret)
db.cur.execute("UPDATE user SET enabled = 0 WHERE email=?;", (email,)) # :todo is this correct SQL?
db.conn.commit()
bottle.response.set_cookie("enabled", "False")
return bottle.redirect("/settings")
@app.route('/static/<filename:path>') @app.route('/static/<filename:path>')
def static(filename): def static(filename):

View file

@ -11,14 +11,21 @@
<div class="area"> <div class="area">
<h1><a href="/"><img src="/static/img/ticketfrei_logo.png" alt="Ticketfrei" height="150px" align="center" style="float: none;"></a></h1> <h1><a href="/"><img src="/static/img/ticketfrei_logo.png" alt="Ticketfrei" height="150px" align="center" style="float: none;"></a></h1>
<form action="/enable" method="POST"> <div id="enablebutton" style="float: right; padding: 2em;">asdf</div>
<button type="submit">Enable</button>
</form>
<a class='button' href="/login/twitter">
<picture>
<source type='image/webp' sizes='20px' srcset="/static-cb/1517673283/twitter-20.webp 20w,/static-cb/1517673283/twitter-40.webp 40w,/static-cb/1517673283/twitter-80.webp 80w,"/>
<source type='image/png' sizes='20px' srcset="/static-cb/1517673283/twitter-20.png 20w,/static-cb/1517673283/twitter-40.png 40w,/static-cb/1517673283/twitter-80.png 80w,"/>
<img src="/static-cb/1517673283/twitter-20.png" alt="" />
</picture>
Log in with Twitter
</a>
<script src="/static/js/functions.js"></script>
<div class=footer> <div class=footer>
Contribute on <a href="https://github.com/b3yond/ticketfrei">GitHub!</a> Contribute on <a href="https://github.com/b3yond/ticketfrei">GitHub!</a>
</div> </div>
</div> </div>
<!--<div class="background" style="background-image: url(static/img/bg_right.jpg)"></div>-->
</body> </body>

View file

@ -47,6 +47,16 @@ button {
font-size: 120%; font-size: 120%;
} }
a.button {
background-color: #1da1f2;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
font-size: 120%;
}
button:hover { button:hover {
opacity: 0.8; opacity: 0.8;
} }
@ -71,8 +81,8 @@ input[type=text], input[type=password] {
.footer { .footer {
padding: 2em; padding: 2em;
bottom: 0; bottom: 0;
text-align: center; float: center;
width: 540px; width: 240px;
height: 30px; height: 30px;
flex-shrink: 0; flex-shrink: 0;
} }

28
static/js/functions.js Normal file
View file

@ -0,0 +1,28 @@
function enableButton() {
var enablebutton = '<form action="/enable" method="POST"> <button type="submit">Enable</button> </form> ';
var disablebutton = '<form action="/disable" method="POST"> <button type="submit">Disable</button> </form> ';
var enabled = getCookie('enabled');
if (enabled == "True") {
return disablebutton;
} else {
return enablebutton;
}
}
function getCookie(cname) {
var name = cname + '=';
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
document.getElementById("enablebutton").innerHTML = enableButton();