From 2f3fa67049aa8ca37bab502b09f90864271b7c62 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 1 Feb 2019 20:28:37 +0000 Subject: [PATCH] Reduce code redundancy --- manager/templatetags/manager.py | 12 ++++++++---- manager/views.py | 7 ++----- templates/manager/addwifi.html | 3 ++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/manager/templatetags/manager.py b/manager/templatetags/manager.py index f1c95df..a860979 100644 --- a/manager/templatetags/manager.py +++ b/manager/templatetags/manager.py @@ -4,19 +4,23 @@ from django.contrib.auth.models import User register = template.Library() +@register.simple_tag(takes_context=True) +def userOrgas(context): + return sorted(context.request.user.organization_set.all(), key=lambda x: x.name) + @register.simple_tag(takes_context=True) def orgaString(context): - return ", ".join(str(orga) for orga in context.request.user.organization_set.all()) + return ", ".join(str(orga) for orga in userOrgas(context)) @register.simple_tag(takes_context=True) def userDevices(context): - return sorted(set(Device.objects.filter(organization__in=context.request.user.organization_set.all())), key=lambda x: x.serial) + return sorted(set(Device.objects.filter(organization__in=userOrgas(context))), key=lambda x: x.serial) @register.simple_tag(takes_context=True) def userWifis(context): - return sorted(set(Wifi.objects.filter(organization__in=context.request.user.organization_set.all())), key=lambda x: x.serial) + return sorted(set(Wifi.objects.filter(organization__in=userOrgas(context))), key=lambda x: x.serial) @register.simple_tag(takes_context=True) def userUsers(context): - return sorted(set(User.objects.filter(organization__in=context.request.user.organization_set.all()) if context.request.user.is_superuser else []), key=lambda x: x.username) + return sorted(set(User.objects.filter(organization__in=userOrgas(context)) if context.request.user.is_superuser else []), key=lambda x: x.username) diff --git a/manager/views.py b/manager/views.py index 5de5ffd..1102b7d 100644 --- a/manager/views.py +++ b/manager/views.py @@ -486,13 +486,10 @@ def makewifi(request): wifi_key = request.POST.get("key", "") wifi_organization = request.POST.get("organization", "") - if not wifi_serial: - orga = Organization.objects.filter(users=request.user) - + if not (wifi_serial and wifi_organization): return render(request, "manager/addwifi.html", { - "title": "Add WiFi", - "organizations": orga + "title": "Add WiFi" } ) diff --git a/templates/manager/addwifi.html b/templates/manager/addwifi.html index 5e00af6..4aee28d 100644 --- a/templates/manager/addwifi.html +++ b/templates/manager/addwifi.html @@ -1,5 +1,5 @@ {% extends "base.html" %} - +{% load manager %} {% block content %}
@@ -19,6 +19,7 @@