Skip to content
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ dmypy.json
.pyre/

# End of https://www.gitignore.io/api/django

venv
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.7.10
3.7.9
14 changes: 14 additions & 0 deletions authentication/templates/authentication/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
14 changes: 14 additions & 0 deletions authentication/templates/authentication/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
</body>
</html>
11 changes: 11 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from . import views

app_name = 'authentication'

urlpatterns = [
# Other URL patterns in your project
path('login/', views.loginView, name='login'),
path('logout/', views.logoutView, name='logout'),
path('register/', views.registerView, name='register'),
]
45 changes: 40 additions & 5 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
from django.shortcuts import render
from django.contrib.auth import login,logout,authenticate
from django.shortcuts import render, redirect, reverse
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib import messages
# Create your views here.


def loginView(request):
pass
if request.method == 'POST':
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect(reverse("index"))
else:
messages.error(request, "Invalid username or password.")
else:
form = AuthenticationForm()

return render(request, 'authentication/login.html', {'form': form})

def logoutView(request):
pass
logout(request)
return redirect('login')

def registerView(request):
pass
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
# Optionally, automatically log in the newly registered user.
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect(reverse("index"))
else:
for field, errors in form.errors.items():
for error in errors:
messages.error(request, f"{field}: {error}")
else:
form = UserCreationForm()

return render(request, 'authentication/register.html', {'form': form})
1 change: 1 addition & 0 deletions library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path('',include('store.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.urls')),
path('auth/',include('authentication.urls')),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ platformdirs==3.8.0
psycopg2-binary==2.9.6
pytz==2023.3
sqlparse==0.4.4
typing_extensions==4.7.0
typing-extensions==4.7.0
whitenoise==6.5.0
zipp==3.15.0
26 changes: 26 additions & 0 deletions store/migrations/0003_auto_20230730_1612.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.19 on 2023-07-30 10:42

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),
),
]
40 changes: 40 additions & 0 deletions store/migrations/0004_auto_20230731_1343.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 3.2.19 on 2023-07-31 08:13

from django.conf import settings
import django.core.validators
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_20230730_1612'),
]

operations = [
migrations.AddField(
model_name='book',
name='rating_count',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='book',
name='rating_total',
field=models.PositiveIntegerField(default=0),
),
migrations.CreateModel(
name='BookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.PositiveIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
('rated_on', models.DateTimeField(auto_now_add=True)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.book')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'unique_together': {('book', 'user')},
},
),
]
18 changes: 18 additions & 0 deletions store/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.

class Book(models.Model):
Expand All @@ -10,6 +11,13 @@ class Book(models.Model):
mrp = models.PositiveIntegerField()
rating = models.FloatField(default=0.0)

# New fields for rating
rating_count = models.PositiveIntegerField(default=0)
rating_total = models.PositiveIntegerField(default=0)

def average_rating(self):
return self.rating_total / self.rating_count if self.rating_count > 0 else 0

class Meta:
ordering = ('title',)

Expand All @@ -30,3 +38,13 @@ 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, on_delete=models.CASCADE)
rating = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(10)])
rated_on = models.DateTimeField(auto_now_add=True)

class Meta:
unique_together = ('book', 'user') # A user can rate a book only once

42 changes: 42 additions & 0 deletions store/static/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Custom styles for base.html */
.sidebar {
background-color: #f8f9fa;
padding: 20px;
height: 100vh;
}

.main-content {
padding: 20px;
}

/* Custom styles for book_detail.html */
.book-details {
padding: 20px;
}

/* Custom styles for book_list.html */
.search-row {
margin-bottom: 20px;
}

.search-button {
margin-top: 32px;
}

.book-table {
margin-top: 20px;
}

/* Custom styles for index.html */
.welcome-text {
margin-top: 20px;
}

/* Custom styles for loaned_books.html */
.loaned-books-table {
margin-top: 20px;
}

.return-button {
margin: 5px;
}
50 changes: 23 additions & 27 deletions store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,39 @@
{% block title %}
<title>Library</title>
{% endblock %}
{% load static %}
<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="{% static 'css/styles.css' %}"> <!-- Custom CSS file -->
<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.first_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> -->
{% endif %}
</ul>


{% endblock %}
<div class="col-sm-2 sidebar">
{% 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 'logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'login'%}">Login</a></li>
{% endif %}
</ul>
{% endblock %}
</div>
<div class="col-sm-10 main-content">
{% block content %}{% endblock %}
</div>
</div>
<div class="col-sm-10 ">
{% block content %}{% endblock %}
</div>
</div>

</div>
</body>

</html>
Loading