diff --git a/inventory-app/inventory.py b/inventory-app/inventory.py index dc26fc8..2d6f89d 100644 --- a/inventory-app/inventory.py +++ b/inventory-app/inventory.py @@ -1,3 +1,4 @@ +import re import boto3 import pymongo import urllib @@ -24,9 +25,12 @@ channel = "inventory" @login_required @page_inventory.route("/m/inventory/by-mac/", methods=['GET']) def view_inventory_by_mac(mac): + if not check_mac_address_valid(mac): + return "Invalid mac address", 400 + mac = mac.lower() item = db.inventory.find_one({ "mac": mac }, projection = { "_id": 1}) if not item or not item.get("_id", False): - return abort(404) + return redirect("/m/inventory/assign-mac/%s" % mac) return redirect(url_for("inventory.view_inventory_view", item_id = item["_id"])) @page_inventory.route("/m/inventory//view") @@ -403,6 +407,7 @@ def delete_thumbs(item): }) +@page_inventory.route("/m/inventory/assign-mac/") @page_inventory.route("/m/inventory/assign-slug/") @page_inventory.route("/m/inventory/clone-with-slug/") @page_inventory.route("/m/inventory") @@ -437,16 +442,25 @@ def view_inventory(slug=None): if slug and not public_view: template = "inventory_pick.html" if request.path.startswith("/m/inventory/clone-with-slug"): + action_method = "get" action_path = "clone-by-slug" action_label = "Clone" action_help = "Cloning item to new slug" pick = True elif request.path.startswith("/m/inventory/assign-slug"): q.update({"shortener.slug": None}) + action_method = "get" action_path = "edit-by-slug" action_label = "Assign slug" action_help = "Assigning slug" pick = True + elif request.path.startswith("/m/inventory/assign-mac"): + q.update({"mac": None}) + action_method = "post" + action_path = "set-mac" + action_label = "Assign MAC" + action_help = "Assigning MAC" + pick = True if grid: template = "inventory_grid.html" if user: @@ -468,6 +482,30 @@ def view_inventory(slug=None): return render_template(template, **locals()) +@page_inventory.route("/m/inventory//set-mac/", methods=['POST']) +@login_required +def view_set_mac(item_id, mac): + if not check_mac_address_valid(mac): + return abort(400) + + result = db.inventory.update_one({ + "_id": ObjectId(item_id), + "mac": {"$exists": False} + }, { + "$set": { + "mac" : mac.lower(), + }, + }) + + if result.matched_count > 0: + return redirect("/m/inventory/%s/view" % item_id) + else: + return abort(404) + +def check_mac_address_valid(mac): + mac_regex = re.compile(r'^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$') + return bool(mac_regex.match(mac)) + @page_inventory.route("/m/inventory//audit", methods=["POST"]) @login_required(groups=["k-space:inventory:audit"]) def view_inventory_audit(item_id): diff --git a/inventory-app/templates/inventory_pick.html b/inventory-app/templates/inventory_pick.html index 52dbdd5..e9c4e12 100644 --- a/inventory-app/templates/inventory_pick.html +++ b/inventory-app/templates/inventory_pick.html @@ -19,7 +19,7 @@ {% if item.inventory.owner %}{{ item.inventory.owner.username | display_name }}{% endif %} {% if item.inventory.user %}{{ item.inventory.user.username | display_name }}{% endif %} -
+