예제 #1
0
파일: views.py 프로젝트: johnfelipe/moocng
def course_add(request):
    allow_public = False
    try:
        allow_public = settings.ALLOW_PUBLIC_COURSE_CREATION
    except AttributeError:
        pass

    if not allow_public and not request.user.is_staff:
        return HttpResponseForbidden(_("Only administrators can create courses"))

    if request.method == "POST":
        if "course_owner" in request.POST:
            email_or_id = request.POST["course_owner"]
            try:
                validate_email(email_or_id)
                # is an email
                try:
                    owner = User.objects.get(email=email_or_id)
                except (User.DoesNotExist):
                    messages.error(request, _("That user doesn't exists, the owner must be an user of the platform"))
                    return HttpResponseRedirect(reverse("course_add"))
            except ValidationError:
                # is name plus id
                owner_id = name_and_id_regex.search(email_or_id)
                if owner_id is None:
                    messages.error(request, _("The owner must be a name plus ID or an email"))
                    return HttpResponseRedirect(reverse("course_add"))
                try:
                    owner_id = owner_id.groups()[0]
                    owner = User.objects.get(id=owner_id)
                except (User.DoesNotExist):
                    messages.error(request, _("That user doesn't exists, the owner must be an user of the platform"))
                    return HttpResponseRedirect(reverse("course_add"))
        else:
            owner = request.user

        name = request.POST["course_name"]
        if name == u"":
            messages.error(request, _("The name can't be an empty string"))
            return HttpResponseRedirect(reverse("course_add"))

        course = Course(name=name, owner=owner, description=_("To fill"))
        unique_slugify(course, name)
        course.save()

        CourseTeacher.objects.create(course=course, teacher=owner)

        if not allow_public:
            subject = _('Your course "%s" has been created') % name
            template = "courses/email_new_course.txt"
            context = {"user": owner.get_full_name(), "course": name, "site": get_current_site(request).name}
            to = [owner.email]
            send_mail_wrapper(subject, template, context, to)

        messages.success(request, _("The course was successfully created"))
        return HttpResponseRedirect(reverse("teacheradmin_info", args=[course.slug]))

    return render_to_response("courses/add.html", {}, context_instance=RequestContext(request))
예제 #2
0
파일: utils.py 프로젝트: HengeSense/moocng
 def create_test_basic_course(self, owner, teacher=None, student=None, name=None):
     if not name:
         name = 'test_basic'
     test_course = Course(name='%s_course' % name,
                          slug='%s_course' % name,
                          description='%s_description' % name,
                          owner=owner)
     test_course.save()
     if teacher:
         CourseTeacher.objects.create(course=test_course, teacher=teacher)
     if student:
         test_course.students.add(student)
     test_course.save()
     return test_course
예제 #3
0
    def create_test_basic_course(self, owner, teacher=None, student=None, name=None):
        if not name:
            name = 'test_basic'
        test_course = Course(name='%s_course' % name,
                             slug='%s_course' % name,
                             description='%s_description' % name,
                             owner=owner)
        test_course.save()

        # A owner is a teacher
        CourseTeacher.objects.create(course=test_course, teacher=owner)

        if teacher:
            CourseTeacher.objects.create(course=test_course, teacher=teacher)
        if student:
            test_course.students.add(student)
        test_course.save()
        return test_course
