コード例 #1
0
ファイル: CourseView.py プロジェクト: Hnguyen1997/grepthink
def update_course_update(request, slug, id):
    """
    Edit an update for a given course
    """
    course = get_object_or_404(Course, slug=slug)
    update = get_object_or_404(CourseUpdate, id=id)

    if request.user.profile.isGT:
        pass
    elif update.creator != request.user:
        return redirect(view_one_course, course.slug)

    if request.method == 'POST':
        form = CourseUpdateForm(request.user.id, request.POST)
        if form.is_valid():
            update.course = course
            update.title = form.cleaned_data.get('title')
            update.content = form.cleaned_data.get('content')
            if not request.user.profile.isGT:
                update.creator = request.user
            update.save()
            return redirect(view_one_course, course.slug)
    else:
        form = CourseUpdateForm(request.user.id, instance=update)

    return render(request, 'courses/update_course_update.html', {
        'form': form,
        'course': course,
        'update': update
    })
コード例 #2
0
ファイル: CourseView.py プロジェクト: Hnguyen1997/grepthink
def update_course(request, slug):
    """
    Post an update for a given course
    """
    course = get_object_or_404(Course.objects.prefetch_related('creator'),
                               slug=slug)
    page_name = "Update Course"
    page_description = "Update %s" % (course.name) or "Post a new update"
    title = "Update Course"

    if not request.user.profile.isGT:
        user_role = Enrollment.objects.filter(user=request.user,
                                              course=course).first().role
    else:
        user_role = 'GT'

    if user_role == "student":
        # redirect them to the /course directory with message
        messages.info(request, 'Only Professor can post a course update')
        return HttpResponseRedirect('/course')

    if request.method == 'POST':
        form = CourseUpdateForm(request.user.id, request.POST)
        if form.is_valid():
            new_update = CourseUpdate(course=course)
            new_update.course = course
            new_update.title = form.cleaned_data.get('title')
            new_update.content = form.cleaned_data.get('content')
            new_update.creator = request.user
            new_update.save()

            # Next 4 lines handle sending an email to class roster
            # grab list of students in the course
            students_in_course = course.students.all().filter()
            # TODO: course variables contains (slug: blah blah)
            subject = "{0} has posted an update to {1}".format(
                request.user, course)
            content = "{0}\n\n www.grepthink.com".format(new_update.content)
            send_email(students_in_course, "*****@*****.**", subject,
                       content)
            messages.add_message(request, messages.SUCCESS,
                                 "Posted and Email Sent!")

            return redirect(view_one_course, course.slug)
    else:
        form = CourseUpdateForm(request.user.id)

    return render(
        request, 'courses/update_course.html', {
            'form': form,
            'course': course,
            'page_name': page_name,
            'page_description': page_description,
            'title': title
        })