Skip to content

Commit

Permalink
Merge pull request #157 from uchicago-capp-30320/140-survey-update-fo…
Browse files Browse the repository at this point in the history
…rms-to-match-new-models

140 survey update forms to match new models
  • Loading branch information
lisette-solis committed May 21, 2024
2 parents 37ede14 + 9260a48 commit d353270
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.env
*.env
app/staticfiles/
.vscode/

.DS_Store
# Byte-compiled / optimized / DLL files
Expand Down
59 changes: 52 additions & 7 deletions app/route_rangers_api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,33 @@
},
"p3": {
"satisfied": "How satisfied are you with the public transit options \
for this route? Consider 1 to be 'Very Unstisfied' and 5 'Very Satisfied",
for this route? Consider 1 to be 'Very Unstisfied' \
and 5 'Very Satisfied",
"transit_improvement": "How could this public transit route be \
improved?",
"transit_improvement_open": "Other suggestions on how this transit \
route could be improved:",
"another_trip": "Do you have another trip you would like to report?",
},
"p4": {
"switch": """Assuming that a new transit route is built connecting these \
"switch": "Assuming that a new transit route is built connecting these \
stops, what factor would most motivate you to choose to take \
public transit?"""
public transit?",
"another_trip": "Do you have another trip you would like to report?",
},
"p5": {
"another_trip": "Do you have another trip you would like to report?",
},
"p5": {"feedback", "Any other feedback or comments:"},
}

BOOL_CHOICES = ((True, "Yes"), (False, "No"))


class RiderSurvey1(ModelForm):
"""
Survey intro page
"""

class Meta:
model = SurveyUser
fields = ["frequent_transit", "car_owner"]
Expand All @@ -87,6 +98,10 @@ class Meta:


class RiderSurvey2(ModelForm):
"""
Question about trip
"""

class Meta:
model = SurveyResponse
fields = ["trip_frequency", "trip_tod", "trip_time", "modes_of_transit"]
Expand All @@ -99,21 +114,38 @@ class Meta:


class RiderSurvey3(ModelForm):
transit_improvement = forms.MultipleChoiceField(
label=QUESTIONS["p3"]["transit_improvement"], choices=TRANSIT_IMPROVEMENT
"""
Questions for transit riders
"""

another_trip = forms.ChoiceField(
label=QUESTIONS["p3"]["another_trip"],
choices=BOOL_CHOICES,
widget=RadioSelect(attrs={"class": "form-radio"}),
)

class Meta:
model = SurveyResponse
fields = ["satisfied", "transit_improvement"]
fields = ["satisfied", "transit_improvement", "transit_improvement_open"]
labels = {
"satisfied": _(QUESTIONS["p3"]["satisfied"]),
"transit_improvement": _(QUESTIONS["p3"]["transit_improvement"]),
}

widgets = {"satisfied": RadioSelect(attrs={"class": "form-radio"})}


class RiderSurvey4(ModelForm):
"""
Questions for drivers and rider share
"""

another_trip = forms.ChoiceField(
label=QUESTIONS["p4"]["another_trip"],
choices=BOOL_CHOICES,
widget=RadioSelect(attrs={"class": "form-radio"}),
)

class Meta:
model = SurveyResponse
fields = ["switch_to_transit"]
Expand All @@ -122,3 +154,16 @@ class Meta:
}

widgets = {"switch_to_transit": RadioSelect(attrs={"class": "form-radio"})}


# use forms.Forms not ModelForm because not writing to database
class RiderSurvey5(forms.Form):
"""
Check if bikers and walkers have another trip to report.
"""

another_trip = forms.ChoiceField(
label=QUESTIONS["p5"]["another_trip"],
choices=BOOL_CHOICES,
widget=RadioSelect(attrs={"class": "form-radio"}),
)
3 changes: 1 addition & 2 deletions app/route_rangers_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.conf.urls.static import static
from django.contrib import admin


app_name = "app"

