Skip to content

Commit

Permalink
Adding profile view (#128)
Browse files Browse the repository at this point in the history
- Adding option to view other users profile
- Adding users profile tests
- Making other users profile view login required
- Adding href to direct to the user profile when clicking post autor username
- Adding href to direct to the user profile when clicking comment author username
  • Loading branch information
MatanP12 authored May 24, 2022
1 parent d75b7f9 commit d2980be
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ResuMe/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
path('profile/', include('users.urls'), name='profile'),
path('edit-profile/', profile_views.edit_profile, name='users-edit-profile'),
path('direct/', include('direct_message.urls')),
path('users/<str:slug>/', profile_views.ProfileDetailView.as_view(), name='profile-detail')
]


urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
4 changes: 2 additions & 2 deletions posts/templates/posts/feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="post-header">
<div class="article-metadata">
<img class="rounded-circle article-img" src="{{ post.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ post.author }}</a>
<a class="mr-2" href="/users/{{ post.author.username }}/">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
</div>
<div class="menu-nav">
Expand Down Expand Up @@ -65,7 +65,7 @@
{% for comment in post.comment_set.all %}
<div class="comment-data">
<img class="rounded-circle article-img" src="{{ comment.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ comment.author }}</a>
<a class="mr-2" href="/users/{{ comment.author.username }}/">{{ comment.author }}</a>
<hr class="comment-hr">
<p class="comment-content">{{ comment.comment_text }}</p>
</div>
Expand Down
4 changes: 2 additions & 2 deletions posts/templates/posts/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="post-header">
<div class="article-metadata">
<img class="rounded-circle article-img" src="{{ post.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ post.author }}</a>
<a class="mr-2" href="/users/{{ post.author.username }}/">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
</div>
<div class="menu-nav">
Expand Down Expand Up @@ -49,7 +49,7 @@
{% for comment in post.comment_set.all %}
<div class="comment-data">
<img class="rounded-circle article-img" src="{{ comment.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ comment.author }}</a>
<a class="mr-2" href="/users/{{ comment.author.username }}/">{{ comment.author }}</a>
<hr class="comment-hr">
<p class="comment-content">{{ comment.comment_text }}</p>
</div>
Expand Down
19 changes: 17 additions & 2 deletions tests/users_tests/test_users_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_redirections_to_login_page_for_not_logget_in_user(self, client, url):
response = client.get(response.url)
assert 'users/login.html' in response.template_name

@pytest.mark.parametrize('url', ['profile', 'edit-profile'])
@pytest.mark.parametrize('url', ['profile', 'edit-profile', 'users/admin'])
def test_redirect_unauthorized_user(self, client, url):
response = client.get(f'/{url}/')
assert response.status_code == 302
Expand All @@ -61,7 +61,8 @@ def test_redirect_unauthorized_user(self, client, url):
assert 'users/login.html' in response.template_name

@pytest.mark.parametrize('url, template_file', [('profile', 'users/profile.html'),
('edit-profile', 'users/edit-profile.html')
('edit-profile', 'users/edit-profile.html'),
('users/admin', 'users/profile_details.html')
]
)
def test_authorized_user_pages_access(self, client, url, template_file, new_user):
Expand All @@ -70,3 +71,17 @@ def test_authorized_user_pages_access(self, client, url, template_file, new_user
response = client.get(f'/{url}/')
assert response.status_code == 200
assertTemplateUsed(response, template_file)

def test_access_to_valid_user_profile(self, client, new_user):
user = save_user(new_user)
client.login(username='MatanPeretz', password='matan1234')
response = client.get(f'/users/{user.username}/')
assert response.status_code == 200
assertTemplateUsed(response, 'users/profile_details.html')

@pytest.mark.parametrize('invalid_profile', ['Jonathan', '', '12', '1', '-1', ''])
def test_access_to_invalid_user_profile(self, client, invalid_profile, new_user):
save_user(new_user)
client.login(username=USERNAME, password=PASSWORD)
response = client.get(f'/users/{invalid_profile}/')
assert response.status_code == 404
22 changes: 22 additions & 0 deletions users/templates/users/profile_details.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}
{% block styles %}
{% endblock styles %}

{% block content %}

<div class="content-section">
<div class="media">
<img class="rounded-circle account-img" src="{{ object.profile_pic.url }}">
<div class="media-body">
<h2 class="account-heading">{{ object.user.username }}</h2>
<hr>
<h4>Profession</h4>
<p class="text-secondary">{{object.profession }}</p>
<h4>About Me</h4>
<p class="text-secondary">{{object.bio }}</p>
<h4>Email</h4>
<p class="text-secondary">{{ object.user.email }}</p>
</div>
</div>
</div>
{% endblock content %}
12 changes: 12 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from django.shortcuts import render, redirect
from .forms import UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required
from django.views.generic import DetailView
from users.models import Profile
from django.contrib.auth.mixins import LoginRequiredMixin


class ProfileDetailView(LoginRequiredMixin, DetailView):
model = Profile
template_name = 'users/profile_details.html'

def get_slug_field(self):
"""Get the name of a slug field to be used to look up by slug."""
return 'user__username'


@login_required
Expand Down

0 comments on commit d2980be

Please sign in to comment.