More code optimization

This commit is contained in:
Kumi 2019-02-01 20:38:29 +00:00
parent 2f3fa67049
commit 1ed1764d45

View file

@ -4,23 +4,32 @@ from django.contrib.auth.models import User
register = template.Library()
def orgaObjects(cls, orga):
return sorted(set(cls.objects.filter(organization__in=orga)), key=lambda x: str(x))
def directUserOrgas(user):
return sorted(user.organization_set.all(), key=lambda x: str(x))
def directOrgaString(user):
return ", ".join(str(orga) for orga in directUserOrgas(user))
@register.simple_tag(takes_context=True)
def userOrgas(context):
return sorted(context.request.user.organization_set.all(), key=lambda x: x.name)
return directUserOrgas(context.request.user)
@register.simple_tag(takes_context=True)
def orgaString(context):
return ", ".join(str(orga) for orga in userOrgas(context))
return directOrgaString(context.request.user)
@register.simple_tag(takes_context=True)
def userDevices(context):
return sorted(set(Device.objects.filter(organization__in=userOrgas(context))), key=lambda x: x.serial)
return orgaObjects(Device, userOrgas(context))
@register.simple_tag(takes_context=True)
def userWifis(context):
return sorted(set(Wifi.objects.filter(organization__in=userOrgas(context))), key=lambda x: x.serial)
return orgaObjects(Wifi, userOrgas(context))
@register.simple_tag(takes_context=True)
def userUsers(context):
return sorted(set(User.objects.filter(organization__in=userOrgas(context)) if context.request.user.is_superuser else []), key=lambda x: x.username)
return orgaObjects(User, userOrgas(context)) if context.request.user.is_staff else []