Esempio n. 1
0
def edit_course(request):
    #This class is used to make empty formset forms required
    # See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032
    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False




    CourseFormSet = formset_factory(CourseForm, max_num=10, formset=RequiredFormSet)
    LectureFormSet = formset_factory(LectureForm, extra = 1, max_num=10, formset=RequiredFormSet)
    TutorialFormSet = formset_factory(TutorialForm, max_num=10, formset=RequiredFormSet)
    LaboratoryFormSet = formset_factory(LabForm, max_num=10, formset=RequiredFormSet)
    DayTimeFormSet = formset_factory(DayTimesForm,max_num=10, formset=RequiredFormSet)

    if request.user.is_authenticated():
        f_user = user_to_fingr(request.user)


        #profile = f_user.profile.objects.get(fingr_user=f_user)
        profile = f_user.profile


        if request.method == 'POST': # If the form has been submitted...

            course_formset = CourseFormSet(request.POST, prefix='course' )
            lecture_formset = LectureFormSet(request.POST, prefix='lecture')
            #lec_day_time_formset = DayTimeFormSet(request.POST,prefix='lec_day_time')
            tutorial_formset = TutorialFormSet(request.POST, prefix='tutorial')
            #tut_day_time_formset = DayTimeFormSet(request.POST, prefix='tut_day_time')
            laboratory_formset = LaboratoryFormSet(request.POST, prefix='laboratory')
            #lab_day_time_formset = DayTimeFormSet(request.POST, prefix='lab_day_time')


            if (course_formset.is_valid() and (validate_formset(lecture_formset) or validate_formset(tutorial_formset) or validate_formset(laboratory_formset))):
                #course_formset.save()
                #lecture_formset.save()
                #lec_day_time_formset.save()
                #tutorial_formset.save()
                #tut_day_time_formset.save()
                #laboratory_formset.save()

                print "ITS VALIDDASFJASFHSAHFQA"
                #print course_formset.getitem(0)
                for form in course_formset:
                    course = Course(course_name = form.cleaned_data['course_name'] , course_code = form.cleaned_data['course_code'])
                    course.save()

                if validate_formset(lecture_formset):
                    for lec_form in lecture_formset:
                        # need to figure out a way to compare theses and print an error !!!
                        if(lec_form.is_valid()):
                            print "inside lecture formset"
                            print lec_form.cleaned_data['start_time']
                            print lec_form.cleaned_data['end_time']
                            lecture = Lecture(choice_of_day = lec_form.cleaned_data['choice_of_day'],
                                              start_time = lec_form.cleaned_data['start_time'], end_time = lec_form.cleaned_data['end_time'])
                            lecture.save()
                            course.lectures.add(lecture)

                if validate_formset(tutorial_formset):
                    for tut_form in tutorial_formset:
                        if(tut_form.is_valid()):
                           tut = Tutorial(choice_of_day = tut_form.cleaned_data['choice_of_day'],
                                          start_time = tut_form.cleaned_data['start_time'], end_time = tut_form.cleaned_data['end_time'])
                           tut.save()
                           course.tutorials.add(tut)

                if validate_formset(laboratory_formset):
                    for lab_form in laboratory_formset:
                       if(lab_form.is_valid()):
                           lab = Labs( choice_of_day = lab_form.cleaned_data['choice_of_day'],
                                          start_time = lab_form.cleaned_data['start_time'], end_time = lab_form.cleaned_data['end_time'])
                           lab.save()
                           course.labs.add(lab)


                profile.courses.add(course)

                profile.save()
                # Not in the else.
                return render_to_response('add_courses_manually.html', context_instance = RequestContext(request))

            else :
                print course_formset.errors
                print lecture_formset.errors
                #lec_day_time_formset.errors
                print tutorial_formset.errors
                #tut_day_time_formset.errors
                print laboratory_formset.errors
                #lab_day_time_formset.errors



        course_formset = CourseFormSet(prefix='course')
        lecture_formset = LectureFormSet(prefix='lecture')
        day_time_formset = DayTimeFormSet(prefix='day_time')
        tutorial_formset = TutorialFormSet(prefix='tutorial')
        laboratory_formset = LaboratoryFormSet(prefix='laboratory')

        c = {'course_formset': course_formset,
             'lecture_formset': lecture_formset,
             'day_time_formset': day_time_formset,
             'tutorial_formset': tutorial_formset,
             'laboratory_formset': laboratory_formset,
            }

        return render_to_response('edit_courses.html',c, context_instance = RequestContext(request))
    else:

        return render_to_response('need_to_login.html', context_instance = RequestContext(request))
