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 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 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 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 CheckPassword(current_user, user_type): if not request.data: return ResponseHelper.ReturnBadRequestResponse() else: user_data = json.loads(request.data) un_encrypted_password = user_data[str(UsersTable.PASSWORD)] if CheckPassword(current_user, un_encrypted_password): return ResponseHelper.ReturnOkResponse("Ok") else: return ResponseHelper.ReturnUnauthorizedResponse( "Current Password is Incorrect!")
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 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 DeleteInvite(current_user, user_type, invite_id): invite_check_dao = InviteDao() if invite_check_dao.CanEditOrDelete(current_user, invite_id): invite_dao = InviteDao() if invite_dao.DeclineInvite(invite_id): return ResponseHelper.ReturnOkResponse("Invite Declined") else: return ResponseHelper.ReturnConflictResponse( "Unable to Decline Invite!") else: return ResponseHelper.ReturnBadRequestResponse()
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 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 UpdateEvent(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) update_data = UpdateData( request_data[RequestConstants.UPDATE_REQUEST_MEMBER_KEYS], request_data[RequestConstants.UPDATE_REQUEST_MEMBER_VALUES], [EventsTable.ID], [event_id], ) if EventsTable.ID in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if EventsTable.HOST_USERNAME in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() if EventsTable.CREATED_DATE in update_data.memberKeys: return ResponseHelper.ReturnBadRequestResponse() event_dao = EventDao() if event_dao.UpdateEvent(update_data): return ResponseHelper.ReturnOkResponse("Event Updated!") else: return ResponseHelper.ReturnConflictResponse("Unable to Update Event!")
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 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 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 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!")
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 hello(): return ResponseHelper.ReturnOkResponse("TrainingPal API V1.0")
def hello(current_user, user_type): return ResponseHelper.ReturnOkResponse("Hello " + current_user)