All tasks Done csoc-2020-task-2#4
Conversation
krashish8
left a comment
There was a problem hiding this comment.
Great work on the assignment @yashjain715-creater ! Will update the points later!
| class rating(models.Model): | ||
| book = models.ForeignKey(Book, related_name='book', on_delete=models.CASCADE) | ||
| rate = models.FloatField(default=0.0) | ||
| user = models.ForeignKey(User, related_name='rate', null=True, blank=True, on_delete=models.SET_NULL) |
There was a problem hiding this comment.
The class name shall start with a capital letter.
The rating shall be 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.
| path('login/',loginView,name="loginView"), | ||
| path('logout/',loginView,name="logoutView"), |
There was a problem hiding this comment.
You have created login/ and logout/ urls both here and in authentication.
| <ul class="sidebar-nav"> | ||
| {% if user.is_authenticated %} | ||
| <li>User: {{ user.username }}</li> | ||
| <li><a href="{% url 'view-loaned' %}">My Borrowed</a></li> | ||
| <li><a href="{% url 'logoutView' %}">Logout</a></li> | ||
| {% else %} | ||
| <li><a href="{% url 'loginView' %}">Login</a></li> | ||
| {% endif %} | ||
| </ul> |
There was a problem hiding this comment.
Always prefer to use base.html to make the code DRY.
There was a problem hiding this comment.
I always use base.html for here for only two HTML files I add upper base.html
| if(title and author and genre): | ||
| books = Book.objects.filter(title=title,author=author,genre=genre) | ||
| elif(title and author): | ||
| books = Book.objects.filter(title=title,author=author) | ||
| elif(title and genre): | ||
| books = Book.objects.filter(title=title,genre=genre) | ||
| elif(author and genre): | ||
| books = Book.objects.filter(author=author,genre=genre) | ||
| elif(title): | ||
| books = Book.objects.filter(title=title) | ||
| elif(author): | ||
| books = Book.objects.filter(author=author) | ||
| elif(genre): | ||
| books = Book.objects.filter(genre=genre) | ||
| else: | ||
| books = Book.objects.all() |
There was a problem hiding this comment.
You could have done this in a simpler way.
Also, you should have used icontains here instead of an exact match during search.
| book_id = None # get the book id from post data | ||
|
|
||
| book_id = request.POST.get("bid") # get the book id from post data | ||
| book = Book.objects.get(id=book_id) |
There was a problem hiding this comment.
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.
| def returnBookView(request): | ||
| pass | ||
| book_id = request.POST.get('bid') | ||
| print(book_id) | ||
| book = BookCopy.objects.get(id=book_id) | ||
| user = User.objects.get(username = request.user.username) | ||
| if(book!=0): | ||
| book.borrower = None | ||
| book.status = True | ||
| book.borrow_date = None | ||
| book.save() | ||
| response_data = { | ||
| 'message': 'success', | ||
| } |
There was a problem hiding this comment.
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.
| rate = request.POST.get('rate') | ||
| bid = request.POST.get('bid') | ||
| book = Book.objects.get(id = bid) | ||
| user = User.objects.get(username = request.user.username) | ||
| rateuser = rating() | ||
| rateuser.user = user | ||
| rateuser.rate = rate | ||
| rateuser.book = book |
There was a problem hiding this comment.
You've not put a backend validation on the rating, so the user can easily put invalid values of rating.
| availableRateUser.delete() | ||
| rateuser.save() |
There was a problem hiding this comment.
You could have updated the rating rather than deleting and then saving it.
|
Points updated! 🎉 |
CSoC Task 2 Submission
I have completed the following tasks