Skip to content

Commit

Permalink
Adding in custom backend. Adding in a style sheet, adding in a login …
Browse files Browse the repository at this point in the history
…view. refactoring to the 'render()' function as opposed to the render_to_response function.
  • Loading branch information
Spencer Romo committed Aug 15, 2016
1 parent 81cbafd commit a1f43c2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 4 deletions.
18 changes: 18 additions & 0 deletions banded/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib.auth.models import User

class EmailAuth:
def authenticate(self, email = '', password = ''):
try:
user = User.objects.get(email = email)
if user.check_password(password):
return user
else:
return None
except Exception, e:
print str(e)
return None
def get_user(self, user_id = ''):
try:
return User.objects.get(pk = user_id)
except User.DoesNotExist:
return None
2 changes: 2 additions & 0 deletions banded/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

AUTHENTICATION_BACKENDS = ['banded.backend.EmailAuth']

ROOT_URLCONF = 'banded.urls'

TEMPLATES = [
Expand Down
6 changes: 5 additions & 1 deletion banded/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import url, include
from django.contrib import admin

import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home),
url(r'^login$', views.loginTest),
url(r'^style$', views.styleSheet),
url(r'^u/', include('users.urls')),
]
13 changes: 10 additions & 3 deletions banded/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from django.shortcuts import render, render_to_response

from django.shortcuts import render
from django.template.context import RequestContext

def home(request):
return render_to_response('home.html')
return render(request, 'home.html')


def loginTest(request):
return render(request, 'login.html')

def styleSheet(request):
return render(request, 'style_sheet.html')
19 changes: 19 additions & 0 deletions static/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'base.html' %}
{% block content %}
{% if form.errors %}
<p>Error</p>
{% endif %}

<form action = '/u/auth' method = 'post'>
{% csrf_token %}
<label for = 'email'>
Email:
</label>
<input type = 'text' name = 'email' value = '' id = 'email'>
<label for = 'password'>
Password:
</label>
<input type = 'password' name = 'password' value = '' id = 'password'>
<input type = 'submit' value = 'login'/>
</form>
{% endblock %}
6 changes: 6 additions & 0 deletions static/templates/style_sheet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'base.html' %}

{% block content %}
<h1> components go here! </h1>

{% endblock %}
6 changes: 6 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.conf.urls import include, url
import views

urlpatterns = [
url(r'^auth$', views.authorization),
]
22 changes: 22 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.http import HttpResponseBadRequest

# Create your views here.


def authorization(request):
try:
em = request.POST.get('email', '')
print 'em: ', em
pw = request.POST.get('password', '')
print 'pw: ', pw
user = authenticate(email = em, password = pw)
if user is not None:
print 'user successfully logged in'
return render(request, 'home.html')
else:
print 'login failed'
return render(request, 'home.html')
except Exception, e:
return HttpResponseBadRequest(str(e), status = 400)



0 comments on commit a1f43c2

Please sign in to comment.