Skip to content

Commit

Permalink
Merge pull request #200 from gcivil-nyu-org/trip-bug-fix
Browse files Browse the repository at this point in the history
Trip bug fix
  • Loading branch information
kravisankaran committed Dec 9, 2023
2 parents 05172bd + fbaca95 commit bba3f42
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 62 deletions.
Binary file modified .coverage
Binary file not shown.
18 changes: 18 additions & 0 deletions trip/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ def city_present_in_country(city, country):
return True if city_tuple in ITALY_CITIES else False
else:
raise TypeError("Country not supported currently")


def get_emergency_contacts(country):
if country == "India":
return {"Medical": "102", "Fire": "101", "Police": "100, 103"}
elif country == "United Kingdom":
return {"Medical": "112, 999", "Fire": "112, 999", "Police": "112, 999"}
elif country == "United States":
return {"Medical": "911", "Fire": "911", "Police": "911"}
elif country == "Canada":
return {"Medical": "911", "Fire": "911", "Police": "911"}
elif country == "Mexico":
return {"Medical": "065", "Fire": "068", "Police": "060"}
elif country == "Italy":
return {"Medical": "112, 118", "Fire": "112, 115", "Police": "112, 113"}
elif country == "France":
return {"Medical": "112, 15", "Fire": "112, 18", "Police": "112, 17"}
return {"Medical": "NA", "Fire": "NA", "Police": "NA"}
18 changes: 18 additions & 0 deletions trip/templates/trip/detail_trip.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ <h4>Trip to {{ usertrip_instance.trip.destination_city }}, {{ usertrip_instance.
<small><a href="{% url 'trip:view_trips' %}">View All Trips</a></small>
</div>
</div>
<br><br><br>
<div class="col-3">
<h3>Emergency Contact List</h3>
<ul class="list-group">
<li class="list-group-item list-group-item-danger d-flex justify-content-between align-items-center">
Medical
<span class="badge bg-primary rounded-pill">{{ emergency_contacts.Medical }}</span>
</li>
<li class="list-group-item list-group-item-warning d-flex justify-content-between align-items-center">
Fire
<span class="badge bg-primary rounded-pill">{{ emergency_contacts.Fire }}</span>
</li>
<li class="list-group-item list-group-item-secondary d-flex justify-content-between align-items-center">
Police
<span class="badge bg-primary rounded-pill">{{ emergency_contacts.Police }}</span>
</li>
</ul>
</div>
</div>


Expand Down
44 changes: 0 additions & 44 deletions trip/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,44 +0,0 @@
from trip.helpers import city_present_in_country


def test_city_present_in_country(self):
city = ["Bangalore"]
country = ["India"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Chicago"]
country = ["United States"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["London"]
country = ["United Kingdom"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Cancun"]
country = ["Mexico"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Toronto"]
country = ["Canada"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Paris"]
country = ["France"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Florence"]
country = ["Italy"]
x = city_present_in_country(city, country)
self.assertTrue(x)


def test_city_not_present_in_country(self):
city = ["Florence"]
country = ["UAE"]
self.assertRaises(TypeError, lambda: city_present_in_country(city, country))
117 changes: 101 additions & 16 deletions trip/tests.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
# import json
import random
from unittest import mock

from django.contrib.auth.models import User

# from django.db.models import Choices
from django.contrib.messages import get_messages
from django.test import TestCase # noqa

# Create your tests here.
from datetime import datetime, timedelta
import time
from django.test import Client
from django.urls import reverse # noqa

from constants import INDIAN_CITIES, TRAVEL_TYPE

# from constants import TRAVEL_TYPE, INDIAN_CITIES
from constants import INDIAN_CITIES, TRAVEL_TYPE, COUNTRY_CHOICES
from trip.forms import UserTripCreationForm # noqa
from trip.helpers import (
start_date_in_future,
end_date_after_start_date,
city_present_in_country,
get_emergency_contacts,
)

# from trip.models import UserTrip, Trip


from trip.models import Trip


Expand Down Expand Up @@ -157,10 +148,6 @@ def test_create_trip_POST_valid(self, mock_form):
"user": user,
"destination_city": [INDIAN_CITIES[0]],
"destination_country": [("India", "India")],
# "trip": {
# "destination_city": [INDIAN_CITIES[0]],
# "destination_country": [("India", "India")],
# },
}

mock_form.return_value.cleaned_data = user_trip
Expand All @@ -171,6 +158,29 @@ def test_create_trip_POST_valid(self, mock_form):
self.assertTemplateUsed(response=None, template_name="trip/create_trip.html")
self.assertRedirects(response, "/trip/view/")

new_user_trip = {
"start_trip": ed,
"end_trip": (date_end + timedelta(days=1)).date(),
"travel_type": TRAVEL_TYPE[1],
"user": user,
"destination_city": [INDIAN_CITIES[-1]],
"destination_country": [("India", "India")],
}

mock_form.return_value.cleaned_data = new_user_trip
mock_form.return_value.usertrip_data = new_user_trip
response = self.client.post("/trip/create/", data=new_user_trip, follow=True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response=None, template_name="trip/create_trip.html")
message = list(get_messages(response.wsgi_request))[0].message
self.assertEqual(
message,
f"You have a trip to {INDIAN_CITIES[0]}, "
f"{COUNTRY_CHOICES[0]} within the current trip dates,"
f" please update that trip or plan for {INDIAN_CITIES[-1]}, "
f"{COUNTRY_CHOICES[0]} another time!",
)

# This is kind of a placeholder. It's not very useful, but it's 12am
# and I'm tired and I want coverage to be at 80. We need to find a way to
# mock retrieve_none_or_403. I've not been able to find a way to raise
Expand All @@ -181,3 +191,78 @@ def test_detail_trip_GET_catch_all(self):
valid_results = [403, 200, 302]
self.assertIn(response.status_code, valid_results)
self.assertTemplateUsed(response=None, template_name="trip/detail_trip.html")

def test_emergency_contacts(self):
self.assertEqual(
get_emergency_contacts("India"),
{"Medical": "102", "Fire": "101", "Police": "100, 103"},
)
self.assertEqual(
get_emergency_contacts("United Kingdom"),
{"Medical": "112, 999", "Fire": "112, 999", "Police": "112, 999"},
)
self.assertEqual(
get_emergency_contacts("United States"),
{"Medical": "911", "Fire": "911", "Police": "911"},
)
self.assertEqual(
get_emergency_contacts("Canada"),
{"Medical": "911", "Fire": "911", "Police": "911"},
)
self.assertEqual(
get_emergency_contacts("Mexico"),
{"Medical": "065", "Fire": "068", "Police": "060"},
)
self.assertEqual(
get_emergency_contacts("Italy"),
{"Medical": "112, 118", "Fire": "112, 115", "Police": "112, 113"},
)
self.assertEqual(
get_emergency_contacts("France"),
{"Medical": "112, 15", "Fire": "112, 18", "Police": "112, 17"},
)
self.assertEqual(
get_emergency_contacts("Pakistan"),
{"Medical": "NA", "Fire": "NA", "Police": "NA"},
)

def test_city_present_in_country(self):
city = ["Bangalore"]
country = ["India"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Chicago"]
country = ["United States"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["London"]
country = ["United Kingdom"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Cancun"]
country = ["Mexico"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Toronto"]
country = ["Canada"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Paris"]
country = ["France"]
x = city_present_in_country(city, country)
self.assertTrue(x)

city = ["Florence"]
country = ["Italy"]
x = city_present_in_country(city, country)
self.assertTrue(x)

def test_city_not_present_in_country(self):
city = ["Florence"]
country = ["UAE"]
self.assertRaises(TypeError, lambda: city_present_in_country(city, country))
35 changes: 33 additions & 2 deletions trip/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.urls import reverse
from django.db.models import Q
from . import forms
from .models import Trip, UserTrip
from .helpers import get_emergency_contacts
from common import retrieve_none_or_403


Expand All @@ -21,6 +23,29 @@ def create_trip(request):
dest_country_raw = usertrip_data["destination_country"]
dest_country = dest_country_raw[0]

# 1. Find all active trips of the user.
# 2. Exclude those trips which are not overlapping with current trip
overlapping_trips = UserTrip.objects.filter(
user=request.user, is_active=True
).exclude(
Q(end_trip__lt=usertrip_data["start_trip"])
| Q(start_trip__gt=usertrip_data["end_trip"])
)

if overlapping_trips.exists():
utrip = overlapping_trips[0]
message = (
f"You have a trip to {utrip.trip.destination_city}, "
f"{utrip.trip.destination_country} within the current trip dates,"
f" please update that trip or plan for {dest_city}, {dest_country}"
f" another time!"
)
messages.error(request, message)
usertrip_creation_form = forms.UserTripCreationForm()
return render(
request, "trip/create_trip.html", {"form": usertrip_creation_form}
)

trip_instance, _ = Trip.objects.get_or_create(
destination_city=dest_city,
destination_country=dest_country,
Expand Down Expand Up @@ -56,15 +81,21 @@ def view_trips(request):

@login_required
def detail_trip(request, ut_id):
usertrip_instance = retrieve_none_or_403(
usertrip_instance: UserTrip = retrieve_none_or_403(
request, UserTrip, ut_id, "You are not allowed to view this."
)

if usertrip_instance is None:
messages.error(request, "Please select a valid trip")
return redirect(reverse("trip:view_trips"))

context = {"usertrip_instance": usertrip_instance}
emergency_contacts = get_emergency_contacts(
usertrip_instance.trip.destination_country
)
context = {
"usertrip_instance": usertrip_instance,
"emergency_contacts": emergency_contacts,
}

return render(request, "trip/detail_trip.html", context)

Expand Down

0 comments on commit bba3f42

Please sign in to comment.