def get(self, request, format=None): paginator = CustomPagination() sport = int(request.query_params.get('sport')) if sport == SWIMMING: try: swimmer = Swimmer.objects.get(type__id=request.user.id) swim_workouts = SwimWorkout.objects.filter( user=swimmer).order_by('-dateStart') serializer = SwimWorkoutSerializer(swim_workouts, many=True) except: return Response(error_response( 'This user has not a swimmer profile enabled.'), status=status.HTTP_404_NOT_FOUND) elif sport == RUNNING: try: runner = Runner.objects.get(type_id=request.user.id) run_workouts = RunWorkout.objects.filter( user=runner).order_by('-dateStart') serializer = RunWorkoutSerializer(run_workouts, many=True) except: return Response(error_response( 'This user has not a runner profile enabled.'), status=status.HTTP_404_NOT_FOUND) return paginator.get_paginated_response( success_response(serializer.data))
def put(self, request, pk, format=None): sport = int(request.data.get('sport', '')) if sport == SWIMMING: try: workout = SwimWorkout.objects.get(id=pk) serializer = SwimWorkoutSerializer(workout, data=request.data, partial=True) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) elif sport == RUNNING: try: workout = RunWorkout.objects.get(id=pk) serializer = RunWorkoutSerializer(workout, data=request.data, partial=True) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) if serializer.is_valid(): serializer.save() return Response(success_response(serializer.data), status=status.HTTP_201_CREATED) return Response(error_response(serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def get(self, request): data = {'landing-doswim': { 'status': 'online', 'url': 'www.landing-doswim.com' }, 'landing-dorun': {'status': 'offline', 'url': 'www.landing-dorun.com' }, 'landing-narwhale': { 'status': 'offline', 'url': 'www.landing-narwhale.com' }} return Response(success_response(data), status=status.HTTP_200_OK)
def get(self, request): version = getattr(settings, 'min_app_version', 1) force_update = getattr(settings, 'force_update', True) config = { 'min_app_version': version, 'force_update': force_update, } return Response(success_response(config), status=status.HTTP_200_OK)
def post(self, request, format=None, sport=None): try: email = request.data['email'] existing_user = User.objects.get(email=email) return Response(error_response( 'User with email %s already exists.' % email), status=status.HTTP_400_BAD_REQUEST) except: pass sport = int(request.query_params.get('sport', '0')) user = UserSerializer() user_fields = [key for key in user.get_fields().keys()] user_data = { key: request.data[key] for key in user_fields if key in request.data } user_data['type_id'] = SPORTS[sport][1] user_serializer = UserSerializer(data=user_data) # Create base user if user_serializer.is_valid(): if not 'password' in request.data: return Response(error_response('Password not provided.'), status=status.HTTP_400_BAD_REQUEST) else: user = user_serializer.save() user.set_password(request.data['password']) user.save() # Create specific user (swimmer, runner...) if sport == 0: swimmer = SwimmerSerializer() swimmer_fields = [key for key in SwimmerSerializer.specific_fields] swimmer_data = { key: request.data[key] for key in swimmer_fields if key in request.data } swimmer_data['type'] = user.id serializer = SwimmerSerializer(data=swimmer_data) elif sport == 1: runner = RunnerSerializer() runner_fields = [key for key in RunnerSerializer.specific_fields] runner_data = { key: request.data[key] for key in runner_fields if key in request.data } runner_data['type'] = user.id serializer = RunnerSerializer(data=runner_data) if serializer.is_valid(): sport_user = serializer.save() return Response(success_response(serializer.data), status=status.HTTP_201_CREATED) return Response(error_response(serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): sport = int(request.query_params.get('sport', '')) if sport == 0: swimmers = Swimmer.objects.all() serializer = SwimmerSerializer(swimmers, many=True) elif sport == 1: runners = Runner.objects.all() serializer = RunnerSerializer(runners, many=True) return Response(success_response(serializer.data))
def post(self, request, format=None, sport=None): try: email = request.data['email'] existing_user = User.objects.get(email=email) return Response(error_response('User with email %s already exists.' % email), status=status.HTTP_400_BAD_REQUEST) except: pass sport = int(request.query_params.get('sport', '0')) user = UserSerializer() user_fields = [key for key in user.get_fields().keys()] user_data = { key: request.data[key] for key in user_fields if key in request.data } user_data['type_id'] = SPORTS[sport][1] user_serializer = UserSerializer(data=user_data) # Create base user if user_serializer.is_valid(): if not 'password' in request.data: return Response(error_response('Password not provided.'), status=status.HTTP_400_BAD_REQUEST) else: user = user_serializer.save() user.set_password(request.data['password']) user.save() # Create specific user (swimmer, runner...) if sport == 0: swimmer = SwimmerSerializer() swimmer_fields = [key for key in SwimmerSerializer.specific_fields] swimmer_data = { key: request.data[key] for key in swimmer_fields if key in request.data } swimmer_data['type'] = user.id serializer = SwimmerSerializer(data=swimmer_data) elif sport == 1: runner = RunnerSerializer() runner_fields = [key for key in RunnerSerializer.specific_fields] runner_data = { key: request.data[key] for key in runner_fields if key in request.data } runner_data['type'] = user.id serializer = RunnerSerializer(data=runner_data) if serializer.is_valid(): sport_user = serializer.save() return Response(success_response(serializer.data), status=status.HTTP_201_CREATED) return Response(error_response(serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): user = User.objects.get(id=pk) user_serializer = UserSerializer() sport = int(request.query_params.get('sport', '')) user_data = { key: request.data[key] for key in user_serializer.fields if key in request.data } for (key, value) in user_data.items(): setattr(user, key, value) user.save() if sport == 0: try: swimmer = Swimmer.objects.get(type__id=user.id) except Swimmer.DoesNotExist: return Response(error_response('Swimmer does not exist'), status=status.HTTP_404_NOT_FOUND) swimmer_fields = [key for key in SwimmerSerializer.specific_fields] swimmer_data = { key: request.data[key] for key in swimmer_fields if key in request.data } swimmer_data['type'] = user.id serializer = SwimmerSerializer(swimmer, data=swimmer_data, partial=True) elif sport == 1: try: runner = Runner.objects.get(type__id=user.id) except Runner.DoesNotExist: return Response(error_response('Runner does not exist'), status=status.HTTP_404_NOT_FOUND) runner_fields = [key for key in RunnerSerializer.specific_fields] runner_data = { key: request.data[key] for key in runner_fields if key in request.data } runner_data['type'] = user.id serializer = RunnerSerializer(runner, data=runner_data, partial=True) if serializer.is_valid(): serializer.save() return Response(success_response(serializer.data), status=status.HTTP_201_CREATED) return Response(error_response(serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data) sport = int(request.query_params.get('sport', '')) if serializer.is_valid(): user = serializer.validated_data['user'] token, created = Token.objects.get_or_create(user=user) if sport == 0: swimmer = Swimmer.objects.get(type=user.id) user_serialized = SwimmerSerializer(swimmer) elif sport == 1: runner = Runner.objects.get(type=user.id) user_serialized = RunnerSerializer(runner) response_data = user_serialized.data response_data['token'] = token.key return Response(success_response(response_data)) else: return Response(error_response("User or password incorrect."))
def get(self, request): sport = int(request.query_params.get('sport')) if sport == SWIMMING: swim_stats = SwimmingStats.objects.get(id=1) data = { 'strokes': swim_stats.strokes, 'duration': swim_stats.duration, 'distance': swim_stats.distance } elif sport == RUNNING: run_stats = RunningStats.objects.get(id=1) data = { 'duration': run_stats.duration, 'distance': run_stats.distance } return Response(success_response(data), status=status.HTTP_200_OK)
def delete(self, request, pk, format=None): sport = int(request.data.get('sport', '')) if sport == SWIMMING: try: workout = SwimWorkout.objects.get(id=pk) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) elif sport == RUNNING: try: workout = RunWorkout.objects.get(id=pk) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) workout.delete() return Response(success_response("null"), status=status.HTTP_200_OK)
def post(self, request, format=None): data = request.data user_id = request.user.id sport = int(request.data.get('sport', '')) if sport == SWIMMING: sport_user = Swimmer.objects.get(type__id=user_id) data['user'] = sport_user.id serializer = SwimWorkoutSerializer(data=data) elif sport == RUNNING: sport_user = Runner.objects.get(type__id=user_id) data['user'] = sport_user.id serializer = RunWorkoutSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(success_response(serializer.data), status=status.HTTP_201_CREATED) return Response(error_response(serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, format=None): #sport = int(request.data.get('sport', '')) sport = int(request.query_params.get('sport')) if sport == SWIMMING: try: workout = SwimWorkout.objects.get(id=pk) serializer = SwimWorkoutSerializer(workout) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) elif sport == RUNNING: try: workout = RunWorkout.objects.get(id=pk) serializer = RunWorkoutSerializer(workout) except: return Response(error_response("Workout with id %s not found" % pk), status=status.HTTP_404_NOT_FOUND) return Response(success_response(serializer.data), status=status.HTTP_200_OK)
def get(self, request, *args, **kwargs): try: provided_token = request.META['HTTP_AUTHORIZATION'] provided_token = provided_token.replace('Token ', '') token = Token.objects.select_related('user').get(key=provided_token) except Exception as e: # modify the original exception response raise exceptions.APIException("Token not valid.") if not token.user.is_active: # can also modify this exception message return Response(error_response("User inactive or deleted.")) sport = int(request.query_params.get('sport', '')) if sport == 0: swimmer = Swimmer.objects.get(type=request.user.id) user_serialized = SwimmerSerializer(swimmer) elif sport == 1: runner = Runner.objects.get(type=request.user.id) user_serialized = RunnerSerializer(runner) return Response(success_response(user_serialized.data))
def get(self, request, *args, **kwargs): try: provided_token = request.META['HTTP_AUTHORIZATION'] provided_token = provided_token.replace('Token ', '') token = Token.objects.select_related('user').get( key=provided_token) except Exception as e: # modify the original exception response raise exceptions.APIException("Token not valid.") if not token.user.is_active: # can also modify this exception message return Response(error_response("User inactive or deleted.")) sport = int(request.query_params.get('sport', '')) if sport == 0: swimmer = Swimmer.objects.get(type=request.user.id) user_serialized = SwimmerSerializer(swimmer) elif sport == 1: runner = Runner.objects.get(type=request.user.id) user_serialized = RunnerSerializer(runner) return Response(success_response(user_serialized.data))