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

Dev Task Week 4 #1

Open
wants to merge 5 commits 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
97 changes: 97 additions & 0 deletions authentication/templates/authentication/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="login" >

<b><label class="tit">Sign up</label></b>
<hr style="border: groove 1px cyan;">
<form action="register" method="POST"> {% csrf_token %}
<label for="first_name" style="margin-left: 33.6%; color: rgb(59, 17, 17); font-size: xx-large;">First Name :</label>
<input type="text" name="first_name" placeholder="First Name" class="us_inp"><br>
<label for="last_name" style="margin-left: 33.6%; color: rgb(59, 17, 17); font-size: xx-large;">Last Name :</label>
<input type="text" name="last_name" placeholder="Last Name" class="us_inp"><br>
<label for="user_id" style="margin-left: 31%; color: rgb(59, 17, 17); font-size: xx-large;">User ID:</label>
<input type="text" name="username" placeholder="Username"class="us_inp"><br>
<label for="pass" style="margin-left: 28%; color: rgb(59, 17, 17); font-size: xx-large;">Password:</label>
<input type="password" name="password" placeholder="Password" class="us_inp"><br>
<div id="alert"></div>
<input type="submit" id="reg" class="subm" value="Sign Up">

<label> Already have an account? <a href="accounts/login">Log in</a></label>

</form>
</div>

</body>
</html>


<style>

.login{
position: absolute;
display: block;
background-color: floralwhite;
margin-top: 10%;
width: 50%;
height: fit-content;
margin-left: 25%;

border: cyan ridge 10px;
}

.us_inp{
display: inline;
margin-right: auto;
margin-left: auto;
position: relative;
width: 30%;
height: 5%;
border: black ridge 5px ;
background-color: rgba(137, 43, 226, 0.096);
font-size: large;
color: rgb(32, 14, 32);
margin-bottom: 30px;
}

.subm{
display: block;
background-color: rgba(255, 217, 0, 0.705);
margin-right: auto;
margin-left: auto;
font-size: xx-large;
border: rgba(255, 217, 0, 0.397) 3px ridge;
border-radius: 20px;
cursor: pointer;
}

.tit{
display: block;
text-align: center;
color: rgb(0, 0, 0);
font-size: 50px;

}

#alert{
color: red;

}

</style>

<script>
var data = JSON.parse("{{data|escapejs}}");

if(data["cas"] == 1){
document.getElementById("alert").innerHTML = "Enter information in proper format!";}
if(data["cas"] == 2){
document.getElementById("alert").innerHTML = "User id unavilable try for some different id!";}

</script>
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.urls import path
from . import views

app_name = "main"


urlpatterns = [
path("register", views.registerView, name="register")
]
29 changes: 27 additions & 2 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from django.shortcuts import render
import json

from django.shortcuts import render, redirect
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.models import User, auth
# Create your views here.

dataDictionary = {'cas': 0}
dataJSON = json.dumps(dataDictionary)
dataDictionary1 = {'cas': 1}
dataJSON1 = json.dumps(dataDictionary1)
dataDictionary2 = {'cas': 2}
dataJSON2 = json.dumps(dataDictionary2)


def loginView(request):
pass
Expand All @@ -10,4 +20,19 @@ def logoutView(request):
pass

def registerView(request):
pass
template_name = 'authentication/register.html'
if request.method == 'POST':
f_name = request.POST['first_name']
l_name = request.POST['last_name']
username = request.POST['username']
password = request.POST['password']
print(type(f_name), type(l_name), type(username), type(password))
if len(f_name) == 0 or len(l_name) == 0 or len(username) == 0 or len(password) == 0:
return render(request, template_name, {'data': dataJSON1})
if User.objects.filter(username=username):
return render(request, template_name, {'data': dataJSON2})
user = User.objects.create_user(username=username, password=password, first_name=f_name, last_name=l_name)
user.save()
return redirect('accounts/login')
else:
return render(request, template_name, {'data': dataJSON})
5 changes: 3 additions & 2 deletions library/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
from pathlib import Path

