'Datastore query request being decremented upon google sign-out/sign-in
Hello all I am trying to get familiar with Google's App Engine and have completed this tutorial:
https://cloud.google.com/appengine/docs/standard/python3/building-app
After completing this tutorial I decided to expand on it and set up a Python web app where some user logs in through google, and can log their meals. The app should then display their last ten meals back to them. The data is being stored in a datastore instance. Everything works like a charm the first time around. But I have noticed that for every time a user logs out and then back in, the data displayed of past meals gets decremented by one. Once it's decremented it stays at the new number even if more meals are logged.
For example, upon the initial login of some google user, they can log up to 10 meals and then anything logged after that will become a rolling list.
If the user logs out and then back in, the rolling list will be 9 meals. So on and so forth until its down to 2 meals.
In main.py, I updated 2 functions to look like:
def store_time(email, m, t):
entity = datastore.Entity(key=datastore_client.key('User', email, 'visit'))
entity.update({
'logged_meal': m
})
entity.update({
'timestamp': t
})
datastore_client.put(entity)
def fetch_times(email, limit):
ancestor = datastore_client.key('User', email)
query = datastore_client.query(kind='visit', ancestor=ancestor)
query.order = ['-timestamp']
times = query.fetch(limit=limit)
return times
and the main function to be:
firebase_request_adapter = requests.Request()
@app.route('/')
def root():
# Verify Firebase auth.
id_token = request.cookies.get("token")
error_message = None
claims = None
times = None
if id_token:
try:
# Verify the token against the Firebase Auth API. This example
# verifies the token on each page load. For improved performance,
# some applications may wish to cache results in an encrypted
# session store (see for instance
# http://flask.pocoo.org/docs/1.0/quickstart/#sessions).
claims = google.oauth2.id_token.verify_firebase_token(
id_token, firebase_request_adapter)
# Record and fetch the recent times a logged-in user has accessed
# the site. This is currently shared amongst all users, but will be
# individualized in a following step.
store_time(claims['email'], request.args.get("meal", ""), datetime.datetime.now(tz=datetime.timezone.utc))
times = fetch_times(claims['email'], 10)
except ValueError as exc:
# This will be raised if the token is expired or any other
# verification checks fail.
error_message = str(exc)
return render_template(
'index.html',
user_data=claims, error_message=error_message, times=times)
index.html looks like:
<!doctype html>
<!--
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modified by Tyler S.
-->
<html>
<head>
<title>Datastore and Firebase Auth Example</title>
<script src="{{ url_for('static', filename='script.js') }}"></script>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<script src="https://www.gstatic.com/firebasejs/ui/4.4.0/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.4.0/firebase-ui-auth.css" />
</head>
<body>
<h1>Datastore and Firebase Auth Example</h1>
<div id="firebaseui-auth-container"></div>
<div id="login-info" hidden=true>
<h2>Login info:</h2>
{% if user_data %}
<dl>
<dt>Name</dt><dd>{{ user_data['name'] }}</dd>
<dt>Email</dt><dd>{{ user_data['email'] }}</dd>
<form id="text-box" action="" method="get">
Last Meal: <input type="text" name="meal">
<input type="submit" value="Log">
</form>
<dt>Last 10 Meals</dt><dd>
{% for time in times %}
<p>{{ time['logged_meal'] }}</p>
{% endfor %} </dd>
</dl>
{% elif error_message %}
<p>Error: {{ error_message }}</p>
{% endif %}
</div>
<br>
<button id="sign-out" hidden=true>Sign Out</button>
</body>
</html>
and for what its worth, index.yaml
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Modified by Tyler S.
indexes:
- kind: visit
ancestor: yes
properties:
- name: timestamp
direction: desc
- name: logged_meal
direction: desc
I removed some parts of index.html which identify my project. They were not relevant to my question at hand.
Thanks for any advice!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
