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

All task completed #6

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pylint = "*"

[packages]
certifi = "==2019.3.9"
Expand Down
80 changes: 78 additions & 2 deletions Pipfile.lock

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

1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
24 changes: 24 additions & 0 deletions authentication/templates/authentication/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'store/base.html' %}

{% block content %}
<h2>Log in</h2>

<form method="POST" >
{% csrf_token %}

{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<h6 style="color: red;">{{error}}</h6>
<button type="submit" class="btn btn-primary">Log in</button>
</form>
{% endblock %}
21 changes: 21 additions & 0 deletions authentication/templates/authentication/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'store/base.html' %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
{% endblock %}
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 authentication.views import *

urlpatterns = [
path('login/',loginView, name='loginView'),
path('logout/',logoutView, name='logoutView'),
path('register/',registerView, name='registerView'),
]
32 changes: 27 additions & 5 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
from django.shortcuts import render
from django.shortcuts import render,redirect
from django.contrib.auth import login,logout,authenticate
# Create your views here.
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.contrib.auth.models import User
from django.db import IntegrityError
from library.forms import SignUpForm


def loginView(request):
pass
if request.method == 'GET':
return render(request,'authentication/login.html',{'form':AuthenticationForm()})
else:
user = authenticate(request,username=request.POST['username'],password=request.POST['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).

if user is None:
return render(request,'authentication/login.html',{'form':AuthenticationForm(),'error':'Username and Password did not match.'})
else:
login(request,user)
return redirect('index')

def logoutView(request):
pass
return redirect('index')

def registerView(request):
pass
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('index')
else:
form = SignUpForm()
return render(request, 'authentication/register.html', {'form': form})
15 changes: 15 additions & 0 deletions library/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Required.')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
email = forms.EmailField(max_length=254, help_text='Required. Input a valid email address.')

class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )


3 changes: 2 additions & 1 deletion library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
urlpatterns = [
path('',include('store.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.urls')),
path('authentication/',include('django.contrib.auth.urls')),
path('',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(UserRating)
27 changes: 0 additions & 27 deletions store/migrations/0002_auto_20190607_1302.py

This file was deleted.

38 changes: 38 additions & 0 deletions store/migrations/0002_auto_20200506_0521.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 2.2.1 on 2020-05-06 05:21

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', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='UserRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.FloatField(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)),
],
),
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.DeleteModel(
name='Profile',
),
]
12 changes: 10 additions & 2 deletions store/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
# Create your models here.


class Book(models.Model):
title = models.CharField(max_length=50)
Expand All @@ -17,11 +17,19 @@ def __str__(self):
return f'{self.title} by {self.author}'


class UserRating(models.Model):
user=models.ForeignKey(User, related_name='user', null=True, blank=True, on_delete=models.SET_NULL)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
rating = models.FloatField(default=0)
Comment on lines +20 to +23
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}'

class BookCopy(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
borrow_date = models.DateField(null=True, blank=True)
# True status means that the copy is available for issue, False means unavailable
status = models.BooleanField(default=False)
status = models.BooleanField(default=True)
borrower = models.ForeignKey(User, related_name='borrower', null=True, blank=True, on_delete=models.SET_NULL)

def __str__(self):
Expand Down
25 changes: 18 additions & 7 deletions store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
<title>Library</title>
{% endblock %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<style>
#nav{
border: 2px solid #cccc;
margin:20px;
padding: 20px;
box-shadow:5px 5px #eeee;
}

</style>
{% load static %}
</head>

Expand All @@ -16,7 +25,7 @@
<div class="container-fluid">

<div class="row">
<div class="col-sm-2">
<div class="col-sm-2" id="nav">
{% block sidebar %}
<ul class="sidebar-nav">
<li><a href="{% url 'index' %}">Home</a></li>
Expand All @@ -25,21 +34,23 @@

<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.first_name }}</li>
<li>User: {{ user.first_name}} {{user.last_name}}</li>
<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 'loginView' %}">Login</a></li>
<li><a href="{% url 'registerView' %}">Register</a></li>
{% endif %}
</ul>


{% endblock %}
</div>
<div class="col-sm-10 ">
{% block content %}{% endblock %}
</div>
<div class="col-sm-8" style="margin-top: 20px;">
{% block content %}{% endblock %}

</div>
</div>

</div>
</body>
Expand Down
Loading