Example #1
0
 def get(self, request, *args, **kwargs):
     try:
         a_song = self.queryset.get(pk=kwargs["pk"])
         return Response(SongsSerializer(a_song).data)
     except Songs.DoesNotExist:
         return Response(data={
             "message":
             "Song with id: {} does not exist".format(kwargs["pk"])
         },
                         status=status.HTTP_404_NOT_FOUND)
Example #2
0
 def delete(self, request, *args, **kwargs):
     try:
         a_song = self.queryset.get(pk=kwargs["pk"])
         a_song.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
     except Songs.DoesNotExist:
         return Response(data={
             "message":
             "Song with id: {} does not exist".format(kwargs["pk"])
         },
                         status=status.HTTP_404_NOT_FOUND)
Example #3
0
 def post(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     email = request.data.get("email", "")
     if not username and not password and not email:
         return Response(data={
             "message":
             "username, password and email is required to register a user"
         },
                         status=status.HTTP_400_BAD_REQUEST)
     new_user = User.objects.create_user(username=username,
                                         password=password,
                                         email=email)
     return Response(data=UserSerializer(new_user).data,
                     status=status.HTTP_201_CREATED)
Example #4
0
 def post(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     user = authenticate(request, username=username, password=password)
     if user is not None:
         # login saves the user’s ID in the session,
         # using Django’s session framework.
         login(request, user)
         serializer = TokenSerializer(
             data={
                 # using boxme-jwt utility functions to generate a token
                 "token": jwt_encode_handler(jwt_payload_handler(user))
             })
         serializer.is_valid()
         return Response(serializer.data)
     return Response(status=status.HTTP_401_UNAUTHORIZED)
Example #5
0
 def decorated(*args, **kwargs):
     # args[0] == GenericView Object
     title = args[0].request.data.get("title", "")
     artist = args[0].request.data.get("artist", "")
     if not title and not artist:
         return Response(data={
             "message":
             "Both title and artist are required to add a song"
         },
                         status=status.HTTP_400_BAD_REQUEST)
     return fn(*args, **kwargs)
Example #6
0
 def post(self, request, *args, **kwargs):
     a_song = Songs.objects.create(title=request.data["title"],
                                   artist=request.data["artist"])
     return Response(data=SongsSerializer(a_song).data,
                     status=status.HTTP_201_CREATED)