Skip to content

Commit

Permalink
Merge pull request harvard-lil#17 from anastasia/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
anastasia authored Dec 5, 2016
2 parents b1de1cc + d7764c9 commit 6bce6af
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# The CaseLaw Access Project API
# The Caselaw Access Project API
Please see here http://lil.law.harvard.edu/projects/caselaw-access-project for details.
15 changes: 8 additions & 7 deletions capi_project/case_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,28 @@ def list_jurisdictions(request):
"""
GET a list of all jurisdictions available
"""
jurisdictions = Case.objects.values_list('jurisdiction', flat=True).distinct()
jurisdictions = Case.objects.values_list('jurisdiction', flat=True).distinct().order_by('jurisdiction')
return Response(jurisdictions)

@api_view(http_method_names=['GET'])
@renderer_classes((renderers.BrowsableAPIRenderer,renderers.JSONRenderer,))
def list_volumes(request):
def list_volumes(request, *args, **kwargs):
"""
GET a list of all volumes in the specified jurisdiction
"""
volume = Case.objects.filter(jurisdiction=jurisdiction).values_list('volume', flat=True).distinct()
return Response(volume)
jurisdiction = kwargs.get('jurisdiction')
reporter = kwargs.get('reporter')
volumes = Case.objects.filter(jurisdiction=jurisdiction, reporter=reporter).values_list('volume', flat=True).distinct().order_by('volume')
return Response(volumes)

@api_view(http_method_names=['GET'])
@renderer_classes((renderers.BrowsableAPIRenderer,renderers.JSONRenderer,))
def list_reporters(request, *args, **kwargs):
"""
GET a list of all volumes in the specified jurisdiction
GET a list of all reporters in the specified jurisdiction
"""
jurisdiction = kwargs.get('jurisdiction')
reporters = Case.objects.filter(jurisdiction__iexact=jurisdiction).values_list('reporter', flat=True).distinct()

reporters = Case.objects.filter(jurisdiction__iexact=jurisdiction).values_list('reporter', flat=True).distinct().order_by('reporter')
return Response(reporters)

@api_view(http_method_names=['GET'])
Expand Down
31 changes: 31 additions & 0 deletions capi_project/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% load static %}


<html>
<head>
<title>Caselaw Access Project</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href='//fonts.googleapis.com/css?family=Rajdhani:400,300,500,700' rel='stylesheet' type='text/css'>

</head>
<body>
<div>
<header class="ftl-header">
<div class="ftl-header-content">
<a href="/">
<img class="ftl-logo" src="{% static "img/logo-small-blue.png" %}" alt="CAP Logo"></img>
</a>
<span class="ftl-text"> CASELAW ACCESS PROJECT</span>
<a href="http://librarylab.law.harvard.edu" target="_blank">
<img class="lil-logo" src="{% static "img/LIL-logo.png" %}" alt="Library Innovation Lab"></img>
</a>
</div>
<div class="border-gray"></div>
</header>


{% block content %}{% endblock content %}
</div>
</body>
</html>
19 changes: 11 additions & 8 deletions capi_project/templates/log-in.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{% extends "base.html" %}
{% load rest_framework %}
{% block branding %}
<h3 style="margin: 0 0 20px;">My Site Name</h3>
{% endblock %}
<form class="form-inline" action="/account/login/" method="POST">
{% csrf_token %}
{% render_form serializer template_pack='rest_framework/vertical' %}
<button type="submit" class="btn btn-default">Sign in</button>
</form>
{% block content %}
{% block branding %}
<h3 style="margin: 0 0 20px;">Log in</h3>
{% endblock %}
<form class="form-inline" action="/accounts/view_details/" method="POST">
{% csrf_token %}
{% render_form serializer template_pack='rest_framework/vertical' %}
<button type="submit" class="btn btn-default">Sign in</button>
</form>
{% endblock content %}
27 changes: 14 additions & 13 deletions capi_project/templates/sign-up.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{% load rest_framework %}
{% extends "base.html" %}
{% block content %}
{% load rest_framework %}

