Support assigning macs to existing items
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2024-11-21 12:42:07 +02:00
parent 4fa4edb637
commit 5c134ed566
2 changed files with 40 additions and 2 deletions

View File

@ -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/<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/<item_id>/view")
@ -403,6 +407,7 @@ def delete_thumbs(item):
})
@page_inventory.route("/m/inventory/assign-mac/<slug>")
@page_inventory.route("/m/inventory/assign-slug/<slug>")
@page_inventory.route("/m/inventory/clone-with-slug/<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/<item_id>/set-mac/<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/<item_id>/audit", methods=["POST"])
@login_required(groups=["k-space:inventory:audit"])
def view_inventory_audit(item_id):

View File

@ -19,7 +19,7 @@
<td>{% if item.inventory.owner %}<a href="/m/user/{{ item.inventory.owner.username }}">{{ item.inventory.owner.username | display_name }}</a>{% endif %}</td>
<td>{% if item.inventory.user %}<a href="/m/user/{{ item.inventory.user.username }}">{{ item.inventory.user.username | display_name }}</a>{% endif %}</td>
<td>
<form action="/m/inventory/{{item._id}}/{{action_path}}/{{slug}}" method="get">
<form action="/m/inventory/{{item._id}}/{{action_path}}/{{slug}}" method="{{action_method}}">
<button class="waves-effect waves-light btn" type="submit">{{action_label}}</button>
</form>
</td>