def check(location_name, usr, start_time, end_time): uuid = beacon.get_uuid(location_name) today_start = sgtime.get_datetime_now().replace(hour=0, minute=0, second=0, microsecond=0) activities = AttendanceActivity.query( AttendanceActivity.uuid == uuid, AttendanceActivity.usr == usr, ndb.AND(AttendanceActivity.time >= today_start, AttendanceActivity.time <= end_time)).order( AttendanceActivity.time).fetch() if not activities: return False before_activity = None for a in activities: if a.time < start_time: before_activity = a elif a.type == constants.ENTER: return True if before_activity is None: return False else: return before_activity.type == constants.ENTER
def add_event(uuid, type, usr): uuids = [b.uuid for b in beacon.get_list()] assert uuid in uuids assert user.get(usr) AttendanceActivity(uuid=uuid, type=type, usr=usr, time=sgtime.get_datetime_now()).put()
def get_list(usr): user_info = user.get(usr) tasks = Task.query( Task.school == user_info.school, Task.due_date >= sgtime.get_datetime_now(), Task.cls == user_info.cls, Task.subject.IN(user_info.subjects) ).fetch() tasks.sort(key=lambda x: x.time_added, reverse=True) for t in tasks: t.done = usr in t.done_users return tasks
def get_user_location(usr): now = sgtime.get_datetime_now() today_start = now.replace(hour=0, minute=0, second=0) locations = AttendanceActivity.query( AttendanceActivity.time < now, AttendanceActivity.time > today_start, AttendanceActivity.usr == usr).order(-AttendanceActivity.time).fetch(1) if locations: location_name = beacon.get_location_name(locations[0].uuid) if locations[0].type == constants.ENTER: return location_name else: return "Unknown, Last known: " + location_name else: return "Unknown"
def add(subject, cls, title, due_date, usr): user_info = user.get(usr) if cls == user_info.cls and (subject in user_info.subjects): Task( school=user_info.school, subject=subject, cls=cls, title=title, due_date=due_date, usr=usr, time_added=sgtime.get_datetime_now() ).put() user.increment_points(usr) else: raise Exception('Unauthorized to add task')
def test_attendance(self): # Enter attendance.add_event("123", 1, self.weijie) location = attendance.get_user_location(self.weijie) self.assertEqual(location, "test") # Exit attendance.add_event("123", 0, self.weijie) location = attendance.get_user_location(self.weijie) self.assertNotEqual(location, "test") # Attendance checks now = sgtime.get_datetime_now() status = attendance.check("test", self.weijie, now - datetime.timedelta(hours=1), now) self.assertEqual(status, True) status = attendance.check("test", self.jerrayl, now - datetime.timedelta(hours=1), now) self.assertEqual(status, False) # Pre-marked attendance attendance.AttendanceActivity(uuid="123", usr=self.jerrayl, type=1, time=now - datetime.timedelta(hours=2)).put() status = attendance.check("test", self.jerrayl, now - datetime.timedelta(hours=1), now) self.assertEqual(status, True) attendance.AttendanceActivity( uuid="123", usr=self.jerrayl, type=0, time=now - datetime.timedelta(hours=1, minutes=30)).put() status = attendance.check("test", self.jerrayl, now - datetime.timedelta(hours=1), now) self.assertEqual(status, False)
def get(usr): usr_details = user.get(usr) usr_location = attendance.get_user_location(usr) today = sgtime.get_datetime_now().replace(hour=0, minute=0, second=0, microsecond=0) suggestions = Suggestion.query( Suggestion.date == today, Suggestion.subject.IN(usr_details.subjects), Suggestion.location == usr_location).fetch() if attendance.get_user_location(usr) == 'Comp Lab S2': for t in task.get_due_homework(usr, 'COMP'): suggestions.append( Suggestion(text=t.title + ' due this lesson', type='homeworkdue', subject=t.subject)) string = unicode("\n大学之道,在明明德, 在亲民,在止于至善。", "utf-8") suggestions.append( Suggestion(text='Quote of the day: ' + string, type='quote', subject='GP')) return suggestions
def get_all(): today = sgtime.get_datetime_now().date() suggestions = Suggestion.query(Suggestion.date >= today).fetch() return suggestions
def get_tasks(): tasks = Task.query( Task.due_date >= sgtime.get_datetime_now() ).fetch() tasks.sort(key=lambda x: x.time_added, reverse=True) return tasks
def get_due_homework(usr, subject): today = sgtime.get_datetime_now().date() return [x for x in get_list(usr) if x.due_date == today or x.subject==subject]