Skip to content
Snippets Groups Projects
models.py 41.6 KiB
Newer Older
    deadline = models.DateTimeField(
        help_text=ugettext_lazy(
            u"The datetime after which users are no longer allowed "
            "to submit photos for verification."
    # The system prefers to set this automatically based on default settings. But
    # if the field is set manually we want a way to indicate that so we don't
    # overwrite the manual setting of the field.
    deadline_is_explicit = models.BooleanField(default=False)
    def set_deadline(cls, course_key, deadline, is_explicit=False):
        """
        Configure the verification deadline for a course.

        If `deadline` is `None`, then the course will have no verification
        deadline.  In this case, users will be able to verify for the course
        at any time.

        Arguments:
            course_key (CourseKey): Identifier for the course.
            deadline (datetime or None): The verification deadline.

        """
        if deadline is None:
            VerificationDeadline.objects.filter(course_key=course_key).delete()
        else:
            record, created = VerificationDeadline.objects.get_or_create(
                course_key=course_key,
                defaults={"deadline": deadline, "deadline_is_explicit": is_explicit}
            )

            if not created:
                record.deadline = deadline
                record.deadline_is_explicit = is_explicit
    def deadlines_for_enrollments(cls, enrollments_qs):
        Retrieve verification deadlines for a user's enrolled courses.
            enrollments_qs: CourseEnrollment queryset. For performance reasons
                            we want the queryset here instead of passing in a
                            big list of course_keys in an "SELECT IN" query.
                            If we have a queryset, Django is smart enough to do
                            a performant subquery at the MySQL layer instead of
                            passing down all the course_keys through Python.

        Returns:
            dict: Map of course keys to datetimes (verification deadlines)
        """
        verification_deadlines = VerificationDeadline.objects.filter(
            course_key__in=enrollments_qs.values('course_id')
        )
            deadline.course_key: deadline.deadline
            for deadline in verification_deadlines
        }

    @classmethod
    def deadline_for_course(cls, course_key):
        """
        Retrieve the verification deadline for a particular course.

        Arguments:
            course_key (CourseKey): The identifier for the course.

        Returns:
            datetime or None

        """
        try:
            deadline = cls.objects.get(course_key=course_key)
            return deadline.deadline
        except cls.DoesNotExist:
            return None