Skip to content

Commit a94525c

Browse files
authored
Merge branch 'main' into video_call_ui
2 parents 7fdc2d5 + 8a7fc93 commit a94525c

File tree

446 files changed

+50706
-2386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

446 files changed

+50706
-2386
lines changed

backend/apps/lessons/tests.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,76 @@
11
from django.test import TestCase
2+
from django.urls import reverse
3+
from rest_framework.test import APITestCase
4+
from rest_framework import status
5+
from django.contrib.auth.models import User
6+
from apps.lessons.models import Assignment
27

38
# Create your tests here.
9+
class AssignmentAPITests(APITestCase):
10+
def setUp(self):
11+
# Create a superuser for testing
12+
self.user = User.objects.create_superuser(
13+
username='testadmin', password='testpassword', email='test@test.com'
14+
)
15+
16+
# Get access token
17+
response = self.client.post(
18+
reverse('token_obtain_pair'), # Use the actual name of the URL if configured
19+
{'username': 'testadmin', 'password': 'testpassword'},
20+
)
21+
self.access_token = response.data['access']
22+
23+
# Set authorization header
24+
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {self.access_token}')
25+
26+
# Create an initial assignment for testing
27+
self.assignment = Assignment.objects.create(
28+
title="Initial Assignment",
29+
description="Initial assignment description.",
30+
assignment_type="HW",
31+
deadline="2025-12-01T12:00:00Z",
32+
)
33+
34+
# Define URLs
35+
self.detail_url = reverse('assignment-detail', kwargs={'pk': self.assignment.pk})
36+
self.create_url = reverse('assignment-create')
37+
38+
# hardcode?
39+
# self.list_url = '/lessons/assignments/'
40+
#self.detail_url = f'/lessons/assignments/{self.assignment.pk}/'
41+
#self.create_url = '/lessons/assignments/create/'
42+
43+
# Test GET single assignment
44+
def test_get_assignment(self):
45+
response = self.client.get(self.detail_url)
46+
self.assertEqual(response.status_code, status.HTTP_200_OK)
47+
self.assertEqual(response.data['data']['title'], 'Initial Assignment')
48+
49+
# Test POST create new assignment
50+
def test_create_assignment(self):
51+
data = {
52+
"title": "New Assignment",
53+
"description": "This is a new assignment.",
54+
"assignment_type": "HW",
55+
"deadline": "2025-12-01T12:00:00Z"
56+
}
57+
response = self.client.post(self.create_url, data, format='json')
58+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
59+
self.assertEqual(response.data['data']['title'], 'New Assignment')
60+
61+
# Test PATCH update assignment
62+
def test_update_assignment(self):
63+
data = {"title": "Updated Title"}
64+
response = self.client.patch(self.detail_url, data, format='json')
65+
self.assertEqual(response.status_code, status.HTTP_200_OK)
66+
self.assertEqual(response.data['data']['title'], 'Updated Title')
67+
68+
# Test DELETE assignment
69+
def test_delete_assignment(self):
70+
response = self.client.delete(self.detail_url)
71+
self.assertEqual(response.status_code, status.HTTP_200_OK)
72+
self.assertEqual(response.data['status'], 'success')
73+
74+
# Verify it is deleted
75+
response = self.client.get(self.detail_url)
76+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

backend/apps/search/serializers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ class TutorSearchResultSerializer(serializers.ModelSerializer):
77
first_name = serializers.CharField(source="profile.user.first_name")
88
last_name = serializers.CharField(source="profile.user.last_name")
99
image_url = serializers.SerializerMethodField() # Custom field to get the file URL
10+
subjects = serializers.SerializerMethodField()
1011

1112
# Specify the model and fields to be included in the serialized output
1213
class Meta:
1314
model = TutorProfile
14-
fields = ["first_name", "last_name", "bio", "hourly_rate", "state", "city", "rating", "image_url"]
15+
fields = ["first_name", "last_name", "bio", "hourly_rate", "state", "city", "rating", "image_url", "subjects"]
1516

1617
def get_image_url(self, result_data):
1718
# Ensure that the profile and profile_picture exist before accessing them
1819
if hasattr(result_data, "profile") and hasattr(result_data.profile, "upload_record"):
1920
return UploadRecord.objects.build_url(result_data.profile.upload_record)
2021
return None # Return None if there's no profile picture
22+
23+
def get_subjects(self, result_data):
24+
# Extract only the titles of the subjects
25+
return list(result_data.subjects.values_list("title", flat=True))

backend/apps/search/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get(self, request, format=None):
9696
# Perform search with 'what' term
9797
search_results = TutorProfile.objects.search(filtered_tutors, what)
9898
# Combine with partial_tutor_matches
99-
partial_tutor_matches = TutorProfile.objects.filter(lookup_tutors_query)
99+
partial_tutor_matches = filtered_tutors.filter(lookup_tutors_query) #changed now
100100
search_results = (search_results | partial_tutor_matches)
101101

102102
try:

backend/apps/users/managers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.db import models
22
from watson import search
3+
from django.db.models import Prefetch
4+
from apps.search.models import Subject
35

46
class ProfileManager(models.Manager):
57

@@ -76,7 +78,9 @@ def search(self, filtered_tutors, what):
7678

7779
def get_result_data(self, search_results):
7880
# Use select_related to reduce queries and retrieve only the required fields
79-
search_results = search_results.select_related('profile__user', 'profile').only(
81+
search_results = search_results.select_related('profile__user', 'profile').prefetch_related(
82+
Prefetch('subjects', queryset=Subject.objects.only('title'))
83+
).only(
8084
'profile__user__first_name',
8185
'profile__user__last_name',
8286
'bio',

backend/apps/users/views.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.contrib.auth import authenticate
55
from django.http import JsonResponse, HttpResponse
66
from django.middleware.csrf import get_token
7+
from .models import Profile
78
from apps.users.models import Profile, TutorProfile
89
from apps.uploads.models import UploadRecord, ProfilePicture
910
from rest_framework import status
@@ -62,7 +63,9 @@ def register_profile(request):
6263
first_name = request.data["firstName"]
6364
last_name = request.data["lastName"]
6465
password = request.data["password"]
65-
role = request.data["role"] # Get the selected role
66+
# TODO: replace "1" with "role" once that is handled by the frontend
67+
# role = request.data["role"] # Get the selected role
68+
role = 3
6669
# Create user
6770
user = User.objects.create_user(
6871
username=username,
@@ -73,6 +76,7 @@ def register_profile(request):
7376
)
7477
# Create associated Profile
7578
profile = Profile.objects.create(user, role)
79+
7680

7781
image=request.data.get("image") #For now, get an optional image
7882

@@ -92,7 +96,6 @@ def register_profile(request):
9296
bio=request.data["bio"]
9397
hourly_rate=request.data["hourly_rate"]
9498
tutor = TutorProfile.objects.create(profile, city, state, bio, hourly_rate)
95-
9699
return Response({"message": "User registered successfully"}, status=status.HTTP_201_CREATED)
97100

98101
@api_view(['POST'])

0 commit comments

Comments
 (0)