Skip to content

Commit

Permalink
RDISCROWD-5714 Save partial answer to server (TP component) (#818)
Browse files Browse the repository at this point in the history
* RDISCROWD-5714 Save partial answer to server (TP component)

* fix code coverage

* update pybossa-theme-default pointer
  • Loading branch information
XiChenn authored Feb 23, 2023
1 parent 31b770e commit 283d23a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
26 changes: 25 additions & 1 deletion pybossa/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from sqlalchemy.sql import text
from sqlalchemy.orm.attributes import flag_modified
from pybossa.core import db
from pybossa.cache import users as cached_users
from pybossa.cache import users as cached_users, ONE_MONTH
from pybossa.cache.task_browse_helpers import get_searchable_columns
from pybossa.cache.users import get_user_pref_metadata
from pybossa.view.projects import get_locked_tasks
Expand Down Expand Up @@ -777,3 +777,27 @@ def assign_task(task_id=None):
current_app.logger.info("User %s assigned to task %s", current_user.fullname, task_id)

return Response(json.dumps({'success': True}), 200, mimetype="application/json")


@jsonpify
@login_required
@csrf.exempt
@blueprint.route('/task/<int:task_id>/partial_answer', methods=['POST', 'GET', 'DELETE'])
@ratelimit(limit=ratelimits.get('LIMIT'), per=ratelimits.get('PER'))
def partial_answer(task_id=None):
"""Save/Get/Delete partial answer to Redis - this API might be called heavily.
Make sure no PostgreSQL DB calls"""
response = {'success': True}
try:
partial_answer_key = f"partial_answer:task:{task_id}:user:{current_user.id}"
if request.method == 'POST':
answer = json.dumps(request.json)
sentinel.master.setex(partial_answer_key, ONE_MONTH, answer)
elif request.method == 'GET':
data = sentinel.master.get(partial_answer_key)
response['data'] = json.loads(data.decode('utf-8'))
elif request.method == 'DELETE':
sentinel.master.delete(partial_answer_key)
except Exception as e:
return error.format_exception(e, target='partial_answer', action=request.method)
return Response(json.dumps(response), status=200, mimetype="application/json")
1 change: 1 addition & 0 deletions pybossa/cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
L2_CACHE_TIMEOUT = ONE_DAY
MUTEX_LOCK_TIMEOUT = ONE_MINUTE
TWO_WEEKS = 14 * ONE_DAY
ONE_MONTH = 30 * ONE_DAY

management_dashboard_stats = [
'project_chart', 'category_chart', 'task_chart',
Expand Down
24 changes: 24 additions & 0 deletions test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -10394,6 +10394,30 @@ def test_unassign_task_succeed(self, fetch_lock):
assert user.email_addr not in task2_modified.user_pref.get('assign_user')
assert other_email in task2_modified.user_pref.get('assign_user')

@with_context
def test_partial_answer_exception(self):
"""Test partial answer API with exception as user doesn't sign in """
url = f"/api/task/123/partial_answer"
resp = self.app_get_json(url)
assert resp.status_code == 415

@with_context
def test_partial_answer(self):
"""Test partial answer API """
user = UserFactory.create(email_addr='a@a.com', fullname="test_user")
self.signin_user(user)

url = f"/api/task/123/partial_answer"
data = {"my_answer": {"k1: ": "test", "k2": [1, 2, "abc"]}}
resp = self.app_post_json(url, data=data, follow_redirects=False)
assert json.loads(resp.data).get('success')

resp = self.app_get_json(url)
assert json.dumps(resp.json.get('data')) == json.dumps(data)

resp = self.app.delete(url)
assert json.loads(resp.data).get('success')


class TestWebQuizModeUpdate(web.Helper):

Expand Down

0 comments on commit 283d23a

Please sign in to comment.