# 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
                             [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)
    if size != -1:
        avatar_url = urlparams(avatar_url, size=size)

    return avatar_url


@library.filter
def get_functional_area(name):
    """Return the Functional Area object given the name."""
    try:
        return FunctionalArea.objects.get(name=name)
    except FunctionalArea.DoesNotExist:
        return None


@library.filter
def get_activity_level(user):
    """Return user's inactivity level."""

# 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

                             [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)
    if size != -1:
        avatar_url = urlparams(avatar_url, size=size)

    return avatar_url


@library.filter
def get_functional_area(name):
    """Return the Functional Area object given the name."""
    try:
        return FunctionalArea.objects.get(name=name)
    except FunctionalArea.DoesNotExist:
        return None


@library.filter
def get_activity_level(user):
    """Return user's inactivity level."""

    last_report = get_last_report(user)