# -*- coding: utf-8 -*-

from django import template

from organizations.models import Organization
from organizations.utils import get_users_organizations
from organizations.utils import get_custom_settings_for_current_organization

register = template.Library()


@register.inclusion_tag('organizations/organization_users.html', takes_context=True)
def organization_users(context, org):
    context.update({'organization_users': org.organization_users.all()})
    return context


@register.assignment_tag
def users_organizations(user):
    """
    Returns all organizations, in wich the user is member.
    Use in Template:
    {% load org_tags %}
    {% users_organizations request.user as my_orgs %}
    """
    if not user or not user.is_authenticated():
        return None
    return get_users_organizations(user)

@register.assignment_tag
def custom_settings_for_current_org(request, kw, fallback):
    """
    Access custom settings in templates.

    {% load org_tags %}
    {% custom_settings_for_current_org request 'foo' 'bar' as custom_setting_kw %}
    {{ custom_setting_kw }}
    """
    if not request.user or not request.user.is_authenticated():
        return None
    return get_custom_settings_for_current_organization(request, kw, fallback)


@register.assignment_tag
def orgname_for_slug(slug):
    """
    Returns organizations name for a given slug.
    Use in Template:
    {% load org_tags %}
    {% orgname_for_slug request.current_organization %}
    """
    try:
        return Organization.objects.get(slug=slug).name
    except Organization.DoesNotExist:
        return None

@@ -17,17 +17,16 @@
 f-8 -*-%0A
-%0A
 from dja
@@ -479,33 +479,8 @@
 t%0A%0A%0A
-@register.assignment_tag%0A
 def 
@@ -794,33 +794,9 @@
 r)%0A%0A
-@register.assignment_tag
 %0A
+
 def 
@@ -1213,33 +1213,8 @@
 )%0A%0A%0A
-@register.assignment_tag%0A
 def 
@@ -1506,16 +1506,16 @@
 tExist:%0A
-
         
@@ -1506,28 +1506,357 @@
 tExist:%0A        return None%0A
+%0Atry:%0A    register.assignment_tag(users_organizations)%0A    register.assignment_tag(custom_settings_for_current_org)%0A    register.assignment_tag(orgname_for_slug)%0Aexcept AttributeError:%0A    register.simple_tag(users_organizations)%0A    register.simple_tag(custom_settings_for_current_org)%0A    register.simple_tag(orgname_for_slug)%0A

support assignment and simple_tags