# -*- coding: utf-8 -*-
from django import template
from django.db.models.query import QuerySet
import datetime
from django import template
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from django.db.models import Q

register = template.Library()
@register.inclusion_tag('sidebar_tree.html')
def children_tag(person):
    if isinstance(person, QuerySet):
        children = person
    else:
        children = person.children.all()
    #zip(children,map(children.content_type,children)
    return {'children': children}

@register.filter(name='has_group')
def has_group(user, group_name):
    return user.groups.filter(name=group_name).exists()

@register.filter(name='get_staff')
def get_staff(group_name):
    group = Group.objects.get(name=group_name)
    users = group.user_set.all()
    return map(lambda x:x.id , users)


@register.filter(name='get_nameFromId')
def get_nameFromId(usrId):
    if usrId:
        user = User.objects.get(id=usrId)
        return user.first_name+user.last_name
    else:
        return ""
    
@register.filter
def get_range( value ):
  """
    Filter - returns a list containing range made from given value
    Usage (in template):

    <ul>{% for i in 3|get_range %}
      <li>{{ i }}. Do something</li>
    {% endfor %}</ul>

    Results with the HTML:
    <ul>
      <li>0. Do something</li>
      <li>1. Do something</li>
      <li>2. Do something</li>
    </ul>

    Instead of 3 one may use the variable set in the views
  """
  return range( value )
  
@register.filter
def get_fields(model):
    return model._meta.get_fields()