Skip to content

Commit

Permalink
Add login and logout views
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhd336 committed Jun 13, 2016
1 parent 58ed1d7 commit c5f8924
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Bookshare/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
}


AUTHENTICATION_BACKENDS = ['bookshare.backends.MyBackend']
AUTHENTICATION_BACKENDS = ['bookshare.backends.MyBackend', 'django.contrib.auth.backends.ModelBackend']

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
Expand Down
Binary file modified Bookshare/settings.pyc
Binary file not shown.
Binary file modified bookshare/backends.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions bookshare/migrations/0002_auto_20160613_1623.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-13 16:23
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bookshare', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='student',
name='extra',
),
migrations.AddField(
model_name='student',
name='first_time',
field=models.BooleanField(default=False),
),
]
Binary file added bookshare/migrations/0002_auto_20160613_1623.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion bookshare/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

class Student(models.Model):
user = models.OneToOneField(User)
extra = models.CharField(max_length = 200, default = "Test")
first_time = models.BooleanField(default = False)

# Create your models here.
Binary file modified bookshare/models.pyc
Binary file not shown.
15 changes: 11 additions & 4 deletions bookshare/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/united/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>

</head>

<body>
Expand All @@ -36,15 +36,22 @@
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
{% if not user.is_authenticated %}
<li>
<a href="#">About</a>
<a href="/signin/">Sign in</a>
</li>
{% endif %}
<li>
<a href="#">Help</a>
<a href="#">Browse all books</a>
</li>
<li>
<a href="#">Contact</a>
<a href="#">About</a>
</li>
{% if user.is_authenticated %}
<li>
<a href="/logout/">Logout</a>
</li>
{% endif %}
</ul>
</div>
<!-- /.navbar-collapse -->
Expand Down
28 changes: 20 additions & 8 deletions bookshare/templates/signin.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
{% extends 'base.html' %}
{% block content %}
<div class = "container">
<form action = "/signin/" method = "post">
{% csrf_token %}
<input type = "text" name = "roll_no"/>
<input type = "password" name = "pass"/>
<input type = "submit"/>
</form>
</div>

<div class="container">
{% if error %}
<div class="alert alert-dismissible alert-danger">
<button type="button" class="close" data-dismiss="alert">&times;</button>
{{ error }}
</div>
{% endif %}
<form class="form-signin" action = "/signin/" method = "post">
{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputRoll" class="sr-only">Roll number</label>
<input type="text" id="inputRoll" class="form-control" placeholder="Roll number" name = "roll_no" required autofocus>
<label for="inputPassword" class="sr-only">Web mail password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" name = "pass" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

</div>
</div>
{% endblock %}
4 changes: 1 addition & 3 deletions bookshare/templates/success.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{% extends 'base.html' %}
{% block content %}
<p>Successfully logged in</p>
{{ user.user.username }}
{{ user.user.password }}
{{ user.extra }}
{{ user.is_authenticated }}
{% endblock %}
1 change: 1 addition & 0 deletions bookshare/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
url(r'^signin/', views.signin),
url(r'^admin/', admin.site.urls),
url(r'^test/', views.test),
url(r'^logout/', views.logout_view),
]
Binary file modified bookshare/urls.pyc
Binary file not shown.
23 changes: 18 additions & 5 deletions bookshare/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

def index(request):
return render(request, 'index.html')
Expand All @@ -12,9 +11,23 @@ def signin(request):
roll_no = request.POST.get("roll_no")
password = request.POST.get("pass")
student = authenticate(username = roll_no, password = password)
return render(request, 'success.html', {'student' : student})
if student != None:
student.user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, student.user)
return render(request, 'success.html')
else:
return render(request, 'signin.html',
{'error' : "Invalid credentials or webmail is down. Please try again"})
return render(request, 'signin.html')

@login_required(login_url = "/signin/")
def logout_view(request):
if request.user.is_authenticated():
logout(request)
return HttpResponse("You are logged out")


def test(request):
return HttpResponse("This is test")
if request.user.is_authenticated():
return HttpResponse("This is test")
return HttpResponse("This is not test")

Binary file modified bookshare/views.pyc
Binary file not shown.
Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit c5f8924

Please sign in to comment.