Skip to content

Commit

Permalink
Creating orders.html, presenting orders history
Browse files Browse the repository at this point in the history
A page on Hotails, where the user can watch the history of all of his
orders, cancel the orders which havn't started yet, and approve orders
as a daycare.

Signed-off-by: Ofir Matasas <[email protected]>
  • Loading branch information
OfirMatasas committed Apr 29, 2022
1 parent 3a78ab0 commit d36c343
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 16 deletions.
1 change: 1 addition & 0 deletions Hotails/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
path('homepage/', views.homepage, name='homepage'),
path('logout/', views.logout_view, name='logout'),
path('about/', views.about, name='about'),
path('orders/', views.orders_view, name='orders'),
]
2 changes: 1 addition & 1 deletion main/templates/main/base_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<ul class="navbar-nav mr-auto">
{% if request.user.is_authenticated %}
<li class="nav-item"><a class="nav-link text-white" href="">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="/orders">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 %}
Expand Down
45 changes: 32 additions & 13 deletions main/tests.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import pytest

from daycare.models import DayCare
from dogowner.models import DogOwner


@pytest.fixture
def create_dog_owner_user():
return DogOwner.create(email='[email protected]',
username='dogOwnerUser01',
password='password123',
dog_name='dog name',
first_name='test',
last_name='user',
phone_number=1234567890,
dog_race='dog race',
dog_picture_url='https://www.google.com/user1.jpg',
dog_age=4,
dog_weight=2,
dog_gender='M'
)
return DogOwner.create(email='[email protected]', username='dogOwnerUser01', password='password123',
dog_name='dog name', first_name='test', last_name='user', phone_number=1234567890,
dog_race='dog race', dog_picture_url='https://www.google.com/user1.jpg', dog_age=4,
dog_weight=2, dog_gender='M')


@pytest.fixture
def create_daycare_user():
return DayCare.create(email="[email protected]", username="testuser01", password="pass",
name="Puppies", description="This is the first daycare test",
price_per_day=10,
capacity=50, area="Merkaz", city="Tel-Aviv", address="The best street 5")


@pytest.mark.django_db
Expand Down Expand Up @@ -87,3 +88,21 @@ def test_unlogged_user_access_to_homepage(self, client):
response = client.get("/homepage/")
assert response.status_code == 302
assert response['Location'] == '/login/?next=/homepage/'


@pytest.mark.django_db
class TestOrdersView:
def test_unlogged_user_access_to_orders(self, client):
response = client.get("/orders/")
assert response.status_code == 302
assert response['Location'] == '/login/?next=/orders/'

def test_orders_page_is_visible_for_dog_owner_user(self, client, create_dog_owner_user):
client.force_login(user=create_dog_owner_user.user)
response = client.get("/orders/")
assert response.status_code == 200

def test_orders_page_is_visible_for_daycare_user(self, client, create_daycare_user):
client.force_login(user=create_daycare_user.user)
response = client.get("/orders/")
assert response.status_code == 200
6 changes: 6 additions & 0 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.shortcuts import redirect
from django.contrib.auth import logout

import orders.views


def index(request):
if request.user.is_authenticated:
Expand All @@ -23,3 +25,7 @@ def about(request):
def logout_view(request):
logout(request)
return index(request)


def orders_view(request):
return orders.views.orders(request)
21 changes: 21 additions & 0 deletions orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,24 @@ def cancel_order(self):

def get_order_total_price(self):
return self.price_per_day * (self.end_date - self.start_date).days

def get_order_status(self):
if self.status == StatusOptions.Pending:
return "Pending"
elif self.status == StatusOptions.Approved:
return "Approved"
elif self.status == StatusOptions.Canceled:
return "Canceled"
elif self.status == StatusOptions.OnGoing:
return "On going"
return "Finished"

def is_the_order_cancelable(self):
if self.status != StatusOptions.OnGoing or self.status != StatusOptions.Finished:
return True
return False

def is_the_order_approvable(self):
if self.status != StatusOptions.Pending:
return True
return False
54 changes: 54 additions & 0 deletions orders/templates/orders/orders.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% extends "main/base_template.html" %}
{% load static %}

{% block stylesheets %}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'css/orders.css' %}">
{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Book Date</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>
<th scope="col">Actions</th>
</tr>
</thead>

<tbody>
{% for order in orders %}
<tr>
<th scope="row">
{% if user == 'daycare' %}
{{order.dog_owner_id.name }}
{% else %}
{{order.daycare_id.name }}
{% endif %}
</th>
<td>{{order.book_date}}</td>
<td>{{order.start_date}}</td>
<td>{{order.end_date}}</td>
<td>{{order.get_order_status}}</td>
<td>
{% if order.is_the_order_approvable %}
<button type="button" class="btn btn-success">Approve</button>
{% endif %}
{% if order.is_the_order_cancelable %}
<button type="button" class="btn btn-danger">Cancel</button>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
20 changes: 18 additions & 2 deletions orders/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

# Create your views here.
from dogowner.models import DogOwner
from orders.models import Order


@login_required()
def orders(request):
if DogOwner.objects.filter(user=request.user).exists():
user = 'dog_owner'
else:
user = 'daycare'

context = {
'orders': Order.objects.all(),
'user': user,
}
return render(request, 'orders/orders.html', context)
8 changes: 8 additions & 0 deletions static/CSS/orders.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
td, th
{
text-align: center;
}

.container {
padding: 2rem 0;
}

0 comments on commit d36c343

Please sign in to comment.