diff --git a/daycare/models.py b/daycare/models.py index 2678d71..54aa6ea 100644 --- a/daycare/models.py +++ b/daycare/models.py @@ -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) @@ -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) diff --git a/daycare/test_image.py b/daycare/test_image.py index 6058999..742e95f 100644 --- a/daycare/test_image.py +++ b/daycare/test_image.py @@ -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: @@ -20,3 +22,12 @@ 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_when_no_customized_picture_was_found(self, create_daycare_user): + daycare_profile_image = create_daycare_user.get_daycare_primary_image_url() + assert daycare_profile_image == DEFAULT_DAYCARE_PROFILE_URL diff --git a/dogowner/templates/dogowner/dog_owner_homepage.html b/dogowner/templates/dogowner/dog_owner_homepage.html new file mode 100644 index 0000000..8434a05 --- /dev/null +++ b/dogowner/templates/dogowner/dog_owner_homepage.html @@ -0,0 +1,26 @@ +{% extends "main/base_template.html" %} +{% load static %} + +{% block stylesheets %} + +{% endblock %} + +{% block content %} +
+
+ {% for daycare in daycares %} +
+ {{ daycare.name }} image +
+
+
{{ daycare.name }}
+
+

{{ daycare.description | truncatechars:250 }}

+ Daycare Profile +
+
+ {% endfor %} +
+
+ +{% endblock %} \ No newline at end of file diff --git a/dogowner/views.py b/dogowner/views.py index fd0e044..53333ed 100644 --- a/dogowner/views.py +++ b/dogowner/views.py @@ -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) diff --git a/main/tests.py b/main/tests.py index a2731e1..3d3107c 100644 --- a/main/tests.py +++ b/main/tests.py @@ -1,4 +1,5 @@ import pytest +from daycare.models import DayCare @pytest.mark.django_db @@ -64,8 +65,15 @@ def test_root_entrypoint_redirection_logged_daycare_user(self, client, create_da assert response['Location'] == '/homepage/' +@pytest.mark.django_db class TestHomepageView: def test_unlogged_user_access_to_homepage(self, client): response = client.get("/homepage/") assert response.status_code == 302 assert response['Location'] == '/login/?next=/homepage/' + + def test_dog_owner_homepage_is_visible_for_dog_owner(self, client, create_dog_owner_user): + 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()) diff --git a/main/urls.py b/main/urls.py new file mode 100644 index 0000000..a754648 --- /dev/null +++ b/main/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.homepage, name='homepage'), + path('about/', views.about, name='about'), +] diff --git a/main/views.py b/main/views.py index 7592772..842c52d 100644 --- a/main/views.py +++ b/main/views.py @@ -2,8 +2,9 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.contrib.auth import logout -from daycare.models import DayCare from dogowner.models import DogOwner +from daycare.models import DayCare +from dogowner.views import dog_owner_home from daycare.views import daycare_home @@ -16,7 +17,7 @@ def index(request): @login_required() def homepage(request): if DogOwner.objects.filter(user=request.user).exists(): - return render(request, 'main/homepage.html') + return dog_owner_home(request) elif DayCare.objects.filter(user=request.user).exists(): return daycare_home(request) diff --git a/static/CSS/dog_owner_homepage.css b/static/CSS/dog_owner_homepage.css new file mode 100644 index 0000000..7c77f17 --- /dev/null +++ b/static/CSS/dog_owner_homepage.css @@ -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; +} \ No newline at end of file diff --git a/static/images/daycare-default-profile-image.jpeg b/static/images/daycare-default-profile-image.jpeg new file mode 100644 index 0000000..e9dafbb Binary files /dev/null and b/static/images/daycare-default-profile-image.jpeg differ