Apply pycodestyle recommendations on template tags
This commit is contained in:
parent
8693d9e095
commit
e123331b02
4 changed files with 51 additions and 9 deletions
|
@ -4,59 +4,78 @@ from django.contrib.auth.models import User
|
|||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
def abcSort(inlist):
|
||||
return sorted(inlist, key=lambda x: str(x))
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def allOrgas():
|
||||
return abcSort(Organization.objects.all())
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def allNets():
|
||||
return abcSort(Network.objects.all())
|
||||
|
||||
|
||||
def orgaObjects(cls, orga):
|
||||
return abcSort(set(cls.objects.filter(organization__in=orga)))
|
||||
|
||||
|
||||
def directUserOrgas(user):
|
||||
return abcSort(user.organization_set.all())
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def directOrgaString(user):
|
||||
return ", ".join(str(orga) for orga in directUserOrgas(user))
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def directOrgaAjax(user):
|
||||
return "[" + ",".join(str(orga.id) for orga in directUserOrgas(user)) + "]"
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def userOrgas(context):
|
||||
return directUserOrgas(context.request.user)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def orgaString(context):
|
||||
return directOrgaString(context.request.user)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def userDevices(context):
|
||||
return orgaObjects(Device, userOrgas(context))
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def userWifis(context):
|
||||
return orgaObjects(Wifi, userOrgas(context))
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def userUsers(context):
|
||||
return orgaObjects(User, userOrgas(context))
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def userNets(context):
|
||||
return orgaObjects(Network, userOrgas(context))
|
||||
|
||||
|
||||
def netOrgas(network):
|
||||
return network.organization.all()
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def netOrgaString(network):
|
||||
return ", ".join(str(orga) for orga in network.organization.all())
|
||||
return ", ".join(str(orga) for orga in netOrgas(network))
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def netOrgaAjax(network):
|
||||
return "[" + ",".join(str(orga.id) for orga in network.organization.all()) + "]"
|
||||
return "[" + ",".join(str(orga.id) for orga in netOrgas(network)) + "]"
|
||||
|
|
|
@ -5,14 +5,27 @@ from django.db.models import Q
|
|||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.inclusion_tag('manager/notifications.html', takes_context=True)
|
||||
def getNotifications(context, login=False):
|
||||
notifications = [notification for notification in Notification.objects.filter(Q(expiry__gte = timezone.now()) | Q(expiry = None)) if (notification.on_login or (not login)) ]
|
||||
notfilter = Notification.objects.filter
|
||||
filtered = notfilter(Q(expiry__gte=timezone.now()) | Q(expiry=None))
|
||||
|
||||
notifications = []
|
||||
|
||||
for notification in filtered:
|
||||
if notification.on_login or not login:
|
||||
notifications.append(notification)
|
||||
|
||||
if context.dicts[3]["exception"]:
|
||||
exception = Notification()
|
||||
exception.status = 2
|
||||
exception.text = context.dicts[3]["exception"].human
|
||||
if context.dicts[3]["user"].is_superuser:
|
||||
exception.text += "<br><br><code>" + str(context.dicts[3]["exception"]) + "</code>"
|
||||
exception.text += (
|
||||
"<br><br><code>"
|
||||
str(context.dicts[3]["exception"])
|
||||
"</code>"
|
||||
)
|
||||
notifications = [exception] + notifications
|
||||
return {'notifications': notifications}
|
||||
|
|
|
@ -4,16 +4,24 @@ from git import Repo
|
|||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def getCommit():
|
||||
repo = Repo(settings.BASE_DIR)
|
||||
return "%s (%s)" % (str(repo.head.commit)[:7], str(repo.head.commit.committed_datetime))
|
||||
return "%s (%s)" % (
|
||||
str(repo.head.commit)[:7],
|
||||
str(repo.head.commit.committed_datetime)
|
||||
)
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def isLatestCommit():
|
||||
repo = Repo(settings.BASE_DIR)
|
||||
origin = repo.remotes[0].refs[0]
|
||||
return "" if origin.commit.committed_datetime <= repo.head.commit.committed_datetime else " – Update available!"
|
||||
if origin.commit.committed_datetime <= repo.head.commit.committed_datetime:
|
||||
return ""
|
||||
return " – Update available!"
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def isDirty():
|
||||
|
|
|
@ -5,6 +5,7 @@ from django.utils import timezone
|
|||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def updateUserStatus(user, sec=False):
|
||||
if user.is_authenticated:
|
||||
|
@ -22,6 +23,7 @@ def updateUserStatus(user, sec = False):
|
|||
return "<!-- %s -->" % str(e)
|
||||
return ""
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def setUser(context):
|
||||
return context.request.user
|
||||
|
|
Loading…
Reference in a new issue