def CreateTarget(current_user, user_type, user_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTargetsAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: request_data = json.loads(request.data) target = Target( None, # For Id that is auto generated by a trigger user_id, # Ensure log is created for the current user, request_data[str(TargetsTable.CONTENT)], request_data[str(TargetsTable.STATUS)], request_data[str(TargetsTable.WEEK)], request_data[str(TargetsTable.YEAR)]) target_dao = TargetDao() if target_dao.CreateTarget(target): return ResponseHelper.ReturnCreatedResponse("Target Created") else: return ResponseHelper.ReturnConflictResponse( "Unable to Create Target!")
def UpdateCoachAccessFromInvite(current_user, user_type, invite_id): if not invite_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_ATH: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) update_data = UpdateData( request_data[RequestConstants.UPDATE_REQUEST_MEMBER_KEYS], request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES], [CoachAthleteTable.ATHLETE_ID, CoachAthleteTable.INVITE_ID], [current_user, invite_id], ) if CoachAthleteTable.ATHLETE_ID in update_data.memberKeys or CoachAthleteTable.COACH_ID in update_data.memberKeys or CoachAthleteTable.INVITE_ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() access_dao = AccessDao() if access_dao.UpdateFromInvite(update_data): return ResponseHelper.ReturnOkResponse("Access Updated!") else: return ResponseHelper.ReturnConflictResponse("Unable to Update Access")
def GrantCoachPrivileges(current_user, user_type, coach_id): if not coach_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_ATH: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) access = CoachAthlete( current_user, coach_id, request_data[CoachAthleteTable.CAN_ACCESS_TRAINING_LOG], request_data[CoachAthleteTable.CAN_ACCESS_TARGETS], request_data[CoachAthleteTable.IS_ACTIVE], request_data[CoachAthleteTable.START_DATE], request_data[CoachAthleteTable.INVITE_ID]) access_dao = AccessDao() if access_dao.CreateAccess(access): return ResponseHelper.ReturnOkResponse("Access Granted!") else: return ResponseHelper.ReturnConflictResponse("Unable to Grant Access")
def UpdateCoachAccess(current_user, user_type, coach_id): if not coach_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_ATH: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) access = CoachAthlete( None, None, request_data[CoachAthleteTable.CAN_ACCESS_TRAINING_LOG], request_data[CoachAthleteTable.CAN_ACCESS_TARGETS], None, None, None, ) access_dao = AccessDao() if access_dao.UpdateCoachAccess(current_user, coach_id, access): return ResponseHelper.ReturnOkResponse("Access Updated!") else: return ResponseHelper.ReturnConflictResponse("Unable to Update Access")
def CoachRequestAccess(current_user, user_type, athlete_id): if not athlete_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) access = CoachAthlete( athlete_id, current_user, request_data[CoachAthleteTable.CAN_ACCESS_TRAINING_LOG], request_data[CoachAthleteTable.CAN_ACCESS_TARGETS], request_data[CoachAthleteTable.IS_ACTIVE], request_data[CoachAthleteTable.START_DATE], request_data[CoachAthleteTable.INVITE_ID]) access_dao = AccessDao() if access_dao.CreateCoachAccessWithInvite(access): return ResponseHelper.ReturnOkResponse("Ok") else: return ResponseHelper.ReturnConflictResponse( "Unable to request access!")
def CheckCoachTrainingLogAccess(current_user, user_type, athlete_id): if not athlete_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() access_dao = AccessDao() if access_dao.CheckCoachTrainingLogAccess(current_user, athlete_id): return ResponseHelper.ReturnOkResponse("Ok") else: return ResponseHelper.ReturnUnauthorizedResponse( "Needs to request access!")
def RevokeCoachPrivileges(current_user, user_type, coach_id): if not coach_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_ATH: return ResponseHelper.ReturnBadRequestResponse() access_dao = AccessDao() if access_dao.RevokeCoachAccess(current_user, coach_id): return ResponseHelper.ReturnOkResponse("Access Deleted!") else: return ResponseHelper.ReturnConflictResponse("Unable to Delete Access")
def GetCoachAccess(current_user, user_type, athlete_id): if not athlete_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() access_dao = AccessDao() access = access_dao.CheckAccess(current_user, athlete_id) if access: return ResponseHelper.ReturnOkDataResponse("Ok", {"Access": access}) else: return ResponseHelper.ReturnUnauthorizedResponse( "Needs to request access!")
def DeleteTrainingLog(current_user, user_type, user_id, training_log_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTrainingLogAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() training_log_dao = TrainingLogDao() if training_log_dao.DeleteWorkout(user_id, training_log_id): return ResponseHelper.ReturnOkResponse("Training Log Deleted") else: return ResponseHelper.ReturnConflictResponse("Unable to Delete Training Log!")
def DeleteTarget(current_user, user_type, user_id, target_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTargetsAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() target_dao = TargetDao() if target_dao.DeleteTarget(user_id, target_id): return ResponseHelper.ReturnOkResponse("Target Deleted") else: return ResponseHelper.ReturnConflictResponse( "Unable to Delete Target!")
def GetTargets(current_user, user_type, user_id, week_num, year_num): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTargetsAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not week_num: return ResponseHelper.ReturnBadRequestResponse() target_dao = TargetDao() targets = target_dao.GetTargetsByWeek(user_id, week_num, year_num) if targets: return ResponseHelper.ReturnOkDataResponse("Request OK", {"Targets": targets}) else: return ResponseHelper.ReturnNotFoundResponse("No Targets Found!")
def GetWorkoutById(current_user, user_type, user_id, training_log_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTrainingLogAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not training_log_id: return ResponseHelper.ReturnBadRequestResponse() training_log_dao = TrainingLogDao() training_log = training_log_dao.GetWorkoutById(training_log_id, user_id) if training_log: return ResponseHelper.ReturnOkDataResponse( "Request OK", {"TrainingLog": training_log} ) else: return ResponseHelper.ReturnNotFoundResponse("Training Log Not Found!")
def UpdateTrainingLog(current_user, user_type, user_id, training_log_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTrainingLogAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: request_data = json.loads(request.data) update_data = UpdateData( request_data[RequestConstants.UPDATE_REQUEST_MEMBER_KEYS], request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES], [TrainingLogTable.ID, TrainingLogTable.ATHLETE_ID], [training_log_id, user_id] ) if TrainingLogTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if TrainingLogTable.ATHLETE_ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() training_log_dao = TrainingLogDao() if training_log_dao.UpdateWorkout(update_data): return ResponseHelper.ReturnOkResponse("Training Log Updated") else: return ResponseHelper.ReturnConflictResponse("Unable to Update Training Log!")
def UpdateTarget(current_user, user_type, user_id, target_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTargetsAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: request_data = json.loads(request.data) update_data = UpdateData( request_data[RequestConstants.UPDATE_REQUEST_MEMBER_KEYS], request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES], [TargetsTable.ID, TargetsTable.ATHLETE_ID], [target_id, user_id]) if TargetsTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if TargetsTable.ATHLETE_ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() target_dao = TargetDao() if target_dao.UpdateTarget(update_data): return ResponseHelper.ReturnOkResponse("Target Updated") else: return ResponseHelper.ReturnConflictResponse( "Unable to Update Target!")
def CreateTrainingLog(current_user, user_type, user_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTrainingLogAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: training_log_data = json.loads(request.data) training_log = TrainingLog( None, # For Id that is auto generated by a trigger user_id, # Ensure log is created for the current user, training_log_data[str(TrainingLogTable.TYPE_ID)], training_log_data[str(TrainingLogTable.LOG_DATE)], training_log_data[str(TrainingLogTable.LOG_TIME)], training_log_data[str(TrainingLogTable.LOG_NAME)], training_log_data[str(TrainingLogTable.LOG_DESC)], training_log_data[str(TrainingLogTable.ATHLETES_COMMENTS)], training_log_data[str(TrainingLogTable.COACHES_COMMENTS)], training_log_data[str(TrainingLogTable.WORKOUT_COMMENTS)], training_log_data[str(TrainingLogTable.DURATION_PLANNED)], training_log_data[str(TrainingLogTable.DURATION_ACTUAL)], training_log_data[str(TrainingLogTable.DISTANCE_PLANNED)], training_log_data[str(TrainingLogTable.DISTANCE_ACTUAL)], training_log_data[str(TrainingLogTable.DISTANCE_UNIT)], training_log_data[str(TrainingLogTable.HR_RESTING_PLANNED)], training_log_data[str(TrainingLogTable.HR_AVG_PLANNED)], training_log_data[str(TrainingLogTable.HR_MAX_PLANNED)], training_log_data[str(TrainingLogTable.HR_RESTING_ACTUAL)], training_log_data[str(TrainingLogTable.HR_AVG_ACTUAL)], training_log_data[str(TrainingLogTable.HR_MAX_ACTUAL)], training_log_data[str(TrainingLogTable.WATTS_AVG_PLANNED)], training_log_data[str(TrainingLogTable.WATTS_MAX_PLANNED)], training_log_data[str(TrainingLogTable.WATTS_AVG_ACTUAL)], training_log_data[str(TrainingLogTable.WATTS_MAX_ACTUAL)], training_log_data[str(TrainingLogTable.RPE_PLANNED)], training_log_data[str(TrainingLogTable.RPE_ACTUAL)], training_log_data[str(TrainingLogTable.HR_ZONE1_TIME)], training_log_data[str(TrainingLogTable.HR_ZONE2_TIME)], training_log_data[str(TrainingLogTable.HR_ZONE3_TIME)], training_log_data[str(TrainingLogTable.HR_ZONE4_TIME)], training_log_data[str(TrainingLogTable.HR_ZONE5_TIME)], training_log_data[str(TrainingLogTable.HR_ZONE6_TIME)], training_log_data[str(TrainingLogTable.CALORIES_BURNED)], training_log_data[str(TrainingLogTable.SLEEP_QUALITY)] ) training_log_dao = TrainingLogDao() if training_log_dao.CreateWorkout(training_log): return ResponseHelper.ReturnCreatedResponse("Training Log Created") else: return ResponseHelper.ReturnConflictResponse("Unable to Create Training Log!")
def GetWorkouts(current_user, user_type, user_id): if current_user != user_id: access_dao = AccessDao() if not access_dao.CheckCoachTrainingLogAccess(current_user, user_id): return ResponseHelper.ReturnBadRequestResponse() day = request.args.get('day') week = request.args.get('week') month = request.args.get('month') year = request.args.get('year') log_type = request.args.get('type') if not day and not week and not month and not year: return ResponseHelper.ReturnBadRequestResponse() else: # Workouts by day if day and month and year: training_log_dao = TrainingLogDao() training_logs = training_log_dao.GetWorkoutsByDay(day, month, year, user_id) if training_logs: return ResponseHelper.ReturnOkDataResponse( "Request OK", {"TrainingLogs": training_logs} ) else: return ResponseHelper.ReturnNotFoundResponse("No Logs Found!") # Workouts by week elif week and year and not log_type: training_log_dao = TrainingLogDao() training_logs = training_log_dao.GetWorkoutsByWeek(week, year, user_id, log_type=None) if training_logs: return ResponseHelper.ReturnOkDataResponse( "Request OK", {"TrainingLogs": training_logs} ) else: return ResponseHelper.ReturnNotFoundResponse("No Logs Found!") # Workouts by week elif week and year and log_type: training_log_dao = TrainingLogDao() training_logs = training_log_dao.GetWorkoutsByWeek(week, year, user_id, log_type) if training_logs: return ResponseHelper.ReturnOkDataResponse( "Request OK", {"TrainingLogs": training_logs} ) else: return ResponseHelper.ReturnNotFoundResponse("No Logs Found!") # Workouts by month elif month and year: month_end = calendar.monthrange(int(year), int(month))[1] training_log_dao = TrainingLogDao() training_logs = training_log_dao.GetWorkoutsByMonth(month, month_end, year, user_id) if training_logs: return ResponseHelper.ReturnOkDataResponse( "Request OK", {"TrainingLogs": training_logs} ) else: return ResponseHelper.ReturnNotFoundResponse("No Logs Found!") # Bad request if no combinations are matched else: return ResponseHelper.ReturnBadRequestResponse()