added authentication

This commit is contained in:
MassiveAtoms 2020-07-09 02:14:08 -03:00
parent 62b9dfd94f
commit cce3b1e733
18 changed files with 349 additions and 89 deletions

View File

@ -15,6 +15,19 @@ import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CALIBRE_DIR = os.path.abspath("C:\\Users\\MassiveAtoms\\Documents\\Calibre Library") CALIBRE_DIR = os.path.abspath("C:\\Users\\MassiveAtoms\\Documents\\Calibre Library")
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.abspath(CALIBRE_DIR),
# '/static/',
]
STATIC_URL = '/static/'
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
@ -22,11 +35,20 @@ CALIBRE_DIR = os.path.abspath("C:\\Users\\MassiveAtoms\\Documents\\Calibre Libra
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u(8^+rb%rz5hsx4v^^y(ul7g(4n7a8!db@s*9(m5cs*2_ppy8+' SECRET_KEY = 'u(8^+rb%rz5hsx4v^^y(ul7g(4n7a8!db@s*9(m5cs*2_ppy8+'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
#### Don't change things beyond this
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
LOGIN_REDIRECT_URL = '/books'
# Application definition # Application definition
@ -127,12 +149,3 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.abspath(CALIBRE_DIR),
# '/static/',
]
STATIC_URL = '/static/'

View File

@ -21,7 +21,8 @@ from django.views.generic import RedirectView
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('library/', include('library.urls')), path('accounts/', include('django.contrib.auth.urls')),
path('', RedirectView.as_view(url='library/', permanent=True)), path('', include('library.urls')),
path('', RedirectView.as_view(url='books/', permanent=True)),
] ]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Binary file not shown.

View File

@ -1,78 +1,112 @@
/* Fixed sidenav, full height */ /* sidenav */
.sidenav {
height: 100%;
width: 200px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 20px;
}
/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
border: none;
background: none;
width:100%;
text-align: left;
cursor: pointer;
outline: none;
}
/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover {
color: #f1f1f1;
}
/* Main content */
.main {
margin-left: 200px; /* Same as the width of the sidenav */
font-size: 20px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
/* Add an active class to the active dropdown button */
.active {
background-color: green;
color: white;
}
/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
display: none;
background-color: #262626;
padding-left: 8px;
}
/* Optional: Style the caret down icon */
.fa-caret-down {
float: right;
padding-right: 8px;
}
table { /* Fixed sidenav, full height */
border-spacing: 0; .sidenav {
width: 100%; height: 100%;
border: 1px solid #ddd; width: 200px;
} position: fixed;
z-index: 1;
th { top: 0;
cursor: pointer; left: 0;
} background-color: #111;
overflow-x: hidden;
th, td { padding-top: 20px;
text-align: left; }
padding: 16px;
} /* Style the sidenav links and the dropdown button */
.sidenav a,
tr:nth-child(even) { .dropdown-btn {
background-color: #f2f2f2 padding: 6px 8px 6px 16px;
} text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
border: none;
background: none;
width: 100%;
text-align: left;
cursor: pointer;
outline: none;
}
/* On mouse-over */
.sidenav a:hover,
.dropdown-btn:hover {
color: #f1f1f1;
}
/* Main content */
.main {
margin-left: 200px;
/* Same as the width of the sidenav */
font-size: 20px;
/* Increased text to enable scrolling */
padding: 0px 10px;
}
/* Add an active class to the active dropdown button */
.active {
background-color: green;
color: white;
}
/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
display: none;
background-color: #262626;
padding-left: 8px;
}
/* Optional: Style the caret down icon */
.fa-caret-down {
float: right;
padding-right: 8px;
}
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.topnav li {
float: right;
}
.topnav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.topnav li a:hover {
background-color: #111;
}
/* Table */
table {
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th {
cursor: pointer;
}
th,
td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}

View File

@ -13,6 +13,20 @@
</head> </head>
<body> <body>
{% block topnav%}
<ul class="topnav">
{% if user.is_authenticated %}
<li class="active"><a href="{{user.get_absolute_url}}"> {{ user.get_username }}</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
{% else %}
<li><a href="{% url 'sign-up'%}?next={{request.path}}">Sign up</a></li>
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
</ul>
{% endblock %}
{% if user.is_authenticated %}
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<div class="col-sm-2"> <div class="col-sm-2">
@ -138,6 +152,18 @@
} }
} }
</script> </script>
{% else %}
<h1>You don't have permission to view this.</h1>
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
or
<li><a href="{% url 'sign-up'%}?next={{request.path}}">Sign up</a></li>
{% endif %}
</body> </body>
</html> </html>

