# Path: web/views/events.py
# def index(request):
# def map(request):
# def add_event(request):
# def edit_event(request, event_id):
# def report_event(request, event_id):
# def view_event_by_country(request, country_code):
# def view_changed_event(request, event_id, slug):
# def view_event(request, event_id, slug):
# def view_event_by_id(request, event_id):
# def list_pending_events(request, country_code):
# def list_approved_events(request, country_code):
# def created_events(request):
# def events_to_report(request):
# def search_events(request):
# def scoreboard(request):
# def change_status(request, event_id):
# def reject_status(request, event_id):
# 
# Path: web/processors/user.py
# def get_ambassadors_for_countries():
#     ambassadors = get_ambassadors()
#     countries_ambassadors = []
#     # list countries minus two CUSTOM_COUNTRY_ENTRIES
#     for code, name in list(countries)[2:]:
#         readable_name = unicode(name)
#         country_ambassadors = [ambassador for ambassador in ambassadors if ambassador.country == code]
#         # load main ambassadors
#         main_ambassadors = [ambassador for ambassador in country_ambassadors if ambassador.is_main_contact]
#         # exclude main ambassadors
#         supporting_ambassadors = [ambassador for ambassador in country_ambassadors if not ambassador.is_main_contact]
#         countries_ambassadors.append(
#             (code, readable_name, supporting_ambassadors, main_ambassadors, facebook(readable_name)))
# 
#     countries_ambassadors.sort(key=lambda country: country[1], reverse=False)
# 
#     return countries_ambassadors
# 
# Path: web/processors/event.py
# def get_country_from_user_ip(ip):
#     """
#     Return country of IP
#     """
#     g = GeoIP()
#     return g.country(ip)
# 
# Path: web/processors/event.py
# def list_countries():
#     all_countries = []
#     for code, name in list(countries):
#         readable_name = unicode(name)
#         all_countries.append((readable_name, code))
#     all_countries.sort()
#     return all_countries
# 
# Path: web/processors/event.py
# def get_country_pos(item):
#     """
#     Return country position
#     """
#     pos = 1
#     # not including the first two fake countries in the list
#     for country in list(countries)[2:]:
#         country_name = country[1]
#         if item == country_name:
#             break
#         else:
#             pos = pos + 1
# 
#     return pos


from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
from django.contrib import messages
from django.views.decorators.cache import never_cache
from web.forms.user_profile import UserForm, UserProfileForm
from web.views.events import get_client_ip
from web.processors.user import get_ambassadors_for_countries
from web.processors.event import get_country_from_user_ip
from web.decorators.events import login_required
from web.processors.event import list_countries
from web.processors.event import get_country_pos
    if request.method == 'POST':
        # populate form with original instance and add post info on top of that
        uform = UserForm(request.POST, instance=request.user)
        pform = UserProfileForm(request.POST, instance=request.user.profile)
        if uform.is_valid() and pform.is_valid():
            uform.save()
            pform.save()
            messages.success(request, 'Profile details updated')
    else:
        user = request.user
        uform = UserForm(instance=user)
        profile = user.profile
        pform = UserProfileForm(instance=profile)

    context = {}
    context.update(csrf(request))
    context['uform'] = uform
    context['pform'] = pform

    return render_to_response(
        'pages/profile.html',
        context,
        context_instance=RequestContext(request))


def ambassadors(request):
    try:
        user_ip = get_client_ip(
            forwarded=request.META.get('HTTP_X_FORWARDED_FOR'),
            remote=request.META.get('REMOTE_ADDR'))

# Path: web/views/events.py
# def index(request):
# def map(request):
# def add_event(request):
# def edit_event(request, event_id):
# def report_event(request, event_id):
# def view_event_by_country(request, country_code):
# def view_changed_event(request, event_id, slug):
# def view_event(request, event_id, slug):
# def view_event_by_id(request, event_id):
# def list_pending_events(request, country_code):
# def list_approved_events(request, country_code):
# def created_events(request):
# def events_to_report(request):
# def search_events(request):
# def scoreboard(request):
# def change_status(request, event_id):
# def reject_status(request, event_id):
# 
# Path: web/processors/user.py
# def get_ambassadors_for_countries():
#     ambassadors = get_ambassadors()
#     countries_ambassadors = []
#     # list countries minus two CUSTOM_COUNTRY_ENTRIES
#     for code, name in list(countries)[2:]:
#         readable_name = unicode(name)
#         country_ambassadors = [ambassador for ambassador in ambassadors if ambassador.country == code]
#         # load main ambassadors
#         main_ambassadors = [ambassador for ambassador in country_ambassadors if ambassador.is_main_contact]
#         # exclude main ambassadors
#         supporting_ambassadors = [ambassador for ambassador in country_ambassadors if not ambassador.is_main_contact]
#         countries_ambassadors.append(
#             (code, readable_name, supporting_ambassadors, main_ambassadors, facebook(readable_name)))
# 
#     countries_ambassadors.sort(key=lambda country: country[1], reverse=False)
# 
#     return countries_ambassadors
# 
# Path: web/processors/event.py
# def get_country_from_user_ip(ip):
#     """
#     Return country of IP
#     """
#     g = GeoIP()
#     return g.country(ip)
# 
# Path: web/processors/event.py
# def list_countries():
#     all_countries = []
#     for code, name in list(countries):
#         readable_name = unicode(name)
#         all_countries.append((readable_name, code))
#     all_countries.sort()
#     return all_countries
# 
# Path: web/processors/event.py
# def get_country_pos(item):
#     """
#     Return country position
#     """
#     pos = 1
#     # not including the first two fake countries in the list
#     for country in list(countries)[2:]:
#         country_name = country[1]
#         if item == country_name:
#             break
#         else:
#             pos = pos + 1
# 
#     return pos


# Path: web/views/users.py
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
from django.contrib import messages
from django.views.decorators.cache import never_cache
from web.forms.user_profile import UserForm, UserProfileForm
from web.views.events import get_client_ip
from web.processors.user import get_ambassadors_for_countries
from web.processors.event import get_country_from_user_ip
from web.decorators.events import login_required
from web.processors.event import list_countries
from web.processors.event import get_country_pos

    if request.method == 'POST':
        # populate form with original instance and add post info on top of that
        uform = UserForm(request.POST, instance=request.user)
        pform = UserProfileForm(request.POST, instance=request.user.profile)
        if uform.is_valid() and pform.is_valid():
            uform.save()
            pform.save()
            messages.success(request, 'Profile details updated')
    else:
        user = request.user
        uform = UserForm(instance=user)
        profile = user.profile
        pform = UserProfileForm(instance=profile)

    context = {}
    context.update(csrf(request))
    context['uform'] = uform
    context['pform'] = pform

    return render_to_response(
        'pages/profile.html',
        context,
        context_instance=RequestContext(request))


def ambassadors(request):
    try:
        user_ip = get_client_ip(
            forwarded=request.META.get('HTTP_X_FORWARDED_FOR'),
            remote=request.META.get('REMOTE_ADDR'))

        user_country = get_country_from_user_ip(user_ip)