<form class="form-inline" action="/account/register_user/" method="POST">
{% csrf_token %}
{% render_form serializer template_pack='rest_framework/inline' %}
<!-- <label>Confirm password</label>
<input type='password' name='password_confirm'/> -->
{% if errors %}
{% for key,value in errors.items %}
{{ key }} : {{ value }}
{% endfor %}
{% endif %}
<button type="submit" class="btn btn-default">Sign up</button>
</form>
<form class="form-inline" action="/accounts/register_user/" method="POST">
{% csrf_token %}
{% render_form serializer template_pack='rest_framework/inline' %}
{% if errors %}
{% for key,value in errors.items %}
{{ key }} : {{ value }}
{% endfor %}
{% endif %}
<button type="submit" class="btn btn-default">Sign up</button>
</form>
{% endblock content %}
Empty file added capi_project/tests/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions capi_project/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.test import TestCase
from capi_project.models import Case

from rest_framework.test import APIRequestFactory
from django.test import Client

import json

class CaseTestCase(TestCase):
def setUp(self):
case = Case.create(caseid=1,firstpage=21,lastpage=25,jurisdiction="Illinois",citation="177 Ill. 2d 21",docketnumber="No. 81291",decisiondate="1997-06-19",court="Illinois Supreme Court",name="TRANS STATES AIRLINES, Appellee, v. PRATT & WHITNEY CANADA, INC., Appellant",court_abbreviation="Ill.",name_abbreviation="Trans States Airlines v. Pratt & Whitney Canada, Inc.",volume="177",reporter="Ill. 2d",)
case = Case.create(caseid=2,firstpage=166,lastpage=184,jurisdiction="New York",citation="229 A.D.2d 313",docketnumber="",decisiondate="1996-07-02",decisiondate_original="1996-07-02",court="New York Supreme Court, Appellate Division",name="Angelo Ramirez, Appellant, v. New York City School Construction Authority, Respondent, et al., Defendant",court_abbreviation="N.Y. App. Div.",name_abbreviation="Ramirez v. New York City School Construction Authority",volume="229",reporter="A.D.2d")

def test_api_urls(self):
c = Client()
response = c.get('/cases/')
assert response.status_code == 200
assert response.accepted_renderer.format != 'json'
response = c.get('/cases/?format=json')
assert response.status_code == 200
assert response.accepted_renderer.format == 'json'
response = c.get('/cases/jurisdictions')
assert response.accepted_renderer.format != 'json'
assert response.status_code == 200
response = c.get('/cases/jurisdictions?format=json')
assert response.status_code == 200
assert response.accepted_renderer.format == 'json'

def test_jurisdictions(self):
c = Client()
response = c.get('/cases/jurisdictions?format=json')
assert response.status_code == 200
assert response.accepted_renderer.format == 'json'
jurisdictions = json.loads(response.content)
assert len(jurisdictions) == 2
assert "New York" in jurisdictions


def test_case(self):
c = Client()
response = c.get('/cases/Illinois/Ill%2E%202d?format=json')
assert response.status_code == 200
assert response.accepted_renderer.format == 'json'
jurisdictions = json.loads(response.content)
assert len(jurisdictions) == 2
assert "New York" in jurisdictions
22 changes: 14 additions & 8 deletions capi_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from django.conf import settings
from django.conf.urls import url, include
from django.shortcuts import render
from rest_framework import routers
Expand All @@ -8,7 +10,7 @@

router = routers.DefaultRouter()
router.register(r'cases', views.CaseViewSet)
router.register(r'account', views.UserViewSet)
router.register(r'accounts', views.UserViewSet)
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='CAP API')
Expand All @@ -22,11 +24,15 @@
url(r'^verify-user/(?P<user_id>[\d+]+)/(?P<activation_nonce>[0-9a-z]+)/?$', views.verify_user),