View File

@ -0,0 +1,6 @@
{% if user.is_authenticated %}
{% else %}
<h1>You don't have permission to view this.</h1>
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}

View File

@ -0,0 +1,6 @@
{% extends "base.html" %}
{% block content %}
<h1> Welcome {{user}} </h1>
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends "stripped_base.html" %}
{% block content %}
<p>Logged out!</p>
<a href="{% url 'login'%}">Click here to login again.</a>
{% endblock %}

View File

@ -0,0 +1,39 @@
{% extends "stripped_base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends "stripped_base.html" %}
{% block content %}
<h1>The password has been changed!</h1>
<p><a href="{% url 'login' %}">log in again?</a></p>
{% endblock %}

View File

@ -0,0 +1,28 @@
{% extends "stripped_base.html" %}
{% block content %}
{% if validlink %}
<p>Please enter (and confirm) your new password.</p>
<form action="" method="post">
{% csrf_token %}
<table>
<tr>
<td>{{ form.new_password1.errors }}
<label for="id_new_password1">New password:</label></td>
<td>{{ form.new_password1 }}</td>
</tr>
<tr>
<td>{{ form.new_password2.errors }}
<label for="id_new_password2">Confirm password:</label></td>
<td>{{ form.new_password2 }}</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Change my password" /></td>
</tr>
</table>
</form>
{% else %}
<h1>Password reset failed</h1>
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}

View File

@ -0,0 +1,4 @@
{% extends "stripped_base.html" %}
{% block content %}
<p>We've emailed you instructions for setting your password. If they haven't arrived in a few minutes, check your spam folder.</p>
{% endblock %}

View File

@ -0,0 +1,2 @@
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

View File

@ -0,0 +1,11 @@
{% extends "stripped_base.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% if form.email.errors %}
{{ form.email.errors }}
{% endif %}
<p>{{ form.email }}</p>
<input type="submit" class="btn btn-default btn-lg" value="Reset password">
</form>
{% endblock %}

View File

@ -0,0 +1,13 @@
{% extends "stripped_base.html" %}
{% block content %}
<form method="post">
{% csrf_token %} {{form.as_p}} <input type="submit" value="Sign up"></form>
{% endblock %}

View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
{% block title %}<title>Local Library</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
{% block topnav%}
<ul class="topnav">
{% if user.is_authenticated %}
<li class="active"><a href="{{user.get_absolute_url}}"></a> {{ user.get_username }}</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
{% else %}
<li><a href="{% url 'sign-up'%}?next={{request.path}}">Sign up</a></li>
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
</ul>
{% endblock %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-10 ">{% block content %}{% endblock %}</div>
</div>
</div>
</body>
</html>

View File

@ -19,5 +19,7 @@ urlpatterns = [
path('results/', views.ResultsView.as_view(), name='results'), path('results/', views.ResultsView.as_view(), name='results'),
path('search/', views.SearchView.as_view(), name='search'), path('search/', views.SearchView.as_view(), name='search'),
path('accounts/sign_up/',views.sign_up,name="sign-up")
] ]

View File

@ -5,8 +5,28 @@ from django.http import HttpResponseRedirect
from .forms import SearchForm from .forms import SearchForm
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
return render(request,'accounts/index.html')
def sign_up(request):
context = {}
form = UserCreationForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
user = form.save()
login(request,user)
return render(request,'registration/index.html')
context['form']=form
return render(request,'registration/sign_up.html',context)
class SearchView(generic.TemplateView): class SearchView(generic.TemplateView):
template_name = 'search.html' template_name = 'search.html'