Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion background/app/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# limitations under the License.

# [START getting_started_background_config]
runtime: python37
runtime: python312
# [END getting_started_background_config]
18 changes: 8 additions & 10 deletions background/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import os

from flask import Flask, redirect, render_template, request
from google.cloud import firestore
from google.cloud import pubsub
from google.cloud import firestore, pubsub
from markupsafe import escape


app = Flask(__name__)
Expand Down Expand Up @@ -61,15 +61,13 @@ def translate():
language (form field 'lang'), by sending a PubSub message to a topic.
"""
source_string = request.form.get("v", "")
to_language = request.form.get("lang", "")
to_language = escape(request.form.get("lang", ""))

if source_string == "":
error_message = "Empty value"
return error_message, 400
return "Invalid request, you must provide a value.", 400

if to_language not in ACCEPTABLE_LANGUAGES:
error_message = "Unsupported language: {}".format(to_language)
return error_message, 400
return f"Unsupported language: {to_language}", 400

message = {
"Original": source_string,
Expand All @@ -78,11 +76,11 @@ def translate():
"OriginalLanguage": "",
}

topic_name = "projects/{}/topics/{}".format(
os.getenv("GOOGLE_CLOUD_PROJECT"), "translate"
topic_name = (
f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate"
)
publisher.publish(
topic=topic_name, data=json.dumps(message).encode("utf8")
topic=topic_name, data=json.dumps(message).encode("utf-8")
)
return redirect("/")

Expand Down
7 changes: 5 additions & 2 deletions background/app/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import uuid

import google.auth
from google.cloud import firestore
from google.cloud import pubsub
from google.cloud import firestore, pubsub, storage
import main
import pytest

Expand All @@ -39,7 +38,10 @@ def clear_collection(collection):
for doc in collection.stream():
doc.reference.delete()

bucket_name = 'system-test-bucket'
client = firestore.Client()
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
translations = client.collection("translations")
clear_collection(translations)
translations.add(
Expand All @@ -51,6 +53,7 @@ def clear_collection(collection):
},
document_id="test translation",
)
assert bucket in locals()
yield client


Expand Down
6 changes: 3 additions & 3 deletions background/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
google-cloud-firestore==2.11.1
google-cloud-pubsub==2.16.1
flask==2.2.5
google-cloud-firestore==2.18.0
google-cloud-pubsub==2.23.0
flask==3.0.3