Example #1
0
  def setUp(self):
    self.author = Author.objects.create(**get_test_author_fields())
    self.other_author = Author.objects.create(**get_test_author_fields())
    
    self.like1 = Like.objects.create(**get_test_like_fields(author=AuthorSerializer(self.author).data))
    self.like2 = Like.objects.create(**get_test_like_fields(author=AuthorSerializer(self.author).data))
    self.like3 = Like.objects.create(**get_test_like_fields(author=AuthorSerializer(self.other_author).data))

    self.likes = [self.like1, self.like2]
Example #2
0
  def setUp(self):
    self.author = Author.objects.create(**get_test_author_fields(i=1))

    self.friends = [
      Author.objects.create(**get_test_author_fields(i=2)), 
      Author.objects.create(**get_test_author_fields(i=3))
    ]

    self.other_follower = Author.objects.create(**get_test_author_fields(i=4))
    
    self.other_followings = Author.objects.create(**get_test_author_fields(i=5))

    self.follows = [
      Follow.objects.create(receiver=self.author, sender=AuthorSerializer(self.friends[0]).data),
      Follow.objects.create(receiver=self.author, sender=AuthorSerializer(self.friends[1]).data),
      Follow.objects.create(receiver=self.author, sender=AuthorSerializer(self.other_follower).data),
    ]

    self.followings = [
      Following.objects.create(receiver=AuthorSerializer(self.friends[0]).data, sender=self.author),
      Following.objects.create(receiver=AuthorSerializer(self.friends[1]).data, sender=self.author),
      Following.objects.create(receiver=AuthorSerializer(self.other_followings).data, sender=self.author),
    ]

    self.friends_obj = [
      AuthorSerializer(self.friends[0]).data,
      AuthorSerializer(self.friends[1]).data
    ] 
Example #3
0
    def setUp(self):
        self.senders = [
            Author.objects.create(**get_test_author_fields(i=1)),
            Author.objects.create(**get_test_author_fields(i=2))
        ]

        self.receiver = Author.objects.create(**get_test_author_fields(i=3))

        self.follows = [
            Follow.objects.create(receiver=self.receiver,
                                  sender=AuthorSerializer(
                                      self.senders[0]).data),
            Follow.objects.create(receiver=self.receiver,
                                  sender=AuthorSerializer(
                                      self.senders[1]).data)
        ]
Example #4
0
 def setUp(self):
     client.force_authenticate(
         User.objects.create(username='******', password='******'))
     self.receiver = Author.objects.create(**get_test_author_fields())
     self.sender = Author.objects.create(**get_test_author_fields())
     self.follow = Following.objects.create(receiver=AuthorSerializer(
         self.receiver).data,
                                            sender=self.sender)
Example #5
0
    def retrieve(self, request, username):
        try:
            author = Author.objects.get(user__username=username)
            serializer = AuthorSerializer(author)
        except Author.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        return Response(serializer.data)
Example #6
0
    def test_get_authors(self):
        for i in range(3):
            Author.objects.create(**get_test_author_fields())

        response = client.get(f'/api/authors/')
        authors = Author.objects.all()
        serializer = AuthorSerializer(authors, many=True)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)
Example #7
0
 def setUp(self):
     client.force_authenticate(
         User.objects.create(username='******', password='******'))
     self.receiver = Author.objects.create(**get_test_author_fields(i=1))
     self.inbox = Inbox.objects.create(author=self.receiver)
     self.sender = get_follow_author_fields()
     self.sender_id = self.sender["id"]
     self.object = {
         "type": "follow",
         "actor": self.sender,
         "object": AuthorSerializer(self.receiver).data
     }
Example #8
0
    def test_update_author(self):
        response = client.post(f'/api/author/{self.john.id}/',
                               data=json.dumps(self.payload),
                               content_type='application/json')

        # Compare each payload field with the updated Author object.
        serializer = AuthorSerializer(Author.objects.get(id=self.john.id))
        for k in self.payload:
            self.assertEqual(serializer.data[k], self.payload[k])

        # Ensure other fields are unchanged
        self.assertEqual(serializer.data['id'], str(self.john.id))
        self.assertEqual(serializer.data['type'], self.john.type)

        # Check the response was alright
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)
Example #9
0
 def test_get_valid_author(self):
     response = client.get(f'/api/author/{self.john.id}/')
     author = Author.objects.get(id=self.john.id)
     serializer = AuthorSerializer(author)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Example #10
0
 def test_get_valid_username(self):
     response = client.get(f'/api/auth-user/{self.john.user.username}/')
     serializer = AuthorSerializer(self.john)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Example #11
0
    def list(self, request):
        authors = Author.objects.all()
        serializer = AuthorSerializer(authors, many=True)

        return Response(serializer.data)
Example #12
0
def book_mgmt(request: Request):
    try:
        if request.method == 'POST' or request.method == 'PUT':
            authors = list(
                Author.objects.filter(name__in=request.data.get('authors')))
            authorIds = []
            if len(authors) > 0:
                for author in authors:
                    authorIds.append(author.id)
            else:
                for name in request.data.get('authors'):
                    author = {"name": name, "books": []}
                    authors.append(author)
                authorSerializer = AuthorSerializer(data=authors, many=True)
                if authorSerializer.is_valid():
                    authorSerializer.save()

                    for author in authorSerializer.data:
                        authorIds.append(author.get('id'))
                else:
                    print('ERROR: book_service',
                          authorSerializer.error_messages)
                    print(authorSerializer.data)
                    return Response(status=status.HTTP_400_BAD_REQUEST)

            bookData: dict = request.data
            bookData['authors'] = authorIds
            bookData['published'] = bookData['published'].split('T')[0]
            bookData['library'] = bookData['library']['placeId']
            if request.method == 'POST':

                serializer = BookWriteSerializer(data=bookData)

                if serializer.is_valid():
                    serializer.save()
                    return Response(status=status.HTTP_201_CREATED)

                print('ERROR: book_service', serializer.error_messages)
                print(serializer.data)
                return Response(status=status.HTTP_400_BAD_REQUEST)
            else:
                book = Book.objects.get(id=bookData['id'])
                bookData.pop('id')
                oldAuthors: QuerySet = book.authors.exclude(id__in=authorIds)

                serializer = BookWriteSerializer(instance=book, data=bookData)
                if serializer.is_valid():
                    serializer.save()
                    delete_authors(oldAuthors)
                    return Response(status=status.HTTP_200_OK)
                print('ERROR: book_service', serializer.error_messages)
                print(serializer.data)
                return Response(status=status.HTTP_400_BAD_REQUEST)

        elif request.method == 'DELETE':
            bookId = request.query_params.get('id')
            try:
                bookData: Book = Book.objects.get(id=bookId)
                authors = bookData.authors.all()

                delete_authors(authors)
                bookData.delete()
            except Book.DoesNotExist:
                return Response(status=status.HTTP_400_BAD_REQUEST)

            return Response(status=status.HTTP_200_OK)
    except KeyError:
        return Response('Incomplete input data',
                        status=status.HTTP_400_BAD_REQUEST)