# Path: remo/base/templatetags/helpers.py
# @library.filter
# def urlparams(url_, hash=None, **query):
#     """Add a fragment and/or query paramaters to a URL.
# 
#     New query params will be appended to exising parameters, except duplicate
#     names, which will be replaced.
#     """
#     url = urlparse.urlparse(url_)
#     fragment = hash if hash is not None else url.fragment
# 
#     # Use dict(parse_qsl) so we don't get lists of values.
#     q = url.query
#     query_dict = dict(urlparse.parse_qsl(smart_str(q))) if q else {}
#     query_dict.update((k, v) for k, v in query.items())
# 
#     query_string = _urlencode([(k, v) for k, v in query_dict.items()
#                                if v is not None])
#     new = urlparse.ParseResult(url.scheme, url.netloc, url.path, url.params,
#                                query_string, fragment)
#     return new.geturl()
# 
# Path: remo/reports/utils.py
# def get_last_report(user):
#     """Return user's last report in the past."""
#     today = now().date()
# 
#     try:
#         reports = user.ng_reports.filter(report_date__lte=today)
#         return reports.latest('report_date')
#     except NGReport.DoesNotExist:
#         return None


import urlparse
from datetime import timedelta
from django.conf import settings
from django.utils import timezone
from django_jinja import library
from libravatar import libravatar_url
from remo.base.templatetags.helpers import urlparams
from remo.profiles.models import FunctionalArea, UserAvatar
from remo.reports.utils import get_last_report


INACTIVE_HIGH = timedelta(weeks=8)
INACTIVE_LOW = timedelta(weeks=4)


@library.filter
def get_avatar_url(user, size=50):
    """Get a url pointing to user's avatar.

    The libravatar network is used for avatars. Return cached value if
    its last update was less than 24 hours before. Optional argument
    size can be provided to set the avatar size.

    """
    if not user:
        return None

    default_img_url = reduce(lambda u, x: urlparse.urljoin(u, x),
                             [settings.SITE_URL,
                              settings.STATIC_URL,
                              'base/img/remo/remo_avatar.png'])

    user_avatar, created = UserAvatar.objects.get_or_create(user=user)
    now = timezone.now()

    if (user_avatar.last_update < now - timedelta(days=7)) or created:
        user_avatar.avatar_url = libravatar_url(email=user.email, https=True)
        user_avatar.save()


# Path: remo/base/templatetags/helpers.py
# @library.filter
# def urlparams(url_, hash=None, **query):
#     """Add a fragment and/or query paramaters to a URL.
# 
#     New query params will be appended to exising parameters, except duplicate
#     names, which will be replaced.
#     """
#     url = urlparse.urlparse(url_)
#     fragment = hash if hash is not None else url.fragment
# 
#     # Use dict(parse_qsl) so we don't get lists of values.
#     q = url.query
#     query_dict = dict(urlparse.parse_qsl(smart_str(q))) if q else {}
#     query_dict.update((k, v) for k, v in query.items())
# 
#     query_string = _urlencode([(k, v) for k, v in query_dict.items()
#                                if v is not None])
#     new = urlparse.ParseResult(url.scheme, url.netloc, url.path, url.params,
#                                query_string, fragment)
#     return new.geturl()
# 
# Path: remo/reports/utils.py
# def get_last_report(user):
#     """Return user's last report in the past."""
#     today = now().date()
# 
#     try:
#         reports = user.ng_reports.filter(report_date__lte=today)
#         return reports.latest('report_date')
#     except NGReport.DoesNotExist:
#         return None


# Path: remo/profiles/templatetags/helpers.py
import urlparse
from datetime import timedelta
from django.conf import settings
from django.utils import timezone
from django_jinja import library
from libravatar import libravatar_url
from remo.base.templatetags.helpers import urlparams
from remo.profiles.models import FunctionalArea, UserAvatar
from remo.reports.utils import get_last_report



INACTIVE_HIGH = timedelta(weeks=8)
INACTIVE_LOW = timedelta(weeks=4)


@library.filter
def get_avatar_url(user, size=50):
    """Get a url pointing to user's avatar.

    The libravatar network is used for avatars. Return cached value if
    its last update was less than 24 hours before. Optional argument
    size can be provided to set the avatar size.

    """
    if not user:
        return None

    default_img_url = reduce(lambda u, x: urlparse.urljoin(u, x),
                             [settings.SITE_URL,
                              settings.STATIC_URL,
                              'base/img/remo/remo_avatar.png'])

    user_avatar, created = UserAvatar.objects.get_or_create(user=user)
    now = timezone.now()

    if (user_avatar.last_update < now - timedelta(days=7)) or created:
        user_avatar.avatar_url = libravatar_url(email=user.email, https=True)
        user_avatar.save()


    avatar_url = urlparams(user_avatar.avatar_url, default=default_img_url)