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

ALL Tasks Done #18

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

ALL Tasks Done #18

wants to merge 10 commits into from

Conversation

hex-plex
Copy link
Member

@hex-plex hex-plex commented May 7, 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! @hex-plex 👍
I see that you've made many errors in the assignment. However, it is good that you have tried. Now, see other's submissions and try to complete the assignment. Also, check how you could have done better.

Comment on lines +5 to +20
class User(models.Model):
username = models.CharField(max_length=50)
email = db.Column(db.String(120),index=True,unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me=db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default = datetime.utcnow)
def __repr__(self):
return '<User '+self.username+'>'
def set_password(self,password):
self.password_hash=generate_password_hash(password)
def check_password(self,password):
return check_password_hash(self.password_hash,password)
def avatar(self,size):
digest = md5(self.email.lower().encode("utf-8")).hexdigest()
return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(digest,size)
Copy link
Member

Choose a reason for hiding this comment

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

There isn't any use of creating this User model here. And you aren't required to include those extra field.

Comment on lines +40 to 49
q=Q()
if 'title' in get_data.keys():
if get_data['title']!='':
q |=Q(title__iexact=get_data['title'])
if get_data['author']!='':
q |=Q(author__iexact=get_data['author'])
if get_data['genre']!='':
q |=Q(genre__iexact=get_data['genre'])
context['books']=Book.objects.filter(q)
return render(request, template_name, context=context)
Copy link
Member

Choose a reason for hiding this comment

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

You should display all the books here, and filter them only when you get GET request with the parameters. Also, while filtering, you should have used icontains for a case-insensitive match, instead of an exact match.

return render(request,template_name)
else:
get_data=request.POST
user = authenticate( username=get_data['username'], password=get_data['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).

self.rating= sum([i[1] for i in self.ratings])/len(self.ratings)
self.save()
return 1
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 +12 to +18
ratings = []
class Meta:
ordering = ('title',)
def checkUser(self,user):
ind=0
for i in self.ratings:
if(i[0]==user):
Copy link
Member

Choose a reason for hiding this comment

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

There's an error with your rating. You should have created another model here to store the Book, the User and the rating given by that user. And then to calculate the average rating of a book, you could have filtered that model to get the corresponding book and the user, and then calculated the average rating, and stored it in the rating field of the Book model.

Currently, when multiple users rate a book, the average rating of the book becomes equal to the last user who has rated it.

Copy link
Member Author

Choose a reason for hiding this comment

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

No that is not the case i have used an array for storing them with the username and have checked it also

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.

Yeah, you've used array but the array can't be stored anywhere in the database. The array will not persist when you close the server and then re-start it. So, after you restart the Django server, all the user ratings for a book would be gone, and then when a user will rate the book, it will be considered as if the rating is inserted in an empty array.

An array isn't persistent. There's no proper field like an array to store data in models. For that, we create another Model using some ForeignKey like relation.

As an example, check the admin panel, you won't find the array to be stored anywhere.

Comment on lines +109 to +113
book=BookCopy.objects.get(pk=book_id)
book.status=True
book.borrower=None
book.borrow_date=None
book.save()
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 +130 to +132
ratinginp=int(ratinginp)
ratinginp=max(0,ratinginp)
ratinginp=min(10,ratinginp)
Copy link
Member

Choose a reason for hiding this comment

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

You should not adjust the value of rating to lie between 0 and 10. Rather you should alert the user in case he rates the book beyond the allowed range.

@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