Skip to content
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

All tasks done #14

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
64 changes: 64 additions & 0 deletions authentication/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!doctype html>
<html lang="en">
<head>
<title>Login</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

</head>
<body class="text-center">
<div class="container my-4">
<hr>
<div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="panel panel-info" >
<div class="panel-heading">
<div class="panel-title">Sign In</div>

</div>

<div style="padding-top:30px" class="panel-body" >

<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

<form id="loginform" class="form-horizontal" role="form" method="post" action="/">
{% csrf_token %}

<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="login-username" type="text" class="form-control" name="username" value="" placeholder="username">
</div>

<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="login-password" type="password" class="form-control" name="password" placeholder="password">
</div>

<div style="margin-top:10px" class="form-group">
<!-- Button -->

<div class="col-sm-12 controls">
<button id="btn-login" class="btn btn-success">Login</button>
</div>
</div>
{% if messages %}
{% for message in messages %}
{% if message.tags %} <script>alert("{{ message }}")</script> {% endif %}
{% endfor %}
{% endif %}
<div class="form-group">
<div class="col-md-12 control">
<div style="border-top: 1px solid#888; padding-top:15px; font-size:85%" >
Don't have an account!
<a href="/register">
Sign Up Here </a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
84 changes: 84 additions & 0 deletions authentication/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!doctype html>
<html lang="en">
<head>
<title>Login</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

</head>
<body class="text-center">
<div class="container my-4">
<hr>
<div id="signupbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="panel panel-info" >
<div class="panel-heading">
<div class="panel-title">Sign Up</div>

</div>

<div style="padding-top:30px" class="panel-body" >
<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

<form id="signupform" class="form-horizontal" role="form" method="post" action="/register">
{% csrf_token %}

<div id="signupalert" style="display:none" class="alert alert-danger">
<p>Error:</p>
<span></span>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">Username</label>
<div class="col-md-9">
<input type="text" class="form-control" name="regusername" placeholder="Username">
</div>
</div>
<div class="form-group">
<label for="firstname" class="col-md-3 control-label">First Name</label>
<div class="col-md-9">
<input type="text" class="form-control" name="firstname" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label for="firstname" class="col-md-3 control-label">Last Name</label>
<div class="col-md-9">
<input type="text" class="form-control" name="lastname" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-md-3 control-label">Email</label>
<div class="col-md-9">
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-3 control-label">Password</label>
<div class="col-md-9">
<input type="password" class="form-control" name="regpassword" placeholder="Password">
</div>
</div>

<div class="form-group">
<!-- Button -->
<div class="col-md-offset-3 col-md-9">
<button id="btn-signup" class="btn btn-info"><i class="icon-hand-right"></i> Sign Up</button>
</div>
</div>
{% if messages %}
{% for message in messages %}
{% if message.tags %} <script>alert("{{ message }}")</script> {% endif %}
{% endfor %}
{% endif %}

<div style="border-top: 1px solid #999; padding-top:20px" class="form-group" font-size:85%" >
Already have an account?
<a href="/">
Sign In</a>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from django.urls import path, include
from authentication.views import *

urlpatterns = [
path('',loginView, name="login"),
path('logout',logoutView, name="logout"),
path('register',registerView, name="register"),
]
63 changes: 58 additions & 5 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
from django.shortcuts import render
from django.shortcuts import redirect, render
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.models import User
from django.views.decorators.cache import cache_control
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.http import *
# Create your views here.


def loginView(request):
pass
username = password = ''
if request.POST:
username= request.POST.get('username')
password= request.POST.get('password')

if(username=="" or password==""):
messages.info(request, 'Please fill all fields')
return redirect("/")

user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect("/store")

else:
messages.info(request, 'Inavlid Username/Password')
return redirect("/")

return render(request,'login.html')

@cache_control(no_cache=True, must_revalidate=True, no_store=True)
@login_required
def logoutView(request):
pass

logout(request)
return render(request,'login.html')

def registerView(request):
pass
if request.POST:
reg_username= request.POST.get('regusername')
reg_email = request.POST.get('email')
reg_password = request.POST.get('regpassword')
reg_name = request.POST.get('firstname')

