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

All stages done - CSOC-task-2 #8

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
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
88 changes: 86 additions & 2 deletions Pipfile.lock

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

22 changes: 22 additions & 0 deletions authentication/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.forms import ModelForm

from django.contrib.auth.models import User


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

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

class LoginForm(forms.Form):
username = forms.CharField(max_length=254)
password = forms.CharField(label=("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 must add widget=forms.PasswordInput, so that the input would be a password, not plain text.

Who likes to show his password on the screen to the fellow users? 😅

class Meta:
model = User
fields = ('username','password',)
47 changes: 47 additions & 0 deletions authentication/templates/registration/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<html>
<head>

{% block title %}
<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">
<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>
{% load static %}
</head>

<body>

<div class="container-fluid">

<div class="row">
<div class="col-sm-2">
{% block sidebar %}
<ul class="sidebar-nav">
<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="{% url 'book-list' %}">All books</a></li>
</ul>

<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.username }}</li>
<li><a href="{% url 'view-loaned' %}">My Borrowed</a></li>
<li><a href="{% url 'user-logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'user-login'%}">Login</a></li>
<li><a href="{% url 'user-signup'%}">Sign Up</a></li>
{% endif %}
</ul>


{% endblock %}
</div>
<div class="col-sm-10 ">
{% block content %}{% endblock %}
</div>
</div>

</div>
</body>
</html>
18 changes: 18 additions & 0 deletions authentication/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'registration/base.html' %}

{% block content %}
<h2>Log In</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p}}
<button type="submit">Log In</button>

</form>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
10 changes: 10 additions & 0 deletions authentication/templates/registration/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'registration/base.html' %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p}}
<button type="submit">Sign up</button>
</form>
{% endblock %}
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 authentication.views import *

urlpatterns = [

path('signup/',registerView, name='user-signup'),
path('login/',loginView, name='user-login'),
path('logout/',logoutView, name='user-logout'),
]
54 changes: 50 additions & 4 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
from django.shortcuts import render
from django.shortcuts import render,redirect
from django.contrib.auth import login,logout,authenticate
from authentication.forms import *
from django.contrib import messages
# Create your views here.


def loginView(request):
pass
if request.method == 'POST':
print(request.POST)
form = LoginForm(request.POST)
if form.is_valid():
print("helloworld")

username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user:
login(request, user)
return redirect('index')

else:
messages.info(request, 'Invalid Username or Password')
form = LoginForm()

else:
form = LoginForm()
context = {
'form': form

}
return render(request, 'registration/login.html', context)

def logoutView(request):
pass
logout(request)
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()

context = {
'form': form
}
return render(request, 'registration/signup.html', context)


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)
6 changes: 6 additions & 0 deletions store/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django import forms

class RatingForm(forms.Form):
rating = forms.FloatField(max_value=10, min_value=0,)
class Meta:
fields=('rating')
25 changes: 25 additions & 0 deletions store/migrations/0003_auto_20200503_1735.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2.1 on 2020-05-03 12:05

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.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),
),
]
25 changes: 25 additions & 0 deletions store/migrations/0004_bookrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2.1 on 2020-05-05 22:27

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

operations = [
migrations.CreateModel(
name='BookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.FloatField()),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.Book')),
('rating_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rating_by', to=settings.AUTH_USER_MODEL)),
],
),
]
5 changes: 5 additions & 0 deletions store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ def __str__(self):
else:
return f'{self.book.title} - Available'

class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
rating_by = models.ForeignKey(User, related_name='rating_by', null=False, blank=False, on_delete=models.CASCADE)
rating = models.FloatField()
Comment on lines +34 to +36
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.

Also, you could have also used unique_together META option here.


7 changes: 4 additions & 3 deletions store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.first_name }}</li>
<li>User: {{ user.username }}</li>
<li><a href="{% url 'view-loaned' %}">My Borrowed</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
<li><a href="{% url 'user-logout' %}">Logout</a></li>
{% else %}
<!-- <li><a href="{% url 'login'%}">Login</a></li> -->
<li><a href="{% url 'user-login'%}">Login</a></li>
<li><a href="{% url 'user-signup'%}">Sign Up</a></li>
{% endif %}
</ul>

Expand Down
Loading