-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from jeremiahbrem/form-add-61
added reminder form, tests, and fixed ocsn dockets url
- Loading branch information
Showing
6 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
from django.test import TestCase | ||
from django.test import TestCase, RequestFactory, Client | ||
import json | ||
|
||
class testFormViews(TestCase): | ||
|
||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.client = Client() | ||
|
||
def test_form_data_view_passed(self): | ||
data = { | ||
"case_num": "CF-2020-1648", | ||
"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) | ||
|
||
self.assertIn(str.encode("Arraignment for case CF-2020-1648 has already passed"), resp.content) | ||
|
||
def test_form_data_view_not_found(self): | ||
data = { | ||
"case_num": "1000000000", | ||
"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) | ||
|
||
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", | ||
"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) | ||
|
||
self.assertIn(str.encode("Reminder scheduled"), resp.content) | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,74 @@ | ||
from django.shortcuts import render, redirect | ||
from datetime import datetime, timedelta | ||
import re | ||
|
||
from django.http import JsonResponse, HttpResponse | ||
from django.shortcuts import render | ||
from django.views.decorators.csrf import csrf_exempt | ||
from django.contrib import messages | ||
|
||
import oscn, requests, json | ||
|
||
|
||
from alerts.models import Alert | ||
|
||
def index(request): | ||
# """View function for home page of site.""" | ||
|
||
# Render the HTML template index.html with the data in the context variable | ||
return render(request, 'index.html') | ||
|
||
def check_valid_case(request): | ||
# 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"https://courtbot-python.herokuapp.com/api/case?year={year}&county={county}&case_num={case_num}" | ||
) | ||
resp_json = json.loads(resp.content) | ||
if resp_json.get('error', None): | ||
return resp_json['error'], None | ||
return '', resp_json.get('arraignment_datetime', None) | ||
|
||
|
||
def set_case_reminder(arraignment_datetime, case_num, phone_num): | ||
reminder_request = requests.post('https://courtbot-python.herokuapp.com/api/reminders', { | ||
"arraignment_datetime": arraignment_datetime, | ||
"case_num": case_num, | ||
"phone_num": f"+1-{phone_num}" | ||
}) | ||
resp = json.loads(reminder_request.content) | ||
if resp.get('error', None): | ||
return False, resp['error'] | ||
message = f'Text reminder for case {case_num} occuring on {arraignment_datetime} was scheduled under {phone_num}.' | ||
return True, message | ||
|
||
|
||
@csrf_exempt | ||
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'] | ||
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) | ||
return redirect('/#form') |