return iter(mirrors)
+ @property
+ def random(self):
+ """
+ Returns all mirrors in a random order
+ """
+ return self._get_mirrors("SELECT * FROM mirrors \
+ WHERE deleted IS FALSE ORDER BY RANDOM()")
+
def create(self, hostname, path="", owner=None, contact=None, user=None):
mirror = self._get_mirror("INSERT INTO mirrors(hostname, path, owner, contact) \
VALUES(%s, %s, %s, %s) RETURNING *", hostname, path, owner, contact)
return self._get_mirror("SELECT * FROM mirrors \
WHERE hostname = %s AND deleted IS FALSE", hostname)
- def get_for_location(self, address):
- country_code = self.backend.geoip.guess_from_address(address)
-
- # Cannot return any good mirrors if location is unknown
- if not country_code:
- return []
+ def make_mirrorlist(self, client_address=None):
+ country_code = self.backend.geoip.guess_from_address(client_address)
+ # Walk through all mirrors in a random order
+ # and put all preferred mirrors to the front of the list
+ # and everything else at the end
mirrors = []
-
- # Walk through all mirrors
- for mirror in self:
- if mirror.country_code == country_code:
+ for mirror in self.random:
+ if mirror.is_preferred_for_country(country_code):
+ mirrors.insert(0, mirror)
+ else:
mirrors.append(mirror)
- # XXX needs to search for nearby countries
-
return mirrors
def get_history(self, limit=None, offset=None, mirror=None, user=None):
def address(self):
return socket.gethostbyname(self.hostname)
+ def is_preferred_for_country(self, country_code):
+ if country_code and self.country_code:
+ return self.country_code == country_code
+
@lazy_property
def country_code(self):
return self.backend.geoip.guess_from_address(self.address) or "UNKNOWN"
"version" : 1,
}
- # A list with mirrors that are sent to the user.
mirrors = []
-
- # Select a list of preferred mirrors
- for mirror in self.mirrors.get_for_location(self.current_address):
- mirrors.append({
- "url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)),
- "location" : mirror.country_code,
- "preferred" : 1,
- })
-
- # Add all other mirrors at the end in a random order
- remaining_mirrors = [m for m in self.backend.mirrors if not m in mirrors]
- random.shuffle(remaining_mirrors)
-
- for mirror in remaining_mirrors:
+ for mirror in self.mirrors.make_mirrorlist(self.current_address):
mirrors.append({
"url" : "/".join((mirror.url, distro.identifier, repo.identifier, arch)),
"location" : mirror.country_code,
- "preferred" : 0,
})
ret["mirrors"] = mirrors