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

initial commit . All Tasks Done #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dasari810
Copy link

@dasari810 dasari810 commented May 7, 2020

CSoC Task 2 Submission

I have completed the following tasks

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

@krashish8
Copy link
Member

krashish8 commented May 9, 2020

Hi. Your submission (store/views.py) resembles a lot with PR #3 and PR #9 and the userBookRating() view resembles with PR #15.

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! @MadhavaDasari

Comment on lines +17 to +18
username=request.POST['username']
password=request.POST['password']
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).

password=password1,
)
user.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.

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 +138 to +141
l= BookRating.objects.filter(book_id=bookid).count()
for i in range(l):
rate=BookRating.objects.filter(book_id=bookid)[i]
sum = sum + int(rate.rating)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good way to call ORM queries. This will run two queries on the database.
You could have done it this way:

for rate in Rating.objects.filter(book_id=bookid):
    sum = sum + int(rate.rating)

@@ -12,58 +15,71 @@ def index(request):

def bookDetailView(request, bid):
template_name = 'store/book_detail.html'
book1=Book.objects.get(pk=bid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may fail with invalid book ID given in POST request, and would lead to server error. Expected behavior is to inform user with Not found (404) error. You have imported get_object_or_404 but haven't used that.

Comment on lines +96 to +105
book_id = request.POST.get("bid")
book = BookCopy.objects.get(pk=book_id)
if(book):
message ="success"
book.status = True
book.borrower = None
book.borrow_date = None
book.save()
else :
message = "failure"
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 +114 to +127
def userBookRating(request):
response_data = {
'message': "failure",
'rating' : 0
}
prating = '0'
userid=request.user.id
data = request.POST
rating =data.get('rating')
bookid = data.get('bid')
print(rating)
if BookRating.objects.filter(user_id=userid).filter(book_id=bookid).count() == 1:
prating = BookRating.objects.filter(user_id=userid).filter(book_id=bookid).first().rating
print('rating')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've not put a backend validation on the rating, so the user can simply edit the JS code you've written in the template and easily put invalid values of rating.

Comment on lines +22 to +25
value= BookRating.objects.filter(book__exact=book1,user__exact=request.user)
user_rating=0
if(value.count()>0):
user_rating=BookRating.objects.filter(book__exact=book1,user__exact=request.user).get().rating
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good way to call ORM queries. This will run two queries on database. You could have directly done the following:

user_rating = value.get().rating

Comment on lines +34 to +37
class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='user',null=True,blank=True,on_delete=models.SET_NULL)
rating = models.FloatField(default=0.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rating shall be given as an integer - please read proper instructions.
The user should not be null here, and a better option would be to use on_delete=models.CASCADE

You could have also used unique_together META option here.

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

None yet

2 participants