from django import template
from django.contrib.auth import get_user
from lazysignup.utils import is_lazy_user
import logging

log = logging.getLogger(__name__)

register = template.Library()


@register.simple_tag(takes_context=True)
def if_in_app(context, app_name, output):
    """If this view is in app 'app_name', output 'output'"""
    current_app = context['request'].current_app
    return output if app_name == current_app else ''


@register.simple_tag(takes_context=True)
def username(context):
    """Return a sensible name for the current user."""
    user = get_user(context['request'])

    if is_lazy_user(user):
        return "Guest user"
    elif user.is_anonymous:
        return "Log in"
    else:
        return user.get_full_name()
