From cce3b1e73333b7093f9f43968645f3332769557e Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Thu, 9 Jul 2020 02:14:08 -0300 Subject: [PATCH] added authentication --- .../CalibreWebCompanion/settings.py | 35 ++-- .../CalibreWebCompanion/urls.py | 5 +- CalibreWebCompanion/db.sqlite3 | Bin 131072 -> 131072 bytes .../library/static/css/styles.css | 186 +++++++++++------- .../library/templates/base.html | 26 +++ .../library/templates/ifelselogin.html | 6 + .../library/templates/registration/index.html | 6 + .../templates/registration/logged_out.html | 7 + .../library/templates/registration/login.html | 39 ++++ .../registration/password_reset_complete.html | 7 + .../registration/password_reset_confirm.html | 28 +++ .../registration/password_reset_done.html | 4 + .../registration/password_reset_email.html | 2 + .../registration/password_reset_form.html | 11 ++ .../templates/registration/sign_up.html | 13 ++ .../library/templates/stripped_base.html | 41 ++++ CalibreWebCompanion/library/urls.py | 2 + CalibreWebCompanion/library/views.py | 20 ++ 18 files changed, 349 insertions(+), 89 deletions(-) create mode 100644 CalibreWebCompanion/library/templates/ifelselogin.html create mode 100644 CalibreWebCompanion/library/templates/registration/index.html create mode 100644 CalibreWebCompanion/library/templates/registration/logged_out.html create mode 100644 CalibreWebCompanion/library/templates/registration/login.html create mode 100644 CalibreWebCompanion/library/templates/registration/password_reset_complete.html create mode 100644 CalibreWebCompanion/library/templates/registration/password_reset_confirm.html create mode 100644 CalibreWebCompanion/library/templates/registration/password_reset_done.html create mode 100644 CalibreWebCompanion/library/templates/registration/password_reset_email.html create mode 100644 CalibreWebCompanion/library/templates/registration/password_reset_form.html create mode 100644 CalibreWebCompanion/library/templates/registration/sign_up.html create mode 100644 CalibreWebCompanion/library/templates/stripped_base.html diff --git a/CalibreWebCompanion/CalibreWebCompanion/settings.py b/CalibreWebCompanion/CalibreWebCompanion/settings.py index 40a5ea8..5e06eef 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/settings.py +++ b/CalibreWebCompanion/CalibreWebCompanion/settings.py @@ -15,6 +15,19 @@ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 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 # 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! 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 = [] +#### 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 @@ -127,12 +149,3 @@ USE_L10N = 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/' diff --git a/CalibreWebCompanion/CalibreWebCompanion/urls.py b/CalibreWebCompanion/CalibreWebCompanion/urls.py index 64829d1..813db69 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/urls.py +++ b/CalibreWebCompanion/CalibreWebCompanion/urls.py @@ -21,7 +21,8 @@ from django.views.generic import RedirectView urlpatterns = [ path('admin/', admin.site.urls), - path('library/', include('library.urls')), - path('', RedirectView.as_view(url='library/', permanent=True)), + path('accounts/', include('django.contrib.auth.urls')), + path('', include('library.urls')), + path('', RedirectView.as_view(url='books/', permanent=True)), ] urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) \ No newline at end of file diff --git a/CalibreWebCompanion/db.sqlite3 b/CalibreWebCompanion/db.sqlite3 index b92ca6170112118dae90f72ba652f6d34fbd1cc9..a609fcd35cb530044d919da07040e4aa74eafe00 100644 GIT binary patch delta 1406 zcmb`G&u`mg7{~26LG6h9vJJ+j!q#a83DDBleqaB}GzoWZ(_~B1v`t+b5TtP$$BykZ z&W|QNq!~;?;{xO|?NSMGM4C_!J0K3g1tIMO7Z3-8!~w*ie*#|mW2Hmtg@dKn@AJIx z^Lf9|_u1MS+1eWUc$}0zdFcizefi#Jm(JWLo&hZa{0V*nUx60*0(`V{y0!`it*OaZ z34%B?T4*-Rcbk5x9{io9qtqqxa3}m?ghJ%to9Nk{AEy`?4M*=1W8aR=(BDVz?p&Ve zM&G`DEgVUI5RofImM?b%+q0Qcr%;s4HNW6VhSR8_cCjP2GD|5_>n@mSrD|sq3znwp zW>(RVrsg}EwOrAbrYoA7Luq!_OfMz7dSV6-E_&l69K=1pSIKluQ&-Y3tIRN&gx1cg zc{a^zD7%!ZW}M*brmT#Wa`Z%|qC2{+sS9S(h1Xfg!mCiY%AyI##Th2f!5HEtRuVR; zNV*@99iMGO*RNH1Y^>V#btD$**kjyv!!H<;P}Hm()_aQF)>IAZiCJq%>S$a>$>WS7 zZ|I6)WxBZy-O_E{Dl&Q^$!S(iSj$~MULD2}jx0=F3&R)=+_+C-KwIyNfX(6!uB zsc881TAORRJmU|mE3*1OREJWW5n=)(@FKo_jU0Au@LO+U=Px%xUJm5JaW+s$h_MV7 zMRY1~Ll`&&cnE$44)`=M%{!nsNu|k?S{;6zbds#pbAG$)NM^{S~yDhOBnBo1M zzkBDOi-zfY1XvyWVT_^g1up!Mru*l@G|_)e9vP?Uk%6~;z9&>?g+jAXs> zCSWOski;Rje~vn!2<>B(-S(QgJf|+MXr;*VO*dOFdFT-RC0Y@V_i8v zIgOT$WNVJoV5OO}->~L*&#)@fo~?8$xKK9N(>In|saAVcDj&nxvyjL`PU815_Qz{a zjJ-JIA(YtuI60&keTAk6?)F=!13ht^6XT2&V?_~TZt%(MZHM1WoJ2lyF03M}?D zxbiGDPVP$`ezIn%Q`Ck1{m}cn4<7D5AfJCTIDUr!zk~0<=ip9oya_fhQZjj<&C^34 Z{*UmHsid9`A03;}%alBN^zwg?%s=n#iyZ&} delta 269 zcmZo@;Am*zm>|ulI8nx#QE_9!B6*<|3~c;m4E!(ouki2YFXNxZ-@RF|A&Gx-nZAk| z4-d0EXJTndMto^;Y7qkigD?k}SCCqin^|0(nJ>=DY|J?MAdm3o%ld`}Y>a$|82Aot zR#e!*$I)cX$m-j|>Nxp9y~5^Kd8~}Gh6;ujRzPHAre|($U~FK(1vHV7{|*BXu>&o= zz`yxUy@miABYzSDf6`{bf(ZV}N%PYM&5`t(8W>qxPQEo?i-VE>0|O9o0X071Z~m}; l`-k<6A`U>!j~MtL0R?aH%gHi3avB*J8R#0A>l$I$4ggGxOws@V diff --git a/CalibreWebCompanion/library/static/css/styles.css b/CalibreWebCompanion/library/static/css/styles.css index 2a56dc8..2d8332c 100644 --- a/CalibreWebCompanion/library/static/css/styles.css +++ b/CalibreWebCompanion/library/static/css/styles.css @@ -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 - } \ No newline at end of file +/* 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 +} \ No newline at end of file diff --git a/CalibreWebCompanion/library/templates/base.html b/CalibreWebCompanion/library/templates/base.html index a1bc9ab..9199e18 100644 --- a/CalibreWebCompanion/library/templates/base.html +++ b/CalibreWebCompanion/library/templates/base.html @@ -13,6 +13,20 @@ + {% block topnav%} + + {% endblock %} + + {% if user.is_authenticated %}
@@ -138,6 +152,18 @@ } } + + {% else %} +

