From cbd9753458c4b219376ee7eba963b7c3c5d1335b Mon Sep 17 00:00:00 2001 From: karser Date: Wed, 14 Dec 2022 15:46:59 +0200 Subject: [PATCH] fix q1 issue in the item cache ChildMindInstitute/mindlogger-applet-builder#910 --- girderformindlogger/external/fix_flankers.py | 45 +++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/girderformindlogger/external/fix_flankers.py b/girderformindlogger/external/fix_flankers.py index b14528795..6baa8cec7 100644 --- a/girderformindlogger/external/fix_flankers.py +++ b/girderformindlogger/external/fix_flankers.py @@ -6,6 +6,7 @@ from girderformindlogger.models.activity import Activity from girderformindlogger.models.folder import Folder from girderformindlogger.models.user import User +from girderformindlogger.models.cache import Cache from girderformindlogger.utility import jsonld_expander from bson.objectid import ObjectId @@ -41,28 +42,36 @@ def prepare_items(activityId): def findInput(name, inputs): return next((i for i in inputs if i['schema:name'][0]['@value'] == name), None) +def fix_q1_issue_in_json(id, model): + if not 'reprolib:terms/inputs' in model: + return False + inputs = model['reprolib:terms/inputs'] + trials = findInput('trials', inputs) + if trials is None or not 'schema:itemListElement' in trials: + return False + print('processing item:', id, model['@id']) + itemChanged = False + for trial in trials['schema:itemListElement']: + if 'q1' == trial['schema:name'][0]['@value']: + trial['schema:name'][0]['@value'] = trial['schema:image'] + trial['schema:image'] = '' + itemChanged = True + + return itemChanged def fix_q1_issue(activityId): activityChanged = False items = Item().find(query={'meta.activityId': activityId, 'meta.screen': {'$exists': True}}) for item in items: - if not 'reprolib:terms/inputs' in item['meta']['screen']: - continue - inputs = item['meta']['screen']['reprolib:terms/inputs'] - trials = findInput('trials', inputs) - if trials is None or not 'schema:itemListElement' in trials: - continue - print('processing item:', item['_id'], item['meta']['screen']['@id']) - # trialsIdx = inputs.index(trials) - itemChanged = False - for trial in trials['schema:itemListElement']: - if 'q1' == trial['schema:name'][0]['@value']: - trial['schema:name'][0]['@value'] = trial['schema:image'] - trial['schema:image'] = '' - itemChanged = True - activityChanged = True - if itemChanged: + if fix_q1_issue_in_json(item['_id'], item['meta']['screen']): Item().setMetadata(item, item['meta'], validate=False) + activityChanged = True + + if 'cached' in item: + cache = Cache().getFromSourceID('item', item['_id']) + if cache is not None and fix_q1_issue_in_json(item['_id'], cache): + Cache().updateCache(item['cached'], 'item', item['_id'], 'screen', cache) + return activityChanged @@ -75,9 +84,7 @@ def fix_q1_issue_in_versions(activityId): print('Fixing ' + str(hCount) + ' historical version(s) of the activity id=' + str(activityId)) for hActivity in hActivities: print('processing activity: ', hActivity['_id']) - if fix_q1_issue(hActivity['_id']): - print('refreshig activity: ', hActivity['_id']) - jsonld_expander.formatLdObject(hActivity, 'activity', None, refreshCache=True, reimportFromUrl=False) + fix_q1_issue(hActivity['_id']) def fix_flankers(activityId, reImport = True):