urlpatterns = [
Expand All @@ -14,9 +13,9 @@
path("dashboard/<str:city>", views.dashboard, name="dashboard"),
path("responses/<str:city>", views.responses, name="responses"),
path("survey/<str:city>/", views.survey_p1, name="survey"),
path("survey/<str:city>/map", views.survey_p1, name="map"),
path("survey/<str:city>/2", views.survey_p2, name="survey_p2"),
path("survey/<str:city>/3", views.survey_p3, name="survey_p3"),
path("survey/<str:city>/4", views.survey_p4, name="survey_p4"),
path("survey/<str:city>/5", views.survey_p5, name="survey_p5"),
path("survey/<str:city>/thanks", views.thanks, name="thanks"),
]
1 change: 0 additions & 1 deletion app/route_rangers_api/utils/city_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
6: "A few times per year",
}

# TODO make these the same as the modes defined in models?
MODES_OF_TRANSIT = {
1: "Bus",
2: "Train",
Expand Down
151 changes: 113 additions & 38 deletions app/route_rangers_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@

import uuid

from app.route_rangers_api.utils.city_mapping import CITY_CONTEXT, CITIES_CHOICES_SURVEY
from app.route_rangers_api.utils.metric_processing import dashboard_metrics
from route_rangers_api.models import TransitRoute, TransitStation, SurveyResponse

from app.route_rangers_api.utils.city_mapping import (
CITY_CONTEXT,
CITIES_CHOICES_SURVEY,
MODES_OF_TRANSIT,
)
from route_rangers_api.models import (
TransitRoute,
TransitStation,
SurveyResponse,
SurveyUser,
)
from route_rangers_api.forms import (
RiderSurvey1,
RiderSurvey2,
RiderSurvey3,
RiderSurvey4,
RiderSurvey5,
)

from django.contrib.gis.geos import GEOSGeometry, MultiLineString, LineString
Expand Down Expand Up @@ -101,24 +110,24 @@ def dashboard(request, city: str):


def survey_p1(request, city: str):
# url = ""
"""
Survey intro page
"""
# Gen unique user id with uuid
request.session["uuid"] = str(uuid.uuid4())
user_id = request.session["uuid"]
print(f'user_id:{request.session["uuid"]} - page 1')
request.session["route_id"] = 1

if request.method == "POST":
# create new object
# create new SurveyUser object
city_survey = CITIES_CHOICES_SURVEY[city]
survey_answer = SurveyAnswer(user_id=request.session["uuid"], city=city_survey)
survey_answer = SurveyUser(user_id=request.session["uuid"], city=city_survey)
update_survey = RiderSurvey1(request.POST, instance=survey_answer)
# update and save
survey_answer = form.save(instance=survey_answer)

update_survey.save()

print("survey answer", survey_answer)
return redirect(reverse("app:survey_p2", kwargs={"city": city}))
else:

else: # GET
form = RiderSurvey1()

context = get_survey_context(city, form)
Expand All @@ -127,32 +136,35 @@ def survey_p1(request, city: str):


def survey_p2(request, city: str, user_id: str = None):
"""
Question about trip
"""
print(request.method)
user_id = request.session.get("uuid")
print(f'user_id:{request.session["uuid"]} - page 1')
if request.method == "POST":
survey_answer = SurveyAnswer.objects.get(user_id=user_id)
update_survey = RiderSurvey2(request.POST, instance=survey_answer)
update_survey.save()
return redirect(reverse("app:survey_p3", kwargs={"city": city}))
else:
form = RiderSurvey2()

context = get_survey_context(city, form)

return render(request, "survey_p2.html", context)
route_id = request.session.get("route_id")


def survey_p2(request, city: str, user_id: str = None):
print(request.method)
user_id = request.session.get("uuid")
print(f'user_id:{request.session["uuid"]} - page 1')
if request.method == "POST":
survey_answer = SurveyAnswer.objects.get(user_id=user_id)
# check if route already exists
city_survey = CITIES_CHOICES_SURVEY[city]
survey_answer = SurveyResponse(
user_id_id=user_id, city=city_survey, route_id=route_id
)
update_survey = RiderSurvey2(request.POST, instance=survey_answer)
# update and save
update_survey.save()
return redirect(reverse("app:survey_p3", kwargs={"city": city}))
else:

# return selected mode of transit from form
selected_mode_index = update_survey.cleaned_data["modes_of_transit"]
selected_mode = MODES_OF_TRANSIT[selected_mode_index]
print("selected mode: ", selected_mode)
if selected_mode == "Train" or selected_mode == "Bus":
return redirect(reverse("app:survey_p3", kwargs={"city": city}))
elif selected_mode == "Car" or selected_mode == "Rideshare":
return redirect(reverse("app:survey_p4", kwargs={"city": city}))
else:
return redirect(reverse("app:survey_p5", kwargs={"city": city}))

else: # GET
form = RiderSurvey2()

context = get_survey_context(city, form)
Expand All @@ -161,14 +173,32 @@ def survey_p2(request, city: str, user_id: str = None):


def survey_p3(request, city: str):
"""
Questions for transit riders
"""
user_id = request.session.get("uuid")
route_id = request.session.get("route_id")
print(request.method)

if request.method == "POST":
survey_answer = SurveyAnswer.objects.get(user_id=user_id)
survey_answer = SurveyResponse.objects.get(
user_id_id=user_id, route_id=route_id
)
update_survey = RiderSurvey3(request.POST, instance=survey_answer)
update_survey.save()
return redirect(reverse("app:survey_p4", kwargs={"city": city}))
else:

# check if user has another trip to report
another_trip = update_survey.cleaned_data["another_trip"]

# Not recognizing T/F as booleans so using string
if another_trip == "True" and int(route_id) < 3:
route_id += 1
request.session["route_id"] = route_id
return redirect(reverse("app:survey_p2", kwargs={"city": city}))
else:
return redirect(reverse("app:thanks", kwargs={"city": city}))

else: # GET
form = RiderSurvey3()

context = get_survey_context(city, form)
Expand All @@ -177,21 +207,66 @@ def survey_p3(request, city: str):


def survey_p4(request, city: str):
"""
Questions for drivers and rider share
"""
user_id = request.session.get("uuid")
route_id = request.session.get("route_id")
print(request.method)

if request.method == "POST":
survey_answer = SurveyAnswer.objects.get(user_id=user_id)
survey_answer = SurveyResponse.objects.get(
user_id_id=user_id, route_id=route_id
)
update_survey = RiderSurvey4(request.POST, instance=survey_answer)
update_survey.save()
return redirect(reverse("app:thanks", kwargs={"city": city}))
else:

# check if user has another trip to report
another_trip = update_survey.cleaned_data["another_trip"]

if another_trip == "True" and int(route_id) < 3:
route_id += 1
request.session["route_id"] = route_id
return redirect(reverse("app:survey_p2", kwargs={"city": city}))
else:
return redirect(reverse("app:thanks", kwargs={"city": city}))

else: # GET
form = RiderSurvey4()

context = get_survey_context(city, form)

return render(request, "survey_p4.html", context)


def survey_p5(request, city: str):
"""
Check if bikers and walkers have another trip to report.
"""
route_id = request.session.get("route_id")
print(request.method)
if request.method == "POST":
form = RiderSurvey5(request.POST)
form.is_valid()

# check if user has another trip to report
another_trip = form.cleaned_data["another_trip"]

if another_trip == "True" and int(route_id) < 3:
route_id += 1
request.session["route_id"] = route_id
return redirect(reverse("app:survey_p2", kwargs={"city": city}))
else:
return redirect(reverse("app:thanks", kwargs={"city": city}))

else: # GET
form = RiderSurvey5()

context = get_survey_context(city, form)

return render(request, "survey_p4.html", context)


def thanks(request, city: str):
url = "thanks"

Expand Down
Loading

0 comments on commit d353270

Please sign in to comment.