Skip to content

Commit

Permalink
Bugfix: Format data values for multi-choice fields (#48)
Browse files Browse the repository at this point in the history
* Bugfix: Format data values for multi-choice fields

This addresses https://phabricator.wikimedia.org/T331262

As I wasn't having any luck creating a custom schema that would
correctly handle both strings and lists, I opted to accept a string
and convert it into a list.

Unfortunately, Flask did not do a good job of converting the resulting
data to a valid JSON, so I had to do it manually.

* Commit variable name fix and CI tweak

* Change function name and update import

This change is completely unrelated to this PR.
  • Loading branch information
nbarnabee committed Mar 7, 2023
1 parent 8c5b891 commit a8bac20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
UserMetricsSchema,
UserSchema,
)
from api.utils import ToolhubClient, build_request, generate_past_date, get_current_user
from api.utils import (
ToolhubClient,
build_put_request,
generate_past_date,
get_current_user,
)

toolhub_client = ToolhubClient(current_app.config["TOOLHUB_API_ENDPOINT"])

Expand Down Expand Up @@ -245,7 +250,7 @@ def put(self, form_data, task_id):
abort(409, message="This task has already been completed.")
elif flask.session and flask.session["token"]:
tool_name = form_data["tool"]
submission_data = build_request(form_data)
submission_data = build_put_request(form_data)
token = flask.session["token"]["access_token"]
chain(
make_put_request.s(tool_name, submission_data, token)
Expand Down
19 changes: 14 additions & 5 deletions api/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json

import flask
import requests
Expand All @@ -7,10 +8,14 @@
from api import oauth


def build_request(form_data):
"""Take form data and return a dict to PUT to Toolhub."""
def build_put_request(form_data):
"""Take form data and return a dict to PUT to Toolhub"""
multi_fields = ["audiences", "content_types", "tasks", "subject_domains"]
field = form_data["field"]
value = form_data["value"]
if field in multi_fields:
value = form_data["value"].split(",")
else:
value = form_data["value"]
comment = f"Updated {field} using Toolhunt"
submission_data = {}
submission_data[field] = value
Expand Down Expand Up @@ -50,7 +55,10 @@ def generate_past_date(days):

class ToolhubClient:
def __init__(self, endpoint):
self.headers = {"User-Agent": "Toolhunt API"}
self.headers = {
"User-Agent": "Toolhunt API",
"Content-Type": "application/json",
}
self.endpoint = endpoint

def get(self, tool):
Expand Down Expand Up @@ -109,6 +117,7 @@ def put(self, tool, data, token):
url = f"{self.endpoint}{tool}/annotations/"
headers = dict(self.headers)
headers.update({"Authorization": f"Bearer {token}"})
response = requests.put(url, data=data, headers=headers)
# Having to do a manual json.dumps() to ensure proper formatting
response = requests.put(url, data=json.dumps(data), headers=headers)
api_response = response.json()
return api_response

0 comments on commit a8bac20

Please sign in to comment.