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

Basic Submission for task 2 #10

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

Conversation

Satendra124
Copy link

@Satendra124 Satendra124 commented May 6, 2020

CSoC Task 2 Submission

I have completed the following tasks

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

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

Comment on lines +34 to +52
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'])
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 done this in a simpler way.
Also, you should have used icontains here instead of an exact match during search.

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

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.

Comment on lines +19 to +20
<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>
Copy link
Member

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.

Copy link
Author

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

booktoget.status=False
booktoget.borrow_date=date.today()
booktoget.borrower=request.user
booktoget.save(update_fields=['status','borrow_date','borrower'])
Copy link
Member

Choose a reason for hiding this comment

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

Looks good!

Comment on lines +109 to +115
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()
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'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."

Comment on lines +107 to +108
rating = request.POST.get('rating')
ratingbook=Book.objects.get(id=bid)
Copy link
Member

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.

Comment on lines +93 to +97
bcid = request.POST.get('bcid')
booktoret = BookCopy.objects.get(id=bcid)
booktoret.status=True
booktoret.borrower = None
booktoret.borrow_date=None
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 +39 to +40
if usercheck is not None:
Copy link
Member

@krashish8 krashish8 May 9, 2020

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.

@krashish8
Copy link
Member

Points updated! 🎉

@krashish8 krashish8 added the Judged The Pull Requests which are judged label May 9, 2020
@Satendra124
Copy link
Author

@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 .
Thanks for the review

@krashish8
Copy link
Member

@Satendra124 I appreciate the originality in your work. You can go through the Django docs for more such better methods!

@Satendra124
Copy link
Author

Yeah sure!

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