# 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/voting/tasks.py
# def rotm_nomination_end_date():
#     return date(now().year, now().month, 10)


from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import Group, User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.utils.timezone import now
from django.utils.encoding import iri_to_uri
from django.utils.safestring import mark_safe
from django.views.decorators.cache import cache_control, never_cache
from django_statsd.clients import statsd
from mozilla_django_oidc.auth import default_username_algo
from product_details import product_details
from remo.base.decorators import permission_check
from remo.base.templatetags.helpers import urlparams
from remo.events.utils import get_events_for_user
from remo.profiles.models import UserProfile, UserStatus
from remo.profiles.models import FunctionalArea, MobilisingInterest, MobilisingSkill
from remo.voting.tasks import rotm_nomination_end_date
import waffle
import forms
        # Django tries to resolve the url based on this display_name, which
        # results in a NoReverseMatch error. See also bug:
        # https://bugzilla.mozilla.org/show_bug.cgi?id=1147541
        user = User.objects.get(pk=user.id)

    group_bits = map(lambda x: user.groups.filter(name=x).exists(),
                     ['Admin', 'Council', 'Mentor', 'Rep', 'Alumni', 'Review', 'Peers',
                      'Resources', 'Onboarding', 'Newsletter'])

    functional_areas = map(int, profileform['functional_areas'].value())
    mobilising_skills = map(int, profileform['mobilising_skills'].value())
    mobilising_interests = map(int, profileform['mobilising_interests'].value())
    user_is_alumni = user.groups.filter(name='Alumni').exists()

    return render(request, 'profiles_edit.jinja',
                  {'userform': userform,
                   'profileform': profileform,
                   'profile_date_form': profile_date_form,
                   'pageuser': user,
                   'group_bits': group_bits,
                   'range_years': range(1950, now().date().year - 11),
                   'functional_areas': functional_areas,
                   'mobilising_skills': mobilising_skills,
                   'mobilising_interests': mobilising_interests,
                   'user_is_alumni': user_is_alumni})


def redirect_list_profiles(request):
    profiles_url = reverse('profiles_list_profiles')
    extra_path = iri_to_uri('/' + request.path_info[len(profiles_url):])

# 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/voting/tasks.py
# def rotm_nomination_end_date():
#     return date(now().year, now().month, 10)


# Path: remo/profiles/views.py
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import Group, User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.utils.timezone import now
from django.utils.encoding import iri_to_uri
from django.utils.safestring import mark_safe
from django.views.decorators.cache import cache_control, never_cache
from django_statsd.clients import statsd
from mozilla_django_oidc.auth import default_username_algo
from product_details import product_details
from remo.base.decorators import permission_check
from remo.base.templatetags.helpers import urlparams
from remo.events.utils import get_events_for_user
from remo.profiles.models import UserProfile, UserStatus
from remo.profiles.models import FunctionalArea, MobilisingInterest, MobilisingSkill
from remo.voting.tasks import rotm_nomination_end_date
import waffle
import forms

        # Django tries to resolve the url based on this display_name, which
        # results in a NoReverseMatch error. See also bug:
        # https://bugzilla.mozilla.org/show_bug.cgi?id=1147541
        user = User.objects.get(pk=user.id)

    group_bits = map(lambda x: user.groups.filter(name=x).exists(),
                     ['Admin', 'Council', 'Mentor', 'Rep', 'Alumni', 'Review', 'Peers',
                      'Resources', 'Onboarding', 'Newsletter'])

    functional_areas = map(int, profileform['functional_areas'].value())
    mobilising_skills = map(int, profileform['mobilising_skills'].value())
    mobilising_interests = map(int, profileform['mobilising_interests'].value())
    user_is_alumni = user.groups.filter(name='Alumni').exists()

    return render(request, 'profiles_edit.jinja',
                  {'userform': userform,
                   'profileform': profileform,
                   'profile_date_form': profile_date_form,
                   'pageuser': user,
                   'group_bits': group_bits,
                   'range_years': range(1950, now().date().year - 11),
                   'functional_areas': functional_areas,
                   'mobilising_skills': mobilising_skills,
                   'mobilising_interests': mobilising_interests,
                   'user_is_alumni': user_is_alumni})


def redirect_list_profiles(request):
    profiles_url = reverse('profiles_list_profiles')
    extra_path = iri_to_uri('/' + request.path_info[len(profiles_url):])

    return redirect(urlparams(profiles_url, hash=extra_path), permanent=True)