# Path: narwhal/core/profile/models.py
# class Profile(Model):
#     user = AutoOneToOneField(User, related_name='profile')
#     key = CharField(max_length=32, default=lambda: md5(os.urandom(40)).hexdigest())
#     
#     avatar = ImageField(upload_to='profiles/avatar/', blank=True, null=True)
#     uploaded = BigIntegerField(default=0)
#     downloaded = BigIntegerField(default=0)
#     
#     # extra_data should only be used for data that should not be 
#     # searched upon, such as chat protocol addresses
#     data = JSONField(default=lambda: {})
#     
#     #def __getattr__(self, name):
#     #    try: return self.extra_data[name]
#     #    except KeyError, e: raise AttributeError, e
#     #def __setattr__(self, name, value):
#     #    self.extra_data[name] = value
#     
#     def __unicode__(self):
#         return self.user.username
#     
#     @permalink
#     def get_absolute_url(self):
#         return ('profile:profile', (), {'username': self.user.username} )
#     
#     def gravatar(self):
#         if self.user.email:
#             return "http://www.gravatar.com/avatar/%s.png?d=%s" % (
#                 md5(self.user.email.lower().strip()).hexdigest(),
#                 settings.PROFILE_GRAVATAR_STANDIN )
#         
#     def ratio(self):
#         return "%.2f"% (self.total_uploaded / float(self.total_downloaded))
# 
# Path: narwhal/core/profile/middleware.py
# USER_TRACK_ACTIVE_KEY = 'profile-user-active-track-%d'
# 
# USER_TRACK_IDLE_KEY = 'profile-user-idle-track-%d'


from django.template import Library
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.admin.models import User
from narwhal.core.profile.models import Profile
from narwhal.core.profile.middleware import USER_TRACK_ACTIVE_KEY, USER_TRACK_IDLE_KEY
import logging

register = Library()

@register.filter()
def profilelink(user):
    if isinstance(user, User):
        try:
            return user.profile.get_absolute_url()
        except Profile.DoesNotExist:
            return ""
    elif isinstance(user, Profile):
        return user.get_absolute_url()
    else:
        return ""

@register.filter()
def active(user, state=None):

# Path: narwhal/core/profile/models.py
# class Profile(Model):
#     user = AutoOneToOneField(User, related_name='profile')
#     key = CharField(max_length=32, default=lambda: md5(os.urandom(40)).hexdigest())
#     
#     avatar = ImageField(upload_to='profiles/avatar/', blank=True, null=True)
#     uploaded = BigIntegerField(default=0)
#     downloaded = BigIntegerField(default=0)
#     
#     # extra_data should only be used for data that should not be 
#     # searched upon, such as chat protocol addresses
#     data = JSONField(default=lambda: {})
#     
#     #def __getattr__(self, name):
#     #    try: return self.extra_data[name]
#     #    except KeyError, e: raise AttributeError, e
#     #def __setattr__(self, name, value):
#     #    self.extra_data[name] = value
#     
#     def __unicode__(self):
#         return self.user.username
#     
#     @permalink
#     def get_absolute_url(self):
#         return ('profile:profile', (), {'username': self.user.username} )
#     
#     def gravatar(self):
#         if self.user.email:
#             return "http://www.gravatar.com/avatar/%s.png?d=%s" % (
#                 md5(self.user.email.lower().strip()).hexdigest(),
#                 settings.PROFILE_GRAVATAR_STANDIN )
#         
#     def ratio(self):
#         return "%.2f"% (self.total_uploaded / float(self.total_downloaded))
# 
# Path: narwhal/core/profile/middleware.py
# USER_TRACK_ACTIVE_KEY = 'profile-user-active-track-%d'
# 
# USER_TRACK_IDLE_KEY = 'profile-user-idle-track-%d'


# Path: narwhal/core/profile/templatetags/profile.py
from django.template import Library
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.admin.models import User
from narwhal.core.profile.models import Profile
from narwhal.core.profile.middleware import USER_TRACK_ACTIVE_KEY, USER_TRACK_IDLE_KEY
import logging


register = Library()

@register.filter()
def profilelink(user):
    if isinstance(user, User):
        try:
            return user.profile.get_absolute_url()
        except Profile.DoesNotExist:
            return ""
    elif isinstance(user, Profile):
        return user.get_absolute_url()
    else:
        return ""

@register.filter()
def active(user, state=None):

    if isinstance(user, Profile):