Skip to content

Commit

Permalink
Merge pull request #90 from hiroTochigi/add-multi-cases
Browse files Browse the repository at this point in the history
Website.views.schedule_remainder post multiple cases issues #65
  • Loading branch information
ezrinjaz authored Apr 7, 2021
2 parents 28b35f7 + 96df42e commit 64698ba
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
__pycache__
db.sqlite3
env/
2 changes: 1 addition & 1 deletion courtbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@
os.path.join(BASE_DIR, 'static'),
]

django_heroku.settings(locals())
#django_heroku.settings(locals())
36 changes: 35 additions & 1 deletion website/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,44 @@ <h2>Fill Out Reminder Form:</h2>
<input type="tel" class="form-control" id="add_phone_num" name="add_phone_num" placeholder="Enter Phone Number"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button id="add-case-btn" type="button" onclick="newCaseNumberGenerator()" class="btn btn-primary">Add Case Number</button>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</section>
<script>
let index = 1
let newCaseNumberGenerator = () => {

let addCaseNum = "add_case_num_" + index.toString()

let caseForm = document.getElementById('case-form')
let addCaseBtn = document.getElementById("add-case-btn")

let additionalCaseNumLabel = document.createElement("label");
additionalCaseNumLabel.setAttribute('for', addCaseNum);
additionalCaseNumLabel.innerHTML = "Additional Case Number " + index.toString() ;

let additionalCaseNumSmall = document.createElement("small");
additionalCaseNumSmall.innerHTML = "(ex: CF-1234-5678)" ;
additionalCaseNumLabel.appendChild(additionalCaseNumSmall)

let additionalCaseNumField = document.createElement("input");
additionalCaseNumField.setAttribute('type',"text");
additionalCaseNumField.setAttribute('name', addCaseNum);
additionalCaseNumField.setAttribute('id', addCaseNum);
additionalCaseNumField.setAttribute('class', "form-control");
additionalCaseNumField.setAttribute('placeholder', "Enter case number");

let additionalCaseNumDiv = document.createElement("div");
additionalCaseNumDiv.setAttribute('class',"form-group");
additionalCaseNumDiv.appendChild(additionalCaseNumLabel)
additionalCaseNumDiv.appendChild(additionalCaseNumField)

index += 1
caseForm.insertBefore(additionalCaseNumDiv, addCaseBtn)
}
</script>
<!--FAQs-->
<section id="faq" class="CBLtBgClr">
<div class="container">
Expand Down
12 changes: 7 additions & 5 deletions website/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_form_data_view_passed(self):
"phone_num": 918-555-5555,
"add_phone_num": 918-111-1111
}
resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)

#resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)
resp = self.client.post("/schedule_reminders", data=data, follow=True)
self.assertIn(str.encode("Arraignment for case CF-2020-1648 has already passed"), resp.content)

def test_form_data_view_not_found(self):
Expand All @@ -27,19 +27,21 @@ def test_form_data_view_not_found(self):
"phone_num": 918-555-5555,
"add_phone_num": 918-111-1111
}
resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)
#resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)
resp = self.client.post("/schedule_reminders", data=data, follow=True)

self.assertIn(str.encode("Unable to find arraignment event with the following year 2020, county Tulsa, case number 1000000000"), resp.content)

def test_form_data_view_scheduled(self):
data = {
"case_num": "CF-2020-2803",
"case_num": "SC-2020-11082",
"year": 2020,
"county": "Tulsa",
"phone_num": 918-555-5555,
"add_phone_num": 918-111-1111
}
resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)
#resp = self.client.post("https://courtbot-python.herokuapp.com/form_data", data=data, follow=True)
resp = self.client.post("/schedule_reminders", data=data, follow=True)

self.assertIn(str.encode("Reminder scheduled"), resp.content)

2 changes: 1 addition & 1 deletion website/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

urlpatterns = [
path('', views.index, name='index'),
path('schedule_reminders', views.schedule_reminders, name='schedule_reminders')
path('schedule_reminders', views.schedule_reminders, name='schedule_reminders'),
]
50 changes: 26 additions & 24 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ def index(request):
# Render the HTML template index.html with the data in the context variable
return render(request, 'index.html')

def check_valid_case(request):
def check_valid_case(case_num, year, county):
# Process form data and requests arraignment data form api/case
case_num = request.POST['case_num']
year = request.POST['year']
county = request.POST['county']
phone_num = request.POST['phone_num']
add_num = request.POST['add_phone_num']
resp = requests.get(
#f"http://127.0.0.1:8000/api/case?year={year}&county={county}&case_num={case_num}"
f"https://courtbot-python.herokuapp.com/api/case?year={year}&county={county}&case_num={case_num}"
)
resp_json = json.loads(resp.content)
Expand All @@ -36,6 +32,7 @@ def check_valid_case(request):


def set_case_reminder(arraignment_datetime, case_num, phone_num):
#reminder_request = requests.post('http://127.0.0.1:8000/api/reminders', {
reminder_request = requests.post('https://courtbot-python.herokuapp.com/api/reminders', {
"arraignment_datetime": arraignment_datetime,
"case_num": case_num,
Expand All @@ -52,25 +49,30 @@ def set_case_reminder(arraignment_datetime, case_num, phone_num):
def schedule_reminders(request):
# If valid case and arraignment time, posts reminder data to api/reminder
# Includes option for extra phone number for additional recipient
case_num = request.POST['case_num']
case_num_list = [
value for key, value in request.POST.items()
if key.find("case_num") > -1 and value
]
year = request.POST['year']
county = request.POST['county']
phone_num = request.POST['phone_num']
add_num = request.POST.get('add_phone_num', None)

valid_case_message, arraignment_datetime = check_valid_case(request)
if not arraignment_datetime:
messages.error(request, valid_case_message)
faq_message = (
f'Please check the case for further information using steps provided at http://court.bot/#faq'
)
messages.error(request, faq_message)
else:
reminder_set, reminder_message = set_case_reminder(arraignment_datetime, case_num, phone_num)
# messages.error(request, message)
messages.info(request, reminder_message)
if not reminder_set:
return redirect('/#form')
if add_num:
_, another_reminder_message = set_case_reminder(arraignment_datetime, case_num, add_num)
messages.info(request, another_reminder_message)
for i, case_num in enumerate(case_num_list):
valid_case_message, arraignment_datetime = check_valid_case(case_num, year, county)
if not arraignment_datetime:
messages.error(request, valid_case_message)
faq_message = (
f'Please check the case for further information using steps provided at http://court.bot/#faq'
)
messages.error(request, faq_message)
else:
reminder_set, reminder_message = set_case_reminder(arraignment_datetime, case_num, phone_num)
# messages.error(request, message)
messages.info(request, reminder_message)
if len(case_num_list)-1 == i and not reminder_set:
return redirect('/#form')
if add_num:
_, another_reminder_message = set_case_reminder(arraignment_datetime, case_num, add_num)
messages.info(request, another_reminder_message)
return redirect('/#form')

0 comments on commit 64698ba

Please sign in to comment.