-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (38 loc) · 1.29 KB
/
app.py
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
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from wakeonlan import send_magic_packet
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class item(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
mac = db.Column(db.String(30))
@app.route("/")
def index():
item_list = item.query.all()
return render_template("base.html", item_list=item_list)
@app.route("/add", methods=["POST"])
def add():
title = request.form.get("title")
mac = request.form.get("mac")
new_item = item(title=title, mac=mac)
db.session.add(new_item)
db.session.commit()
return redirect(url_for("index"))
@app.route("/wake/<int:item_id>")
def wake(item_id):
wol = item.query.filter_by(id=item_id).first()
send_magic_packet(wol.mac)
db.session.commit()
return redirect(url_for("index"))
@app.route("/delete/<int:item_id>")
def delete(item_id):
wol = item.query.filter_by(id=item_id).first()
db.session.delete(wol)
db.session.commit()
return redirect(url_for("index"))
if __name__ == "__main__":
db.create_all()
app.run(host="0.0.0.0", debug=True)