|
2 | 2 | import statistics |
3 | 3 |
|
4 | 4 | from collections import defaultdict |
| 5 | +from datetime import timedelta, timezone as datetime_timezone |
5 | 6 | from typing import List |
6 | 7 |
|
7 | 8 | from django.http import HttpRequest, HttpResponse |
|
10 | 11 | from django.utils import timezone |
11 | 12 | from django.contrib import messages |
12 | 13 | from django.shortcuts import render, get_object_or_404, redirect |
| 14 | +from django.urls import reverse |
13 | 15 |
|
14 | 16 | from django.db.models import Prefetch, Value, Count |
15 | 17 | from django.db.models import Case, When, IntegerField |
@@ -393,6 +395,144 @@ def course_view(request: HttpRequest, course_slug: str) -> HttpResponse: |
393 | 395 | return render(request, "courses/course.html", context) |
394 | 396 |
|
395 | 397 |
|
| 398 | +def escape_ics_text(value: str) -> str: |
| 399 | + return ( |
| 400 | + value.replace("\\", "\\\\") |
| 401 | + .replace("\n", "\\n") |
| 402 | + .replace(";", "\\;") |
| 403 | + .replace(",", "\\,") |
| 404 | + ) |
| 405 | + |
| 406 | + |
| 407 | +def format_ics_datetime(value) -> str: |
| 408 | + if timezone.is_naive(value): |
| 409 | + value = timezone.make_aware( |
| 410 | + value, timezone.get_current_timezone() |
| 411 | + ) |
| 412 | + |
| 413 | + value = value.astimezone(datetime_timezone.utc) |
| 414 | + return value.strftime("%Y%m%dT%H%M%SZ") |
| 415 | + |
| 416 | + |
| 417 | +def calendar_event( |
| 418 | + *, |
| 419 | + uid: str, |
| 420 | + summary: str, |
| 421 | + start, |
| 422 | + url: str, |
| 423 | + description: str, |
| 424 | + dtstamp, |
| 425 | +) -> list[str]: |
| 426 | + end = start + timedelta(minutes=30) |
| 427 | + |
| 428 | + return [ |
| 429 | + "BEGIN:VEVENT", |
| 430 | + f"UID:{escape_ics_text(uid)}", |
| 431 | + f"DTSTAMP:{format_ics_datetime(dtstamp)}", |
| 432 | + f"DTSTART:{format_ics_datetime(start)}", |
| 433 | + f"DTEND:{format_ics_datetime(end)}", |
| 434 | + f"SUMMARY:{escape_ics_text(summary)}", |
| 435 | + f"DESCRIPTION:{escape_ics_text(description)}", |
| 436 | + f"URL:{escape_ics_text(url)}", |
| 437 | + "END:VEVENT", |
| 438 | + ] |
| 439 | + |
| 440 | + |
| 441 | +def course_calendar_view( |
| 442 | + request: HttpRequest, |
| 443 | + course_slug: str, |
| 444 | +) -> HttpResponse: |
| 445 | + course = get_object_or_404(Course, slug=course_slug, visible=True) |
| 446 | + dtstamp = timezone.now() |
| 447 | + events = [] |
| 448 | + |
| 449 | + homeworks = Homework.objects.filter(course=course).order_by("due_date") |
| 450 | + for homework in homeworks: |
| 451 | + url = request.build_absolute_uri( |
| 452 | + reverse( |
| 453 | + "homework", |
| 454 | + kwargs={ |
| 455 | + "course_slug": course.slug, |
| 456 | + "homework_slug": homework.slug, |
| 457 | + }, |
| 458 | + ) |
| 459 | + ) |
| 460 | + events.extend( |
| 461 | + calendar_event( |
| 462 | + uid=f"homework-{homework.id}@courses.datatalks.club", |
| 463 | + summary=f"{course.title}: {homework.title} deadline", |
| 464 | + start=homework.due_date, |
| 465 | + url=url, |
| 466 | + description=( |
| 467 | + f"Homework deadline for {homework.title}. " |
| 468 | + f"Open the assignment: {url}" |
| 469 | + ), |
| 470 | + dtstamp=dtstamp, |
| 471 | + ) |
| 472 | + ) |
| 473 | + |
| 474 | + projects = Project.objects.filter(course=course).order_by( |
| 475 | + "submission_due_date", |
| 476 | + "peer_review_due_date", |
| 477 | + ) |
| 478 | + for project in projects: |
| 479 | + project_url = request.build_absolute_uri( |
| 480 | + reverse( |
| 481 | + "project", |
| 482 | + kwargs={ |
| 483 | + "course_slug": course.slug, |
| 484 | + "project_slug": project.slug, |
| 485 | + }, |
| 486 | + ) |
| 487 | + ) |
| 488 | + events.extend( |
| 489 | + calendar_event( |
| 490 | + uid=f"project-{project.id}-submission@courses.datatalks.club", |
| 491 | + summary=f"{course.title}: {project.title} submission deadline", |
| 492 | + start=project.submission_due_date, |
| 493 | + url=project_url, |
| 494 | + description=( |
| 495 | + f"Project submission deadline for {project.title}. " |
| 496 | + f"Open the project: {project_url}" |
| 497 | + ), |
| 498 | + dtstamp=dtstamp, |
| 499 | + ) |
| 500 | + ) |
| 501 | + events.extend( |
| 502 | + calendar_event( |
| 503 | + uid=f"project-{project.id}-peer-review@courses.datatalks.club", |
| 504 | + summary=f"{course.title}: {project.title} peer review deadline", |
| 505 | + start=project.peer_review_due_date, |
| 506 | + url=project_url, |
| 507 | + description=( |
| 508 | + f"Project peer review deadline for {project.title}. " |
| 509 | + f"Open the project: {project_url}" |
| 510 | + ), |
| 511 | + dtstamp=dtstamp, |
| 512 | + ) |
| 513 | + ) |
| 514 | + |
| 515 | + calendar_lines = [ |
| 516 | + "BEGIN:VCALENDAR", |
| 517 | + "VERSION:2.0", |
| 518 | + "PRODID:-//DataTalks.Club//Course Management Platform//EN", |
| 519 | + "CALSCALE:GREGORIAN", |
| 520 | + "METHOD:PUBLISH", |
| 521 | + f"X-WR-CALNAME:{escape_ics_text(course.title)} deadlines", |
| 522 | + *events, |
| 523 | + "END:VCALENDAR", |
| 524 | + ] |
| 525 | + |
| 526 | + response = HttpResponse( |
| 527 | + "\r\n".join(calendar_lines) + "\r\n", |
| 528 | + content_type="text/calendar; charset=utf-8", |
| 529 | + ) |
| 530 | + response["Content-Disposition"] = ( |
| 531 | + f'inline; filename="{course.slug}-deadlines.ics"' |
| 532 | + ) |
| 533 | + return response |
| 534 | + |
| 535 | + |
396 | 536 | def get_homeworks_for_course(course: Course, user) -> List[Homework]: |
397 | 537 | if user.is_authenticated: |
398 | 538 | queryset = Submission.objects.filter(student=user) |
|
0 commit comments