added authentication
This commit is contained in:
		@@ -1,78 +1,112 @@
 | 
			
		||||
 /* Fixed sidenav, full height */
 | 
			
		||||
 .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;
 | 
			
		||||
  } 
 | 
			
		||||
/* sidenav */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
  }
 | 
			
		||||
/* Fixed sidenav, full height */
 | 
			
		||||
.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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
@@ -13,6 +13,20 @@
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<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="row">
 | 
			
		||||
      <div class="col-sm-2">
 | 
			
		||||
@@ -138,6 +152,18 @@
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  </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>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										6
									
								
								CalibreWebCompanion/library/templates/ifelselogin.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								CalibreWebCompanion/library/templates/ifelselogin.html
									
									
									
									
									
										Normal 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 %} 
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
{% extends "base.html" %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
 | 
			
		||||
<h1> Welcome {{user}} </h1>
 | 
			
		||||
 | 
			
		||||
{% endblock %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
@@ -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 %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										41
									
								
								CalibreWebCompanion/library/templates/stripped_base.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								CalibreWebCompanion/library/templates/stripped_base.html
									
									
									
									
									
										Normal 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>
 | 
			
		||||
@@ -19,5 +19,7 @@ urlpatterns = [
 | 
			
		||||
    path('results/', views.ResultsView.as_view(), name='results'),
 | 
			
		||||
    path('search/', views.SearchView.as_view(), name='search'),
 | 
			
		||||
 | 
			
		||||
    path('accounts/sign_up/',views.sign_up,name="sign-up")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
]
 | 
			
		||||
@@ -5,8 +5,28 @@ from django.http import HttpResponseRedirect
 | 
			
		||||
from .forms import SearchForm
 | 
			
		||||
from django.db import models
 | 
			
		||||
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):
 | 
			
		||||
    template_name = 'search.html'
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user