|
4 | 4 | from django.db.utils import IntegrityError |
5 | 5 | from django.http import JsonResponse |
6 | 6 | from django.core.exceptions import ValidationError as DjangoValidationError |
| 7 | +from django.db import models |
7 | 8 | from pydantic import ValidationError |
8 | 9 |
|
9 | 10 | from django_email_learning.api import serializers |
10 | 11 | from django_email_learning.models import ( |
11 | 12 | Course, |
12 | 13 | CourseContent, |
13 | 14 | ImapConnection, |
| 15 | + Lesson, |
14 | 16 | OrganizationUser, |
15 | 17 | Organization, |
16 | 18 | ) |
@@ -68,6 +70,14 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt |
68 | 70 | try: |
69 | 71 | serializer = serializers.CreateCourseContentRequest.model_validate(payload) |
70 | 72 | course = Course.objects.get(id=kwargs["course_id"]) |
| 73 | + if serializer.priority is None: |
| 74 | + # Set priority to max existing priority + 1 |
| 75 | + max_priority = ( |
| 76 | + CourseContent.objects.filter(course_id=course.id) |
| 77 | + .aggregate(max_priority=models.Max("priority")) |
| 78 | + .get("max_priority") |
| 79 | + ) |
| 80 | + serializer.priority = (max_priority or 0) + 1 |
71 | 81 | course_content = serializer.to_django_model(course=course) |
72 | 82 |
|
73 | 83 | return JsonResponse( |
@@ -130,6 +140,8 @@ def delete(self, request, *args, **kwargs): # type: ignore[no-untyped-def] |
130 | 140 | except (IntegrityError, ValueError) as e: |
131 | 141 | return JsonResponse({"error": str(e)}, status=409) |
132 | 142 |
|
| 143 | + # TODO: Implement POST method for updating course content. |
| 144 | + |
133 | 145 |
|
134 | 146 | @method_decorator(accessible_for(roles={"admin", "editor"}), name="post") |
135 | 147 | @method_decorator(accessible_for(roles={"admin", "editor"}), name="delete") |
@@ -255,6 +267,29 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt |
255 | 267 | return JsonResponse({"error": str(e)}, status=409) |
256 | 268 |
|
257 | 269 |
|
| 270 | +@method_decorator(accessible_for(roles={"admin", "editor"}), name="post") |
| 271 | +class LessonView(View): |
| 272 | + def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
| 273 | + payload = json.loads(request.body) |
| 274 | + try: |
| 275 | + serializer = serializers.LessonUpdate.model_validate(payload) |
| 276 | + lesson = Lesson.objects.get(id=kwargs["lesson_id"]) |
| 277 | + if serializer.title is not None: |
| 278 | + lesson.title = serializer.title |
| 279 | + if serializer.content is not None: |
| 280 | + lesson.content = serializer.content |
| 281 | + lesson.save() |
| 282 | + |
| 283 | + return JsonResponse( |
| 284 | + {}, |
| 285 | + status=204, |
| 286 | + ) |
| 287 | + except Lesson.DoesNotExist: |
| 288 | + return JsonResponse({"error": "Lesson not found"}, status=404) |
| 289 | + except ValidationError as e: |
| 290 | + return JsonResponse({"error": e.errors()}, status=400) |
| 291 | + |
| 292 | + |
258 | 293 | @method_decorator(is_an_organization_member(), name="post") |
259 | 294 | class UpdateSessionView(View): |
260 | 295 | def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
|
0 commit comments