def handle_job(self): utc = datetime.utcnow() for tz, offset in _tz_offsets.items(): time = datetime(utc.year, utc.month, utc.day, utc.hour) + timedelta(hours=offset) logging.info("Checking for sweep at: " + str(time) + " with tz: " + tz) sweeps = AutoSweep.query(AutoSweep.time == time, AutoSweep.tz == tz) for sweep in sweeps: logging.info(sweep) leader_email = sweep.key.parent().string_id() team_leader = RoseboticsStudent.get_by_id(leader_email) try: export_student_name = len( str(sweep.options.get("student_name", ""))) > 0 export_rose_username = len( str(sweep.options.get("rose_username", ""))) > 0 team_urlsafe = sweep.team_key.urlsafe() timezone = sweep.options.get("timezone") or sweep.tz course_progress = len( str(sweep.options.get("course_progress", ""))) > 0 track_progress = len( str(sweep.options.get("track_progress", ""))) > 0 unit_points = len(str(sweep.options.get("ppu", ""))) > 0 ppu = float(sweep.options.get("ppu", "1")) ppt = float(sweep.options.get("ppt", "1")) csv_data = get_csv_export_lists(team_leader, team_urlsafe, export_student_name, export_rose_username, unit_points, ppu, ppt, course_progress, track_progress, timezone) self.response.headers['Content-Type'] = 'application/csv' mem_stream = StringIO.StringIO() writer = csv.writer(mem_stream) for csv_row in csv_data: writer.writerow(csv_row) self.send_email_with_attachment( leader_email, "You've received an AutoSweep!", mem_stream.getvalue()) mem_stream.close() except Exception, e: import traceback traceback.print_exc() logging.error(e) self.send_email( leader_email, "ERROR: Sorry there was an error generating your AutoSweep" )
def delete_team(self, team): """ Deletes a team that you own """ user_email = get_user_email() team = team.team_key.get() if team.leader != user_email: raise endpoints.BadRequestException("You are not the leader of this team!") return members = RoseboticsTeamMember.query(ancestor=team.key) for member in members: member.key.delete() for sweep in AutoSweep.query(AutoSweep.team_key == team.key): sweep.key.delete() team.key.delete() return Team(name="deleted")
def delete_team(self, team): """ Deletes a team that you own """ user_email = get_user_email() team = team.team_key.get() if team.leader != user_email: raise endpoints.BadRequestException( 'You are not the leader of this team!') return members = RoseboticsTeamMember.query(ancestor=team.key) for member in members: member.key.delete() for sweep in AutoSweep.query(AutoSweep.team_key == team.key): sweep.key.delete() team.key.delete() return Team(name='deleted')
def query_sweeps(self, query): """ Get a user's AutoSweeps """ user_email = get_user_email() query = AutoSweep.query(AutoSweep.team_key == query.team_key).order(-AutoSweep.time) response = Sweeps() for auto_sweep in query: sweep = Sweep() sweep.sweep_key = auto_sweep.key sweep.team_key = auto_sweep.team_key sweep.options = auto_sweep.unparse_options() sweep.email = user_email sweep.year = auto_sweep.time.year sweep.month = auto_sweep.time.month sweep.day = auto_sweep.time.day sweep.hour = auto_sweep.time.hour sweep.tz = auto_sweep.tz response.sweeps.append(sweep) return response
def query_sweeps(self, query): """ Get a user's AutoSweeps """ user_email = get_user_email() query = AutoSweep.query( AutoSweep.team_key == query.team_key).order(-AutoSweep.time) response = Sweeps() for auto_sweep in query: sweep = Sweep() sweep.sweep_key = auto_sweep.key sweep.team_key = auto_sweep.team_key sweep.options = auto_sweep.unparse_options() sweep.email = user_email sweep.year = auto_sweep.time.year sweep.month = auto_sweep.time.month sweep.day = auto_sweep.time.day sweep.hour = auto_sweep.time.hour sweep.tz = auto_sweep.tz response.sweeps.append(sweep) return response
def insert_sweep(self, sweep): """ Create or edit an AutoSweep """ user_email = get_user_email() auto_sweep = None if sweep.sweep_key: auto_sweep = sweep.sweep_key.get() if not auto_sweep: new_sweep = AutoSweep(parent=ndb.Key(Sweep, user_email)) else: if user_email != auto_sweep.key.parent().string_id(): raise endpoints.NotFoundException( 'No AutoSweep with this key exists') new_sweep = auto_sweep new_sweep.time = sweep.get_date_time() new_sweep.options = sweep.parse_options() new_sweep.team_key = sweep.team_key new_sweep.tz = sweep.tz sweep.sweep_key = new_sweep.put() return sweep
def insert_sweep(self, sweep): """ Create or edit an AutoSweep """ user_email = get_user_email() auto_sweep = None if sweep.sweep_key: auto_sweep = sweep.sweep_key.get() if not auto_sweep: new_sweep = AutoSweep(parent=ndb.Key(Sweep, user_email)) else: if user_email != auto_sweep.key.parent().string_id(): raise endpoints.NotFoundException("No AutoSweep with this key exists") new_sweep = auto_sweep new_sweep.time = sweep.get_date_time() new_sweep.options = sweep.parse_options() new_sweep.team_key = sweep.team_key new_sweep.tz = sweep.tz sweep.sweep_key = new_sweep.put() return sweep