Skip to content

Commit

Permalink
Merge pull request #104 from hpi-sam/67-n00-backend-adapt-to-updated-…
Browse files Browse the repository at this point in the history
…frontend-api

67 n00 backend adapt to updated frontend api
  • Loading branch information
Toni000 authored Mar 25, 2024
2 parents 63290e7 + 3309c61 commit ba294ac
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
12 changes: 6 additions & 6 deletions backend/dps_training_k/game/consumers/abstract_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,17 @@ def authenticate(self, token):
def _send_exercise(self):
exercise = Exercise.createExercise()
patient = Patient.objects.create(
name="Max Mustermann", exercise=exercise, patientCode=123456
name="Max Mustermann", exercise=exercise, patientId=123456
)
exercise_object = {
"exercise": {
"exerciseId": exercise.invitation_code,
"exerciseId": exercise.exerciseId,
"areas": [
{
"areaName": "X",
"patients": [
{
"patientId": patient.patientCode,
"patientId": patient.patientId,
"patientName": patient.name,
"patientCode": 0
}
Expand All @@ -176,10 +176,10 @@ def _send_exercise(self):
"personnelName": "X"
}
],
"devices": [
"material": [
{
"deviceId": 0,
"deviceName": "X"
"materialId": 0,
"materialName": "X"
}
]
}
Expand Down
14 changes: 12 additions & 2 deletions backend/dps_training_k/game/consumers/patient_consumer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from .abstract_consumer import AbstractConsumer
from urllib.parse import parse_qs
from game.models import Exercise, Patient


class PatientConsumer(AbstractConsumer):
class PatientIncomingMessageTypes:
EXAMPLE = "example"
TEST_PASSTHROUGH = "test-passthrough"

class PatientOutgoingMessageTypes:
RESPONSE = "response"
EXERCISE = "exercise"
TEST_PASSTHROUGH = "test-passthrough"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -19,7 +20,10 @@ def __init__(self, *args, **kwargs):
self.handle_example,
"exercise_code",
"patient_code",
)
),
self.PatientIncomingMessageTypes.TEST_PASSTHROUGH: (
self.handle_test_passthrough,
),
}

def connect(self):
Expand All @@ -36,3 +40,9 @@ def handle_example(self, exercise_code, patient_code):
self.PatientOutgoingMessageTypes.RESPONSE,
content=f"exercise_code {self.exercise_code} & patient_code {self.patient_code}",
)

def handle_test_passthrough(self):
self.send_event(
self.PatientOutgoingMessageTypes.TEST_PASSTHROUGH,
message="received test event",
)
2 changes: 0 additions & 2 deletions backend/dps_training_k/game/consumers/trainer_consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from .abstract_consumer import AbstractConsumer
from game.models import Exercise, Patient
from django.conf import settings


class TrainerConsumer(AbstractConsumer):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.1 on 2024-03-22 10:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('game', '0004_remove_scheduledevent_owner_owner_event'),
]

operations = [
migrations.RenameField(
model_name='exercise',
old_name='invitation_code',
new_name='exerciseId',
),
migrations.RemoveField(
model_name='patient',
name='patientCode',
),
migrations.AddField(
model_name='patient',
name='patientId',
field=models.IntegerField(default=1, help_text='patientId used to log into patient - therefore part of authentication'),
preserve_default=False,
),
]
4 changes: 2 additions & 2 deletions backend/dps_training_k/game/models/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ExerciseStateTypes(models.TextChoices):
to="Trainer",
on_delete=models.CASCADE,
) """
invitation_code = models.CharField(
exerciseId = models.CharField(
unique=True,
editable=settings.DEBUG,
max_length=settings.INVITATION_LOGIC.code_length,
Expand All @@ -35,7 +35,7 @@ def createExercise(cls):
new_Exercise = cls.objects.create(
# config=settings.DEFAULT_EXCERCISE_CONFIG,
# trainer=trainer
invitation_code=settings.INVITATION_LOGIC.get_invitation_code(),
exerciseId=settings.INVITATION_LOGIC.get_exerciseId(),
state=cls.ExerciseStateTypes.CONFIGURATION,
)
return new_Exercise
Expand Down
8 changes: 4 additions & 4 deletions backend/dps_training_k/game/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class Patient(Eventable, models.Model):
name = models.CharField(
max_length=100, default="Max Mustermann"
) # technically patientData but kept here for simplicity for now
# patientID = models.ForeignKey() # currently called "SensenID"
# patientCode = models.ForeignKey() # currently called "SensenID"
exercise = models.ForeignKey("Exercise", on_delete=models.CASCADE)
# stateID = models.ForeignKey()
# measureID = models.ManyToManyField()
patientCode = models.IntegerField(
help_text="patientCode used to log into patient - therefore part of authentication"
patientId = models.IntegerField(
help_text="patientId used to log into patient - therefore part of authentication"
)

def __str__(self):
return f"Patient #{self.id} called {self.name} with code {self.patientCode}"
return f"Patient #{self.id} called {self.name} with ID {self.patientId}"

# ToDo remove when actual method is implemented
def schedule_temporary_event(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_access_success(self):
"""
Test the access with correct credentials.
"""
data = {"exerciseCode": "testuser", "patientCode": "testpassword"}
data = {"exerciseId": "testuser", "patientId": "testpassword"}
response = self.client.post(self.access_url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check if the response contains a token
Expand All @@ -27,7 +27,7 @@ def test_access_fail_wrong_credentials(self):
"""
Test the access with wrong credentials.
"""
data = {"exerciseCode": "wronguser", "patientCode": "wrongpassword"}
data = {"exerciseId": "wronguser", "patientId": "wrongpassword"}
response = self.client.post(self.access_url, data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# Check that the response indicates a failure
Expand All @@ -39,8 +39,8 @@ def test_access_fail_missing_fields(self):
Test the access with missing fields.
"""
data = {
"exerciseCode": "testuser"
# Missing "patientCode"
"exerciseId": "testuser"
# Missing "patientId"
}
response = self.client.post(self.access_url, data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand Down
6 changes: 3 additions & 3 deletions backend/dps_training_k/game/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

class PatientAccessView(APIView):
def post(self, request, *args, **kwargs):
if not (request.data.get("exerciseCode") and request.data.get("patientCode")):
if not (request.data.get("exerciseId") and request.data.get("patientId")):
return Response(
status=status.HTTP_400_BAD_REQUEST,
data="Some required fields are missing",
)
exercise_code = request.data.get("exerciseCode")
patient_code = request.data.get("patientCode")
exercise_code = request.data.get("exerciseId")
patient_code = request.data.get("patientId")
user = authenticate(username=exercise_code, password=patient_code)
if user:
token, created = Token.objects.get_or_create(user=user)
Expand Down
6 changes: 3 additions & 3 deletions backend/dps_training_k/helpers/invitation_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def __init__(self, code_length):
self.code_length = code_length
self.codes_taken = []

def get_invitation_code(self):
def get_exerciseId(self):
"""
Generates an unique alphabetic invitation_code of length self.code_length,
Generates an unique alphabetic exerciseId of length self.code_length,
that is also distinctive from other non-finished excercises (levenshtein distance >= 3).
:return: a string, the invitation_code
:return: a string, the exerciseId
"""
new_code = None
letters = string.ascii_lowercase
Expand Down

0 comments on commit ba294ac

Please sign in to comment.