Implemented User Management
This commit is contained in:
62
app.py
62
app.py
@@ -142,6 +142,64 @@ def login():
|
||||
return redirect(url_for(session.get("main", "index_ade")))
|
||||
|
||||
|
||||
@app.route("/register", methods=["GET", "POST"])
|
||||
def register():
|
||||
if request.method == "POST":
|
||||
act = request.form.get("action")
|
||||
match act:
|
||||
case "create":
|
||||
username = request.form["username"]
|
||||
password = request.form["password"]
|
||||
udata = db.users.find_one({"id": username})
|
||||
if udata or not username or not password:
|
||||
abort(400)
|
||||
salt = urandom(32)
|
||||
hash = scrypt(password.encode("utf-8"), salt=salt, **SCRYPT_PARAMS)
|
||||
db.users.insert_one(
|
||||
{
|
||||
"id": username,
|
||||
"hash": hash,
|
||||
"salt": salt,
|
||||
"perm": [],
|
||||
"quad": ["ade"],
|
||||
}
|
||||
)
|
||||
case "edit":
|
||||
for entry in request.form:
|
||||
if entry.count("/") == 2:
|
||||
ptype, pid, uid = entry.split("/")
|
||||
value = request.form[entry]
|
||||
if ptype == "perm":
|
||||
cur_perms = db.users.find_one(
|
||||
{"_id": ObjectId(uid)}, {"perm": 1}
|
||||
).get("perm", [])
|
||||
if value == "True":
|
||||
if pid not in cur_perms:
|
||||
cur_perms.append(pid)
|
||||
else:
|
||||
if pid in cur_perms:
|
||||
cur_perms.remove(pid)
|
||||
db.users.update_one(
|
||||
{"_id": ObjectId(uid)}, {"$set": {"perm": cur_perms}}
|
||||
)
|
||||
if ptype == "quad":
|
||||
cur_quads = db.users.find_one(
|
||||
{"_id": ObjectId(uid)}, {"quad": 1}
|
||||
).get("quad", [])
|
||||
if value == "True":
|
||||
if pid not in cur_quads:
|
||||
cur_quads.append(pid)
|
||||
else:
|
||||
if pid in cur_quads:
|
||||
cur_quads.remove(pid)
|
||||
db.users.update_one(
|
||||
{"_id": ObjectId(uid)}, {"$set": {"quad": cur_quads}}
|
||||
)
|
||||
udatas = list(db.users.find())
|
||||
ddatas = sorted(db.domains.find({"public": False}), key=lambda ddata: ddata["id"])
|
||||
return render_template("register.html", udatas=udatas, ddatas=ddatas)
|
||||
|
||||
|
||||
@app.route("/search")
|
||||
def search():
|
||||
stype = request.args.get("search-type")
|
||||
@@ -347,6 +405,8 @@ def lighting():
|
||||
result[-1] = {"name": "Ungrouped", "lights": {}}
|
||||
for light in raw["lights"]:
|
||||
ldata = raw["lights"][light]
|
||||
if not ldata["state"]["reachable"]:
|
||||
continue
|
||||
if not ldata["state"]["on"]:
|
||||
color = "#000000"
|
||||
elif ldata["state"].get("colormode") == "hs":
|
||||
@@ -361,6 +421,6 @@ def lighting():
|
||||
ungrouped = False
|
||||
if ungrouped:
|
||||
result[-1]["lights"][light] = ldata
|
||||
return render_template("lighting.html", ldata=result)
|
||||
return render_template("lighting.html", ldatas=result)
|
||||
except requests.exceptions.ConnectionError:
|
||||
abort(500)
|
||||
|
||||
Reference in New Issue
Block a user