示例#1
0
 def get(self):
   user = users.get_current_user()
   authz = Authorize()
   if not authz.authorize():
     self.error(403)
     return
   class_id = self.request.get('class_id')
   class_key = ndb.Key('Class', int(class_id))
   the_class = class_key.get()
   the_class.id = the_class.key.id()
   date_ordinal = self.request.get('date')
   errmsg = self.request.get('errmsg')
   today_as_ordinal = self.today_as_ordinal(the_class.timezone)
   if not date_ordinal:
     date_ordinal = today_as_ordinal
   if int(date_ordinal) == today_as_ordinal:
     today = True
   else:
     today = False
   date_struct = datetime.date.fromordinal(int(date_ordinal))
   attendance_key = ndb.Key('Class', int(class_id), 'Attendance', int(date_ordinal))
   attendance = attendance_key.get()
   students = ndb.get_multi(the_class.enrolled)
   students.sort(key= lambda x: x.first_name, reverse=False)
   for student in students:
     student.present = False
     student.hours = the_class.default_hours
     student.id = student.key.id()
     if attendance:
       for student_present in attendance.attending:
         if student_present.student == student.key:
           student.present = True
           student.hours = student_present.hours
           break
   
   school = namespace_manager.google_apps_namespace()
   if not school: school = 'Test school'
   template_values = { 'students': students,
                       'date_ordinal': date_ordinal,
                       'today': today,
                       'class': the_class,
                       'school': school,
                       'date_struct': date_struct,
                       'username': authz.get_name(),
                       'errmsg': errmsg }
   path = os.path.join(os.path.dirname(__file__), 'templates/students.html')
   self.response.out.write(template.render(path, template_values))
示例#2
0
 def get(self):
   authz = Authorize()
   if not authz.authorize():
     self.error(403)
     return
   query = Class.query()
   # Max classes that can be handled is 50 per school
   classes = query.fetch(50)
   for the_class in classes:
     the_class.id = the_class.key.id()
   school = namespace_manager.google_apps_namespace()
   if not school: school = 'Test school'
   template_values = { 'classes': classes,
                       'school': school,
                       'username': authz.get_name(),
                       'logout': users.create_logout_url("/") }
   path = os.path.join(os.path.dirname(__file__), 'templates/classes.html')
   self.response.out.write(template.render(path, template_values))
示例#3
0
  def get(self):
    user = users.get_current_user()
    authz = Authorize()
    if not authz.authorize():
      self.error(403)
      return
    self.response.headers['Content-Type'] = 'text/plain'
    class_id = self.request.get('class_id')
    class_key = ndb.Key('Class', int(class_id))
    the_class = class_key.get()
    enrolled = the_class.enrolled
    all_students = {}
    self.response.out.write(',')
    for student_key in enrolled:
      if student_key not in all_students:
        the_student = student_key.get()
        all_students[student_key] = the_student
        self.response.out.write('%s %s,' % (the_student.first_name, the_student.last_name))
    self.response.out.write('\n')

    qry = Attendance.query(ancestor=class_key).order(Attendance.key)
    attendance = qry.fetch(200)
    for the_attendance in attendance:
      the_attendance_key = the_attendance.key
      date_ordinal = the_attendance_key.id()
      date_struct = datetime.date.fromordinal(int(date_ordinal))
      date_str = date_struct.isoformat()
      self.response.out.write('%s,' % (date_str))
      students = the_attendance.attending
      attending_students = {}
      for student in students:
        student_key = student.student
        attending_students[student_key] = student.hours
      for the_enrolled in enrolled:
        if the_enrolled in attending_students:
          output = attending_students[the_enrolled]
          if not output: output = 1
          self.response.out.write('%s,' % (output))
        else:
          self.response.out.write(',')
      self.response.out.write('\n')
示例#4
0
  def get(self):
    # authorize web requests
    user = users.get_current_user()
    authz = Authorize()
    if not authz.authorize():
      self.error(403)
      return
    # get the form input
    yes = int(self.request.get('yes'))
    class_id = self.request.get('class_id')
    student_id = self.request.get('student_id')
    if student_id:
      student_key = ndb.Key('Student', int(student_id))
      student = student_key.get()
    date_ordinal = self.request.get('date')
    date_struct = datetime.date.fromordinal(int(date_ordinal))
    class_key = ndb.Key('Class', int(class_id))
    the_class = class_key.get()
    hours = self.request.get('hours')
    today_as_ordinal = self.today_as_ordinal(the_class.timezone)
    # can only edit attendance for today
    attendance = None
    if int(date_ordinal) == today_as_ordinal:
      attendance_key = ndb.Key('Class', int(class_id), 'Attendance', int(date_ordinal))
      attendance = attendance_key.get()
      attendance_already_exists = False
      try:
        if attendance:
          attendance_already_exists = True
          if yes:
            if student_id:
              self.add_student(student_key, hours, attendance.attending)
            else:
              students = ndb.get_multi(the_class.enrolled)
              for student in students:
                self.add_student(student.key, hours, attendance.attending)
          else:
            self.remove_student(student_key, attendance.attending)
        else:
          if yes:
            attendance = Attendance(key=attendance_key, attending=[])
            if student_id:
              self.add_student(student_key, hours, attendance.attending)
            else:
              students = ndb.get_multi(the_class.enrolled)
              for student in students:
                self.add_student(student.key, hours, attendance.attending)

      except ValueError:
        # hours was not a float
        self.redirect('/students?class_id=%s&date=%s&errmsg=Invalid%%20value%%20for%%20hours' % (class_id, date_ordinal))
        return
    if attendance:
      if yes:
        status = "present"
      else:
        status = "absent"
      logging.info('Change by %s: %s %s marked as %s for %s (hours: %s)' % 
                   (authz.get_name(), student.first_name, student.last_name, status, the_class.name, hours))
      attendance.put()
    elif attendance_already_exists:
      attendance_key.delete()
    self.redirect('/students?class_id=%s&date=%s' % (class_id, date_ordinal))