Commit b1d68f26 authored by Arno Töll's avatar Arno Töll
Browse files

pseudo-randomize the sponsor list upon output to make sure its not always the...

pseudo-randomize the sponsor list upon output to make sure its not always the same sponsor being listed first
parent 66a0aaa0
...@@ -36,6 +36,9 @@ __copyright__ = 'Copyright © 2011 Arno Töll' ...@@ -36,6 +36,9 @@ __copyright__ = 'Copyright © 2011 Arno Töll'
__license__ = 'MIT' __license__ = 'MIT'
import logging import logging
import random
import struct
import socket
from debexpo.lib.base import BaseController, c, config, render, session, \ from debexpo.lib.base import BaseController, c, config, render, session, \
redirect, url, abort, request redirect, url, abort, request
...@@ -131,6 +134,25 @@ class SponsorController(BaseController): ...@@ -131,6 +134,25 @@ class SponsorController(BaseController):
.filter(SponsorMetrics.availability >= constants.SPONSOR_METRICS_RESTRICTED)\ .filter(SponsorMetrics.availability >= constants.SPONSOR_METRICS_RESTRICTED)\
.all() .all()
def hash_ip():
"""
This is a simple hashing algorithm not supposed to be called from anywhere
but for internal use only.
It reads the client IP address and returns a float between 0.01 and 0.91 which is
used as input for random.shuffle
"""
ip = request.environ['REMOTE_ADDR']
try:
ip = struct.unpack( "!L", socket.inet_pton( socket.AF_INET, ip ))
ip = ip[0]
except socket.error:
ip = struct.unpack( "!QQ", socket.inet_pton( socket.AF_INET6, ip ))
ip = ip[1]
ip = (float(ip % 10) + 0.01) / 10
return ip
random.shuffle(c.sponsors, hash_ip)
# The select above works fine, except that it sucks. # The select above works fine, except that it sucks.
# It suffers from a poor performance and it could be significantly improved by querying the tag # It suffers from a poor performance and it could be significantly improved by querying the tag
# labels and descriptions (i.e. the SponsorTags table by joining them with SponsorMetricsTags. # labels and descriptions (i.e. the SponsorTags table by joining them with SponsorMetricsTags.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment