|
26 | 26 | from ..users.models import User |
27 | 27 | from .forms import ( |
28 | 28 | AssignmentForm, |
| 29 | + ChooseFileActionForm, |
| 30 | + FileActionForm, |
29 | 31 | FileSubmissionForm, |
30 | 32 | FileUploadForm, |
31 | 33 | FolderForm, |
|
35 | 37 | SubmissionCapForm, |
36 | 38 | TextSubmissionForm, |
37 | 39 | ) |
38 | | -from .models import Assignment, CooldownPeriod, QuizLogMessage, SubmissionCap |
| 40 | +from .models import Assignment, CooldownPeriod, FileAction, QuizLogMessage, SubmissionCap |
39 | 41 | from .tasks import run_moss |
40 | 42 |
|
41 | 43 | logger = logging.getLogger(__name__) |
@@ -529,6 +531,104 @@ def file_action_view(request, assignment_id, action_id): |
529 | 531 | return redirect("assignments:manage_files", assignment.id) |
530 | 532 |
|
531 | 533 |
|
| 534 | +@teacher_or_superuser_required |
| 535 | +def manage_file_actions(request, course_id: int): |
| 536 | + """Add, remove and edit :class:`.FileAction`s for a :class:`.Course` |
| 537 | +
|
| 538 | + Args: |
| 539 | + request: The request |
| 540 | + course_id: The primary key of the :class:`.Course` |
| 541 | + """ |
| 542 | + course = get_object_or_404( |
| 543 | + Course.objects.filter_editable(request.user), |
| 544 | + id=course_id, |
| 545 | + ) |
| 546 | + |
| 547 | + if request.method == "POST": |
| 548 | + form = ChooseFileActionForm(request.POST) |
| 549 | + if form.is_valid(): |
| 550 | + file_action = form.cleaned_data["file_action"] |
| 551 | + file_action.courses.add(course) |
| 552 | + return http.JsonResponse({"success": True}) |
| 553 | + return http.JsonResponse({"success": False, "errors": form.errors.as_json()}, status=400) |
| 554 | + |
| 555 | + actions = FileAction.objects.exclude(courses=course) |
| 556 | + course_actions = course.file_actions.all() |
| 557 | + return render( |
| 558 | + request, |
| 559 | + "assignments/manage_file_actions.html", |
| 560 | + { |
| 561 | + "actions": actions, |
| 562 | + "course_actions": course_actions, |
| 563 | + "course": course, |
| 564 | + "nav_item": "Manage file actions", |
| 565 | + }, |
| 566 | + ) |
| 567 | + |
| 568 | + |
| 569 | +@teacher_or_superuser_required |
| 570 | +def create_file_action(request, course_id: int): |
| 571 | + """Creates or edits a :class:`.FileAction` |
| 572 | +
|
| 573 | + If the ``GET`` request has a ``action`` parameter, |
| 574 | + the view will action as an edit view. |
| 575 | +
|
| 576 | + Args: |
| 577 | + request: The request |
| 578 | + course_id: The primary key of the :class:`.Course` |
| 579 | + """ |
| 580 | + course = get_object_or_404(Course.objects.filter_editable(request.user), id=course_id) |
| 581 | + if (action_id := request.GET.get("action", "")).isdigit(): |
| 582 | + action = get_object_or_404(course.file_actions, id=action_id) |
| 583 | + else: |
| 584 | + action = None |
| 585 | + |
| 586 | + if request.method == "POST": |
| 587 | + form = FileActionForm(request.POST, instance=action) |
| 588 | + if form.is_valid(): |
| 589 | + action = form.save(commit=False) |
| 590 | + if request.POST.get("copy"): |
| 591 | + action.pk = None |
| 592 | + action._state.adding = True |
| 593 | + action.save() |
| 594 | + action.courses.add(course) |
| 595 | + return redirect("courses:show", course.id) |
| 596 | + else: |
| 597 | + form = FileActionForm(instance=action) |
| 598 | + |
| 599 | + return render( |
| 600 | + request, |
| 601 | + "assignments/custom_file_action.html", |
| 602 | + { |
| 603 | + "form": form, |
| 604 | + "action": action, |
| 605 | + "course": course, |
| 606 | + "nav_item": "Create file action", |
| 607 | + }, |
| 608 | + ) |
| 609 | + |
| 610 | + |
| 611 | +@teacher_or_superuser_required |
| 612 | +@require_POST |
| 613 | +def delete_file_action_view(request, course_id: int): |
| 614 | + """Removes a :class:`.FileAction` from a :class:`.Course`. |
| 615 | +
|
| 616 | + This does NOT permanently delete the :class:`.FileAction`. |
| 617 | +
|
| 618 | + Args: |
| 619 | + request: The request |
| 620 | + course_id: The primary key of the :class:`.Course` |
| 621 | + action_id: The primary key of the :class:`.FileAction` |
| 622 | + """ |
| 623 | + course = get_object_or_404(Course.objects.filter_editable(request.user), id=course_id) |
| 624 | + form = ChooseFileActionForm(request.POST) |
| 625 | + if form.is_valid(): |
| 626 | + action = form.cleaned_data["file_action"] |
| 627 | + action.courses.remove(course) |
| 628 | + return http.JsonResponse({"success": True}) |
| 629 | + return http.JsonResponse({"success": False, "errors": form.errors.as_json()}, status=400) |
| 630 | + |
| 631 | + |
532 | 632 | @teacher_or_superuser_required |
533 | 633 | def student_submissions_view(request, assignment_id, student_id): |
534 | 634 | """See the submissions of a student |
|
0 commit comments