Esempio n. 2
0
def automatic_timetable(file, request):
    days = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4}
    unsw_start_dates = {
   '11s1':'28/2/2011',
   '11s2':'18/7/2011',
   '12s1':'27/2/2012',
   '12s2':'16/7/2012',
   '13s1':'4/3/2013',
   '13s2':'29/7/2013',
   '14s1':'3/3/2014',
   '14s2':'28/7/2014',
   '15s1':'2/3/2015',
   '15s2':'27/7/2015',
   '16s1':'29/2/2016',
   '16s2':'25/7/2016'
    }
    if request.user.is_authenticated():
        f_user = user_to_fingr(request.user)
        source = file.read()
        f = source.replace('\r', '')
        s = BeautifulSoup(f.replace("\n",""))

        # gets the year and sem
        sem = re.sub(r'.*Semester (\d+) \S\S(\d+).*', u'\\2s\\1', s.find("option", {'selected':'true'}).text)
        title = sem + " Timetable"
        #
        if not re.match(r'\d\ds\d', sem):
            current_time = datetime.datetime.now()
            sem = '%ds%d' % (current_time.year % 100, 1 if current_time.month < 7 else 2)

        courses = [x.contents[0] for x in s.findAll("td", {"class":"sectionHeading"})]
        for course in courses:
            print course

            course_code = str(course).split("-")[0]
            course_name = str(course).split("-")[1]

            course_code = course_code.strip()
            course_name = course_name.strip()

            add_course = Course(course_code = course_code, course_name = course_name)
            add_course.save()


            classes = s.find(text=course).findNext("table").findAll("tr", {"class": re.compile("data")})
            ctype, code, day, tim, weeks, place, t = ['' for x in xrange(7)]
            for c in classes:
                a = [(x.contents[0] if x.contents else "") for x in c.findAll("td", recursive=False)]
                print a
                g = (t for t in a)
                print g

                t = g.next()

                if t.strip() != "&nbsp;":
                    ctype = t


                t = g.next()
                t= g.next()

                if t.strip() not in days:
                    code = t
                    day = g.next().strip()
                else:
                    day = t.strip()
                    tim = g.next()
                    weeks = g.next()
                    place = g.next()
                    t = ' '.join(g.next().findAll(text=True))

                if tim.find(" - ") == -1:
                    continue

                start, end = tim.split(" - ")
                start = datetime.datetime.strptime(unsw_start_dates[sem] + ' ' + start, "%d/%m/%Y %I:%M%p")
                end = datetime.datetime.strptime(unsw_start_dates[sem] + ' ' + end, "%d/%m/%Y %I:%M%p")


                start = str(start).split(" ")[1]
                end = str(end).split(" ")[1]

                start = str(start).split(":")[0]
                end = str(end).split(":")[0]

                day = day.strip()
                day = day.upper()


                if(ctype == "Lecture"):
                    new_lecture = Lecture(choice_of_day = day, start_time = start, end_time = end)
                    new_lecture.save()
                    add_course.lectures.add(new_lecture)

                elif(ctype == "Tutorial"):
                    new_tutorial = Tutorial(choice_of_day = day, start_time = start, end_time = end)
                    new_tutorial.save()
                    add_course.tutorials.add(new_tutorial)

                elif(ctype == "Laboratory"):
                    new_lab = Labs(choice_of_day = day, start_time = start, end_time = end)
                    new_lab.save()
                    add_course.labs.add(new_lab)


            f_user.profile.courses.add(add_course)
            f_user.profile.save()