Commit e62501c9 authored by Bruno Martin's avatar Bruno Martin
Browse files

refactor class to its own django app

parent 88ec9df7
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.text import slugify
from django.conf import settings
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'),
default=False,
)
user_can_certificate_even_without_progress = models.BooleanField(
_('Certification Allowed Even Without Progress'),
default=False,
)
def __str__(self):
return u'%s @ %s' % (self.name, self.course)
def get_absolute_url(self):
return reverse('class', kwargs={'pk': self.id})
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):
from courses.models import CourseStudent
return CourseStudent.objects.filter(course=self.course, user__in=self.students.all()).order_by('user__first_name')
from django.db.models.signals import post_save
from courses.models import CourseStudent
from courses_certification.models import CourseCertification
@receiver(m2m_changed, sender=Class.students.through)
def remove_duplicate_classes(sender, instance, action, reverse, model, pk_set, **kwargs):
"""Clean the same student in twice classes."""
# add student to default_class everytime that m2m cleans
if action == 'pre_clear':
default_class = instance.course.default_class
for student in instance.students.all():
student.classes.add(default_class)
# garantee that student has been subscribe in only one class
if action == 'post_add':
try:
for student in instance.students.all():
classes = student.classes.filter(course=instance.course).exclude(id=instance.id)
for classe in classes:
student.classes.remove(classe)
# garantee that this block executes only when instance == Class.
except AttributeError:
pass
......@@ -19,90 +19,11 @@ from .utils import hash_name
from .videos.models import Video
from .notes.models import Note
from .course_material.models import CourseMaterial
from .classes.models import Class
import re
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'),
default=False,
)
user_can_certificate_even_without_progress = models.BooleanField(
_('Certification Allowed Even Without Progress'),
default=False,
)
def __str__(self):
return u'%s @ %s' % (self.name, self.course)
def get_absolute_url(self):
return reverse('class', kwargs={'pk': self.id})
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):
return CourseStudent.objects.filter(course=self.course, user__in=self.students.all()).order_by('user__first_name')
def remove_duplicate_classes(sender, instance, action, reverse, model, pk_set, **kwargs):
"""Clean the same student in twice classes."""
# add student to default_class everytime that m2m cleans
if action == 'pre_clear':
default_class = instance.course.default_class
for student in instance.students.all():
student.classes.add(default_class)
# garantee that student has been subscribe in only one class
if action == 'post_add':
try:
for student in instance.students.all():
classes = student.classes.filter(course=instance.course).exclude(id=instance.id)
for classe in classes:
student.classes.remove(classe)
# garantee that this block executes only when instance == Class.
except AttributeError:
pass
m2m_changed.connect(remove_duplicate_classes, sender=Class.students.through)
class Course(models.Model):
STATES = (
('draft', _('Draft')),
......@@ -197,7 +118,7 @@ class Course(models.Model):
default=False,
)
default_class = models.OneToOneField(
Class,
'Class',
models.SET_NULL,
verbose_name=_('Default Class'),
related_name='default_course',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment