models.py 2.21 KB
Newer Older
1 2 3 4
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.text import slugify
from django.conf import settings
5
from django.urls import reverse
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

class Class(models.Model):
    name = models.CharField(
        max_length=255,
    )
    assistants = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        verbose_name=_('Assistants'),
        related_name='professor_classes',
        blank=True,
    )
    students = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='classes',
        blank=True,
    )
    course = models.ForeignKey(
        'Course',
        models.CASCADE,
        verbose_name=_('Course'),
    )
    user_can_certificate = models.BooleanField(
        _('Certification Allowed'),
29
        default=True,
30 31 32 33 34
    )
    user_can_certificate_even_without_progress = models.BooleanField(
        _('Certification Allowed Even Without Progress'),
        default=False,
    )
35 36 37 38 39 40 41
    certificate_template = models.ForeignKey(
        'certification.CertificateTemplate',
        models.SET_NULL,
        verbose_name=_('CertificateTemplate'),
        related_name='classes',
        null=True,
    )
42 43 44 45 46

    def __str__(self):
        return u'%s @ %s' % (self.name, self.course)

    def get_absolute_url(self):
47
        return reverse('courses_legacy:class', kwargs={'pk': self.id})
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    def add_students(self, *objs):
        for obj in objs:
            try:
                c = Class.objects.get(course=self.course, students=obj)
                c.students.remove(obj)
            except Class.DoesNotExist:
                pass
            self.students.add(obj)

    def remove_students(self, *objs):
        for obj in objs:
            self.students.remove(obj)
            if CourseStudent.objects.filter(course=self.course,
                                            user=obj).exists():
                self.course.default_class.students.add(obj)

    @property
    def get_students(self):
67
        return self.course.course_students.filter(
68
            user__in=self.students.all(), user__is_active=True
69
        ).select_related('user').order_by('user__first_name')
70 71 72 73

    @property
    def valid_students(self):
        return self.students.filter(is_active=True)