Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding user's info to the navbar #77

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Hotails/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'main.context_processors.navbar_extras'
],
},
},
Expand Down
11 changes: 8 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from review.models import Review


DOG_OWNER_FIXTURE_PROFILE_PICTURE_URL = "https://www.akc.org/wp-content/uploads/2019/06/Bohemian-Shepherd.1.jpg"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this change (and similar changes) to their own commit.

DAYCARE_FIXTURE_PROFILE_PICTURE_URL_1 = "../../static/images/daycare_image_test_01.jpeg"
DAYCARE_FIXTURE_PROFILE_PICTURE_URL_2 = "../../static/images/daycare_image_test_02.jpeg"


@pytest.fixture
def create_order():
return Order.create(dog_owner_id=DogOwner.objects.get(id=1),
Expand Down Expand Up @@ -60,7 +65,7 @@ def create_dog_owner_user():
last_name='USER',
phone_number=1234567890,
dog_race='lavrador',
dog_picture_url="https://www.akc.org/wp-content/uploads/2019/06/Bohemian-Shepherd.1.jpg",
dog_picture_url=DOG_OWNER_FIXTURE_PROFILE_PICTURE_URL,
dog_age=10,
dog_weight=6,
dog_gender='M'
Expand Down Expand Up @@ -90,12 +95,12 @@ def create_daycare_user(daycare_data):

@pytest.fixture
def create_image1(create_daycare_user):
return Image.create(url="../../static/images/daycare_image_test_01.jpeg", daycare_id=create_daycare_user)
return Image.create(url=DAYCARE_FIXTURE_PROFILE_PICTURE_URL_1, daycare_id=create_daycare_user)


@pytest.fixture
def create_image2(create_daycare_user):
return Image.create(url="../../static/images/daycare_image_test_02.jpeg", daycare_id=create_daycare_user)
return Image.create(url=DAYCARE_FIXTURE_PROFILE_PICTURE_URL_2, daycare_id=create_daycare_user)


@pytest.fixture
Expand Down
5 changes: 4 additions & 1 deletion daycare/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from django.core.exceptions import ObjectDoesNotExist


DAYCARE_DEFAULT_PICTURE_URL = "../../static/images/daycare-default-profile-image.jpeg"


class DayCare(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, default=None, blank=True, null=False, editable=True)
name = models.CharField(max_length=20, blank=True, unique=True, validators=[MaxLengthValidator])
Expand Down Expand Up @@ -53,7 +56,7 @@ def get_daycare_primary_image_url(self):
daycare_images = Image.get_images_by_daycare_id(daycare_id=self.id)
if daycare_images is not None and daycare_images.first() is not None:
return daycare_images.first().url
return "../../static/images/daycare-default-profile-image.jpeg"
return DAYCARE_DEFAULT_PICTURE_URL


class Image(models.Model):
Expand Down
17 changes: 8 additions & 9 deletions daycare/test_image.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pytest
from .models import DayCare
from .models import Image
from django.core.exceptions import ValidationError

DEFAULT_DAYCARE_PROFILE_URL = "../../static/images/daycare-default-profile-image.jpeg"
from conftest import DAYCARE_FIXTURE_PROFILE_PICTURE_URL_1
from .models import DayCare, Image, DAYCARE_DEFAULT_PICTURE_URL
from django.core.exceptions import ValidationError


@pytest.mark.django_db()
Expand All @@ -23,11 +22,11 @@ def test_image_creation_with_invalid_image_url(self, create_daycare_user):
match="Invalid URL image - URL should end with \'.gif\', \'.png\', \'.jpg\' or \'.jpeg\'."):
Image.create(url="NOT_VALID_URL", daycare_id=DayCare.objects.get(id=create_daycare_user.id))

def test_daycare_has_customized_profile_image(self, create_image1, create_image2, create_daycare_user):
def test_daycare_has_customized_profile_image(self, create_image1, create_image2, create_daycare_user: DayCare):
daycare_profile_image = create_daycare_user.get_daycare_primary_image_url()
assert daycare_profile_image != DEFAULT_DAYCARE_PROFILE_URL
assert daycare_profile_image is not None
assert daycare_profile_image != DAYCARE_DEFAULT_PICTURE_URL
assert daycare_profile_image == DAYCARE_FIXTURE_PROFILE_PICTURE_URL_1
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its enough to assert on DAYCARE_FIXTURE_PROFILE_PICTURE_URL_1


