class EmbargoedCourse(models.Model): """ Enable course embargo on a course-by-course basis. Deprecated by `RestrictedCourse` """ objects = NoneToEmptyManager() # The course to embargo course_id = CourseKeyField(max_length=255, db_index=True, unique=True) # Whether or not to embargo embargoed = models.BooleanField(default=False) @classmethod def is_embargoed(cls, course_id): """ Returns whether or not the given course id is embargoed. If course has not been explicitly embargoed, returns False. """ try: record = cls.objects.get(course_id=course_id) return record.embargoed except cls.DoesNotExist: return False def __unicode__(self): not_em = "Not " if self.embargoed: not_em = "" return u"Course '{}' is {}Embargoed".format(text_type(self.course_id), not_em)
class Role(models.Model): """ Maps users to django_comment_client roles for a given course .. no_pii: """ objects = NoneToEmptyManager() name = models.CharField(max_length=30, null=False, blank=False) users = models.ManyToManyField(User, related_name="roles") course_id = CourseKeyField(max_length=255, blank=True, db_index=True) class Meta: # use existing table that was originally created from lms.djangoapps.discussion.django_comment_client app db_table = 'django_comment_client_role' def __str__(self): return self.name + " for " + (str(self.course_id) if self.course_id else "all courses") # TODO the name of this method is a little bit confusing, # since it's one-off and doesn't handle inheritance later def inherit_permissions(self, role): """ Make this role inherit permissions from the given role. Permissions are only added, not removed. Does not handle inheritance. """ if role.course_id and role.course_id != self.course_id: logging.warning( "%s cannot inherit permissions from %s due to course_id inconsistency", self, role, ) for per in role.permissions.all(): self.add_permission(per) def add_permission(self, permission): self.permissions.add(Permission.objects.get_or_create(name=permission)[0]) # lint-amnesty, pylint: disable=no-member def has_permission(self, permission): """ Returns True if this role has the given permission, False otherwise. """ course = modulestore().get_course(self.course_id) if course is None: raise ItemNotFoundError(self.course_id) if permission_blacked_out(course, {self.name}, permission): return False return self.permissions.filter(name=permission).exists() @staticmethod def user_has_role_for_course(user, course_id, role_names): """ Returns True if the user has one of the given roles for the given course """ return Role.objects.filter(course_id=course_id, name__in=role_names, users=user).exists()
class CertificateWhitelist(models.Model): """ Tracks students who are whitelisted, all users in this table will always qualify for a certificate regardless of their grade. This model is deprecated. CertificateAllowlist should be used in its place. .. no_pii: """ class Meta: app_label = "certificates" unique_together = [['course_id', 'user']] objects = NoneToEmptyManager() user = models.ForeignKey(User, on_delete=models.CASCADE) course_id = CourseKeyField(max_length=255, blank=True, default=None) whitelist = models.BooleanField(default=0) created = AutoCreatedField(_('created')) notes = models.TextField(default=None, null=True)
class CertificateAllowlist(TimeStampedModel): """ Tracks students who are on the certificate allowlist for a given course run. .. no_pii: """ class Meta: app_label = "certificates" unique_together = [['course_id', 'user']] objects = NoneToEmptyManager() user = models.ForeignKey(User, on_delete=models.CASCADE) course_id = CourseKeyField(max_length=255, blank=True, default=None) allowlist = models.BooleanField(default=0) notes = models.TextField(default=None, null=True) # This is necessary because CMS does not install the certificates app, but it # imports this model's code. Simple History will attempt to connect to the installed # model in the certificates app, which will fail. if 'certificates' in apps.app_configs: history = HistoricalRecords() @classmethod def get_certificate_allowlist(cls, course_id, student=None): """ Return the certificate allowlist for the given course as a list of dict objects with the following key-value pairs: [{ id: 'id (pk) of CertificateAllowlist item' user_id: 'User Id of the student' user_name: 'name of the student' user_email: 'email of the student' course_id: 'Course key of the course to whom certificate exception belongs' created: 'Creation date of the certificate exception' notes: 'Additional notes for the certificate exception' }, {...}, ...] """ allowlist = cls.objects.filter(course_id=course_id, allowlist=True) if student: allowlist = allowlist.filter(user=student) result = [] generated_certificates = GeneratedCertificate.eligible_certificates.filter( course_id=course_id, user__in=[allowlist_item.user for allowlist_item in allowlist], status=CertificateStatuses.downloadable) generated_certificates = { certificate['user']: certificate['created_date'] for certificate in generated_certificates.values( 'user', 'created_date') } for item in allowlist: certificate_generated = generated_certificates.get( item.user.id, '') result.append({ 'id': item.id, 'user_id': item.user.id, 'user_name': str(item.user.username), 'user_email': str(item.user.email), 'course_id': str(item.course_id), 'created': item.created.strftime("%B %d, %Y"), 'certificate_generated': certificate_generated and certificate_generated.strftime("%B %d, %Y"), 'notes': str(item.notes or ''), }) return result
class CertificateWhitelist(models.Model): """ Tracks students who are whitelisted, all users in this table will always qualify for a certificate regardless of their grade. This model is deprecated. CertificateAllowlist should be used in its place. .. no_pii: """ class Meta: app_label = "certificates" unique_together = [['course_id', 'user']] objects = NoneToEmptyManager() user = models.ForeignKey(User, on_delete=models.CASCADE) course_id = CourseKeyField(max_length=255, blank=True, default=None) whitelist = models.BooleanField(default=0) created = AutoCreatedField(_('created')) notes = models.TextField(default=None, null=True) @classmethod def get_certificate_white_list(cls, course_id, student=None): """ Return certificate white list for the given course as dict object, returned dictionary will have the following key-value pairs [{ id: 'id (pk) of CertificateWhitelist item' user_id: 'User Id of the student' user_name: 'name of the student' user_email: 'email of the student' course_id: 'Course key of the course to whom certificate exception belongs' created: 'Creation date of the certificate exception' notes: 'Additional notes for the certificate exception' }, {...}, ...] """ white_list = cls.objects.filter(course_id=course_id, whitelist=True) if student: white_list = white_list.filter(user=student) result = [] generated_certificates = GeneratedCertificate.eligible_certificates.filter( course_id=course_id, user__in=[exception.user for exception in white_list], status=CertificateStatuses.downloadable ) generated_certificates = { certificate['user']: certificate['created_date'] for certificate in generated_certificates.values('user', 'created_date') } for item in white_list: certificate_generated = generated_certificates.get(item.user.id, '') result.append({ 'id': item.id, 'user_id': item.user.id, 'user_name': str(item.user.username), 'user_email': str(item.user.email), 'course_id': str(item.course_id), 'created': item.created.strftime("%B %d, %Y"), 'certificate_generated': certificate_generated and certificate_generated.strftime("%B %d, %Y"), 'notes': str(item.notes or ''), }) return result