if(reg_username=="" or reg_password=="" or reg_email=="" or reg_name==""):
messages.info(request, 'Please fill all fields')
return redirect("/register")

if User.objects.filter(username=reg_username).exists():
messages.info(request, 'Username already exists')
return redirect("/register")

if User.objects.filter(email=reg_email).exists():
messages.info(request, 'Email already exists')
return redirect("/register")

user = User.objects.create_user(reg_username, reg_email, reg_password)
user.first_name = reg_name
user.email = reg_email
user.save()
user = authenticate(username=reg_username, password=reg_password)
if user is not None:
login(request, user)
return redirect("/store")

return render(request,'register.html')
3 changes: 2 additions & 1 deletion library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from django.conf import settings

urlpatterns = [
path('',include('store.urls')),
path('store/',include('store.urls')),
path('',include('authentication.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.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)
35 changes: 35 additions & 0 deletions store/migrations/0003_auto_20210718_1029.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 2.2.1 on 2021-07-18 10:29

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='BookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.IntegerField(default=0)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.Book')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='user', to=settings.AUTH_USER_MODEL)),
],
),
]
12 changes: 12 additions & 0 deletions store/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.contrib.auth.models import User
# Create your models here.

Expand Down Expand Up @@ -30,3 +31,14 @@ def __str__(self):
else:
return f'{self.book.title} - Available'


class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
rating = models.IntegerField(default=0, validators=[
MaxValueValidator(10),
MinValueValidator(0)
])
user = models.ForeignKey(User, related_name='user', null=True, blank=True, on_delete=models.SET_NULL)
Comment on lines +37 to +41
Copy link
Member

Choose a reason for hiding this comment

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

Great use of validators here.


def __str__(self):
return f'{self.book.title}'
4 changes: 2 additions & 2 deletions store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
{% if user.is_authenticated %}
<li>User: {{ user.first_name }}</li>
<li><a href="{% url 'view-loaned' %}">My Borrowed</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
<li><a href="/logout">Logout</a></li>
{% else %}
<!-- <li><a href="{% url 'login'%}">Login</a></li> -->
<!-- <li><a href="{% url 'login'%}">Login</a></li> -->
{% endif %}
</ul>

Expand Down
46 changes: 44 additions & 2 deletions store/templates/store/book_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ <h2>Title: {{ book.title }}</h2>
<dd>Rs. {{ book.mrp }}</dd>
<dt>Available Copies:</dt>
<dd>{{ num_available }}</dd>
{% if user.is_authenticated %}
<dt>Rate {{ book.title }}:</dt>
<input type="number" id="rate" name="rate" min="0" max="10">
<button class="btn btn-primary" id="rate-button" type="submit">Rate</button>
{% comment %} {% if messages %}
{% for message in messages %}
{% if message.tags %} <script>alert("{{ message }}")</script> {% endif %}
{% endfor %}
{% endif %} {% endcomment %}
</dl>
<button class="btn btn-primary" id="loan-button">Loan {{ book.title }}</button>
{% endif %}
<script>

$("#loan-button").click(function(){
$.ajax({
url: "{% url 'loan-book' %}",
Expand All @@ -33,7 +44,7 @@ <h2>Title: {{ book.title }}</h2>
success: function(data, status, xhr){
if(data['message'] == "success"){
alert("Book successfully issued");
window.location.replace("/books/loaned");
window.location.replace("/store/books/loaned");
}
else{
alert("Unable to issue this book");
Expand All @@ -42,8 +53,39 @@ <h2>Title: {{ book.title }}</h2>
error: function(xhr, status, err){
alert("Some error occured");
}

})
})

$("#rate-button").click(function(){

rating = $("#rate").val()
if(rating=='' || rating<0 || rating>10 || rating % 1 != 0)
{
alert('Rate should be an integer within the range of 0-10 only');
return;
}

$.ajax({
url: "{% url 'rate-book' %}",
method: "POST",
data: {
bid: {{ book.id }},
brate: $("#rate").val()
},
success: function(data, status, xhr){
if(data['message'] == "success"){
alert("Book Rated successfully");
window.location.replace("/store/books");
}
else{
alert("Unable to rate this book");
}
},
error: function(xhr, status, err){
alert("Some error occured");
}
})
})

</script>
{% endblock %}
Loading