Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):

        try:
            self.object = self.get_object()
        except NotEnrolled:
            return redirect('micourses:notenrolled', 
                            course_code=self.course.code)
        except NotInstructor:
            return redirect('micourses:coursemain', 
                            course_code=self.course.code)


        # update attendance, assuming date is valid
        date_form = DateForm({'date':  request.POST['attendance_date']})
        valid_day = False

        if date_form.is_valid():
            attendance_date = date_form.cleaned_data['date']

            # check if date is a class day
            for class_day in self.course.attendancedate_set.all():
                if attendance_date == class_day.date:
                    valid_day = True
                    break
            
        else:
            attendance_date = None

        if valid_day:
            with transaction.atomic(), reversion.create_revision():

                for ce in self.course.courseenrollment_set.filter(withdrew=False):
                    present=request.POST.get('student_%s' % ce.id, ABSENT)
                    try:
                        present = int(present)
                    except ValueError:
                        present = ABSENT

                    sa, created = ce.studentattendance_set.get_or_create(
                        date=attendance_date,
                        defaults = {'present': present})
                    # don't override an excused absence with an absence
                    if not created and \
                       not (sa.present == EXCUSED and present==ABSENT):
                        sa.present = present
                        sa.save()

                if not self.course.last_attendance_date \
                        or attendance_date > self.course.last_attendance_date:
                    self.course.last_attendance_date = attendance_date
                    self.course.save()


            message = "Attendance updated for %s" % \
                      attendance_date.strftime("%B %d, %Y")

            from django.utils.encoding import escape_uri_path
            url = reverse('micourses:update_attendance',
                          kwargs={'course_code': self.course.code})
            url += "?message=%s" % escape_uri_path(message)
            return redirect(url) 


        else:
            context = self.get_context_data(object=self.object)

            context['message'] = "Attendance not updated.  " \
                + "%s is not a valid course day" % \
                (request.POST['attendance_date'])

            return self.render_to_response(context)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):

        try:
            self.object = self.get_object()
        except NotEnrolled:
            return redirect('micourses:notenrolled', 
                            course_code=self.course.code)
        except NotInstructor:
            return redirect('micourses:coursemain', 
                            course_code=self.course.code)

        
        # possible actions: update, add_excused, delete_excused
        action = request.POST.get("action")

        self.last_attendance_date = self.course.last_attendance_date

        message = ""

        if action=="add_excused":
            date_form = DateForm({'date':  request.POST['attendance_date']})
            valid_day = False
            if date_form.is_valid():
                attendance_date = date_form.cleaned_data['date']

                # check if date is a class day
                for class_day in self.course.attendancedate_set.all():
                    if attendance_date == class_day.date:
                        valid_day = True
                        break
            else:
                attendance_date = None

            if valid_day:
                with transaction.atomic(), reversion.create_revision():
                    sa, created = self.student_enrollment.studentattendance_set\
                        .get_or_create(
                        date=attendance_date, defaults = {'present': EXCUSED})

                    if not created:
                        sa.present=EXCUSED
                        sa.save()

                message = "Added excused absence for %s on %s." % \
                          (self.student, attendance_date.strftime("%B %d, %Y"))

                from django.utils.encoding import escape_uri_path
                url = reverse('micourses:update_individual_attendance',
                              kwargs={'course_code': self.course.code})
                url += "?student=%s&message=%s" % \
                       (self.student.id, escape_uri_path(message))
                return redirect(url) 

            else:
                
                message = "Excused absence not added.  " \
                          + "%s is not a valid course day" % \
                          (request.POST['attendance_date'])
            
                # get attendance forms with data from database
                self.get_forms()


        elif action=="delete_excused":
            
            excused_to_delete = request.POST.getlist("excused_date")

            n_deleted=0
            deleted_date=None
            for excused_id in excused_to_delete:
                try:
                    excused=self.student_enrollment.studentattendance_set.get(
                        id=excused_id, present=EXCUSED)
                except ObjectDoesNotExist:
                    pass
                else:
                    deleted_date = excused.date
                    with transaction.atomic(), reversion.create_revision():
                        excused.delete()
                    n_deleted +=1

            if n_deleted == 1:
                message = "Deleted 1 excused absence for %s: %s." % \
                          (self.student, deleted_date.strftime("%B %d, %Y"))
            else:
                message = "Deleted %s excused absences for %s." % \
                          (n_deleted, self.student)

            from django.utils.encoding import escape_uri_path
            url = reverse('micourses:update_individual_attendance',
                          kwargs={'course_code': self.course.code})
            url += "?student=%s&message=%s" % \
                   (self.student.id, escape_uri_path(message))
            return redirect(url) 



        elif action=="update":
            # get attendance forms populated with POST data
            self.get_forms(request.POST)

            if not self.last_attendance_date:
                message = "Last attendance date for course not set.  Cannot update attendance."
            elif self.attendance_dates_form.is_valid():
                with transaction.atomic(), reversion.create_revision():
                    self.student_enrollment.studentattendance_set\
                        .filter(date__lte = self.last_attendance_date).delete()
                for (date, present) in \
                    self.attendance_dates_form.date_attendance():
                    if present:
                        self.student_enrollment.studentattendance_set\
                                        .create(date=date, present=present)

                message = "Updated attendance of %s." % self.student

                from django.utils.encoding import escape_uri_path
                url = reverse('micourses:update_individual_attendance',
                              kwargs={'course_code': self.course.code})
                url += "?student=%s&message=%s" % \
                       (self.student.id, escape_uri_path(message))
                return redirect(url) 
                 
        
        context = self.get_context_data(object=self.object)

        context['message'] = message

        return self.render_to_response(context)