def view_tutorials(request, lecturer_id=None, tutorial_id=None):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        form = TutorialForm(request.POST, lecturer_id=lecturer.id)
        if form.is_valid():
            # Lecturer as parameter (lecturer_id is in the form, but this way we save one DB query)
            TutorialService.update(form, lecturer)
            messages.success(request, _('Tutorial was updated successfully') if form.cleaned_data['tutorial_id'] else _(
                'Tutorial was added successfully.'))
            return HttpResponseRedirect('/lecturer/details/')
    else:
        lecturer = get_object_or_404(Profesor, id=lecturer_id if lecturer_id else request.user.id)
        initial_data = {}
        if request.user.has_perm('principal.profesor'):
            # Edit tutorial
            if tutorial_id:
                tutorial_to_edit = get_object_or_404(Tutoria, id=tutorial_id)

                if not tutorial_to_edit.profesor == lecturer:
                    messages.warning(request, _('You cannot edit this tutorial.'))
                else:
                    initial_data['tutorial_id'] = tutorial_to_edit.id
                    initial_data['start_hour'] = tutorial_to_edit.horainicio.strftime('%H:%M')
                    initial_data['finish_hour'] = tutorial_to_edit.horafin.strftime('%H:%M')
                    initial_data['day'] = tutorial_to_edit.dia

        form = TutorialForm(lecturer_id=request.user.id, initial=initial_data)

    return render_to_response('lecturer/details.html', {'lecturer': lecturer, 'form': form},
                              context_instance=RequestContext(request))
def enable_tutorials(request):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        TutorialService.enable_tutorials(lecturer)
        messages.add_message(request, messages.SUCCESS, _('Tutorials have been enabled.'))

    return HttpResponseRedirect(request.POST['return_url'])
def enable_tutorials(request):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        TutorialService.enable_tutorials(lecturer)
        messages.add_message(request, messages.SUCCESS,
                             _('Tutorials have been enabled.'))

    return HttpResponseRedirect(request.POST['return_url'])
def disable_tutorials(request):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        motivation = request.POST['motivation']
        add_error = False

        if motivation:
            if not motivation.isspace() and not '' == motivation:
                TutorialService.disable_tutorials(lecturer, motivation)
                messages.success(request, _('Tutorials have been disabled.'))
            else:
                add_error = True
        else:
            add_error = True

        if add_error:
            messages.error(request, _('You must enter a reason to disable tutorials.'))

    return HttpResponseRedirect(request.POST['return_url'])
def view_tutorials(request, lecturer_id=None, tutorial_id=None):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        form = TutorialForm(request.POST, lecturer_id=lecturer.id)
        if form.is_valid():
            # Lecturer as parameter (lecturer_id is in the form, but this way we save one DB query)
            TutorialService.update(form, lecturer)
            messages.success(
                request,
                _('Tutorial was updated successfully')
                if form.cleaned_data['tutorial_id'] else
                _('Tutorial was added successfully.'))
            return HttpResponseRedirect('/lecturer/details/')
    else:
        lecturer = get_object_or_404(
            Profesor, id=lecturer_id if lecturer_id else request.user.id)
        initial_data = {}
        if request.user.has_perm('principal.profesor'):
            # Edit tutorial
            if tutorial_id:
                tutorial_to_edit = get_object_or_404(Tutoria, id=tutorial_id)

                if not tutorial_to_edit.profesor == lecturer:
                    messages.warning(request,
                                     _('You cannot edit this tutorial.'))
                else:
                    initial_data['tutorial_id'] = tutorial_to_edit.id
                    initial_data[
                        'start_hour'] = tutorial_to_edit.horainicio.strftime(
                            '%H:%M')
                    initial_data[
                        'finish_hour'] = tutorial_to_edit.horafin.strftime(
                            '%H:%M')
                    initial_data['day'] = tutorial_to_edit.dia

        form = TutorialForm(lecturer_id=request.user.id, initial=initial_data)

    return render_to_response('lecturer/details.html', {
        'lecturer': lecturer,
        'form': form
    },
                              context_instance=RequestContext(request))
def disable_tutorials(request):
    if request.method == 'POST':
        lecturer = Profesor.objects.get(id=request.user.id)
        motivation = request.POST['motivation']
        add_error = False

        if motivation:
            if not motivation.isspace() and not '' == motivation:
                TutorialService.disable_tutorials(lecturer, motivation)
                messages.success(request, _('Tutorials have been disabled.'))
            else:
                add_error = True
        else:
            add_error = True

        if add_error:
            messages.error(request,
                           _('You must enter a reason to disable tutorials.'))

    return HttpResponseRedirect(request.POST['return_url'])