From 610899a521aba6a6aa9bb764ae96fab803e5e7d0 Mon Sep 17 00:00:00 2001 From: AndyGrant Date: Sat, 22 Feb 2020 19:25:45 -0500 Subject: [PATCH] Update the views for Login, Logout, Register, and Profile --- OpenBench/urls.py | 3 +- OpenBench/views.py | 108 ++++++++++++++++----------- Templates/OpenBench/base.html | 2 +- Templates/OpenBench/profile.html | 33 ++++++++ Templates/OpenBench/viewProfile.html | 33 -------- 5 files changed, 99 insertions(+), 80 deletions(-) create mode 100644 Templates/OpenBench/profile.html delete mode 100644 Templates/OpenBench/viewProfile.html diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 4642b73..a41fe76 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -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), diff --git a/OpenBench/views.py b/OpenBench/views.py index 7aedbac..3ffe039 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -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=''): diff --git a/Templates/OpenBench/base.html b/Templates/OpenBench/base.html index af6b49c..d6555a9 100644 --- a/Templates/OpenBench/base.html +++ b/Templates/OpenBench/base.html @@ -36,7 +36,7 @@
  • ADMIN
  • {% if user.is_authenticated %}
  • Logout
  • -
  • Profile
  • +
  • Profile
  • {% else %}
  • Login
  • Register
  • diff --git a/Templates/OpenBench/profile.html b/Templates/OpenBench/profile.html new file mode 100644 index 0000000..2793983 --- /dev/null +++ b/Templates/OpenBench/profile.html @@ -0,0 +1,33 @@ +{% extends "OpenBench/base.html" %} + +{% block content %} + +
    + {% csrf_token %} + + +
    +
    +
    +
    +
    + + +
    + + + +
    + +{% endblock %} diff --git a/Templates/OpenBench/viewProfile.html b/Templates/OpenBench/viewProfile.html deleted file mode 100644 index 5fe47c5..0000000 --- a/Templates/OpenBench/viewProfile.html +++ /dev/null @@ -1,33 +0,0 @@ -{% extends "OpenBench/base.html" %} - -{% block content %} - -
    - {% csrf_token %} - - -
    -
    -
    -
    -
    - - -
    - - - -
    - -{% endblock %}