Skip to content

Commit

Permalink
Refs #257, changed with_uuids with full, modifiy to_json to give more…
Browse files Browse the repository at this point in the history
… information on invitations
  • Loading branch information
doumdi committed Dec 12, 2024
1 parent b0f5a66 commit d296c5b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
default=None)

# Additional parameters
get_parser.add_argument('with_uuids', type=bool, help="Include UUIDs in results", default=False)
get_parser.add_argument('full', type=bool, help="Include full information for session, test_type, user, participant, device", default=False)
get_parser.add_argument('with_urls', type=bool, help="Include URLs in results", default=False)

post_parser = api.parser()
Expand Down Expand Up @@ -154,22 +154,22 @@ def get(self):

# No arguments means we return all accessible invitations

if all(args[arg] is None or arg in ['with_uuids', 'with_urls'] for arg in args):
if all(args[arg] is None or arg in ['full', 'with_urls'] for arg in args):
for invitation in accessible_invitations:
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
else:

# Go through all args and get the requested information
if args['id_test_invitation'] is not None:
for invitation in TeraTestInvitation.query.filter(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by(
id_test_invitation=args['id_test_invitation']).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['test_invitation_key'] is not None:
for invitation in TeraTestInvitation.query.filter(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by(
test_invitation_key=args['test_invitation_key']).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['user_uuid'] is not None:
user : TeraUser = TeraUser.get_user_by_uuid(args['user_uuid'])
if user:
Expand All @@ -178,7 +178,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_user == args['id_user'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['participant_uuid'] is not None:
participant : TeraParticipant = TeraParticipant.get_participant_by_uuid(args['participant_uuid'])
if participant:
Expand All @@ -187,7 +187,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_participant == args['id_participant'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['device_uuid'] is not None:
device : TeraDevice = TeraDevice.get_device_by_uuid(args['device_uuid'])
if device:
Expand All @@ -196,7 +196,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_device == args['id_device'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['session_uuid'] is not None:
session : TeraSession = TeraSession.get_session_by_uuid(args['session_uuid'])
if session:
Expand All @@ -205,7 +205,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_session == args['id_session'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['test_type_uuid'] is not None:
test_type : TeraTestType = TeraTestType.get_test_type_by_uuid(args['test_type_uuid'])
if test_type:
Expand All @@ -214,7 +214,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_test_type == args['id_test_type'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['id_project'] is not None:
project : TeraProject = TeraProject.get_project_by_id(args['id_project'])
if project and project.id_project in service_access.get_accessible_projects_ids():
Expand All @@ -223,10 +223,7 @@ def get(self):
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraParticipant.id_project == project.id_project).all():

invitations.append(invitation.to_json())

if args['with_uuids']:
invitations = self._insert_uuids_to_invitations(invitations)
invitations.append(invitation.to_json(minimal=not args['full']))

if args['with_urls']:
invitations = self._insert_urls_to_invitations(invitations)
Expand Down Expand Up @@ -291,17 +288,17 @@ def post(self):
try:
if invitation['id_test_invitation'] == 0:
# create new invitation
new_invitation = TeraTestInvitation()
new_invitation : TeraTestInvitation = TeraTestInvitation()
new_invitation.from_json(invitation)
TeraTestInvitation.insert(new_invitation)
response_data.append(new_invitation.to_json())
response_data.append(new_invitation.to_json(minimal=False))
else:
# Update existing invitation (only count)
existing_invitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation'])
existing_invitation : TeraTestInvitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation'])
if existing_invitation:
TeraTestInvitation.update(existing_invitation.id_test_invitation,
{'test_invitation_count': invitation['test_invitation_count']})
response_data.append(existing_invitation.to_json())
response_data.append(existing_invitation.to_json(minimal=False))

except KeyError:
return gettext('Required parameter is missing'), 400
Expand All @@ -317,7 +314,7 @@ def post(self):
except SchemaError as e:
return gettext('Invalid JSON schema') + str(e), 400

return self._insert_uuids_to_invitations(response_data)
return response_data

@api.doc(description='Delete a specific test invitation',
responses={200: 'Success',
Expand Down Expand Up @@ -351,42 +348,6 @@ def delete(self):

return '', 200

def _insert_uuids_to_invitations(self, invitations : list[dict]) -> list[dict]:
"""
Add UUIDs to invitations
"""
for invitation in invitations:

# Prepare UUIDs with None
invitation['user_uuid'] = None
invitation['participant_uuid'] = None
invitation['device_uuid'] = None
invitation['session_uuid'] = None
invitation['test_type_uuid'] = None

if 'id_user' in invitation and invitation['id_user'] is not None:
user : TeraUser = TeraUser.get_user_by_id(invitation['id_user'])
if user:
invitation['user_uuid'] = user.user_uuid
if 'id_participant' in invitation and invitation['id_participant'] is not None:
participant : TeraParticipant = TeraParticipant.get_participant_by_id(invitation['id_participant'])
if participant:
invitation['participant_uuid'] = participant.participant_uuid
if 'id_device' in invitation and invitation['id_device'] is not None:
device : TeraDevice = TeraDevice.get_device_by_id(invitation['id_device'])
if device:
invitation['device_uuid'] = device.device_uuid
if 'id_session' in invitation and invitation['id_session'] is not None:
session : TeraSession = TeraSession.get_session_by_id(invitation['id_session'])
if session:
invitation['session_uuid'] = session.session_uuid
if 'id_test_type' in invitation and invitation['id_test_type'] is not None:
test_type : TeraTestType = TeraTestType.get_test_type_by_id(invitation['id_test_type'])
if test_type:
invitation['test_type_uuid'] = test_type.test_type_uuid

return invitations

def _insert_urls_to_invitations(self, invitations : list[dict]) -> list[dict]:
"""
Add URLs to invitations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
default=None)

# Additional parameters
get_parser.add_argument('with_uuids', type=bool, help="Include UUIDs in results", default=False)
get_parser.add_argument('full', type=bool, help="Include more information in invitations with user, participant, device, test_type and session.",
default=False)
get_parser.add_argument('with_urls', type=bool, help="Include URLs in results", default=False)

post_parser = api.parser()
Expand Down Expand Up @@ -157,21 +158,21 @@ def get(self):
invitations : list[dict] = []

# No arguments means we return all accessible invitations
if all(args[arg] is None or arg in ['with_uuids', 'with_urls'] for arg in args):
if all(args[arg] is None or arg in ['full', 'with_urls'] for arg in args):
for invitation in accessible_invitations:
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
else:
# Go through all args and get the requested information
if args['id_test_invitation'] is not None:
for invitation in TeraTestInvitation.query.filter(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by(
id_test_invitation=args['id_test_invitation']).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['test_invitation_key'] is not None:
for invitation in TeraTestInvitation.query.filter(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by(
test_invitation_key=args['test_invitation_key']).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['user_uuid'] is not None:
user : TeraUser = TeraUser.get_user_by_uuid(args['user_uuid'])
if user:
Expand All @@ -180,7 +181,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_user == args['id_user'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['participant_uuid'] is not None:
participant : TeraParticipant = TeraParticipant.get_participant_by_uuid(args['participant_uuid'])
if participant:
Expand All @@ -189,7 +190,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_participant == args['id_participant'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['device_uuid'] is not None:
device : TeraDevice = TeraDevice.get_device_by_uuid(args['device_uuid'])
if device:
Expand All @@ -198,7 +199,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_device == args['id_device'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['session_uuid'] is not None:
session : TeraSession = TeraSession.get_session_by_uuid(args['session_uuid'])
if session:
Expand All @@ -207,7 +208,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_session == args['id_session'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['test_type_uuid'] is not None:
test_type : TeraTestType = TeraTestType.get_test_type_by_uuid(args['test_type_uuid'])
if test_type:
Expand All @@ -216,7 +217,7 @@ def get(self):
for invitation in TeraTestInvitation.query.filter(and_(
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraTestInvitation.id_test_type == args['id_test_type'])).all():
invitations.append(invitation.to_json())
invitations.append(invitation.to_json(minimal=not args['full']))
if args['id_project'] is not None:
project : TeraProject = TeraProject.get_project_by_id(args['id_project'])
if project and project.id_project in user_access.get_accessible_projects_ids():
Expand All @@ -225,10 +226,7 @@ def get(self):
TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids),
TeraParticipant.id_project == project.id_project).all():

invitations.append(invitation.to_json())

if args['with_uuids']:
invitations = self._insert_uuids_to_invitations(invitations)
invitations.append(invitation.to_json(minimal=not args['full']))

if args['with_urls']:
invitations = self._insert_urls_to_invitations(invitations)
Expand Down Expand Up @@ -296,14 +294,14 @@ def post(self):
new_invitation = TeraTestInvitation()
new_invitation.from_json(invitation)
TeraTestInvitation.insert(new_invitation)
response_data.append(new_invitation.to_json())
response_data.append(new_invitation.to_json(minimal=False))
else:
# Update existing invitation (only count)
existing_invitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation'])
existing_invitation : TeraTestInvitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation'])
if existing_invitation:
TeraTestInvitation.update(existing_invitation.id_test_invitation,
{'test_invitation_count': invitation['test_invitation_count']})
response_data.append(existing_invitation.to_json())
response_data.append(existing_invitation.to_json(minimal=False))

except KeyError:
return gettext('Required parameter is missing'), 400
Expand All @@ -319,7 +317,7 @@ def post(self):
except SchemaError as e:
return gettext('Invalid JSON schema') + str(e), 400

return self._insert_uuids_to_invitations(response_data)
return response_data, 200

@api.doc(description='Delete a specific test invitation',
responses={200: 'Success',
Expand Down Expand Up @@ -352,42 +350,6 @@ def delete(self):

return '', 200

def _insert_uuids_to_invitations(self, invitations : list[dict]) -> list[dict]:
"""
Add UUIDs to invitations
"""
for invitation in invitations:

# Prepare UUIDs with None
invitation['user_uuid'] = None
invitation['participant_uuid'] = None
invitation['device_uuid'] = None
invitation['session_uuid'] = None
invitation['test_type_uuid'] = None

if 'id_user' in invitation and invitation['id_user'] is not None:
user : TeraUser = TeraUser.get_user_by_id(invitation['id_user'])
if user:
invitation['user_uuid'] = user.user_uuid
if 'id_participant' in invitation and invitation['id_participant'] is not None:
participant : TeraParticipant = TeraParticipant.get_participant_by_id(invitation['id_participant'])
if participant:
invitation['participant_uuid'] = participant.participant_uuid
if 'id_device' in invitation and invitation['id_device'] is not None:
device : TeraDevice = TeraDevice.get_device_by_id(invitation['id_device'])
if device:
invitation['device_uuid'] = device.device_uuid
if 'id_session' in invitation and invitation['id_session'] is not None:
session : TeraSession = TeraSession.get_session_by_id(invitation['id_session'])
if session:
invitation['session_uuid'] = session.session_uuid
if 'id_test_type' in invitation and invitation['id_test_type'] is not None:
test_type : TeraTestType = TeraTestType.get_test_type_by_id(invitation['id_test_type'])
if test_type:
invitation['test_type_uuid'] = test_type.test_type_uuid

return invitations

def _insert_urls_to_invitations(self, invitations : list[dict]) -> list[dict]:
"""
Add URLs to invitations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def decorated(*args, **kwargs):

# Use the service token to get the test invitation
response = ServiceAccessManager.service.get_from_opentera('/api/service/tests/invitations',
params={'test_invitation_key': test_invitation_key, 'with_uuids': True})
params={'test_invitation_key': test_invitation_key, 'full': True})

# Check if the test invitation is valid
if response.status_code != 200 or len(response.json()) != 1:
Expand Down
Loading

0 comments on commit d296c5b

Please sign in to comment.