Skip to content
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

Fixed arbitrary issue #1350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
203 changes: 105 additions & 98 deletions girderformindlogger/api/v1/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,42 +383,42 @@ def getResponsesForApplet(
})
if owner_account and owner_account.get('db', None):
self._model.reconnectToDb(db_uri=owner_account.get('db', None))
try:
for user in users:
query = {
"created": { "$lte": toDate, "$gt": fromDate },
"meta.applet.@id": ObjectId(applet['_id']),
"meta.subject.@id": user['_id'],
"reviewing": {'$exists': False}
}
if activities:
query["meta.activity.@id"] = { "$in": activities },

for user in users:
query = {
"created": { "$lte": toDate, "$gt": fromDate },
"meta.applet.@id": ObjectId(applet['_id']),
"meta.subject.@id": user['_id'],
"reviewing": {'$exists': False}
}
if activities:
query["meta.activity.@id"] = { "$in": activities },

responses = self._model.find(
query=query,
force=True,
sort=[("created", DESCENDING)]
)
responses = self._model.find(
query=query,
force=True,
sort=[("created", DESCENDING)]
)

# we need this to handle old responses
for response in responses:
response['meta']['subject']['userTime'] = response["created"].replace(tzinfo=pytz.timezone("UTC")).astimezone(
timezone(
timedelta(
hours=user["timezone"] if 'timezone' not in response['meta']['subject'] else response['meta']['subject']['timezone']
# we need this to handle old responses
for response in responses:
response['meta']['subject']['userTime'] = response["created"].replace(tzinfo=pytz.timezone("UTC")).astimezone(
timezone(
timedelta(
hours=user["timezone"] if 'timezone' not in response['meta']['subject'] else response['meta']['subject']['timezone']
)
)
)
)

tokens = ResponseTokens().getResponseTokens(user, retrieveUserKeys=True)
tokens = ResponseTokens().getResponseTokens(user, retrieveUserKeys=True)

add_latest_daily_response(data, responses, tokens)
add_latest_daily_response(data, responses, tokens)

self._model.reconnectToDb()
data.update(getOldVersions(data['responses'], applet))

data.update(getOldVersions(data['responses'], applet))

return data
return data
finally:
self._model.reconnectToDb()

@access.user(scope=TokenScope.DATA_READ)
@autoDescribeRoute(
Expand Down Expand Up @@ -732,29 +732,32 @@ def checkFileUploaded(
if owner_account and owner_account.get('db', None):
self._model.reconnectToDb(db_uri=owner_account.get('db', None))

owner_client = owner_account and owner_account.get('s3Bucket', None) and owner_account.get('accessKeyId', None)
storage_client = file_storage.resolve_from_account(owner_account) if owner_client else file_storage.resolve_default()

result = []
for fileId in fileIds.split(','):
exists = False
item = self._model.findOne(query={'meta.fileIds': fileId, 'meta.applet.@id': applet['_id']})
if item:
fileResponses = item['meta'].get('responses', {})
for key, fileResponse in fileResponses.items():
fileValue = fileResponse.get('value', {})
if fileId == fileValue.get('fileId'):
uri = fileValue.get('uri', '')
if storage_client and storage_client.checkPathExists(uri):
exists = True

result.append({'fileId': fileId, 'exists': exists})
try:
owner_client = owner_account and owner_account.get('s3Bucket', None) and owner_account.get('accessKeyId', None)
storage_client = file_storage.resolve_from_account(owner_account) if owner_client else file_storage.resolve_default()

result = []
for fileId in fileIds.split(','):
exists = False
item = self._model.findOne(query={'meta.fileIds': fileId, 'meta.applet.@id': applet['_id']})
if item:
fileResponses = item['meta'].get('responses', {})
for key, fileResponse in fileResponses.items():
fileValue = fileResponse.get('value', {})
if fileId == fileValue.get('fileId'):
uri = fileValue.get('uri', '')
if storage_client and storage_client.checkPathExists(uri):
exists = True

result.append({'fileId': fileId, 'exists': exists})

if log is not None:
log['result'] = result
ResponseLogModel().markSuccess(log)
if log is not None:
log['result'] = result
ResponseLogModel().markSuccess(log)

return result
return result
finally:
self._model.reconnectToDb()


@access.user(scope=TokenScope.DATA_READ)
Expand Down Expand Up @@ -805,16 +808,18 @@ def checkResponseExists(
})
if owner_account and owner_account.get('db', None):
self._model.reconnectToDb(db_uri=owner_account.get('db', None))
try:
item = self._model.findOne(query={'meta.responseStarted': activityStartedAt, 'meta.applet.@id': applet['_id'], 'meta.activity.@id': activity['_id']})

item = self._model.findOne(query={'meta.responseStarted': activityStartedAt, 'meta.applet.@id': applet['_id'], 'meta.activity.@id': activity['_id']})

result = {'exists': item is not None}
result = {'exists': item is not None}

if log is not None:
log['result'] = result
ResponseLogModel().markSuccess(log)
if log is not None:
log['result'] = result
ResponseLogModel().markSuccess(log)

return result
return result
finally:
self._model.reconnectToDb()


@access.public
Expand Down Expand Up @@ -1144,7 +1149,6 @@ def createResponseItem(

if not pending:
newItem['readOnly'] = True
#self._model.reconnectToDb()

# update profile activity
profile = Profile()
Expand Down Expand Up @@ -1208,6 +1212,8 @@ def createResponseItem(
print(sys.exc_info())
print(traceback.print_tb(sys.exc_info()[2]))
return(str(traceback.print_tb(sys.exc_info()[2])))
finally:
self._model.reconnectToDb()


@access.public
Expand Down Expand Up @@ -1423,53 +1429,54 @@ def updateReponseHistory(self, applet, user, responses):
if owner_account and owner_account.get('db', None):
self._model.reconnectToDb(db_uri=owner_account.get('db', None))

for responseId in responses['dataSources']:
query = {
"meta.applet.@id": applet['_id'],
"_id": ObjectId(responseId)
}
if my_response:
query["meta.subject.@id"] = profile['_id']

self._model.update(
query,
{
'$set': {
'meta.dataSource': responses['dataSources'][responseId],
'meta.userPublicKey': responses['userPublicKey'],
'updated': now
}
},
multi=False
)

responseTokenModel = ResponseTokens()
try:
for responseId in responses['dataSources']:
query = {
"meta.applet.@id": applet['_id'],
"_id": ObjectId(responseId)
}
if my_response:
query["meta.subject.@id"] = profile['_id']

self._model.update(
query,
{
'$set': {
'meta.dataSource': responses['dataSources'][responseId],
'meta.userPublicKey': responses['userPublicKey'],
'updated': now
}
},
multi=False
)

for tokenUpdateId in responses['tokenUpdates']:
query = {
'appletId': applet['_id'],
'_id': ObjectId(tokenUpdateId),
'userId': profile['userId']
}
responseTokenModel = ResponseTokens()

tokenUpdate = responseTokenModel.findOne(query)
tokenUpdate.update({
'data': responses['tokenUpdates'][tokenUpdateId],
'userPublicKey': responses['userPublicKey'],
'updated': now
})
for tokenUpdateId in responses['tokenUpdates']:
query = {
'appletId': applet['_id'],
'_id': ObjectId(tokenUpdateId),
'userId': profile['userId']
}

responseTokenModel.save(tokenUpdate)
tokenUpdate = responseTokenModel.findOne(query)
tokenUpdate.update({
'data': responses['tokenUpdates'][tokenUpdateId],
'userPublicKey': responses['userPublicKey'],
'updated': now
})

self._model.reconnectToDb()
responseTokenModel.save(tokenUpdate)

if profile.get('refreshRequest', None):
profile.pop('refreshRequest')
Profile().save(profile, validate=False)
if profile.get('refreshRequest', None):
profile.pop('refreshRequest')
Profile().save(profile, validate=False)

return ({
"message": "responses are updated successfully."
})
return ({
"message": "responses are updated successfully."
})
finally:
self._model.reconnectToDb()

def save():
return(lambda x: x)
40 changes: 21 additions & 19 deletions girderformindlogger/external/clear_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,30 @@

if owner_account and not owner_account.get('db', None):
_item.reconnectToDb(db_uri=owner_account.get('db', None))
try:
retentionSettings = applet['meta'].get('retentionSettings', None)

retentionSettings = applet['meta'].get('retentionSettings', None)
retention = retentionSettings.get('retention', 'year')
period = retentionSettings.get('period', 5)

retention = retentionSettings.get('retention', 'year')
period = retentionSettings.get('period', 5)
if retention == 'indefinitely':
continue

if retention == 'indefinitely':
continue
timedelta_in_days = int(period) * int(RETENTION_SET[retention])

timedelta_in_days = int(period) * int(RETENTION_SET[retention])
items = _item.find(query={
'baseParentType': 'user',
'meta.applet.@id': ObjectId(applet['_id']),
'created': {
'$lte': datetime.datetime.now() - datetime.timedelta(days=timedelta_in_days)
}
})

items = _item.find(query={
'baseParentType': 'user',
'meta.applet.@id': ObjectId(applet['_id']),
'created': {
'$lte': datetime.datetime.now() - datetime.timedelta(days=timedelta_in_days)
}
})

if items:
_item.remove({'_id': {
'$in': [ObjectId(item['_id']) for item in items]
}})
if items:
_item.remove({'_id': {
'$in': [ObjectId(item['_id']) for item in items]
}})

print(f'Responses were removed for applet id - {applet.get("_id")}')
print(f'Responses were removed for applet id - {applet.get("_id")}')
finally:
_item.reconnectToDb()