import django_heroku
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
Expand Down Expand Up @@ -57,7 +58,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [BASE_DIR / '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 @@ -20,6 +20,7 @@

urlpatterns = [
path('',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)
30 changes: 30 additions & 0 deletions store/migrations/0003_auto_20210714_0609.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.2.1 on 2021-07-14 06:09

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


class Migration(migrations.Migration):

dependencies = [
('store', '0002_auto_20190607_1302'),
]

operations = [
migrations.AddField(
model_name='book',
name='number',
field=models.PositiveIntegerField(default=0),
),
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),
),
]
19 changes: 19 additions & 0 deletions store/migrations/0004_auto_20210715_0628.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.1 on 2021-07-15 06:28

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('store', '0003_auto_20210714_0609'),
]

operations = [
migrations.AlterField(
model_name='bookcopy',
name='borrow_date',
field=models.DateField(blank=True, default=django.utils.timezone.now, null=True),
),
]
23 changes: 23 additions & 0 deletions store/migrations/0005_bookrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.1 on 2021-07-15 06:39

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


class Migration(migrations.Migration):

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

operations = [
migrations.CreateModel(
name='BookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ratedBy', models.CharField(max_length=50)),
('rating', models.SmallIntegerField()),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.Book')),
],
),
]
18 changes: 18 additions & 0 deletions store/migrations/0006_auto_20210715_1250.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.1 on 2021-07-15 12:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0005_bookrating'),
]

operations = [
migrations.AlterField(
model_name='book',
name='rating',
field=models.FloatField(default=-1),
),
]
14 changes: 12 additions & 2 deletions store/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.utils import timezone

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
Expand All @@ -8,7 +10,8 @@ class Book(models.Model):
genre = models.CharField(max_length=50)
description = models.TextField(null=True)
mrp = models.PositiveIntegerField()
rating = models.FloatField(default=0.0)
rating = models.FloatField(default=-1)
number = models.PositiveIntegerField(default=0)

class Meta:
ordering = ('title',)
Expand All @@ -19,7 +22,7 @@ def __str__(self):

class BookCopy(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
borrow_date = models.DateField(null=True, blank=True)
borrow_date = models.DateField(null=True, blank=True, default=timezone.now)
# True status means that the copy is available for issue, False means unavailable
status = models.BooleanField(default=False)
borrower = models.ForeignKey(User, related_name='borrower', null=True, blank=True, on_delete=models.SET_NULL)
Expand All @@ -30,3 +33,10 @@ def __str__(self):
else:
return f'{self.book.title} - Available'


class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
ratedBy = models.CharField(max_length=50)
rating = models.SmallIntegerField()


2 changes: 1 addition & 1 deletion store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<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> -->
<li><a href="{% url 'login'%}">Login</a></li>
{% endif %}
</ul>

Expand Down
49 changes: 48 additions & 1 deletion store/templates/store/book_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ <h2>Title: {{ book.title }}</h2>
<dt>Description:</dt>
<dd>{{ book.description }}</dd>
<dt>Rating:</dt>
<dd>{{ book.rating }}</dd>
{% if book.rating == -1 %}
<dd>Unrated</dd>
{% else %}
<dd>{{ book.rating }}</dd>
{% endif %}
<dt>MRP:</dt>
<dd>Rs. {{ book.mrp }}</dd>
<dt>Available Copies:</dt>
<dd>{{ num_available }}</dd>
</dl>
<button class="btn btn-primary" id="loan-button">Loan {{ book.title }}</button>
<button class="btn btn-primary" id="rate-button">Rate {{ book.title }}</button>
<script>

function isNumeric(str) {
if (typeof str != "string") return false
return !isNaN(str) &&
!isNaN(parseFloat(str))
}


$("#loan-button").click(function(){
$.ajax({
url: "{% url 'loan-book' %}",
Expand All @@ -45,5 +58,39 @@ <h2>Title: {{ book.title }}</h2>

})
})



$("#rate-button").click(function(){
var rate = prompt("Please enter rating from 1 to 10 : ");
var num = parseInt(rate)
if(rate < 0 || rate > 10 || !(isNumeric(rate))){
alert("Enter a valid rating.");}
else{

$.ajax({
url: "{% url 'rate-book' %}",
method: "POST",
data: {
rate: num,
bid: {{ book.id }}
},
success: function(data, status, xhr){
alert("Book successfully rated.");
location.reload();
},
error: function(xhr, status, err){
alert("Some error occured");
}

})


}

})



</script>
{% endblock %}
Loading