-
Notifications
You must be signed in to change notification settings - Fork 32
Task 2 #17
base: master
Are you sure you want to change the base?
Task 2 #17
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on the assignment! @yashking007
Maybe you didn't get enough time to do this assignment, or maybe you have doubts. It's good that you've at least tried. Now, try to finish this assignment, and look at the other submissions to understand how you could have completed the rest of the parts of the assignment.
try : | ||
new_rating = BookRating.objects.get(user=request.user, book=book) | ||
new_rating.rating = post_data['rating'] | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a good coding practice, whenever you use try-except block, capture only the exceptions which you want to catch (IndexError, IntegrityError, etc.)
bookCopy = BookCopy.objects.get( id__exact=post_data['bid'], borrower=request.user ) | ||
message = 'success' | ||
bookCopy.status = True | ||
bookCopy.borrower = None | ||
bookCopy.save() | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There must be a validation in the backend when a user is returning the book, to make sure that he has only borrowed the book. Otherwise, a simple POST request will make the BookCopy to be returned, and would set its status as True.
book = get_object_or_404(Book, pk=post_data['bid']) | ||
response = {'message':'failure'} | ||
if post_data['rating']>10 or post_data['rating']<0: | ||
return JsonResponse(response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good! Perfect use of get_object_or_404
and validation of rating.
pass | ||
post_data = request.POST | ||
try: | ||
bookCopy = BookCopy.objects.get( id__exact=post_data['bid'], borrower=request.user ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are directly accessing POST data without checking if it even exists. This may lead to server crash if a user access this endpoint with invalid request data. The good behavior would have been to throw a client error (400), rather than server error (500).
title__contains=get_data['title'], | ||
author__contains=get_data['author'], | ||
genre__contains=get_data['genre'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could have used icontains
here for a case-insensitive match. However, this is fine for this assignment.
Points have been updated! 🎉 |
CSoC Task 2 Submission
I have completed the following tasks