예제 #1
0
파일: views.py 프로젝트: sfu-fas/coursys
def view_topic(request, course_slug, topic_slug):
    """
    Page to view a discussion topic and reply
    """
    course, view = _get_course_and_view(request, course_slug)
    if view is None:
        # course is an HttpResponse in this case
        return course
    topic = get_object_or_404(DiscussionTopic, slug=topic_slug, offering=course)
    if view == 'student' and topic.status == 'HID':
        raise Http404
    replies = DiscussionMessage.objects.filter(topic=topic).order_by('created_at')
    
    if request.method == 'POST':
        if topic.status == 'CLO' and not view  == 'staff':
            raise Http404
        form = DiscussionMessageForm(data=request.POST)
        if form.is_valid():
            message = form.save(commit=False)
            message.topic = topic
            message.author = _get_member(request.user.username, view, course_slug)
            message.save()
            messages.add_message(request, messages.SUCCESS, 'Sucessfully replied')
            return HttpResponseRedirect(reverse('offering:discussion:view_topic', kwargs={'course_slug': course_slug, 'topic_slug': topic.slug}))
    else:
        form = DiscussionMessageForm()
    context = {'course': course, 'topic': topic, 'replies': replies, 'view': view, 'form': form,
               'username': request.user.username}
    return render(request, 'discuss/topic.html', context)
예제 #2
0
def edit_message(request, course_slug, topic_slug, message_slug):
    """
    Form to edit a recently posted reply (5 min window)
    """
    course, view = _get_course_and_view(request, course_slug)
    if view is None:
        # course is an HttpResponse in this case
        return course
    topic = get_object_or_404(DiscussionTopic, slug=topic_slug, offering=course)
    message = get_object_or_404(DiscussionMessage, slug=message_slug, topic=topic)
    if not message.author.person.userid == request.user.username:
        return HttpResponseForbidden
    if (datetime.datetime.now() - message.created_at) > datetime.timedelta(minutes = 5):
        raise Http404
    
    if request.method == 'POST':
        creole = topic.get_creole()
        form = DiscussionMessageForm(data=request.POST, instance=message, creole=creole)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, 'Reply successfully edited.')
            return HttpResponseRedirect(reverse('discuss.views.view_topic', kwargs={'course_slug': course_slug, 'topic_slug': topic.slug}))
    else:
        form = DiscussionMessageForm(instance=message, creole=None)
    return render(request, 'discuss/edit_reply.html', {'course':course, 'topic': topic, 'message': message, 'form': form})
예제 #3
0
def view_topic(request, course_slug, topic_slug):
    """
    Page to view a discussion topic and reply
    """
    course, view = _get_course_and_view(request, course_slug)
    if view is None:
        # course is an HttpResponse in this case
        return course
    topic = get_object_or_404(DiscussionTopic, slug=topic_slug, offering=course)
    if view == 'student' and topic.status == 'HID':
        raise Http404
    replies = DiscussionMessage.objects.filter(topic=topic).order_by('created_at')
    
    if request.method == 'POST':
        if topic.status == 'CLO' and not view  == 'staff':
            raise Http404
        form = DiscussionMessageForm(data=request.POST)
        if form.is_valid():
            message = form.save(commit=False)
            message.topic = topic
            message.author = _get_member(request.user.username, view, course_slug)
            message.save()
            messages.add_message(request, messages.SUCCESS, 'Sucessfully replied')
            return HttpResponseRedirect(reverse('offering:discussion:view_topic', kwargs={'course_slug': course_slug, 'topic_slug': topic.slug}))
    else:
        form = DiscussionMessageForm()
    context = {'course': course, 'topic': topic, 'replies': replies, 'view': view, 'form': form,
               'username': request.user.username}
    return render(request, 'discuss/topic.html', context)
예제 #4
0
def view_topic(request, course_slug, topic_slug):
    """
    Page to view a discussion topic and reply
    """
    course, view = _get_course_and_view(request, course_slug)
    if view is None:
        # course is an HttpResponse in this case
        return course
    topic = get_object_or_404(DiscussionTopic,
                              slug=topic_slug,
                              offering=course)
    if view == 'student' and topic.status == 'HID':
        raise Http404
    replies = DiscussionMessage.objects.filter(
        topic=topic).order_by('created_at')

    # syntaxhighlighter brushes needed
    brushes = set(
        itertools.chain(topic.brushes(), *(r.brushes() for r in replies)))
    # who needs mathjax activated?
    need_mathjax = ['reply-content-%i' % (r.id) for r in replies if r.math()]
    if topic.math():
        need_mathjax.append('topic-content')
    any_math = bool(need_mathjax)
    need_mathjax = json.dumps(need_mathjax)

    if request.method == 'POST':
        if topic.status == 'CLO' and not view == 'staff':
            raise Http404
        creole = topic.get_creole()
        form = DiscussionMessageForm(data=request.POST, creole=creole)
        if form.is_valid():
            message = form.save(commit=False)
            message.topic = topic
            message.author = _get_member(request.user.username, view,
                                         course_slug)
            message.save()
            messages.add_message(request, messages.SUCCESS,
                                 'Sucessfully replied')
            return HttpResponseRedirect(
                reverse('discuss.views.view_topic',
                        kwargs={
                            'course_slug': course_slug,
                            'topic_slug': topic.slug
                        }))
    else:
        form = DiscussionMessageForm(creole=None)
    context = {
        'course': course,
        'topic': topic,
        'replies': replies,
        'view': view,
        'form': form,
        'brushes': brushes,
        'need_mathjax': need_mathjax,
        'any_math': any_math,
        'username': request.user.username
    }
    return render(request, 'discuss/topic.html', context)
예제 #5
0
def view_topic(request, course_slug, topic_slug):
    """
    Page to view a discussion topic and reply
    """
    course, view = _get_course_and_view(request, course_slug)
    if view is None:
        # course is an HttpResponse in this case
        return course
    topic = get_object_or_404(DiscussionTopic, slug=topic_slug, offering=course)
    if view == 'student' and topic.status == 'HID':
        raise Http404
    replies = DiscussionMessage.objects.filter(topic=topic).order_by('created_at')

    # syntaxhighlighter brushes needed
    brushes = set(itertools.chain(topic.brushes(), *(r.brushes() for r in replies)))
    # who needs mathjax activated?
    need_mathjax = ['reply-content-%i' % (r.id) for r in replies if r.math()]
    if topic.math():
        need_mathjax.append('topic-content')
    any_math = bool(need_mathjax)
    need_mathjax = json.dumps(need_mathjax)
    
    if request.method == 'POST':
        if topic.status == 'CLO' and not view  == 'staff':
            raise Http404
        creole = topic.get_creole()
        form = DiscussionMessageForm(data=request.POST, creole=creole)
        if form.is_valid():
            message = form.save(commit=False)
            message.topic = topic
            message.author = _get_member(request.user.username, view, course_slug)
            message.save()
            messages.add_message(request, messages.SUCCESS, 'Sucessfully replied')
            return HttpResponseRedirect(reverse('discuss.views.view_topic', kwargs={'course_slug': course_slug, 'topic_slug': topic.slug}))
    else:
        form = DiscussionMessageForm(creole=None)
    context = {'course': course, 'topic': topic, 'replies': replies, 'view': view, 'form': form,
               'brushes': brushes, 'need_mathjax': need_mathjax, 'any_math': any_math, 'username': request.user.username}
    return render(request, 'discuss/topic.html', context)