Commit 4225f9ff authored by Brian Guadalupe's avatar Brian Guadalupe

Merge branch 'fix-auth-views' into 'master'

Fix auth-related views + Edit own user profiles

See merge request brian/mymusiclist!6
parents 379d0bf9 31d28323
.DS_Store .DS_Store
*.pyc __pycache__/
*.sqlite *.py[cod]
*.sqlite3 *.sqlite3
desktop.ini desktop.ini
...@@ -6,6 +6,7 @@ from django.contrib.auth import login, authenticate ...@@ -6,6 +6,7 @@ from django.contrib.auth import login, authenticate
# from django.contrib.auth.forms import UserCreationForm # from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic.edit import UpdateView
from core.forms import SignUpForm from core.forms import SignUpForm
...@@ -26,10 +27,23 @@ def signup(request): ...@@ -26,10 +27,23 @@ def signup(request):
def home(request): def home(request):
return render(request, 'home.html') return render(request, 'home.html')
def user_profile_page(request, username): def user_profile_page(request, slug):
user = get_object_or_404(User, username=username) user = get_object_or_404(User, username=slug)
return render(request, 'profile.html', {'profile': user}) return render(request, 'profile.html', {'profile': user})
# if request.user.is_authenticated:
# return render(request, 'profile.html') class EditProfile(UpdateView):
# else: model = User
# return render(request, 'profile_public.html') fields = ['username', 'email', 'first_name', 'last_name']
template_name = 'edit_profile.html'
slug_field = 'username'
slug_url_kwarg = 'slug'
# def edit_profile(request, username):
# if request.method == 'POST':
# form = EditProfile(request.POST, instance=request.user)
# if form.is_valid():
# form.save()
# return HttpResponseRedirect(reverse('update_profile_success'))
# else:
# form = EditProfile(initial={'username': request.user.username, 'email': request.user.email, 'first_name': request.user.first_name, 'last_name': request.user.last_name})
# return render(request, 'edit_profile.html', {'form': form})
\ No newline at end of file
...@@ -14,16 +14,18 @@ Including another URLconf ...@@ -14,16 +14,18 @@ Including another URLconf
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" """
from django.conf.urls import url from django.conf.urls import url
from django.urls import reverse_lazy
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from core import views as core_views from core import views as core_views
"""import * pls"""
urlpatterns = [ urlpatterns = [
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'), url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^signup/', core_views.signup, name='signup'), url(r'^signup/$', core_views.signup, name='signup'),
url(r'^profile/(?P<username>[A-Za-z0-9-+_.@]+)/$', core_views.user_profile_page, name='profile'), url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/$', core_views.user_profile_page, name='viewprofile'),
url(r'^$', core_views.home), url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/edit/$', core_views.EditProfile.as_view(success_url=reverse_lazy('home')), name='editprofile'),
# url(r'^profile/(?P<slug>[A-Za-z0-9-+_.@]+)/edit/$', core_views.EditProfile.as_view(success_url=reverse_lazy('profile', kwargs={'update':'true'})), name='editprofile'),
url(r'^$', core_views.home, name='home'),
] ]
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
</head> </head>
<body> <body>
<header> <header>
{% if user.is_authenticated %} {% if request.user.is_authenticated %}
<ul id='nav_bar'> <ul id='nav_bar'>
<li><a class="nav_bar_home" href="/">MyMusicList</a></li> <li><a class="nav_bar_home" href="/">MyMusicList</a></li>
<li><a class="nav_bar_tab" href="/profile/{{user.username}}">Hi, {{user.username}}</a></li> <li><a class="nav_bar_tab" href="/profile/{{request.user.username}}">Hi, {{request.user.username}}</a></li>
<li style="float:right"><a class="nav_bar_tab" href="{% url 'logout' %}">Logout</a></li> <li style="float:right"><a class="nav_bar_tab" href="{% url 'logout' %}">Logout</a></li>
</ul> </ul>
{% else %} {% else %}
......
{% extends 'base.html' %}
{% block title %}Edit Profile{% endblock %}
{% block content %}
<div class="boxified main profile">
{% if request.user.is_authenticated %}
{% if user.username == request.user.username %}
<h2>Edit Profile</h2>
<!-- <form method="post"> -->
<form method="post" action="{% url 'editprofile' slug=user.username %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="buttoned">Save changes</button>
</form>
{% else %}
<p>You can only edit your own user profile. Go to your own <a href="{% url 'viewprofile' slug=request.user.username %}">profile page</a>.</p>
{% endif %}
{% else %}
<p>Please <a href="{% url 'login' %}">log in</a> to your account to edit your profile.</p>
{% endif %}
</div>
{% endblock %}
...@@ -33,6 +33,6 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel tristique ris ...@@ -33,6 +33,6 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel tristique ris
Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris suscipit nec velit non egestas. Quisque auctor viverra urna, at euismod turpis ornare id. Aliquam pharetra purus quis dui blandit tincidunt. Quisque massa ligula, malesuada vel leo eu, accumsan vulputate nulla. Integer faucibus malesuada pharetra. Duis varius sapien sit amet justo malesuada fringilla. Integer eget ex sed urna auctor vestibulum. Aliquam sit amet molestie velit, a pretium ligula. Mauris gravida turpis vitae scelerisque finibus. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris suscipit nec velit non egestas. Quisque auctor viverra urna, at euismod turpis ornare id. Aliquam pharetra purus quis dui blandit tincidunt. Quisque massa ligula, malesuada vel leo eu, accumsan vulputate nulla. Integer faucibus malesuada pharetra. Duis varius sapien sit amet justo malesuada fringilla. Integer eget ex sed urna auctor vestibulum. Aliquam sit amet molestie velit, a pretium ligula. Mauris gravida turpis vitae scelerisque finibus.
</div> </div>
{% if user.is_authenticated %} {% if request.user.is_authenticated %}
{% endif %} {% endif %}
{% endblock %} {% endblock %}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<h2>Logged out</h2> <h2>Logged out</h2>
<p> <p>
You have been successfully logged out. <br> Please login again. You have been successfully logged out. <br> Go back to the <a href="{% url 'home' %}">home page</a>.
</p> </p>
</div> </div>
......
...@@ -4,15 +4,17 @@ ...@@ -4,15 +4,17 @@
{% block content %} {% block content %}
<div class="boxified main login_form"> <div class="boxified main login_form">
{% if request.user.is_authenticated %}
<h2>Login</h2> <p>You are already logged in as {{request.user.username}}. You need to log out before logging in as a different user.</p>
<p><a href="/logout">Log out</a> or <a href="/">return to the home page</a>.</p>
{% else %}
<form method="post"> <h2>Login</h2>
<form method="post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<button type="submit" class="buttoned">Login</button> <button type="submit" class="buttoned">Login</button>
</form> </form>
</div> </div>
{% endif %}
{% endblock %} {% endblock %}
...@@ -3,16 +3,19 @@ ...@@ -3,16 +3,19 @@
{% block title %}{{profile.username}}'s Profile{% endblock %} {% block title %}{{profile.username}}'s Profile{% endblock %}
{% block content %} {% block content %}
<div class="boxified main profile"> <div class="boxified main profile">
{% if request.user.is_authenticated %}
<h2>{{profile.username}}'s Profile</h2> <h2>{{profile.username}}'s Profile</h2>
<ul> <ul>
<li><strong>First Name:</strong> {{profile.first_name}}</li> <li><strong>First Name:</strong> {{profile.first_name}}</li>
<li><strong>Last Name:</strong> {{profile.last_name}}</li> <li><strong>Last Name:</strong> {{profile.last_name}}</li>
<li><strong>Email Address:</strong> {{profile.email}}</li> <li><strong>Email Address:</strong> {{profile.email}}</li>
</ul> </ul>
{% if profile.username == user.username and user.is_authenticated %} {% if profile.username == request.user.username %}
<a href="#" class="buttoned">Edit user profile</a> <a href="edit" class="buttoned">Edit user profile</a>
</div>
{% endif %} {% endif %}
{% else %}
<p>Please <a href="{% url 'login' %}">log in</a> to your account to view {{profile.username}}'s profile.</p>
{% endif %}
</div>
{% endblock %} {% endblock %}
...@@ -2,15 +2,20 @@ ...@@ -2,15 +2,20 @@
{% block content %} {% block content %}
<div class='boxified main signup_form'> <div class='boxified main signup_form'>
<h2>Create your account</h2> {% if user.is_authenticated %}
<p> <p>You are already logged in as {{user.username}}.</p>
Sign up now to start building your music collection with us. <br/> Already have an account? Login <p><a href="/logout">Log out</a> or <a href="/">return to home page</a>.</p>
</p> {% else %}
<form method="post"> <h2>Create your account</h2>
<p>
Sign up now to start building your music collection with us. <br/> Already have an account? <a href="/login">Log in</a>
</p>
<form method="post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<button type="submit" class="buttoned">Sign up</button> <button type="submit" class="buttoned">Sign up</button>
</form> </form>
</div> {% endif %}
</div>
{% endblock %} {% endblock %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment