Update the views for Login, Logout, Register, and Profile
This commit is contained in:
1 parent
8adb23d974
commit
610899a521
5 files changed
+99
-80
No files matched your search
+1
-2
@@ -26,8 +26,7 @@ urlpatterns = [
|
||||
django.urls.path(r'register/', OpenBench.views.register),
|
||||
django.urls.path(r'login/', OpenBench.views.login),
|
||||
django.urls.path(r'logout/', OpenBench.views.logout),
|
||||
django.urls.path(r'viewProfile/', OpenBench.views.viewProfile),
|
||||
django.urls.path(r'editProfile/', OpenBench.views.editProfile),
|
||||
django.urls.path(r'profile/', OpenBench.views.profile),
|
||||
|
||||
# Links for viewing test tables
|
||||
django.urls.path(r'index/', OpenBench.views.index),
|
||||
|
||||
+64
-44
@@ -20,6 +20,7 @@
|
||||
|
||||
#from django.shortcuts import render as djangoRender
|
||||
|
||||
import django.http
|
||||
import django.shortcuts
|
||||
import django.contrib.auth
|
||||
|
||||
@@ -53,14 +54,6 @@ def render(request, template, data={}):
|
||||
# Administrative Views #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : #
|
||||
# #
|
||||
# POST : #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
def register(request):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
@@ -85,69 +78,96 @@ def register(request):
|
||||
if User.objects.filter(username=request.POST['username']):
|
||||
return index(request, error='That Username is taken')
|
||||
|
||||
|
||||
email = request.POST['email']
|
||||
username = request.POST['username']
|
||||
password = request.POST['password1']
|
||||
|
||||
user = User.objects.create_user(username, email, password)
|
||||
Profile.objects.create(user=user)
|
||||
django.contrib.auth.login(request, user)
|
||||
Profile.objects.create(user=user)
|
||||
|
||||
|
||||
return HttpResponseRedirect('/index/')
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
def login(request):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : Return the HTML template used for logging in a User #
|
||||
# #
|
||||
# POST : Attempt to login the User and authenticate their credentials. #
|
||||
# If their login is invalid, let them know. In all cases, return #
|
||||
# the User back to the main page #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
if request.method == 'GET':
|
||||
return render(request, 'login.html')
|
||||
|
||||
try:
|
||||
django.contrib.auth.login(request,
|
||||
django.contrib.auth.authenticate(
|
||||
username=request.POST['username'],
|
||||
password=request.POST['password']))
|
||||
return HttpResponseRedirect('/index/')
|
||||
user = django.contrib.auth.authenticate(
|
||||
username=request.POST['username'],
|
||||
password=request.POST['password'])
|
||||
|
||||
except Exception as error:
|
||||
if user is None:
|
||||
return index(request, error='Unable to Authenticate User')
|
||||
|
||||
django.contrib.auth.login(request, user)
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
def logout(request):
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : Logout the User if they are logged in. Return to the main page #
|
||||
# #
|
||||
# POST : Logout the User if they are logged in. Return to the main page #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
django.contrib.auth.logout(request)
|
||||
return HttpResponseRedirect('/index/')
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
@login_required(login_url='/login/')
|
||||
def viewProfile(request):
|
||||
def profile(request):
|
||||
|
||||
# render() gets the profile data for us
|
||||
return render(request, 'viewProfile.html', {})
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# #
|
||||
# GET : If the User is logged in, return the HTML template which shows #
|
||||
# all of the information about the User, and a form to change the #
|
||||
# email address, password, and default engine of the User. If the #
|
||||
# User is not logged in, return them to the main page #
|
||||
# #
|
||||
# POST : Modify the User's email address and selected Engine, if the #
|
||||
# User has requested this. Update the password for the User if #
|
||||
# they have requested a change and provided a new set of matching #
|
||||
# passwords. Return the User to the main page #
|
||||
# #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
@login_required(login_url='/login/')
|
||||
def editProfile(request):
|
||||
if not request.user.is_authenticated:
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
# Update Email, Source Repo, and Default Engine
|
||||
profile = Profile.objects.get(user=request.user)
|
||||
profile.user.email = request.POST['email']
|
||||
profile.engine = request.POST['enginename']
|
||||
profile.repo = request.POST['repo']
|
||||
profile.user.save()
|
||||
profile.save()
|
||||
if request.method == 'GET':
|
||||
return render(request, 'profile.html', {})
|
||||
|
||||
# Change Passwords if requested
|
||||
password1 = request.POST['password1']
|
||||
password2 = request.POST['password2']
|
||||
if password1 != '' and password1 == password2:
|
||||
profile.user.set_password(password1)
|
||||
profile.user.save()
|
||||
loginUser(request, django.contrib.auth.authenticate(
|
||||
username=request.user.username,
|
||||
password=password1))
|
||||
profile = Profile.objects.filter(user=request.user)
|
||||
profile.update(engine=request.POST['engine'], repo=request.POST['repo'])
|
||||
|
||||
# Send back to see the homepage
|
||||
return HttpResponseRedirect('/index/')
|
||||
request.user.email = request.POST['email']
|
||||
request.user.save()
|
||||
|
||||
if request.POST['password1'] != request.POST['password2']:
|
||||
return index(request, error='Passwords Do Not Match')
|
||||
|
||||
if request.POST['password1'] != '':
|
||||
request.user.set_password(request.POST['password1'])
|
||||
django.contrib.auth.login(request, request.user)
|
||||
request.user.save()
|
||||
|
||||
return django.http.HttpResponseRedirect('/index/')
|
||||
|
||||
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
# Test List Views #
|
||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||
|
||||
def index(request, page=1, pageLength=24, greens=False, username=None, error=''):
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
<li class="header">ADMIN</li>
|
||||
{% if user.is_authenticated %}
|
||||
<li><a href="/logout/">Logout</a></li>
|
||||
<li><a href="/viewProfile/">Profile</a></li>
|
||||
<li><a href="/profile/">Profile</a></li>
|
||||
{% else %}
|
||||
<li><a href="/login">Login </a></li>
|
||||
<li><a href="/register">Register</a></li>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
{% extends "OpenBench/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<form method="POST" action="/profile/">
|
||||
{% csrf_token %}
|
||||
|
||||
<!-- Profile information. Everything may be changed but the Username -->
|
||||
<label>Username</label><input readonly value="{{profile.user.username}}"><br>
|
||||
<label>Email</label><input value="{{profile.user.email}}" name="email"><br>
|
||||
<label>Source Repo</label><input value="{{profile.repo}}" name="repo"><br>
|
||||
<label>New Password</label><input type="password" name="password1"><br>
|
||||
<label>New Password</label><input type="password" name="password2"><br>
|
||||
|
||||
<!-- User's have a default engine. Select the default, otherwise show an empty line -->
|
||||
<label>Engine</label> <select id="engine" name="engine">
|
||||
{% for engine in config.engines %}
|
||||
{% if profile.engine == engine %}
|
||||
<option selected value="{{engine}}">{{engine}}</option>
|
||||
{% else %}
|
||||
<option value="{{engine}}">{{engine}}</option>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if not profile.engine %}
|
||||
<option selected value=""></option>
|
||||
{% endif %}
|
||||
</select><br>
|
||||
|
||||
<input type="submit" value="Update Profile">
|
||||
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,33 +0,0 @@
|
||||
{% extends "OpenBench/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<form method="POST" action="/editProfile/">
|
||||
{% csrf_token %}
|
||||
|
||||
<!-- User definition. Everything may be changed but the Username -->
|
||||
<label>Username </label> <input readonly value="{{profile.user.username}}" name="username"><br>
|
||||
<label>Email </label> <input value="{{profile.user.email}}" name="email"><br>
|
||||
<label>Source Repo </label> <input value="{{profile.repo}}" name="repo"><br>
|
||||
<label>New Password</label> <input type="password" name="password1"><br>
|
||||
<label>New Password</label> <input type="password" name="password2"><br>
|
||||
|
||||
<!-- User's have a default engine. Select the default, otherwise show an empty line -->
|
||||
<label>Engine</label> <select id="enginename" name="enginename" onchange="configure(testMode)">
|
||||
{% for engine in config.engines %}
|
||||
{% if profile.engine == engine %}
|
||||
<option selected value="{{engine}}">{{engine}}</option>
|
||||
{% else %}
|
||||
<option value="{{engine}}">{{engine}}</option>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if not profile.engine %}
|
||||
<option selected value=""></option>
|
||||
{% endif %}
|
||||
</select><br>
|
||||
|
||||
<input type="submit" name="submit" value="Update Profile">
|
||||
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
Reference in new issue
Block a user