Skip to content
Snippets Groups Projects
Commit 64240536 authored by Max Lou's avatar Max Lou
Browse files

Fix: random statement generator and LRS forwarding

parent c487237d
Branches
Tags
No related merge requests found
......@@ -17,10 +17,10 @@ TOKEN = settings.LRS.get('TOKEN')
def store_in_lrs(x_api_statement):
header = {"Authorization": f"Basic {TOKEN}",
"X-Experience-API-Version": "1.0.3"}
response = requests.post(URL, json=x_api_statement,
response = requests.post(f'{URL}/data/xAPI/statements', json=x_api_statement,
headers=header
)
print(response.content)
return response.ok
class CreateStatement(APIView):
......@@ -33,7 +33,8 @@ class CreateStatement(APIView):
except ObjectDoesNotExist:
return JsonResponse({"message": "No consent provider available."}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
for provider in latest_consent.definition:
essential_verbs.extend([verb["id"] for verb in provider["essentialVerbs"]])
essential_verbs.extend([verb["id"]
for verb in provider["essentialVerbs"]])
# validate statement
if (not "actor" in request.data) or (not "mbox" in request.data["actor"]) or (not request.data["actor"]["mbox"].startswith("mailto:")):
......@@ -62,7 +63,8 @@ class CreateStatement(APIView):
return JsonResponse({"message": 'discarded. User has paused data collection'}, status=status.HTTP_200_OK)
# has the user given consent to this verb?
user_consent = UserConsents.objects.filter(user=user, verb=verb, consented=True).first()
user_consent = UserConsents.objects.filter(
user=user, verb=verb, consented=True).first()
if not user_consent:
return JsonResponse({"message": 'error. User has not given consent to this verb'}, status=status.HTTP_403_FORBIDDEN)
......@@ -70,7 +72,7 @@ class CreateStatement(APIView):
# has the user given consent to the object at hand?
object_consent = False
verb_objects = json.loads(user_consent.object)
if not verb_objects: # no objects defined, "consent" by default
if not verb_objects: # no objects defined, "consent" by default
object_consent = True
else:
for obj in verb_objects:
......@@ -80,5 +82,8 @@ class CreateStatement(APIView):
if not object_consent:
return JsonResponse({"message": 'error. User has not given consent to this object'}, status=status.HTTP_403_FORBIDDEN)
store_in_lrs(request.data)
return JsonResponse({"message": 'processed'}, status=status.HTTP_200_OK)
response_ok = store_in_lrs(request.data)
if response_ok:
return JsonResponse({"message": 'processed'}, status=status.HTTP_200_OK)
else:
return JsonResponse({"message": 'LRS request failed'}, status=status.HTTP_400_BAD_REQUEST)
......@@ -91,12 +91,11 @@ def get_rand_statement():
def start_random_generation(args):
random_statement = get_rand_statement()
url = args.target[0]
print('Running...')
while True:
send_x_api_statement(url, random_statement)
send_x_api_statement(url, get_rand_statement())
time.sleep(1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment