def hello_post(key): """Saves user data and responds with the updated form.""" # Build the schedule from the form data, dropping any empty entries. form = request.form list_form = form.to_dict(flat=False) schedule_form = zip(list_form['schedule_name'], list_form['schedule_start'], list_form['schedule_image']) schedule = [{ 'name': name, 'start': start, 'image': image } for name, start, image in schedule_form if name and start and image] # Update the existing user data or create a new one. firestore = Firestore() firestore.set_user( key, { 'home': form['home'], 'work': form['work'], 'travel_mode': form['travel_mode'], 'schedule': schedule }) # Redirect back to the GET version. return redirect(settings_url(key))
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)
def _google_calendar_flow(key): """Creates the OAuth flow.""" secrets = Firestore().google_calendar_secrets() return OAuth2WebServerFlow(client_id=secrets['client_id'], client_secret=secrets['client_secret'], scope=GOOGLE_CALENDAR_SCOPE, state=key, redirect_uri=_oauth_url())
def user_auth(image_response=None, bad_response=forbidden_response): """A decorator for Flask route functions to enforce user authentication.""" firestore = Firestore() def decorator(func): @wraps(func) def wrapper(*args, **kwargs): try: # Look for a key debug request argument first. key = request.args['key'] except KeyError: # Otherwise, expect a basic access authentication header. authorization = request.authorization if not authorization: return bad_response() key = authorization['password'] # Disallow malformed keys. if not _valid_key(key): return bad_response() # Look up the user from the key. user = firestore.user(key) if not user: if image_response: # For image requests, start the new user flow. return settings_response(key, image_response) else: # Otherwise, return a forbidden response. return bad_response() # Inject the key and user into the function arguments. kwargs['key'] = key kwargs['user'] = user return func(*args, **kwargs) return wrapper return decorator
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)
def hello_get(key): """Responds with a form for editing user data.""" # Look up any existing user data. calendar_storage = GoogleCalendarStorage(key) calendar_credentials = calendar_storage.get() # Force a Google Calendar credentials refresh to get the latest status. if calendar_credentials: try: calendar_credentials.refresh(build_http()) except HttpAccessTokenRefreshError as e: error('Calendar token refresh error: %s' % e) calendar_storage.delete() calendar_credentials = None calendar_connected = calendar_credentials is not None return render_template(HELLO_TEMPLATE, key=key, user=Firestore().user(key), calendar_connected=calendar_connected, calendar_connect_url=google_calendar_step1(key), calendar_disconnect_url=ACCOUNT_ACCESS_URL)
def __init__(self, geocoder): self.dark_sky_api_key = Firestore().dark_sky_api_key() self.geocoder = geocoder
def __init__(self, geocoder): self.google_maps_api_key = Firestore().google_maps_api_key() self.local_time = LocalTime(geocoder) self.vision_client = vision.ImageAnnotatorClient()
def __init__(self): google_maps_api_key = Firestore().google_maps_api_key() GoogleGeocoder.__init__(self, api_key=google_maps_api_key, cache=False)
def __init__(self, geocoder): self._geocoder = geocoder self._google_maps = GoogleMaps(geocoder) self._firestore = Firestore()
def __init__(self, geocoder): self._open_weather_api_key = Firestore().open_weather_api_key() self._geocoder = geocoder
from credentials import Credentials from firestore import Firestore credentials = Credentials() firestore = Firestore()