Skip to content

my_commit #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions authentication/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "store/base.html" %}

{% block title %}
<title>
Login | Library
</title>
{% endblock %}

{% block content %}
<h2>Log In</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log In</button>
</form>
{% endblock %}
48 changes: 48 additions & 0 deletions authentication/templates/registration/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% extends "store/base.html" %}

{% block title %}
<title>
Sign in | Library
</title>
{% endblock %}

{% block content %}

{% if messages %}
<h5 style="text-align:center;">
{% for message in messages %}
{{ message }}
{% endfor %}
</h5>
<hr style="border-width: 5px">
{% endif %}

<h2>Sign In</h2>
<form action="" method="POST">
{% csrf_token %}
<label for="first_name">First name: </label><br>
<input id="first_name" type="text" name="first_name" required>
<br><br>
<label for="last_name">Last name: </label><br>
<input id="last_name" type="text" name="last_name" required>
<br><br>
<label for="email">Email: </label><br>
<input id="email" type="email" name="email" required>
<br><br>
<label for="username">Username: </label><br>
<input id="username" type="text" name="username" required>
<br><br>
Password must contain at least one number, one uppercase, one lowercase letter, and at least 8 or more characters
<br><br>
<label for="password">Enter password: </label><br>
<input id="password" type="password" name="password"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="Must contain at least one number, one uppercase, one lowercase letter, and at least 8 or more characters"
required>
<br><br>
<label for="passConf">Confirm password: </label><br>
<input id="passConf" type="password" name="passConf">
<br><br><br>
<button type="submit">Sign In</button>
</form>
{% endblock %}
6 changes: 6 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from authentication.views import *

urlpatterns = [
path('register/', registerView, name='register'),
]
26 changes: 25 additions & 1 deletion authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.shortcuts import render
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.models import User
from django.contrib import messages
# Create your views here.


Expand All @@ -10,4 +12,26 @@ def logoutView(request):
pass

def registerView(request):
pass
if(request.method == "POST"):
post_data = request.POST
username = post_data['username']
password = post_data['password']
passConf = post_data['passConf']
email = post_data['email']
first_name = post_data['first_name']
last_name = post_data['last_name']
if(username and password and email and first_name and last_name):
if(User.objects.filter(username=username).exists()):
messages.error(request, 'Username '+ username +' already exists!')
elif(password == passConf):
User.objects.create_user(username=username, password=password, email=email, first_name=first_name, last_name=last_name)
user = authenticate(request, username=username, password=password)
if(user):
login(request, user)
return render(request, 'store/index.html')
else:
messages.error(request,'Password did not match')
else:
messages.error(request,'Please fill all fields correctly')

return render(request, 'registration/register.html')
2 changes: 1 addition & 1 deletion library/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR,'authentication/templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
1 change: 1 addition & 0 deletions library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path('',include('store.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.urls')),
path('authentication/', include('authentication.urls')),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
1 change: 1 addition & 0 deletions store/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

admin.site.register(Book)
admin.site.register(BookCopy)
admin.site.register(BookRating)
38 changes: 38 additions & 0 deletions store/migrations/0003_auto_20210721_2003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 2.2.1 on 2021-07-21 14:33

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('store', '0002_auto_20190607_1302'),
]

operations = [
migrations.AlterField(
model_name='bookcopy',
name='borrow_date',
field=models.DateField(blank=True, null=True),
),
migrations.AlterField(
model_name='bookcopy',
name='borrower',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='borrower', to=settings.AUTH_USER_MODEL),
),
migrations.CreateModel(
name='Rating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.FloatField(default=0.0)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='parentBook', to='store.Book')),
('ratedBy', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('rating',),
},
),
]
31 changes: 31 additions & 0 deletions store/migrations/0004_auto_20210721_2229.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.2.1 on 2021-07-21 16:59

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('store', '0003_auto_20210721_2003'),
]

