Ejemplo n.º 1
0
class Everyone(ImageContent):
    """A map of Accent users around the world."""
    def __init__(self, geocoder):
        self.geocoder = geocoder
        self.google_maps = GoogleMaps(geocoder)
        self.firestore = Firestore()

    @cached(cache=TTLCache(maxsize=1, ttl=CACHE_TTL_S))
    def _markers(self):
        """Returns a list of users' home locations to be shown on a map."""

        markers = ''

        for user in self.firestore.users():
            try:
                home = user.get('home')
                location = self.geocoder[home]
                # Use (city) name and region instead of latitude and longitude
                # to avoid leaking users' exact addresses.
                markers += '|%s,%s' % (location.name, location.region)
            except (KeyError, AstralError):
                # Skip users with address errors.
                pass

        return markers

    def image(self, user):
        """Generates a map with user locations."""

        return self.google_maps.map_image(markers=self._markers(),
                                          marker_icon=MARKER_ICON_URL)
Ejemplo n.º 2
0
class Everyone(ImageContent):
    """A map of Accent users around the world."""

    def __init__(self, geocoder):
        self._geocoder = geocoder
        self._google_maps = GoogleMaps(geocoder)
        self._firestore = Firestore()

    @cached(cache=TTLCache(maxsize=1, ttl=CACHE_TTL_S))
    def _markers(self):
        """Returns a list of users' home locations to be shown on a map."""

        markers = ''

        for user in self._firestore.users():
            try:
                home = user.get('home')
                location = self._geocoder[home]

                # Use the latitude and longitude of the city name and region
                # instead of the exact coordinates to anonymize user addresses.
                city = '%s, %s' % (location.name, location.region)
                anonymized = self._geocoder[city]

                markers += '|%f,%f' % (anonymized.latitude,
                                       anonymized.longitude)
            except (KeyError, AstralError):
                # Skip users with address errors.
                pass

        return markers

    def image(self, user, width, height):
        """Generates a map with user locations."""

        try:
            return self._google_maps.map_image(width, height,
                                               markers=self._markers(),
                                               marker_icon=MARKER_ICON_URL)
        except DataError as e:
            raise ContentError(e)