url(r'^cases/jurisdictions/?$', views.list_jurisdictions, name='list-jurisdictions'),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/?$', views.CaseViewSet.as_view({'get':'list'}), name='list-for-jurisdiction'),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/$', views.CaseViewSet.as_view({'get':'list'}), name='list-for-jurisdiction'),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/reporters/?$', views.list_reporters, name='list-reporters'),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\w+]+)', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\w+]+)/volumes/?$', views.list_volumes),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\w+]+)/(?P<volume>[\d+]+)/?$', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\w+]+)/(?P<volume>[\d+]+)/(?P<first_page>[\d+]+)/?$', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\w+]+)/(?P<volume>[\d+]+)/(?P<first_page>[\d+]+)/(?P<shortname>[\d+]+)?$', views.get_case),
]
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/?$', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/volumes/?$', views.list_volumes),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/(?P<volume>[\d+]+)/?$', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/(?P<volume>[\d+]+)/(?P<first_page>[\d+]+)/?$', views.CaseViewSet.as_view({'get':'list'})),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/(?P<volume>[\d+]+)/(?P<first_page>[\d+]+)/(?P<shortname>[\w\s+]+)?$', views.get_case),
url(r'^cases/(?P<jurisdiction>[\w\s+]+)/(?P<reporter>[\d\s\w.]+)/(?P<volume>[\d+]+)/(?P<shortname>[\w\s+]+)?$', views.get_case),

url(r'^case/(?P<shortname>[\w\s+]+)?$', views.get_case),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
33 changes: 17 additions & 16 deletions capi_project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,23 @@ def register_user(self, request):
else:
return Response({'serializer':serializer, 'errors':serializer.errors}, template_name='sign-up-success.html', status=status.HTTP_400_BAD_REQUEST)

@list_route(methods=['get', 'post'], permission_classes=[AllowAny])
@list_route(methods=['get'], permission_classes=[AllowAny])
def login(self, request):
if request.method == 'GET':
serializer = LoginSerializer()
return Response({'serializer':serializer}, template_name='log-in.html')
serializer = LoginSerializer()
return Response({'serializer':serializer}, template_name='log-in.html')

@list_route(methods=['post'], permission_classes=[AllowAny])
def view_details(self, request):
serializer = LoginSerializer(data=request.data)
if serializer.is_valid():
try:
user = serializer.verify_with_password(email=request.data.get('email'), password=request.data.get('password'))
return Response({'email':user.email, 'api_key':user.get_api_key}, template_name='token.html',)
except Exception as e:
print e
return Response({'errors':e}, template_name='token.html', status=status.HTTP_400_BAD_REQUEST)
else:
serializer = LoginSerializer(data=request.data)
if serializer.is_valid():
try:
user = serializer.verify_with_password(email=request.data.get('email'), password=request.data.get('password'))
return Response({'email':user.email, 'api_key':user.get_api_key}, template_name='token.html',)
except Exception as e:
print e
return Response({'errors':e}, template_name='token.html', status=status.HTTP_400_BAD_REQUEST)
else:
return Response({'errors':serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
return Response({'errors':serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

@api_view(http_method_names=['GET'])
@renderer_classes((renderers.BrowsableAPIRenderer,renderers.JSONRenderer,))
Expand Down Expand Up @@ -114,9 +115,9 @@ def get_token(request):
user = serializer.verify_with_password(email, password)
if user.is_validated:
api_key = user.get_api_key()
data = {'email':user.email,'api_key':api_key}
data = {'email':user.email,'api_key':api_key, 'case_allowance':user.case_allowance}
if request.accepted_renderer.format != 'json' :
return Response(data, template_name='token.html')
return Response(data, template_name='user-account.html')
else:
return JSONResponse(data)
except Exception as e:
Expand Down
15 changes: 14 additions & 1 deletion develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In terminal:
createdb cap
psql
CREATE ROLE capuser WITH LOGIN PASSWORD 'cap';
GRANT ALL PRIVILEGES ON DATABASE cap TO cap;
GRANT ALL PRIVILEGES ON DATABASE cap TO capuser;
ALTER USER capuser WITH PASSWORD 'cap';
ALTER USER capuser CREATEDB;
ALTER ROLE capuser SET client_encoding TO 'utf8';
Expand Down Expand Up @@ -40,3 +40,16 @@ local all postgres md5
See [here] (https://stackoverflow.com/questions/18664074/getting-error-peer-authentication-failed-for-user-postgres-when-trying-to-ge) for an explanation.

You should be all set!


For testing:
```
psql
GRANT ALL PRIVILEGES ON DATABASE test_cap TO capuser;
ALTER DATABASE test_cap OWNER TO capuser;
\q
```

```
python manage.py test capi_project.tests
```
Binary file added static/img/LIL-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/logo-small-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6bce6af

Please sign in to comment.