def test_daycare_has_default_profile_image_when_no_customized_picture_was_found(self, create_daycare_user):
def test_daycare_has_default_profile_image_when_no_customized_picture_was_found(self, create_daycare_user: DayCare):
daycare_profile_image = create_daycare_user.get_daycare_primary_image_url()
assert daycare_profile_image == DEFAULT_DAYCARE_PROFILE_URL
assert daycare_profile_image == DAYCARE_DEFAULT_PICTURE_URL
7 changes: 6 additions & 1 deletion dogowner/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from .validators import ValidateDogOwner, MaxLength


DOG_OWNER_DEFAULT_PROFILE_PICTURE_URL = "../../static/images/dog-owner-default-profile-picture.jpeg"


class Gender(models.TextChoices):
Male = 'M', 'Male'
Female = 'F', 'Female'
Expand Down Expand Up @@ -32,7 +35,6 @@ def create(email, username, password, dog_name,
first_name, last_name, phone_number,
dog_race, dog_picture_url, dog_age,
dog_weight, dog_gender):

ValidateDogOwner(email, username, password, dog_name,
first_name, last_name, phone_number,
dog_race, dog_picture_url, dog_age,
Expand All @@ -56,3 +58,6 @@ def create(email, username, password, dog_name,
new_dog_owner.user.save()
new_dog_owner.save()
return new_dog_owner

def get_dog_owner_profile_image_url(self):
return self.dog_picture_url if self.dog_picture_url else DOG_OWNER_DEFAULT_PROFILE_PICTURE_URL
Comment on lines +61 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isnt this logic part of create()?

12 changes: 11 additions & 1 deletion dogowner/test_dog_owner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .models import DogOwner
from conftest import DOG_OWNER_FIXTURE_PROFILE_PICTURE_URL
from .models import DogOwner, DOG_OWNER_DEFAULT_PROFILE_PICTURE_URL
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from .validators import MaxLength
Expand Down Expand Up @@ -163,3 +164,12 @@ def test_dog_owner_user_creation_with_url_not_an_image(self):
dog_picture_url="https://www.not/an/image.com",
dog_age=10, dog_weight=6, dog_gender='M'
)

def test_dogowner_has_customized_profile_image(self, create_dog_owner_user: DogOwner):
dogowner_profile_image = create_dog_owner_user.get_dog_owner_profile_image_url()
assert dogowner_profile_image == DOG_OWNER_FIXTURE_PROFILE_PICTURE_URL

def test_dogowner_has_default_profile_image(self, create_dog_owner_user: DogOwner):
create_dog_owner_user.dog_picture_url = None
dogowner_profile_image = create_dog_owner_user.get_dog_owner_profile_image_url()
assert dogowner_profile_image == DOG_OWNER_DEFAULT_PROFILE_PICTURE_URL
21 changes: 21 additions & 0 deletions main/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from daycare.models import DayCare
from dogowner.models import DogOwner


def navbar_extras(request):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to filler out the authenticated user from the models?
Is it possible to use the logged in user in this context?

navbar_picture_url = navbar_name = None
if request.user.is_authenticated:
if DogOwner.objects.filter(user=request.user).exists():
dog_owner = DogOwner.objects.filter(user=request.user).first()
navbar_name = dog_owner.__str__()
navbar_picture_url = dog_owner.get_dog_owner_profile_image_url()
else:
daycare = DayCare.objects.filter(user=request.user).first()
navbar_name = daycare.name
navbar_picture_url = daycare.get_daycare_primary_image_url()

context = {
'navbar_picture_url': navbar_picture_url,
'navbar_name': navbar_name,
}
return context
43 changes: 25 additions & 18 deletions main/templates/main/base_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,43 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'css/navbar.css' %}">
{% block stylesheets %}
{% endblock %}
</head>
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-dark">
<nav class="navbar navbar-expand-lg navbar-light bg-dark" style="padding: 0 10px 0 10px;">
<a class="navbar-brand" href="..\">
<img src="..\..\..\static\images\Hotails-logo-smaller.png" style="width:50px; height:50px;" class="d-inline-block align-top" loading="lazy"></a>
<button class="navbar-toggler" type="button" data-target="#navbarText"
aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
</button>
<ul class="navbar-nav mr-auto">
{% if request.user.is_authenticated %}
<img src="{% static 'images/Hotails-logo-smaller.png' %}" style="width:50px; height:50px;" class="d-inline-block align-top" loading="lazy">
</a>
<button class="navbar-toggler" type="button" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation"></button>
<ul class="navbar-nav mr-auto">
{% if request.user.is_authenticated %}
<li class="nav-item"><a class="nav-link text-white" href="/profile">Profile</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Orders</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Search</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Chats</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link text-white" href="/about">About</a></li>
</ul>
<li class="nav-item"><a class="nav-link text-white" href="">Orders</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Search</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Chats</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link text-white" href="/about">About</a></li>
</ul>
{% if request.user.is_authenticated %}
<ul class="nav navbar-nav navbar-right">
<li class="nav-item"><a class="nav-link text-white" style="font-size:1.3rem" href="/logout">Logout</a></li>
<li class="nav-item"><img src="{{ navbar_picture_url }}" alt="{{ name }} image" class="navbar-profile-img"></li>
<li class="nav-item">
<span id="welcomeMsg">Welcome, {{ navbar_name }}!</span><a class="nav-link" id="logoutHref" href="/logout">Logout</a>
</li>
</ul>
{% endif %}
</nav>
</header>

