Reduce code redundancy

This commit is contained in:
Kumi 2019-02-01 20:28:37 +00:00
parent 4a21dc8ea1
commit 2f3fa67049
3 changed files with 12 additions and 10 deletions

View file

@ -4,19 +4,23 @@ from django.contrib.auth.models import User
register = template.Library() 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) @register.simple_tag(takes_context=True)
def orgaString(context): 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) @register.simple_tag(takes_context=True)
def userDevices(context): 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) @register.simple_tag(takes_context=True)
def userWifis(context): 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) @register.simple_tag(takes_context=True)
def userUsers(context): 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)

View file

@ -486,13 +486,10 @@ def makewifi(request):
wifi_key = request.POST.get("key", "") wifi_key = request.POST.get("key", "")
wifi_organization = request.POST.get("organization", "") wifi_organization = request.POST.get("organization", "")
if not wifi_serial: if not (wifi_serial and wifi_organization):
orga = Organization.objects.filter(users=request.user)
return render(request, "manager/addwifi.html", return render(request, "manager/addwifi.html",
{ {
"title": "Add WiFi", "title": "Add WiFi"
"organizations": orga
} }
) )

View file

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load manager %}
{% block content %} {% block content %}
<form action="#" method="post"> <form action="#" method="post">
@ -19,6 +19,7 @@
<div class="form-group"> <div class="form-group">
<label for="organization">Organization</label> <label for="organization">Organization</label>
<select class="custom-select mr-sm-2" id="organization" name="organization"> <select class="custom-select mr-sm-2" id="organization" name="organization">
{% userOrgas as organizations %}
{% for choice in organizations %} {% for choice in organizations %}
<option value="{{ choice.id }}">{{ choice.name }}</option> <option value="{{ choice.id }}">{{ choice.name }}</option>
{% endfor %} {% endfor %}