예제 #4
0
파일: views.py 프로젝트: Yangff/moocng
def course_add(request):

    """
    Create new courses. By default courses are created by platform admins unless
    ALLOW_PUBLIC_COURSE_CREATION is set to True.

    The view validates the email of the course_owner field and the course_name
    unless a regular user is creating the course, in which case the course_owner
    is directly taken from the current user.

    After that the course name is slugified and the CourseTeacher and Course are
    saved, and the user is returned to the TeacherAdmin module.

    .. versionadded:: 0.1
    """
    allow_public = False
    try:
        allow_public = settings.ALLOW_PUBLIC_COURSE_CREATION
    except AttributeError:
        pass

    if not allow_public and not request.user.is_staff:
        return HttpResponseForbidden(_("Only administrators can create courses"))

    if request.method == 'POST':
        if 'course_owner' in request.POST:
            email_or_id = request.POST['course_owner']
            try:
                validate_email(email_or_id)
                # is an email
                try:
                    owner = User.objects.get(email=email_or_id)
                except (User.DoesNotExist):
                    messages.error(request, _('That user doesn\'t exists, the owner must be an user of the platform'))
                    return HttpResponseRedirect(reverse('course_add'))
            except ValidationError:
                # is name plus id
                owner_id = name_and_id_regex.search(email_or_id)
                if owner_id is None:
                    messages.error(request, _('The owner must be a name plus ID or an email'))
                    return HttpResponseRedirect(reverse('course_add'))
                try:
                    owner_id = owner_id.groups()[0]
                    owner = User.objects.get(id=owner_id)
                except (User.DoesNotExist):
                    messages.error(request, _('That user doesn\'t exists, the owner must be an user of the platform'))
                    return HttpResponseRedirect(reverse('course_add'))
        else:
            owner = request.user

        name = request.POST['course_name']
        if (name == u''):
            messages.error(request, _('The name can\'t be an empty string'))
            return HttpResponseRedirect(reverse('course_add'))

        course = Course(name=name, owner=owner, description=_('To fill'))
        course.slug = slugify(course)
        course.save()

        CourseTeacher.objects.create(course=course, teacher=owner)

        if not allow_public:
            subject = _('Your course "%s" has been created') % name
            template = 'courses/email_new_course.txt'
            context = {
                'user': owner.get_full_name(),
                'course': name,
                'site': get_current_site(request).name
            }
            to = [owner.email]
            send_mail_wrapper(subject, template, context, to)

        messages.success(request, _('The course was successfully created'))
        return HttpResponseRedirect(reverse('teacheradmin_info',
                                            args=[course.slug]))

    return render_to_response('courses/add.html', {},
                              context_instance=RequestContext(request))
예제 #5
0
def course_add(request):
    """
    Create new courses. By default courses are created by platform admins unless
    ALLOW_PUBLIC_COURSE_CREATION is set to True.

    The view validates the email of the course_owner field and the course_name
    unless a regular user is creating the course, in which case the course_owner
    is directly taken from the current user.

    After that the course name is slugified and the CourseTeacher and Course are
    saved, and the user is returned to the TeacherAdmin module.

    .. versionadded:: 0.1
    """
    allow_public = False
    try:
        allow_public = settings.ALLOW_PUBLIC_COURSE_CREATION
    except AttributeError:
        pass

    if not allow_public and not request.user.is_staff:
        return HttpResponseForbidden(
            _("Only administrators can create courses"))

    if request.method == 'POST':
        if 'course_owner' in request.POST:
            email_or_id = request.POST['course_owner']
            try:
                validate_email(email_or_id)
                # is an email
                try:
                    owner = User.objects.get(email=email_or_id)
                except (User.DoesNotExist):
                    messages.error(
                        request,
                        _('That user doesn\'t exists, the owner must be an user of the platform'
                          ))
                    return HttpResponseRedirect(reverse('course_add'))
            except ValidationError:
                # is name plus id
                owner_id = name_and_id_regex.search(email_or_id)
                if owner_id is None:
                    messages.error(
                        request,
                        _('The owner must be a name plus ID or an email'))
                    return HttpResponseRedirect(reverse('course_add'))
                try:
                    owner_id = owner_id.groups()[0]
                    owner = User.objects.get(id=owner_id)
                except (User.DoesNotExist):
                    messages.error(
                        request,
                        _('That user doesn\'t exists, the owner must be an user of the platform'
                          ))
                    return HttpResponseRedirect(reverse('course_add'))
        else:
            owner = request.user

        name = request.POST['course_name']
        if (name == u''):
            messages.error(request, _('The name can\'t be an empty string'))
            return HttpResponseRedirect(reverse('course_add'))

        course = Course(name=name, owner=owner, description=_('To fill'))
        unique_slugify(course, name)
        course.save()

        CourseTeacher.objects.create(course=course, teacher=owner)

        if not allow_public:
            subject = _('Your course "%s" has been created') % name
            template = 'courses/email_new_course.txt'
            context = {
                'user': owner.get_full_name(),
                'course': name,
                'site': get_current_site(request).name
            }
            to = [owner.email]
            send_mail_wrapper(subject, template, context, to)

        messages.success(request, _('The course was successfully created'))
        return HttpResponseRedirect(
            reverse('teacheradmin_info', args=[course.slug]))

    return render_to_response('courses/add.html', {},
                              context_instance=RequestContext(request))