<commit_before>################################################
# Processors for events views
################################################
import datetime
from django.conf import settings
from django.contrib.gis.geoip import GeoIP
from api.models import Event
from django_countries import countries
from countries_plus.models import Country

from web.processors import media
from mailer.event_report_mailer import send_email_to_country_ambassadors


def get_client_ip(forwarded=None, remote=None):
    if settings.DEBUG and remote == '127.0.0.1':
        return '172.16.31.10'
    if forwarded:
        return forwarded.split(',')[0]
    return remote


def get_lat_lon_from_user_ip(ip):
    """
    Return latitude and longitude of IP
    """
    g = GeoIP()
    return g.lat_lon(ip)


def get_country_from_user_ip(ip):
    """
    Return country of IP
    """
    g = GeoIP()
    return g.country(ip)


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


def list_active_countries():
    """ List countries with at least an Event associated """
    active_countries = []
    events = Event.objects.filter(start_date__gte=datetime.date(2014, 1, 1))
    approved_events = events.filter(status='APPROVED')

    for event in approved_events:
        event_tuple = (event.country.name.decode(), event.country.code)
        if not (event_tuple in active_countries):
            active_countries.append(event_tuple)
    return active_countries


def get_initial_data(event):
    """
    Processing event to fill in form data
    """
    initial = event.__dict__
    initial['tags'] = event.get_tags()
    initial['audience'] = event.get_audience_array()
    initial['theme'] = event.get_theme_array()
    return initial


def create_or_update_event(event_id=None, **event_data):
    """
    Creates or updates Event object
    """<commit_msg>Optimize list_active_countries() used on the homepage<commit_after>################################################
# Processors for events views
################################################
import datetime
from django.conf import settings
from django.contrib.gis.geoip import GeoIP
from api.models import Event
from django_countries import countries
from countries_plus.models import Country

from web.processors import media
from mailer.event_report_mailer import send_email_to_country_ambassadors


def get_client_ip(forwarded=None, remote=None):
    if settings.DEBUG and remote == '127.0.0.1':
        return '172.16.31.10'
    if forwarded:
        return forwarded.split(',')[0]
    return remote


def get_lat_lon_from_user_ip(ip):
    """
    Return latitude and longitude of IP
    """
    g = GeoIP()
    return g.lat_lon(ip)


def get_country_from_user_ip(ip):
    """
    Return country of IP
    """
    g = GeoIP()
    return g.country(ip)


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


def list_active_countries():
    """ List countries with at least an Event associated """
    events_countries = Event.objects.filter(
            start_date__gte=datetime.date(2014, 1, 1),
            status='APPROVED'
        ).only('country').distinct()

    active_countries = {(e.country.name.decode(), e.country.code) for e in events_countries}

    return list(active_countries)


def get_initial_data(event):
    """
    Processing event to fill in form data
    """
    initial = event.__dict__
    initial['tags'] = event.get_tags()
    initial['audience'] = event.get_audience_array()
    initial['theme'] = event.get_theme_array()
    return initial


def create_or_update_event(event_id=None, **event_data):
    """
    Creates or updates Event object
    """
{'fraction_non_alphanumeric': 0.1436839456467036, 'fraction_numerical': 0.011071967790639155, 'mean_word_length': 3.777644230769231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272845', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1159, 'n_words': 293}