diff --git a/manager/templatetags/manager.py b/manager/templatetags/manager.py index ecb4712..b22d039 100644 --- a/manager/templatetags/manager.py +++ b/manager/templatetags/manager.py @@ -1,8 +1,22 @@ from django import template -from manager.models import Organization +from manager.models import Organization, Device, Wifi +from django.contrib.auth.models import User register = template.Library() -@register.simple_tag -def orgaString(user): - return ", ".join(str(orga) for orga in user.organization_set.all()) +@register.simple_tag(takes_context=True) +def orgaString(context): + return ", ".join(str(orga) for orga in context.request.user.organization_set.all()) + +@register.simple_tag(takes_context=True) +def userDevices(context): + return set(Device.objects.filter(organization__in=context.request.user.organization_set.all())) + +@register.simple_tag(takes_context=True) +def userWifis(context): + return set(Wifi.objects.filter(organization__in=context.request.user.organization_set.all())) + +@register.simple_tag(takes_context=True) +def userUsers(context): + return set(User.objects.filter(organization__in=context.request.user.organization_set.all()) if context.request.user.is_superuser else []) + diff --git a/manager/views.py b/manager/views.py index 5391670..dd12538 100644 --- a/manager/views.py +++ b/manager/views.py @@ -255,22 +255,10 @@ def ping(request, device_id): @login_required def devices(request): - devices = set() - wifis = set() - users = set() - - for organization in request.user.organization_set.all(): - devices = devices.union(organization.device_set.all()) - wifis = wifis.union(organization.wifi_set.all()) - users = users.union(organization.users.all()) if request.user.is_superuser else users - return render(request, "manager/index.html", { "title": "Device Administration", - "user": request.user, - "devices": sorted(devices, key=lambda x: x.serial), - "wifis": sorted(wifis, key=lambda x: x.serial), - "users": sorted(users, key=lambda x: x.username) + "user": request.user } ) @@ -514,14 +502,14 @@ def makewifi(request): } ) - wifi = Wifi.objects.create( + wifi = Wifi.objects.create( serial = wifi_serial, ssid = wifi_ssid, key = wifi_key, organization = Organization.objects.filter(id=wifi_organization)[0] ) - return redirect("/") + return redirect("/") @user_passes_test(is_superuser) def makedevice(request): diff --git a/templates/manager/index.html b/templates/manager/index.html index 888bf68..e6be1f9 100644 --- a/templates/manager/index.html +++ b/templates/manager/index.html @@ -3,7 +3,7 @@ {% load manager %} {% block content %} -

Organization: {% orgaString user %}

+

Organization: {% orgaString %}

User: {{ user.first_name }} {{ user.last_name }} ({{ user.username }})

Manage: DevicesWiFi{% if user.is_staff %} ‐ Users{% endif %}
@@ -25,6 +25,7 @@ + {% userDevices as devices %} {% for device in devices %}
 
{{ device.serial }}
@@ -52,6 +53,7 @@ + {% userWifis as wifis %} {% for wifi in wifis %}
{{ wifi.serial }}
@@ -64,7 +66,7 @@ -{% if users %} +{% if request.user.is_staff %}

Users

@@ -79,6 +81,7 @@ + {% userUsers as users %} {% for auser in users %}
{% if auser.is_staff %}{% endif %}{{ auser.username }}{% if auser.is_staff %}{% endif %}