def create_tutorial(request):
    if request.method == 'POST':
        student_id = request.user.alumno.id
        form = PeticionCitaForm(request.POST)
        if form.is_valid():
            tutorial_request = PeticionCitaService.create(form, student_id)

            # Business rule: a student cannot request more than two appointments for the same lecturer and day.
            appointment_date = tutorial_request.fechacita.date()
            appointment_day = appointment_date.day
            appointment_month = appointment_date.month
            appointment_year = appointment_date.year

            parameters = [appointment_year, appointment_month, appointment_day, tutorial_request.profesor.id,
                          tutorial_request.alumno.id]

            query = '''SELECT idcita FROM peticioncita
                      WHERE YEAR(fechaCita)=%s AND MONTH(fechaCita)=%s
                      AND DAY(fechaCita)=%s AND profesor=%s AND alumno=%s'''

            same_day_same_lecturer = len(list(Peticioncita.objects.raw(query, parameters)))

            if same_day_same_lecturer < 2:
                tutorial_request.save()
                messages.add_message(request, messages.SUCCESS, _('The request has been sent successfully.'))
                return HttpResponseRedirect('/student/tutorial/create/')
            else:
                messages.add_message(request, messages.ERROR, _(
                    'Sorry, but you cannot request more than two appointments for the same teacher in the same day.'))
    else:
        form = PeticionCitaForm()

    return render_to_response('tutorial/edit.html', {'form': form}, context_instance=RequestContext(request))
def seek_tutorial(request, lecturer_id):
    lecturer = get_object_or_404(Profesor, id=lecturer_id)

    if request.method == "POST":
        form = TutorialRequestForm(request.POST)
        if form.is_valid():
            tutorial_request = PeticionCitaService.create(form, request.user.id)

            if PeticionCitaService.can_request(tutorial_request):
                tutorial_request.save()
                EmailViews.send_tutorial_request_mail(request, tutorial_request)
                messages.add_message(request, messages.SUCCESS, _("The request has been sent successfully."))

                return HttpResponseRedirect("/student/tutorial/seek/%s" % lecturer_id)
            else:
                messages.add_message(
                    request,
                    messages.ERROR,
                    _("Sorry, but you cannot request more than two appointments for the same teacher in the same day."),
                )

    else:
        form = TutorialRequestForm(initial={"lecturer": lecturer.id})

    return render_to_response(
        "tutorial_request/calendar.html", {"lecturer": lecturer, "form": form}, context_instance=RequestContext(request)
    )
def create_tutorial(request):
    if request.method == 'POST':
        student_id = request.user.alumno.id
        form = PeticionCitaForm(request.POST)
        if form.is_valid():
            tutorial_request = PeticionCitaService.create(form, student_id)

            # Business rule: a student cannot request more than two appointments for the same lecturer and day.
            appointment_date = tutorial_request.fechacita.date()
            appointment_day = appointment_date.day
            appointment_month = appointment_date.month
            appointment_year = appointment_date.year

            parameters = [
                appointment_year, appointment_month, appointment_day,
                tutorial_request.profesor.id, tutorial_request.alumno.id
            ]

            query = '''SELECT idcita FROM peticioncita
                      WHERE YEAR(fechaCita)=%s AND MONTH(fechaCita)=%s
                      AND DAY(fechaCita)=%s AND profesor=%s AND alumno=%s'''

            same_day_same_lecturer = len(
                list(Peticioncita.objects.raw(query, parameters)))

            if same_day_same_lecturer < 2:
                tutorial_request.save()
                messages.add_message(
                    request, messages.SUCCESS,
                    _('The request has been sent successfully.'))
                return HttpResponseRedirect('/student/tutorial/create/')
            else:
                messages.add_message(
                    request, messages.ERROR,
                    _('Sorry, but you cannot request more than two appointments for the same teacher in the same day.'
                      ))
    else:
        form = PeticionCitaForm()

    return render_to_response('tutorial/edit.html', {'form': form},
                              context_instance=RequestContext(request))
def seek_tutorial(request, lecturer_id):
    lecturer = get_object_or_404(Profesor, id=lecturer_id)

    if request.method == 'POST':
        form = TutorialRequestForm(request.POST)
        if form.is_valid():
            tutorial_request = PeticionCitaService.create(form, request.user.id)

            if PeticionCitaService.can_request(tutorial_request):
                tutorial_request.save()
                EmailViews.send_tutorial_request_mail(request, tutorial_request)
                messages.add_message(request, messages.SUCCESS, _('The request has been sent successfully.'))

                return HttpResponseRedirect('/student/tutorial/seek/%s' % lecturer_id)
            else:
                messages.add_message(request, messages.ERROR, _(
                    'Sorry, but you cannot request more than two appointments for the same teacher in the same day.'))

    else:
        form = TutorialRequestForm(initial={'lecturer': lecturer.id})

    return render_to_response('tutorial_request/calendar.html', {'lecturer': lecturer, 'form': form},
                              context_instance=RequestContext(request))