Exemplo n.º 1
0
def add_sessions_view(request, slug):
    timetable = get_object_or_404(Timetable, slug=slug)
    SessionFormset = modelformset_factory(Session, fields = ('subject', 'room_number'), max_num=13, extra=1)

    if request.method == 'POST':
        form = TimetableAddSessionForm(request.POST) 
        formset = SessionFormset(request.POST)
        if form.is_valid():
            session_choice = form.cleaned_data['session_choice']
            session_day = int(form.cleaned_data['day_choice'])
            first_session_date = timetable.start_date + datetime.timedelta(days=session_day)
            dates = generate_dates(first_session_date, timetable.end_date)
        if formset.is_valid():
            for smallform in formset.cleaned_data:
                for date in dates:
                  newsession = Session()
                  newsession.session_number = session_choice
                  newsession.timetable = timetable
                  newsession.subject = smallform['subject']
                  newsession.room_number = smallform['room_number']
                  newsession.date = date
                  newsession.save()
            
        return HttpResponseRedirect('/tafe/timetable/%s' %(timetable.slug))
    else:
        form = TimetableAddSessionForm()
        formset = SessionFormset(queryset=Session.objects.none())
    return render_to_response('tafe/timetable_add_session.html',{'form':form, 'formset':formset, 'timetable':timetable,}, RequestContext(request))
Exemplo n.º 2
0
def session_create(request):
    ''' For each subject, we will create a session object for each day that it is taught '''
    if request.method == 'POST':
        form = SessionRecurringForm(request.POST)
        if form.is_valid():
            s = Session()
            s.subject = form.cleaned_data['subject']
            s.timetable = form.cleaned_data['timetable']
            s.session_number = form.cleaned_data['session_number']

            recurring = form.cleaned_data['recurring']
            if not recurring:  
                s.date = form.cleaned_data['first_date']
                s.save()
                return HttpResponseRedirect('/tafe/')

            first_date = form.cleaned_data['first_date'] 
            for_semester = form.cleaned_data['for_semester'] 
            if for_semester:
                second_date = s.timetable.end_date
            else:
                second_date = form.cleaned_data['second_date']

            date = first_date
            while date <= second_date:
                new_s = Session()
                new_s.subject = form.cleaned_data['subject']
                new_s.timetable = form.cleaned_data['timetable']
                new_s.session_number = form.cleaned_data['session_number']
                new_s.date = date
                new_s.save()
                date += datetime.timedelta(7)

            return HttpResponseRedirect('/tafe/')

    else:
        form = SessionRecurringForm()
    return render_to_response('tafe/session_create.html',{'form':form}, RequestContext(request))