-
Notifications
You must be signed in to change notification settings - Fork 32
Basic Submission for task 2 #10
base: master
Are you sure you want to change the base?
Conversation
Costomlogin
Switched debug off
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! @Satendra124
bookdata = Book.objects.all() | ||
else: | ||
bookdata = Book.objects.filter(genre=get_data['genre']) | ||
else: | ||
if get_data['genre']=='' : | ||
bookdata = Book.objects.filter(author=get_data['author']) | ||
else: | ||
bookdata = Book.objects.filter(genre=get_data['genre'],author=get_data['author']) | ||
else: | ||
if get_data['author']=='' : | ||
if get_data['genre']=='' : | ||
bookdata = Book.objects.filter(title=get_data['title']) | ||
else: | ||
bookdata = Book.objects.filter(genre=get_data['genre'],title=get_data['title']) | ||
else: | ||
if get_data['genre']=='' : | ||
bookdata = Book.objects.filter(author=get_data['author'],title=get_data['title']) | ||
else: | ||
bookdata = Book.objects.filter(genre=get_data['genre'],author=get_data['author'],title=get_data['title']) |
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 done this in a simpler way.
Also, you should have used icontains
here instead of an exact match during search.
book = models.ForeignKey(Book, on_delete=models.CASCADE) | ||
rater = models.ForeignKey(User, related_name='rater',null=True,on_delete=models.SET_NULL) | ||
rating = models.FloatField(validators=[MinValueValidator(1), MaxValueValidator(10)],default=10.0) |
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 use of validators here.
However, the rater can't be null and the rating shall be an integer - please read proper instructions.
Also, you could have also used unique_together
META option here.
<input type="text" class="form-control p-2" name="Username" placeholder="Username" id="Username" required><br> | ||
<input type="password" class="form-control p-2" name="password" placeholder="Password" id="password" required><br> |
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.
Better keep the name and id field to be consistent everywhere (all lowercase would be fine). However, this is fine for this assignment.
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.
Yes i messed it up in one place then dodnt changed it and realised that the id was comming alot of places .
def index(request): | ||
return render(request, 'store/index.html') | ||
|
||
def bookDetailView(request, bid): | ||
template_name = 'store/book_detail.html' | ||
book = Book.objects.get(id=bid) |
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.
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.
booktoget.status=False | ||
booktoget.borrow_date=date.today() | ||
booktoget.borrower=request.user | ||
booktoget.save(update_fields=['status','borrow_date','borrower']) |
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.
Looks good!
try: | ||
rate=Rate.objects.get(rater=request.user) | ||
rate.rating=rating | ||
rate.save(update_fields=['rating']) | ||
except Rate.DoesNotExist: | ||
ratingobj = Rate(book=ratingbook,rater=request.user,rating=rating) | ||
ratingobj.save() |
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've used get_or_create()
instead of try-except.
Read more here. "This is meant to prevent duplicate objects from being created when requests are made in parallel, and as a shortcut to boilerplatish code."
rating = request.POST.get('rating') | ||
ratingbook=Book.objects.get(id=bid) |
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.
Though the validation of rating is present in the models, it will raise an error during save as you haven't added any validation here.
Also, just after rating the book, the value isn't updated in the DOM. You could've atleast redirected to some other page, or have reloaded the page.
bcid = request.POST.get('bcid') | ||
booktoret = BookCopy.objects.get(id=bcid) | ||
booktoret.status=True | ||
booktoret.borrower = None | ||
booktoret.borrow_date=None |
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.
if usercheck is not None: |
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.
This isn't a correct way to check whether a username exist. You could've used filter instead.
It raises an error "Username exists" even though the username does not exist in the database.
Points updated! 🎉 |
@krashish8 i see that there we many alternative and better method which you mentioned but since i learned it quite fast so i had only the basics and my logic . |
@Satendra124 I appreciate the originality in your work. You can go through the Django docs for more such better methods! |
Yeah sure! |
CSoC Task 2 Submission
I have completed the following tasks