You don't have permission to view this.

+
  • Login
  • + or +
  • Sign up
  • + {% endif %} + + + + + \ No newline at end of file diff --git a/CalibreWebCompanion/library/templates/ifelselogin.html b/CalibreWebCompanion/library/templates/ifelselogin.html new file mode 100644 index 0000000..5188599 --- /dev/null +++ b/CalibreWebCompanion/library/templates/ifelselogin.html @@ -0,0 +1,6 @@ + {% if user.is_authenticated %} + + {% else %} +

    You don't have permission to view this.

    +
  • Login
  • + {% endif %} \ No newline at end of file diff --git a/CalibreWebCompanion/library/templates/registration/index.html b/CalibreWebCompanion/library/templates/registration/index.html new file mode 100644 index 0000000..ef19bfe --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/index.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} +{% block content %} + +

    Welcome {{user}}

    + +{% endblock %} \ No newline at end of file diff --git a/CalibreWebCompanion/library/templates/registration/logged_out.html b/CalibreWebCompanion/library/templates/registration/logged_out.html new file mode 100644 index 0000000..c0697a4 --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/logged_out.html @@ -0,0 +1,7 @@ +{% extends "stripped_base.html" %} + + {% block content %} +

    Logged out!

    + Click here to login again. +{% endblock %} + diff --git a/CalibreWebCompanion/library/templates/registration/login.html b/CalibreWebCompanion/library/templates/registration/login.html new file mode 100644 index 0000000..ac16faf --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/login.html @@ -0,0 +1,39 @@ +{% extends "stripped_base.html" %} + +{% block content %} + + {% if form.errors %} +

    Your username and password didn't match. Please try again.

    + {% endif %} + + {% if next %} + {% if user.is_authenticated %} +

    Your account doesn't have access to this page. To proceed, + please login with an account that has access.

    + {% else %} +

    Please login to see this page.

    + {% endif %} + {% endif %} + +
    + {% csrf_token %} + + + + + + + + + +
    {{ form.username.label_tag }}{{ form.username }}
    {{ form.password.label_tag }}{{ form.password }}
    + + +
    + + {# Assumes you setup the password_reset view in your URLconf #} +

    Lost password?

    + + + +{% endblock %} diff --git a/CalibreWebCompanion/library/templates/registration/password_reset_complete.html b/CalibreWebCompanion/library/templates/registration/password_reset_complete.html new file mode 100644 index 0000000..29c2099 --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/password_reset_complete.html @@ -0,0 +1,7 @@ +{% extends "stripped_base.html" %} +{% block content %} +

    The password has been changed!

    +

    log in again?

    +{% endblock %} + + diff --git a/CalibreWebCompanion/library/templates/registration/password_reset_confirm.html b/CalibreWebCompanion/library/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000..c5eff65 --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/password_reset_confirm.html @@ -0,0 +1,28 @@ +{% extends "stripped_base.html" %} +{% block content %} + {% if validlink %} +

    Please enter (and confirm) your new password.

    +
    + {% csrf_token %} + + + + + + + + + + + + + +
    {{ form.new_password1.errors }} + {{ form.new_password1 }}
    {{ form.new_password2.errors }} + {{ form.new_password2 }}
    +
    + {% else %} +

    Password reset failed

    +

    The password reset link was invalid, possibly because it has already been used. Please request a new password reset.

    + {% endif %} +{% endblock %} diff --git a/CalibreWebCompanion/library/templates/registration/password_reset_done.html b/CalibreWebCompanion/library/templates/registration/password_reset_done.html new file mode 100644 index 0000000..ca093eb --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/password_reset_done.html @@ -0,0 +1,4 @@ +{% extends "stripped_base.html" %} +{% block content %} +

    We've emailed you instructions for setting your password. If they haven't arrived in a few minutes, check your spam folder.

    +{% endblock %} diff --git a/CalibreWebCompanion/library/templates/registration/password_reset_email.html b/CalibreWebCompanion/library/templates/registration/password_reset_email.html new file mode 100644 index 0000000..61fba5c --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/password_reset_email.html @@ -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 %} \ No newline at end of file diff --git a/CalibreWebCompanion/library/templates/registration/password_reset_form.html b/CalibreWebCompanion/library/templates/registration/password_reset_form.html new file mode 100644 index 0000000..fafceb0 --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/password_reset_form.html @@ -0,0 +1,11 @@ +{% extends "stripped_base.html" %} +{% block content %} +
    + {% csrf_token %} + {% if form.email.errors %} + {{ form.email.errors }} + {% endif %} +

    {{ form.email }}

    + +
    +{% endblock %} diff --git a/CalibreWebCompanion/library/templates/registration/sign_up.html b/CalibreWebCompanion/library/templates/registration/sign_up.html new file mode 100644 index 0000000..d46cece --- /dev/null +++ b/CalibreWebCompanion/library/templates/registration/sign_up.html @@ -0,0 +1,13 @@ +{% extends "stripped_base.html" %} + +{% block content %} + +
    + {% csrf_token %} {{form.as_p}}
    + + + +{% endblock %} + + + diff --git a/CalibreWebCompanion/library/templates/stripped_base.html b/CalibreWebCompanion/library/templates/stripped_base.html new file mode 100644 index 0000000..80468ed --- /dev/null +++ b/CalibreWebCompanion/library/templates/stripped_base.html @@ -0,0 +1,41 @@ + + + + + {% block title %}Local Library{% endblock %} + + + + + {% load static %} + + + + + {% block topnav%} +
      + + {% if user.is_authenticated %} +
    • {{ user.get_username }}
    • +
    • Logout
    • + {% else %} +
    • Sign up
    • +
    • Login
    • + {% endif %} +
    + {% endblock %} + + + +
    +
    + +
    {% block content %}{% endblock %}
    +
    +
    + + + + + \ No newline at end of file diff --git a/CalibreWebCompanion/library/urls.py b/CalibreWebCompanion/library/urls.py index 188880e..6a5e2a1 100644 --- a/CalibreWebCompanion/library/urls.py +++ b/CalibreWebCompanion/library/urls.py @@ -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") + ] \ No newline at end of file diff --git a/CalibreWebCompanion/library/views.py b/CalibreWebCompanion/library/views.py index c785d89..f6a174a 100644 --- a/CalibreWebCompanion/library/views.py +++ b/CalibreWebCompanion/library/views.py @@ -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'