<body>
{% block content %}
{% endblock %}
<div class="row">
<div class="col-12 col-lg-12">
{% block content %}
{% endblock %}
</div>
</div>

</body>

</html>
</html>
23 changes: 22 additions & 1 deletion main/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,25 @@ def test_dog_owner_homepage_is_visible_for_dog_owner(self, client, create_dog_ow
client.force_login(user=create_dog_owner_user.user)
response = client.get("/homepage/")
assert response.status_code == 200
assert list(response.context['daycares']) == list(DayCare.objects.all())
list_of_daycares_from_response = list(response.context['daycares'])
list_of_all_daycares = list(DayCare.objects.all())
assert list_of_daycares_from_response == list_of_all_daycares
Comment on lines +79 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to the scope of this PR, right?



@pytest.mark.django_db
class TestNavbarView:
def test_navbar_getting_dog_owner_nickname_and_profile_picture(self, client, create_dog_owner_user):
client.force_login(user=create_dog_owner_user.user)
response = client.get("/homepage/")
dog_owner_picture_url = create_dog_owner_user.dog_picture_url
assert response.context['navbar_picture_url'] == dog_owner_picture_url
Comment on lines +89 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pass variables substitutions in cases like this, and assert the actual value directly instead:

assert response.context['navbar_picture_url'] ==  create_dog_owner_user.dog_picture_url

Also, it would make the test look nicer if the assert line would be separated with new line from the test setup logic.

dog_owner_name = create_dog_owner_user.__str__()
assert response.context['navbar_name'] == dog_owner_name

def test_navbar_getting_daycare_nickname_and_profile_picture(self, client, create_daycare_user):
client.force_login(user=create_daycare_user.user)
response = client.get("/homepage/")
daycare_name = create_daycare_user.name
assert response.context['navbar_name'] == daycare_name
daycare_primary_image_url = create_daycare_user.get_daycare_primary_image_url()
assert response.context['navbar_picture_url'] == daycare_primary_image_url
5 changes: 3 additions & 2 deletions static/CSS/dog_owner_homepage.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
.row
{
height: 30rem;
width: 120rem;
margin-left: 200px;
width: auto;
margin-left: auto;
margin-right: auto;
}

.card
Expand Down
29 changes: 29 additions & 0 deletions static/CSS/navbar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
header ul li {
font-size: 1.1rem;
}

.navbar-profile-img
{
border-radius: 50%;
height: 2.5rem;
width: 2.5rem;
}

#welcomeMsg {
padding-left:10px;
padding-bottom:0;
margin-top:5px;
color:#FFF;
float:left;
}

#logoutHref {
background-color:#1e1e1e;
border-radius:12px;
color:#E19512;
font-weight:bold;
float:right;
padding: 5px 10px 5px 10px;
font-size:1rem;
margin:1px 0 0 20px;
}
Loading