operations = [
migrations.CreateModel(
name='BookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.FloatField(default=0.0)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.Book')),
('ratedBy', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('rating',),
},
),
migrations.DeleteModel(
name='Rating',
),
]
18 changes: 18 additions & 0 deletions store/migrations/0005_bookrating_desc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.1 on 2021-07-23 07:06

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0004_auto_20210721_2229'),
]

operations = [
migrations.AddField(
model_name='bookrating',
name='desc',
field=models.TextField(null=True),
),
]
11 changes: 11 additions & 0 deletions store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ def __str__(self):
else:
return f'{self.book.title} - Available'

class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
ratedBy = models.ForeignKey(User, on_delete=models.CASCADE)
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.

Rating given can only be integer as per instructions in README.

desc = models.TextField(null=True)

class Meta:
ordering = ('rating',)

def __str__(self):
return f'{self.book} rated {self.rating} by {self.ratedBy}'
3 changes: 2 additions & 1 deletion store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<li><a href="{% url 'view-loaned' %}">My Borrowed</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
{% else %}
<!-- <li><a href="{% url 'login'%}">Login</a></li> -->
<a href="{% url 'login'%}">Login</a></li><br>
<a href="{% url 'register'%}">Register</a></li>
{% endif %}
</ul>

Expand Down
42 changes: 42 additions & 0 deletions store/templates/store/book_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ <h2>Title: {{ book.title }}</h2>
<dd>{{ num_available }}</dd>
</dl>
<button class="btn btn-primary" id="loan-button">Loan {{ book.title }}</button>
{% if user.is_authenticated %}
<br><br><br>
<h5>Rate this book from 0 to 10: </h5> <input type="text" name="rating" id="rating"><br>
<h6>Leave comments below: </h6>
<textarea rows = "5" cols = "60" name = "desc" id= "desc"></textarea><br>
<button class="btn btn-primary" id="rate-button">Rate {{ book.title }}</button>
<br><br><hr style="border-width: 5px">
<h4>See recent customer reviews</h4>
{% for bookRating in book_allRatings %}
<hr>
<h5>{{ bookRating.ratedBy }} : {{ bookRating.rating }}</h5>
{% if bookRating.desc %}
<h6>{{ bookRating.desc }}</h6>
{% endif%}
{% endfor %}
{% endif %}
<script>
$("#loan-button").click(function(){
$.ajax({
Expand All @@ -45,5 +61,31 @@ <h2>Title: {{ book.title }}</h2>

})
})

$("#rate-button").click(function(){
$.ajax({
url: "{% url 'rate-book' %}",
method: "POST",
data: {
bid: {{ book.id }},
rating: $("#rating").val(),
desc: $("#desc").val(),
},
success: function(data, status, xhr){
if(data['message'] == "success"){
alert("Book successfully rated");
window.location.replace("/book/{{ book.id }}/");
}
else{
alert("Please enter valid rating");
}
},
error: function(xhr, status, err){
alert("Some error occured");
}

})
})

</script>
{% endblock %}
21 changes: 20 additions & 1 deletion store/templates/store/loaned_books.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,26 @@ <h3>Loaned Books list</h3>
<script>
// Fill in this function by yourself. It should make a post call to the returnBookView and display an appropriate message
function returnBook(bid){
;
$.ajax({
url: "{% url 'return-book' %}",
method: "POST",
data: {
bid: bid
},
success: function(data, status, xhr){
if(data['message'] == "success"){
alert("Book successfully returned");
window.location.replace("/books/loaned");
}
else{
alert("Unable to return this book");
}
},
error: function(xhr, status, err){
alert("Some error occured");
}

})
}
</script>
{% endblock %}
1 change: 1 addition & 0 deletions store/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
path('books/loaned/', viewLoanedBooks, name="view-loaned"),
path('books/loan/', loanBookView, name="loan-book"),
path('books/return/', returnBookView, name="return-book"),
path('books/rate/', rateBookView, name="rate-book"),
]
Loading