Skip to content

Participant utils

participant.utils:

country(request)

Get country code of requesting ip

Source code in participant/utils.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def country(request):
    """Get country code of requesting ip"""

    country_code = ""
    key = 'country_code'

    # Get country_code from session
    country_code = request.session.get(key)

    # If country code is missing, guess country by ip address
    if not country_code:
        ip_address = visitor_ip_address(request)

        if settings.DEBUG:
            # On development, always fake netherlands
            country_code = "nl"
        else:
            country_code = get_country_code(ip_address)
            if country_code is None:
                country_code = ""

        request.session[key] = country_code

    return country_code

get_country_code(ip_address)

Get country code from given ip address

Source code in participant/utils.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def get_country_code(ip_address):
    """Get country code from given ip address"""

    # Check if location provided is configured
    if not settings.LOCATION_PROVIDER:
        return None

    # Prepare url
    ip_address = urllib.parse.quote(ip_address)
    location_url = settings.LOCATION_PROVIDER.format(ip_address)

    # Request location data
    with urllib.request.urlopen(location_url) as url:
        try:
            return url.read().decode()
        except:
            return None

get_or_create_participant(request)

Get a participant from URL, the session, or create/add a new one

Source code in participant/utils.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def get_or_create_participant(request) -> Participant:
    """Get a participant from URL, the session, or create/add a new one"""
    # check if query string contains  participant
    participant_id_url = request.GET.get("participant_id")  # can be None
    try:
        if participant_id_url:
            # get participant from query string
            participant = Participant.objects.get(participant_id_url = participant_id_url)
            set_participant(request, participant)
            return participant
        else:
            # Get participant from session
            participant = get_participant(request)

    except Participant.DoesNotExist:
        # create new participant
        country_code = country(request)
        access_info = request.META.get('HTTP_USER_AGENT')

        # Create a new Participant, store the country code once
        participant = Participant(country_code=country_code, access_info=access_info, participant_id_url=participant_id_url)
        participant.save()
        set_participant(request, participant)
    return participant

located_in_nl(request)

Return True if the requesting IP-address is located in NL

Source code in participant/utils.py
14
15
16
def located_in_nl(request):
    """Return True if the requesting IP-address is located in NL"""
    return country(request) == 'nl'

set_participant(request, participant)

Set a participant to the session

Source code in participant/utils.py
116
117
118
119
120
121
def set_participant(request, participant):
    """Set a participant to the session"""
    if participant:
        request.session[PARTICIPANT_KEY] = participant.id
    else:
        del request.session[PARTICIPANT_KEY]

visitor_ip_address(request)

Get visitor ip address from request

Source code in participant/utils.py
64
65
66
67
68
69
70
71
72
73
def visitor_ip_address(request):
    """Get visitor ip address from request"""

    # You may want to change the header based on your production settings
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if x_forwarded_for:
        return x_forwarded_for.split(',')[0]

    return request.META.get('REMOTE_ADDR')