Esempio n. 1
0
def edit_homework(request, subject_name, homework_id):
    """
    This view displays a form to edit a homework resp.
    saves a homework using the form data.
    """
    
    homework = get_homework_or_404(subject_name, homework_id)
    
    # Check if a `next_view` was specified in POST data 
    # (e.g. when clicking "edit" from a single displayed homework)
    if 'next_view' in request.POST.keys():
        next_view = request.POST['next_view']
    else:
        next_view = False
    
    if not request.POST or len(request.POST) == 3:
        form = HomeworkForm(instance=homework)
    else:
        form = HomeworkForm(request.POST)
        
        if form.is_valid():
            homework.short_description = form.cleaned_data['short_description']
            homework.long_description = form.cleaned_data['long_description']
            homework.date_ends = form.cleaned_data['date_ends']
            homework.subject = form.cleaned_data['subject']
            
            homework.save()
            
            request.user.message_set.create(message="Your homework was saved.")
            
            if next_view:
                return HttpResponseRedirect(reverse(next_view, 
                    args=[homework.subject.name, homework.id]))
            else:
                return HttpResponseRedirect(reverse('hw_list_all'))

    data = {
        'form': form,
        'new': False,
        'homework': homework,
        'next_view': next_view
    }

    return render_to_response(
        'homeworkmanager/edit.html',
        data,
        context_instance = RequestContext(request),
    )
Esempio n. 2
0
def show_homework(request, subject_name, homework_id, form=False):
    """This view shows a single homework and all its comments."""
    
    homework = get_homework_or_404(subject_name, homework_id)
    
    # Create a comment form if it's not given (via the `add_comment` view)
    if not form:
        form = HomeworkCommentForm()
        
        
    data = {
        'homework': homework,
        'form': form
    }

    return render_to_response(
        'homeworkmanager/show.html',
        data,
        context_instance = RequestContext(request),
    )
Esempio n. 3
0
def add_comment(request, subject_name, homework_id):
    """This view creates a comment on the given homework (subject name, id)."""
    
    homework = get_homework_or_404(subject_name, homework_id)
    
    form = HomeworkCommentForm(request.POST or None)

    if form.is_valid():
        comment = form.save(commit=False)
        comment.user = request.user
        comment.homework = homework
        comment.save()
        
        request.user.message_set.create(message="Comment was created.")
        
        return HttpResponseRedirect(
            reverse('hw_show', args=[
                subject_name,
                homework_id
            ]) + '#comments'
        )
    
    return show_homework(request, subject_name, homework_id, form)