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

initial commit . All Tasks Done #16

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ gunicorn = "*"
django-heroku = "*"

[requires]
python_version = "3.8"
python_version = "3.7.4"
4 changes: 2 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions authentication/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/css/iziToast.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"
type="text/css" rel="stylesheet">
</head>
<body>
<form action="/login/" method="post" >
{% csrf_token %}
<div style="padding-left:6%; max-width:70%; padding-top:6%;">
<div style="padding-bottom:10px">
<span style="color:white;font-size:40px;">
Login
</span>
</div>
{% if message %}

<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" value="{{arr.u}}"id="inputUsername" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" value="{{arr.p}}" id="inputPassword" required>
</div>

{% else %}

<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" id="inputUsername" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" id="inputPassword" required>
</div>

{% endif %}

<input type="submit" class="btn btn-outline-success my-2 my-sm-0" name="submit">
</div>
</form>
<div style="position: center;margin-left: 300px">
<b>Don't have an Account ? Please <a href="/register">Register</a></b>
</div>
<br>
<br>
<div class="form-group" style="position: center;margin-left: 300px; margin-bottom:100px;font-size:20px;">
{% if message %}
<h3>{{ message }}</h3>
{% endif %}

</div>
</body>
</html>
109 changes: 109 additions & 0 deletions authentication/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html>
<head>
<title> Registration </title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/css/iziToast.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"
type="text/css" rel="stylesheet">
</head>
<body>

{% if message %}

<form action="/register/" method="post">
{% csrf_token %}
<div style="padding-left:6%; max-width:70%; padding-top:6%;">
<div style="padding-bottom:10px">
<span style="color:white;font-size:40px;">
Register
</span>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>First Name</label>
<input type="text" class="form-control" name="first_name" value="{{arr.f}}" id="inputFirstName" required>
</div>
<div class="form-group col-md-6">
<label>Last Name</label>
<input type="text" class="form-control" name="last_name" value="{{arr.l}}" id="inputLastName" required>
</div>
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" class="form-control" name="email" value="{{arr.e}}" id="inputEmail" required>
</div>
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name= "username" value="{{arr.u}}" id="inputUsername" required>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>Password :</label>
<input type="Password" class="form-control" name="password1" value="{{arr.p1}}" id="Password" required>
</div>
<div class="form-group col-md-6">
<label>Confrim Your Password : </label>
<input type="Password" class="form-control" name="password2" value="{{arr.p2}}" id="Password" required>
</div>
</div>
<input type="submit" class="btn btn-outline-success my-2 my-sm-0" name="submit">
</div>
</form>

{% else %}

<form action="/register/" method="post">
{% csrf_token %}
<div style="padding-left:6%; max-width:70%; padding-top:6%;">
<div style="padding-bottom:10px">
<span style="color:white;font-size:20px;">
Register
</span>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>First Name</label>
<input type="text" class="form-control" name="first_name" id="inputFirstName" required>
</div>
<div class="form-group col-md-6">
<label>Last Name</label>
<input type="text" class="form-control" name="last_name" id="inputLastName" required>
</div>
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" class="form-control" name="email" id="inputEmail" required>
</div>
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name= "username" id="inputUsername" required>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label>Password :</label>
<input type="Password" class="form-control" name="password1" id="Password" required>
</div>
<div class="form-group col-md-6">
<label>Confrim Your Password : </label>
<input type="Password" class="form-control" name="password2" id="Password" required>
</div>
</div>
<input type="submit" class="btn btn-outline-success my-2 my-sm-0" name="submit">
</div>
</form>

{% endif %}

<div style="position: center;margin-left: 300px;margin-top:-20px">
<b>Already have an Account ? Please <a href="/login">Login</a></b>
</div>
<div class="form-group" style="position: center;margin-left: 300px;margin-top:10px;font-size:20;">

{% if message %}
<h3> {{message}}</h3>
{% endif %}

</div>
</body>
</html>
8 changes: 8 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views

urlpatterns = [
path('login/',views.loginUser,name="login"),
path('register/',views.register,name="register"),
path('logout/',views.logoutUser,name="logout")
]
82 changes: 75 additions & 7 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,81 @@
from django.shortcuts import render
from django.contrib.auth import login,logout,authenticate
from django.shortcuts import render,redirect
from django.contrib import messages
from django.contrib.auth.models import User,auth
# Create your views here.


def loginView(request):
pass
def loginUser(request):

class given :

def __init__(self,u,p):
self.u = u
self.p = p

def logoutView(request):
pass
if(request.method == 'POST'):
username=request.POST['username']
password=request.POST['password']
Comment on lines +17 to +18
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).

user = auth.authenticate(username=username,password=password)

givene = given(username,password)

if user is not None :
auth.login(request,user)
return redirect('index')
else:
return render(request,'login.html',{'message':'Invalid Credentials !','arr':givene})
else:
return render(request,'login.html')

def register(request):

class given :

def __init__(self,f,l,e,u,p1,p2):
self.f = f
self.l = l
self.e = e
self.u = u
self.p1 = p1
self.p2 = p2

if (request.method == 'POST') :
first_name=request.POST['first_name']
last_name=request.POST['last_name']
username=request.POST['username']
email=request.POST['email']
password1=request.POST['password1']
password2=request.POST['password2']

givene = given(first_name,last_name,email,username,password1,password2)

if(password1!=password2):
givene.p1=''
givene.p2=''
return render(request,'register.html',{'message':'Passwords Not Matching !','arr': givene })

try :
user = User.objects.create_user(
username=username,
first_name=first_name,
last_name=last_name,
email=email,
password=password1,
)
user.save()
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.)

givene.u = ''
return render(request,'register.html',{'message':"Username Already Taken ! Please Try with Other Username",'arr': givene})
user = authenticate(request,username=username,password=password1)
if user is not None :
login(request,user)
return redirect('index')
else :
return render(request,'register.html')


def logoutUser(request):
logout(request)
return redirect('index')

def registerView(request):
pass
4 changes: 2 additions & 2 deletions library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

urlpatterns = [
path('',include('store.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.urls')),
path('',include('authentication.urls')),
path('admin/', admin.site.urls)
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
2 changes: 2 additions & 0 deletions store/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from store.models import *

# Register your models here.

admin.site.register(Book)
admin.site.register(BookCopy)
admin.site.register(BookRating)
35 changes: 35 additions & 0 deletions store/migrations/0003_auto_20200506_1926.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 2.2.1 on 2020-05-06 19:26

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.FloatField(default=0.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)),
],
),
]
7 changes: 7 additions & 0 deletions store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ def __str__(self):
else:
return f'{self.book.title} - Available'


class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='user',null=True,blank=True,on_delete=models.SET_NULL)
rating = models.FloatField(default=0.0)
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

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

The rating shall be given as an integer - please read proper instructions.
The user should not be null here, and a better option would be to use on_delete=models.CASCADE

You could have also used unique_together META option here.

def __str__(self):
return f'{self.book.title}'
Loading