Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Task 2 #17

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Task 2 #17

wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented May 7, 2020

CSoC Task 2 Submission

I have completed the following tasks

  • Stage 1
  • Stage 2
  • Stage 3
  • Stage 4

@ghost ghost marked this pull request as draft May 7, 2020 18:26
Copy link
Member

@krashish8 krashish8 left a 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:
Copy link
Member

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.)

Comment on lines +104 to +109
bookCopy = BookCopy.objects.get( id__exact=post_data['bid'], borrower=request.user )
message = 'success'
bookCopy.status = True
bookCopy.borrower = None
bookCopy.save()
except:
Copy link
Member

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.

Comment on lines +121 to +124
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)
Copy link
Member

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 )
Copy link
Member

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).

Comment on lines +32 to +34
title__contains=get_data['title'],
author__contains=get_data['author'],
genre__contains=get_data['genre'],
Copy link
Member

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.

@krashish8
Copy link
Member

Points have been updated! 🎉

@krashish8 krashish8 added the Judged The Pull Requests which are judged label May 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Judged The Pull Requests which are judged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants