Skip to content

Commit

Permalink
Creating dog owner homepage
Browse files Browse the repository at this point in the history
When the dog owner is logged in to Hotails, or when the home is pressed
via navbar, All the daycares in the database will be shown to the dog
owner on its homepage, with the possibility to check their profile.
If the daycare doesn't have any profile picture, a default one will be
presented.

Signed-off-by: Ofir Matasas <[email protected]>
Signed-off-by: tamirmatok <[email protected]>
  • Loading branch information
ErezCohenn authored and OfirMatasas committed Apr 24, 2022
1 parent b0145b1 commit e640936
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
9 changes: 7 additions & 2 deletions daycare/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def create(email, username, password, name, description, price_per_day, capacity
validate_max_length(address, 50, "address")
validate_price(price_per_day)

new_daycare = DayCare(user=User.objects.create_user(email=email, username=username, password=password,
),
new_daycare = DayCare(user=User.objects.create_user(email=email, username=username, password=password),
name=name, description=description, price_per_day=price_per_day,
capacity=capacity, area=area, city=city, address=address)

Expand All @@ -50,6 +49,12 @@ def create(email, username, password, name, description, price_per_day, capacity

return new_daycare

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"


class Image(models.Model):
url = models.CharField(max_length=1000)
Expand Down
12 changes: 12 additions & 0 deletions daycare/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .models import Image
from django.core.exceptions import ValidationError

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


@pytest.mark.django_db()
class TestImageModel:
Expand All @@ -20,3 +22,13 @@ def test_image_creation_with_invalid_image_url(self, create_daycare_user):
with pytest.raises(ValidationError,
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):
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

def test_daycare_has_default_profile_image(self, create_daycare_user):
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
26 changes: 26 additions & 0 deletions dogowner/templates/dogowner/dog_owner_homepage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "main/base_template.html" %}
{% load static %}

{% block stylesheets %}
<link rel="stylesheet" href="{% static 'CSS/dog_owner_homepage.css' %}">
{% endblock %}

{% block content %}
<div class="row row-cols-1 g-3 cards">
<div style="display: flex; flex-direction: row; flex-wrap: wrap">
{% for daycare in daycares %}
<div class="card">
<img src="{{ daycare.get_daycare_primary_image_url }}" alt="{{ daycare.name }} image" class="card-img-top">
<div class="card-body">
<div style="display: flex; flex-direction: row">
<h5 class="card-title">{{ daycare.name }}</h5>
</div>
<p class="card-text">{{ daycare.description | truncatechars:250 }}</p>
<a href="/daycare/{{ daycare.id }}" class="btn btn-primary">Daycare Profile</a>
</div>
</div>
{% endfor %}
</div>
</div>

{% endblock %}
12 changes: 10 additions & 2 deletions dogowner/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from daycare.models import DayCare

# Create your views here.

@login_required()
def dog_owner_home(request):
context = {
'daycares': DayCare.objects.all(),
}
return render(request, 'dogowner/dog_owner_homepage.html', context)
7 changes: 7 additions & 0 deletions main/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from . import views

urlpatterns = [
path('', views.homepage, name='homepage'),
path('about/', views.about, name='about'),
]
5 changes: 4 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.contrib.auth import logout
from dogowner.views import dog_owner_home
from dogowner.models import DogOwner


def index(request):
Expand All @@ -13,7 +15,8 @@ def index(request):

@login_required()
def homepage(request):
return render(request, 'main/homepage.html')
if DogOwner.objects.filter(user=request.user).exists():
return dog_owner_home(request)


def about(request):
Expand Down
41 changes: 41 additions & 0 deletions static/CSS/dog_owner_homepage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.cards
{
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: 50px;
justify-content: center;
align-items: center;
}

.row
{
height: 30rem;
width: 120rem;
margin-left: 200px;
}

.card
{
width: 18.5%;
height: 30rem;
margin: 60px 60px 0 60px;
max-width: 100%;
}

.card-body
{
display: flex;
flex-direction: column;
}

.card-body .btn
{
margin-right: 50%;
margin-top: auto;
}

.card-img-top
{
height: 200px;
}
Binary file added static/images/daycare-default-profile-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e640936

Please sign in to comment.