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

Completed required tasks #2

Open
wants to merge 1 commit 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
Empty file added accounts/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = 'accounts'
82 changes: 82 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import unicodedata
from django import forms
from django.contrib.auth import models
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.contrib.auth import (
authenticate, get_user_model, password_validation,
)


class UsernameField(forms.CharField):
def to_python(self, value):
return unicodedata.normalize('NFKC', super().to_python(value))

def widget_attrs(self, widget):
return {
**super().widget_attrs(widget),
'autocapitalize': 'none',
'autocomplete': 'username',
}

class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': ('The two password fields didn’t match.'),
}
password1 = forms.CharField(
label=("Password"),
strip=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
# help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=("Password confirmation"),
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
strip=False,
# help_text=("Enter the same password as before, for verification."),
)

class Meta:
model = User
fields = ("username","first_name","last_name")
field_classes = {'username': UsernameField}
help_texts = {
'username': None,
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2

def _post_clean(self):
super()._post_clean()
# Validate the password after self.instance is updated with form data
# by super().
password = self.cleaned_data.get('password2')
if password:
try:
password_validation.validate_password(password, self.instance)
except ValidationError as error:
self.add_error('password2', error)

def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
Empty file added accounts/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
10 changes: 10 additions & 0 deletions accounts/templates/registration/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'store/base.html' %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
{% endblock %}
3 changes: 3 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from .views import SignUpView


urlpatterns = [
path('signup/', SignUpView.as_view(), name='signup'),
]
10 changes: 10 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.shortcuts import render
from django.urls import reverse_lazy
# from django.contrib.auth.forms import UserCreationForm
from .forms import UserCreationForm
from django.views.generic import CreateView
# Create your views here.
class SignUpView(CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
3 changes: 2 additions & 1 deletion library/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'django.contrib.staticfiles',
'store',
'authentication',
'accounts',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -108,7 +109,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

Expand Down
1 change: 1 addition & 0 deletions 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('accounts.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_20210716_0944.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.2.1 on 2021-07-16 04:14

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='ratings',
field=models.TextField(null=True),
),
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),
),
]
18 changes: 18 additions & 0 deletions store/migrations/0004_auto_20210716_0948.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.1 on 2021-07-16 04:18

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='book',
name='ratings',
field=models.TextField(default=''),
),
]
18 changes: 18 additions & 0 deletions store/migrations/0005_auto_20210716_0952.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.1 on 2021-07-16 04:22

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='book',
name='ratings',
field=models.TextField(blank=True),
),
]
30 changes: 30 additions & 0 deletions store/migrations/0006_auto_20210716_1031.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.2.1 on 2021-07-16 05:01

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

operations = [
migrations.RemoveField(
model_name='book',
name='ratings',
),
migrations.CreateModel(
name='bookRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ratingValue', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
('bookName', 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)),
],
),
]
18 changes: 18 additions & 0 deletions store/migrations/0007_book_ctrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.1 on 2021-07-16 12:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0006_auto_20210716_1031'),
]

operations = [
migrations.AddField(
model_name='book',
name='ctrating',
field=models.IntegerField(default=0),
),
]
7 changes: 7 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 MaxValueValidator, MinValueValidator
# Create your models here.

class Book(models.Model):
Expand All @@ -9,6 +10,7 @@ class Book(models.Model):
description = models.TextField(null=True)
mrp = models.PositiveIntegerField()
rating = models.FloatField(default=0.0)
ctrating = models.IntegerField(default=0)

class Meta:
ordering = ('title',)
Expand All @@ -30,3 +32,8 @@ def __str__(self):
else:
return f'{self.book.title} - Available'

class bookRating(models.Model):
bookName = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='user', null=True, blank=True, on_delete=models.SET_NULL)
ratingValue = models.IntegerField(null=True, validators = [MinValueValidator(0), MaxValueValidator(10)])
Copy link
Member

Choose a reason for hiding this comment

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

Good use of Django validators here 👏🏻 .


13 changes: 13 additions & 0 deletions store/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "store/base.html" %}


{% block content %}

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

{% endblock %}
16 changes: 12 additions & 4 deletions store/templates/store/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% load static %}
</head>

<body>
<body onload="foo()">

<div class="container-fluid">

Expand All @@ -29,11 +29,10 @@
<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>
<li><a href="{% url 'signup'%}">Sign up</a></li>
{% endif %}
</ul>


{% endblock %}
</div>
<div class="col-sm-10 ">
Expand All @@ -42,5 +41,14 @@
</div>

</div>
<script>
function foo(){
{% if user.is_authenticated %}
{% if request.path == "/accounts/signup/" or request.path == "/accounts/login/" %}
window.location.href='/'
{% endif %}
{% endif %}
}
</script>
</body>
</html>
Loading