-
-
Notifications
You must be signed in to change notification settings - Fork 428
Fix/google form response contract #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| from string import punctuation | ||
| from heapq import nlargest | ||
| import random | ||
| import webbrowser | ||
| from apiclient import discovery | ||
| from httplib2 import Http | ||
| from oauth2client import client, file, tools | ||
|
|
@@ -263,9 +262,13 @@ def get_content(): | |
|
|
||
| @app.route("/generate_gform", methods=["POST"]) | ||
| def generate_gform(): | ||
| data = request.get_json() | ||
| qa_pairs = data.get("qa_pairs", "") | ||
| data = request.get_json() or {} | ||
| qa_pairs = data.get("qa_pairs", []) | ||
| question_type = data.get("question_type", "") | ||
|
|
||
| if not isinstance(qa_pairs, list): | ||
| return jsonify({"error": "qa_pairs must be a list"}), 400 | ||
|
|
||
|
Comment on lines
+265
to
+271
|
||
| SCOPES = "https://www.googleapis.com/auth/forms.body" | ||
| DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1" | ||
|
|
||
|
|
@@ -423,11 +426,9 @@ def generate_gform(): | |
| formId=result["formId"], body=NEW_QUESTION | ||
| ).execute() | ||
|
|
||
| edit_url = jsonify(result["responderUri"]) | ||
| webbrowser.open_new_tab( | ||
| "https://docs.google.com/forms/d/" + result["formId"] + "/edit" | ||
| ) | ||
| return edit_url | ||
| form_link = result.get("responderUri") | ||
| edit_link = f"https://docs.google.com/forms/d/{result['formId']}/edit" | ||
| return jsonify({"form_link": form_link, "edit_link": edit_link}) | ||
|
|
||
|
|
||
| @app.route("/get_shortq_hard", methods=["POST"]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -91,25 +91,42 @@ const Output = () => { | |||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| const qaPairsFromStorage = | ||||||||||||
| JSON.parse(localStorage.getItem("qaPairs")) || {}; | ||||||||||||
| let qaPairsFromStorage = {}; | ||||||||||||
| try { | ||||||||||||
| qaPairsFromStorage = JSON.parse(localStorage.getItem("qaPairs")) || {}; | ||||||||||||
| } catch (error) { | ||||||||||||
| console.error("Invalid qaPairs in localStorage", error); | ||||||||||||
| qaPairsFromStorage = {}; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (qaPairsFromStorage) { | ||||||||||||
| const combinedQaPairs = []; | ||||||||||||
| const output = Array.isArray(qaPairsFromStorage["output"]) | ||||||||||||
| ? qaPairsFromStorage["output"] | ||||||||||||
| : []; | ||||||||||||
| const outputMcqQuestions = Array.isArray( | ||||||||||||
| qaPairsFromStorage["output_mcq"]?.["questions"] | ||||||||||||
| ) | ||||||||||||
| ? qaPairsFromStorage["output_mcq"]["questions"] | ||||||||||||
| : []; | ||||||||||||
| const outputBoolQuestions = Array.isArray( | ||||||||||||
| qaPairsFromStorage["output_boolq"]?.["Boolean_Questions"] | ||||||||||||
| ) | ||||||||||||
| ? qaPairsFromStorage["output_boolq"]["Boolean_Questions"] | ||||||||||||
| : []; | ||||||||||||
|
|
||||||||||||
| if (qaPairsFromStorage["output_boolq"]) { | ||||||||||||
| qaPairsFromStorage["output_boolq"]["Boolean_Questions"].forEach( | ||||||||||||
| (question, index) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question, | ||||||||||||
| question_type: "Boolean", | ||||||||||||
| context: qaPairsFromStorage["output_boolq"]["Text"], | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
| ); | ||||||||||||
| outputBoolQuestions.forEach((question) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question, | ||||||||||||
| question_type: "Boolean", | ||||||||||||
| context: qaPairsFromStorage["output_boolq"]["Text"], | ||||||||||||
| }); | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (qaPairsFromStorage["output_mcq"]) { | ||||||||||||
| qaPairsFromStorage["output_mcq"]["questions"].forEach((qaPair) => { | ||||||||||||
| outputMcqQuestions.forEach((qaPair) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question: qaPair.question_statement, | ||||||||||||
| question_type: "MCQ", | ||||||||||||
|
|
@@ -121,7 +138,7 @@ const Output = () => { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (qaPairsFromStorage["output_mcq"] || questionType === "get_mcq") { | ||||||||||||
| qaPairsFromStorage["output"].forEach((qaPair) => { | ||||||||||||
| output.forEach((qaPair) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question: qaPair.question_statement, | ||||||||||||
| question_type: "MCQ", | ||||||||||||
|
|
@@ -132,15 +149,15 @@ const Output = () => { | |||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (questionType == "get_boolq") { | ||||||||||||
| qaPairsFromStorage["output"].forEach((qaPair) => { | ||||||||||||
| if (questionType === "get_boolq") { | ||||||||||||
| output.forEach((qaPair) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question: qaPair, | ||||||||||||
| question_type: "Boolean", | ||||||||||||
| }); | ||||||||||||
| }); | ||||||||||||
| } else if (qaPairsFromStorage["output"] && questionType !== "get_mcq") { | ||||||||||||
| qaPairsFromStorage["output"].forEach((qaPair) => { | ||||||||||||
| output.forEach((qaPair) => { | ||||||||||||
| combinedQaPairs.push({ | ||||||||||||
| question: | ||||||||||||
| qaPair.question || qaPair.question_statement || qaPair.Question, | ||||||||||||
|
|
@@ -154,15 +171,23 @@ const Output = () => { | |||||||||||
|
|
||||||||||||
| setQaPairs(combinedQaPairs); | ||||||||||||
| } | ||||||||||||
| }, []); | ||||||||||||
| }, [questionType]); | ||||||||||||
|
|
||||||||||||
| const generateGoogleForm = async () => { | ||||||||||||
| try { | ||||||||||||
| const result = await apiClient.post("/generate_gform", { | ||||||||||||
| qa_pairs: qaPairs, | ||||||||||||
| question_type: questionType, | ||||||||||||
| }); | ||||||||||||
| const formUrl = result.form_link; | ||||||||||||
| const formUrl = | ||||||||||||
| (result && result.form_link) || | ||||||||||||
| (typeof result === "string" ? result : null); | ||||||||||||
|
|
||||||||||||
| if (!formUrl) { | ||||||||||||
| console.error("Google Form URL missing in API response", result); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
Comment on lines
+182
to
+190
|
||||||||||||
| window.open(formUrl, "_blank"); | ||||||||||||
|
||||||||||||
| window.open(formUrl, "_blank"); | |
| const newWindow = window.open(formUrl, "_blank", "noopener,noreferrer"); | |
| if (newWindow) { | |
| newWindow.opener = null; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -112,7 +112,15 @@ function Question() { | |||||||||||
|
|
||||||||||||
| if (response.ok) { | ||||||||||||
| const result = await response.json(); | ||||||||||||
| const formUrl = result.form_link; | ||||||||||||
| const formUrl = | ||||||||||||
| (result && result.form_link) || | ||||||||||||
| (typeof result === "string" ? result : null); | ||||||||||||
|
|
||||||||||||
| if (!formUrl) { | ||||||||||||
| console.error("Google Form URL missing in API response", result); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+115
to
+122
|
||||||||||||
|
|
||||||||||||
| window.open(formUrl, "_blank"); | ||||||||||||
|
||||||||||||
| window.open(formUrl, "_blank"); | |
| const newWindow = window.open(formUrl, "_blank", "noopener,noreferrer"); | |
| if (newWindow) { | |
| newWindow.opener = null; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -115,7 +115,15 @@ function SidePanel() { | |||||||||||
|
|
||||||||||||
| if (response.ok) { | ||||||||||||
| const result = await response.json(); | ||||||||||||
| const formUrl = result.form_link; | ||||||||||||
| const formUrl = | ||||||||||||
| (result && result.form_link) || | ||||||||||||
| (typeof result === "string" ? result : null); | ||||||||||||
|
|
||||||||||||
| if (!formUrl) { | ||||||||||||
| console.error("Google Form URL missing in API response", result); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+118
to
+125
|
||||||||||||
|
|
||||||||||||
| window.open(formUrl, "_blank"); | ||||||||||||
|
||||||||||||
| window.open(formUrl, "_blank"); | |
| const newWindow = window.open(formUrl, "_blank", "noopener,noreferrer"); | |
| if (newWindow) { | |
| newWindow.opener = null; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate JSON root type before calling
.get().At Line 265–266, a JSON array/string payload will make
data.get(...)fail with 500 instead of returning a clear 400. Add an object-shape check first.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents