def test_get_owner_of_repo(self): response = client.get( reverse("get_repo_owner", kwargs={"pk": self.repo.id})) user = User.objects.get(pk=self.user.id) serialized = UserSerializer(user) self.assertEqual(response.data, serialized.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_valid_single_user(self): response = client.get( reverse("get_delete_update_user", kwargs={"pk": self.user.id})) user = User.objects.get(pk=self.user.id) serialized = UserSerializer(user) self.assertEqual(response.data, serialized.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def get_repo_owner(self, request, pk): try: repo = Repo.objects.get(pk=pk) except Repo.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) else: serialized_repo = UserSerializer(repo.owner) return Response(serialized_repo.data)
def scrape_user_repos(request, username): if "per_page" in request.query_params: per_page = int(request.query_params.get("per_page")) else: per_page = None if "page" in request.query_params: page = int(request.query_params.get("page")) else: page = None sort = request.query_params.get("sort") type = request.query_params.get("type") direction = request.query_params.get("direction") auth_username = os.getenv("USERNAME") access_token = os.getenv("ACCESS_TOKEN") sc = Scraper(username=auth_username, access_token=access_token) # check that we have this user, if not, fetch and store try: _ = User.objects.get(login=username.lower()) except User.DoesNotExist: raw_user = sc.api.get_user(username=username.lower()) serialized_user = UserSerializer(data=raw_user) if serialized_user.is_valid(): serialized_user.save() # now fetch repos for the user and store try: response = sc.api.get_repos_of_user( username=username.lower(), per_page=per_page, page=page, type=type, direction=direction, sort=sort, ) except Exception as e: return Response({"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: for repo in response.get("data"): transformed_repo = sc.transform_repo(repo) sc.update_or_create_repo(data=transformed_repo) return Response(response, status=status.HTTP_200_OK)
def update_or_create_user(self, data): pk = data.get("id") try: user = User.objects.get(pk=pk) except User.DoesNotExist: serialized_user = UserSerializer(data=data) if serialized_user.is_valid(): serialized_user.save() return serialized_user.data else: raise CreateDBRecordError({ "entity": User.__name__, "error": serialized_user.errors }) else: serialized_user = UserSerializer(user, data=data) if serialized_user.is_valid(): serialized_user.save() return serialized_user.data else: raise UpdateDBRecordError({ "entity": User.__name__, "error": serialized_user.errors })
def post(request): serializer = JSONWebTokenSerializer(data=request.data) serializer.is_valid(raise_exception=True) serialized_data = serializer.validate(request.data) user_data = UserSerializer( User.objects.get(username=request.data['username'])).data if not user_data['groups']: return Response({'groups': "User not registered with any groups."}, status=status.HTTP_400_BAD_REQUEST) return Response( { 'status': True, 'token': serialized_data['token'], 'user': user_data, }, status=status.HTTP_200_OK)
def get(self, request, format=None): """ List all the users. """ try: users = User.objects.all() user_serializer = UserSerializer(users, many=True) users = user_serializer.data return Response({ 'status': True, 'Response': users }, status=status.HTTP_200_OK) except Exception as e: return Response({ 'status': False, 'message': str(e) }, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): user_serializer = UserSerializer(data=request.data) user_serializer.is_valid(raise_exception=True) user = user_serializer.save() data = generate_jwt_token(user, user_serializer.data) return Response(data, status=status.HTTP_200_OK)
def test_get_all_users(self): response = client.get(reverse("get_post_users")) users = User.objects.all() serialized = UserSerializer(users, many=True) self.assertEqual(response.data, serialized.data) self.assertEqual(response.status_code, status.HTTP_200_OK)