|
1 | | -from datetime import date, timedelta |
2 | | -from pathlib import Path |
| 1 | +from datetime import timedelta |
3 | 2 |
|
| 3 | +from celery.result import AsyncResult |
| 4 | +from django.http import JsonResponse |
4 | 5 | from django.shortcuts import get_object_or_404, redirect, render |
5 | 6 | from django.utils import timezone |
6 | 7 |
|
|
14 | 15 | StudentForm, |
15 | 16 | ) |
16 | 17 | from .models import Course, Period, StudentImport |
| 18 | +from .tasks import import_course_data_tasks |
17 | 19 |
|
18 | 20 |
|
19 | 21 | # Create your views here. |
@@ -178,66 +180,10 @@ def import_from_selected_course(request, course_id, other_course_id): |
178 | 180 | if request.method == "POST": |
179 | 181 | form = ImportFromSelectedCourseForm(data=request.POST, course=other_course) |
180 | 182 | if form.is_valid(): |
181 | | - assignments_to_process = [] |
182 | | - |
183 | | - # Import folders |
184 | | - if form.cleaned_data["folders"]: |
185 | | - for folder in form.cleaned_data["folders"]: |
186 | | - assignments = list(folder.assignments.all()) |
187 | | - folder.pk = None |
188 | | - folder._state.adding = True |
189 | | - folder.course = course |
190 | | - folder.save() |
191 | | - for assignment in assignments: |
192 | | - assignments_to_process.append((assignment, course, folder)) |
193 | | - |
194 | | - # Import assignments |
195 | | - if form.cleaned_data["assignments"]: |
196 | | - for assignment in form.cleaned_data["assignments"]: |
197 | | - assignments_to_process.append((assignment, course, None)) |
198 | | - |
199 | | - for assignment, course, folder in assignments_to_process: |
200 | | - old_id = assignment.id |
201 | | - |
202 | | - # Save as new |
203 | | - assignment.pk = None |
204 | | - assignment._state.adding = True |
205 | | - |
206 | | - # Update course, folder, assigned date, and grader file |
207 | | - assignment.course = course |
208 | | - if folder: |
209 | | - assignment.folder = folder |
210 | | - assignment.assigned = timezone.now() |
211 | | - assignment.grader_file = None |
212 | | - |
213 | | - # Some user options need to be applied before saving |
214 | | - if form.cleaned_data["hide"]: |
215 | | - assignment.hidden = True |
216 | | - if form.cleaned_data["shift_due_dates"]: |
217 | | - due = assignment.due |
218 | | - try: |
219 | | - assignment.due = due.replace(year=assignment.due.year + 1) |
220 | | - except ValueError: # February 29 -> February 28 |
221 | | - assignment.due = due + date(due.year + 1, 3, 1) - date(due.year, 3, 1) |
222 | | - |
223 | | - assignment.save() |
224 | | - |
225 | | - # Make directory with new ID |
226 | | - assignment.make_assignment_dir() |
227 | | - |
228 | | - # Access the old assignment |
229 | | - old_assignment = Assignment.objects.get(id=old_id) |
230 | | - |
231 | | - if form.cleaned_data["copy_graders"] and old_assignment.grader_file: |
232 | | - with open(old_assignment.grader_file.path) as f: |
233 | | - assignment.save_grader_file(f.read()) # Save to new directory |
234 | | - |
235 | | - if form.cleaned_data["copy_files"]: |
236 | | - for _, filename, path, _, _ in old_assignment.list_files(): |
237 | | - content = Path(path).read_bytes() |
238 | | - assignment.save_file(content, filename) |
| 183 | + task_data = form.serialize_for_task() |
239 | 184 |
|
240 | | - return redirect("courses:show", course.id) |
| 185 | + task = import_course_data_tasks.delay(course.id, other_course.id, task_data) |
| 186 | + return redirect("courses:import_status", course_id=course.id, task_id=task.id) |
241 | 187 | else: |
242 | 188 | form = ImportFromSelectedCourseForm(course=other_course) |
243 | 189 |
|
@@ -424,3 +370,26 @@ def edit_period_view(request, course_id, period_id): |
424 | 370 | "courses/edit_create.html", |
425 | 371 | {"form": form, "course": course, "nav_item": "Edit Period"}, |
426 | 372 | ) |
| 373 | + |
| 374 | + |
| 375 | +@teacher_or_superuser_required |
| 376 | +def import_status_view(request, course_id, task_id): |
| 377 | + course = get_object_or_404(Course.objects.filter_editable(request.user), id=course_id) |
| 378 | + task = AsyncResult(task_id) |
| 379 | + |
| 380 | + if request.headers.get("X-Requested-With") == "XMLHttpRequest": |
| 381 | + response_data = { |
| 382 | + "state": task.state, |
| 383 | + "ready": task.ready(), |
| 384 | + } |
| 385 | + if task.state == "PROGRESS": |
| 386 | + response_data.update(task.info) |
| 387 | + elif task.ready(): |
| 388 | + response_data["result"] = task.result |
| 389 | + return JsonResponse(response_data) |
| 390 | + |
| 391 | + return render( |
| 392 | + request, |
| 393 | + "courses/import_status.html", |
| 394 | + {"course": course, "task_id": task_id, "nav_item": "Import Status"}, |
| 395 | + ) |
0 commit comments