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.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)

View file

@ -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"
}
)

View file

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