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 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 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 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 decorated(*args, **kwargs): token = None if RequestConstants.REQUEST_TOKEN_IDENTIFIER in request.headers: split_string = request.headers[ RequestConstants.REQUEST_TOKEN_IDENTIFIER].split(" ") auth_type = split_string[0] token = split_string[1] if auth_type != RequestConstants.REQUEST_TOKEN_AUTH_TYPE: return ResponseHelper.ReturnBadRequestResponse() if not token: return ResponseHelper.ReturnBadRequestResponse() try: data = TokenHelper.DecodeTokenAndValidate(token) current_user = data[TokenConstants.PAYLOAD_USER_KEY] user_type = data[TokenConstants.RESPONSE_USER_TYPE_KEY] except jwt.ExpiredSignatureError: return ResponseHelper.ReturnUnauthorizedResponse( "Token Has Expired!") except jwt.InvalidTokenError: return ResponseHelper.ReturnUnauthorizedResponse("Bad Token!") except Exception: return ResponseHelper.ReturnBadRequestResponse() return f(current_user, user_type, *args, **kwargs)
def CreateTemplate(current_user, user_type, user_id): if current_user != user_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: training_log_data = json.loads(request.data) template = TrainingLogTemplate( None, # For Id that is auto generated by a trigger user_id, # Ensure log is created for the current user, training_log_data[str(TrainingLogTemplateTable.NAME)], training_log_data[str(TrainingLogTemplateTable.DURATION_PLANNED)], training_log_data[str(TrainingLogTemplateTable.DURATION_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.DISTANCE_PLANNED)], training_log_data[str(TrainingLogTemplateTable.DISTANCE_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.DISTANCE_UNIT)], training_log_data[str( TrainingLogTemplateTable.HR_RESTING_PLANNED)], training_log_data[str(TrainingLogTemplateTable.HR_AVG_PLANNED)], training_log_data[str(TrainingLogTemplateTable.HR_MAX_PLANNED)], training_log_data[str(TrainingLogTemplateTable.HR_RESTING_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.HR_AVG_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.HR_MAX_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.WATTS_AVG_PLANNED)], training_log_data[str(TrainingLogTemplateTable.WATTS_MAX_PLANNED)], training_log_data[str(TrainingLogTemplateTable.WATTS_AVG_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.WATTS_MAX_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.RPE_PLANNED)], training_log_data[str(TrainingLogTemplateTable.RPE_ACTUAL)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE1_TIME)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE2_TIME)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE3_TIME)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE4_TIME)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE5_TIME)], training_log_data[str(TrainingLogTemplateTable.HR_ZONE6_TIME)]) training_log_template_dao = TrainingLogTemplateDao() if training_log_template_dao.CreateTrainingLogTemplate(template): return ResponseHelper.ReturnCreatedResponse("Template Created") else: return ResponseHelper.ReturnConflictResponse( "Unable to Create Template!")
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 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 GetUser(current_user, user_type, username): if not username: return ResponseHelper.ReturnBadRequestResponse() user_dao = UserDao() if username == current_user: user = user_dao.GetUserById(current_user) if user: return ResponseHelper.ReturnOkDataResponse("User Found", { "User": DAOHelper.RemoveValueFromObject(UsersTable.PASSWORD, user) }) else: return ResponseHelper.ReturnNotFoundResponse("User Not Found!") else: user = user_dao.GetUserById(username) if user: return ResponseHelper.ReturnOkDataResponse("Request OK", { "User": DAOHelper.RemoveValueFromObject(UsersTable.PASSWORD, user) }) else: return ResponseHelper.ReturnNotFoundResponse("User Not Found!")
def CreateMultipleInvites(current_user, user_type): if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: request_data = json.loads(request.data) invite = Invite( None, # For Id that is auto generated by a trigger request_data[str(InvitesTable.INVITE_TYPE)], current_user, # Ensure log is created for the current user, None, request_data[str(InvitesTable.STATUS)], request_data[str(InvitesTable.SEND_DATE)], request_data[str(InvitesTable.EVENT_ID)]) recipients = request_data["Recipients"] invite_dao = InviteDao() if invite_dao.CreateMultipleInvites(invite, recipients): return ResponseHelper.ReturnCreatedResponse("Invites Created") else: return ResponseHelper.ReturnConflictResponse( "Unable to Create Invites!")
def Login(): auth = request.authorization if not auth or not auth.username or not auth.password: return ResponseHelper.ReturnBadRequestResponse() user_dao = UserDao() user = user_dao.GetUserById(auth.username) if not user: return ResponseHelper.ReturnUnauthorizedResponse( "User Does Not Exist!") if CheckPassword(auth.username, auth.password): try: return ResponseHelper.ReturnOkDataResponse( "User Authenticated", { "Token": TokenHelper.CreateToken(user[UsersTable.ID], user[UsersTable.TYPE]) }) except: return ResponseHelper.ReturnErrorResponse() else: return ResponseHelper.ReturnUnauthorizedResponse("Incorrect Password!")
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 DeleteTemplate(current_user, user_type, user_id, template_id): if current_user != user_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() training_log_template_dao = TrainingLogTemplateDao() if training_log_template_dao.DeleteTrainingLogTemplate( user_id, template_id): return ResponseHelper.ReturnOkResponse("Template Deleted") else: return ResponseHelper.ReturnConflictResponse( "Unable to Delete Template!")
def GetTemplates(current_user, user_type, user_id): if current_user != user_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() training_log_template_dao = TrainingLogTemplateDao() templates = training_log_template_dao.GetTemplates(user_id) if templates: return ResponseHelper.ReturnOkDataResponse("Request OK", {"Templates": templates}) else: return ResponseHelper.ReturnNotFoundResponse("No Templates Found!")
def AcceptParticipants(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() event_access_dao = EventDao() if not event_access_dao.CanAccess(event_id, current_user): return ResponseHelper.ReturnBadRequestResponse() event_dao = EventDao() if event_dao.AcceptParticipant(event_id, current_user): return ResponseHelper.ReturnOkResponse("Participants Accepted!") else: return ResponseHelper.ReturnConflictResponse( "Unable to Accept Participants!")
def UpdateMultipleInvite(current_user, user_type): if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: request_data = json.loads(request.data) update_data = UpdateInviteData( request_data[RequestConstants.UPDATE_REQUEST_MEMBER_KEYS], request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES], len(request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES]), request_data["Invite_Ids"], None, None) invite_check_dao = InviteDao() if invite_check_dao.CanEditOrDeleteMultiple(current_user, update_data.inviteIds): if InvitesTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if InvitesTable.SENT_BY in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if InvitesTable.SENT_TO in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() invite_dao = InviteDao() if invite_dao.UpdateMultipleInvites(update_data): return ResponseHelper.ReturnOkResponse("Invite Updated") else: return ResponseHelper.ReturnConflictResponse( "Unable to Update Invite!") else: return ResponseHelper.ReturnBadRequestResponse()
def DeleteEvent(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() host_check_dao = EventDao() if not host_check_dao.CheckHostAccess(current_user, event_id): return ResponseHelper.ReturnBadRequestResponse() invite_dao = InviteDao() invite_dao.DeleteInvitesFromEvent(event_id) event_dao = EventDao() if event_dao.DeleteEvent(event_id): return ResponseHelper.ReturnOkResponse("Event Deleted!") else: return ResponseHelper.ReturnConflictResponse("Unable to Delete Event!")
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 RemoveParticipants(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() event_access_dao = EventDao() if not event_access_dao.CanAccess(event_id, current_user): return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) event_dao = EventDao() if event_dao.RemoveParticipants(event_id, request_data["Users"]): return ResponseHelper.ReturnOkResponse("Participants Deleted!") else: return ResponseHelper.ReturnConflictResponse( "Unable to Delete Participants!")
def GetUsers(current_user, user_type, search_type, search_value): if not search_type and search_value: return ResponseHelper.ReturnBadRequestResponse() user_dao = UserDao() users = user_dao.GetUsers(search_type, search_value) if users: return ResponseHelper.ReturnOkDataResponse("Request OK", {"Users": users}) else: return ResponseHelper.ReturnNotFoundResponse("Users Not Found!")
def UpdateUser(current_user, user_type, user_id): if 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], [UsersTable.ID], [user_id], ) if UsersTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if UsersTable.PASSWORD in update_data.memberKeys: update_data.memberValues[update_data.memberKeys.index(UsersTable.PASSWORD)] = \ PasswordHashingHelper.GetEncryptedHash(update_data.memberValues[ update_data.memberKeys.index(UsersTable.PASSWORD)]) user_dao = UserDao() if user_dao.UpdateUser(update_data): return ResponseHelper.ReturnOkResponse("User Updated") else: return ResponseHelper.ReturnConflictResponse( "Unable to Update User!")
def GetEvent(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() event_dao = EventDao() event = event_dao.GetEvent(event_id) if event: return ResponseHelper.ReturnOkDataResponse("Request OK", {"Event": event}) else: return ResponseHelper.ReturnNotFoundResponse("No Event Found!")
def CanAccess(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() event_dao = EventDao() can_access = event_dao.CanAccess(event_id, current_user) if can_access: return ResponseHelper.ReturnOkDataResponse("Ok", can_access) else: return ResponseHelper.ReturnUnauthorizedResponse( "Needs to request access!")
def GetAthletes(current_user, user_type): if user_type != RequestConstants.USER_TYPE_COA: return ResponseHelper.ReturnBadRequestResponse() user_dao = UserDao() users = user_dao.GetCoachAthletes(current_user) if users: return ResponseHelper.ReturnOkDataResponse("Request OK", {"Users": users}) else: return ResponseHelper.ReturnNotFoundResponse("Users Not Found!")
def CreateParticipants(current_user, user_type, event_id): if not event_id: return ResponseHelper.ReturnBadRequestResponse() if not request.data: return ResponseHelper.ReturnBadRequestResponse() host_check_dao = EventDao() if not host_check_dao.CheckHostAccess(current_user, event_id): return ResponseHelper.ReturnBadRequestResponse() request_data = json.loads(request.data) event_dao = EventDao() if event_dao.CreateParticipants(event_id, request_data["Users"]): return ResponseHelper.ReturnOkResponse("Participants Added!") else: return ResponseHelper.ReturnConflictResponse( "Unable to Add Participants!")
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 UpdateTemplate(current_user, user_type, user_id, template_id): if current_user != user_id: return ResponseHelper.ReturnBadRequestResponse() if user_type != RequestConstants.USER_TYPE_COA: 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], [TrainingLogTemplateTable.ID, TrainingLogTemplateTable.COACH_ID], [template_id, user_id]) if TrainingLogTemplateTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if TrainingLogTemplateTable.COACH_ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() training_log_template_dao = TrainingLogTemplateDao() if training_log_template_dao.UpdateTrainingLogTemplate(update_data): return ResponseHelper.ReturnOkResponse("Template Updated") else: return ResponseHelper.ReturnConflictResponse( "Unable to Update Template!")