Added status and current_total data to Campaign resource in the API.
Added an API help page to describe how to use the API and which shows a user's api_key and sample calls and link to campaign widgets. Added facility to redirect back to current URL after sign in.pull/1/head
parent
7212a31257
commit
a2b622b061
|
@ -13,11 +13,8 @@ from tastypie.authentication import ApiKeyAuthentication, Authentication
|
|||
|
||||
from regluit.core import models
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
class UserResource(ModelResource):
|
||||
class Meta:
|
||||
authentication = ApiKeyAuthentication()
|
||||
|
@ -61,9 +58,19 @@ class CampaignResource(ModelResource):
|
|||
|
||||
for o in data['objects']:
|
||||
o.data['in_wishlist'] = o.obj.id in wishlist_work_ids
|
||||
# there's probably a better place up the chain (where the Campaign objects are directly available) to grab the status
|
||||
c = models.Campaign.objects.get(id=o.data["id"])
|
||||
o.data['status'] = c.status
|
||||
o.data['current_total'] = c.current_total
|
||||
|
||||
# TODO: add pledging information
|
||||
return data
|
||||
|
||||
def alter_detail_data_to_serialize(self, request, obj):
|
||||
c = models.Campaign.objects.get(id=obj.data["id"])
|
||||
obj.data['status'] = c.status
|
||||
obj.data['current_total'] = c.current_total
|
||||
return obj
|
||||
|
||||
class Meta:
|
||||
authentication = ApiKeyAuthentication()
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
{% extends "base.html" %}
|
||||
{% block extra_head %}
|
||||
<style type="text/css">
|
||||
.undefined {text-decoration:underline;}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block title %}API Help{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>API Help</h1>
|
||||
<p>Some of the data from Unglue It is avaiable via a JSON API. You will need a key and username to be able to use the API.
|
||||
</p>
|
||||
{% if user.is_authenticated %}
|
||||
<p> Welcome {{user.username}}. Your API key is <span style="font-weight:bold">{{api_key}}</span>.</p>
|
||||
{% else %}
|
||||
<p> Please <a href="{% url auth_login %}?next={% firstof request.path '/' %}"><span>sign in</span></a> first.</p>
|
||||
{% endif %}
|
||||
|
||||
<h2>Basic API info</h2>
|
||||
|
||||
<a href="/api/v1/?format=json">Available Resources (JSON)</a>
|
||||
|
||||
{% if user.is_authenticated %}
|
||||
<h2>Campaign info</h2>
|
||||
<p>JSON to get data on all campaigns</p>
|
||||
<a href="/api/v1/campaign/?format=json&api_key={{api_key}}&username={{user.username}}">{{base_url}}/api/v1/campaign/?format=json&api_key={api_key}&username={username}</a>
|
||||
{% endif %}
|
||||
|
||||
<h2>Campaign Widgets</h2>
|
||||
<p>You don't need a key to embed campaign (HTML) widgets. </p>
|
||||
{% if campaign %}
|
||||
Here's a sample widget for the book <span style="font-style: italic">{{campaign.name}}</span> with ISBN {{campaign_isbn}}: <a href="{% url widget campaign_isbn %}">{{base_url}}{% url widget campaign_isbn %}</a>
|
||||
{% else %}
|
||||
<p>Since there are currently no campaigns, there are no corresponding widgets.</p>
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
|
@ -61,9 +61,6 @@
|
|||
<p class="classname">No work corresponding to ISBN {{isbn}} available</p>
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.conf.urls.defaults import *
|
||||
from tastypie.api import Api
|
||||
from regluit.api.views import ApiHelpView
|
||||
|
||||
from regluit.api import resources
|
||||
|
||||
|
@ -13,6 +14,7 @@ v1_api.register(resources.SubjectResource())
|
|||
v1_api.register(resources.WishlistResource())
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^help$', ApiHelpView.as_view(), name="api_help"),
|
||||
url(r'^widget/(?P<isbn>\w+)/$','regluit.api.views.widget', name="widget"),
|
||||
(r'^', include(v1_api.urls)),
|
||||
)
|
||||
|
|
31
api/views.py
31
api/views.py
|
@ -3,8 +3,10 @@ from django.shortcuts import render_to_response, get_object_or_404
|
|||
from django.contrib import auth
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
from django.views.generic.base import TemplateView
|
||||
|
||||
from regluit.core import models
|
||||
from tastypie.models import ApiKey
|
||||
|
||||
def isbn(request,isbn):
|
||||
|
||||
|
@ -77,4 +79,31 @@ def widget(request,isbn):
|
|||
'logged_in_username':logged_in_username},
|
||||
context_instance=RequestContext(request)
|
||||
)
|
||||
|
||||
|
||||
class ApiHelpView(TemplateView):
|
||||
template_name = "api_help.html"
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(ApiHelpView, self).get_context_data(**kwargs)
|
||||
|
||||
# base_url passed in to allow us to write absolute URLs for this site
|
||||
base_url = self.request.build_absolute_uri("/")[:-1]
|
||||
context["base_url"] = base_url
|
||||
|
||||
# if user is logged in, pass in the user's API key
|
||||
u = auth.get_user(self.request)
|
||||
if u.is_authenticated():
|
||||
api_key = ApiKey.objects.filter(user=u)[0].key
|
||||
context['api_key'] = api_key
|
||||
|
||||
# pass in a sample Campaign whose widget can be displayed
|
||||
campaigns = models.Campaign.objects.all()
|
||||
if len(campaigns):
|
||||
c = campaigns[0]
|
||||
if c.work.editions.all()[0].isbn_10 is not None:
|
||||
isbn = c.work.editions.all()[0].isbn_10
|
||||
else:
|
||||
isbn = c.work.editions.all()[0].isbn_13
|
||||
context["campaign"] = campaigns[0]
|
||||
context["campaign_isbn"] = isbn
|
||||
|
||||
return context
|
||||
|
|
|
@ -38,10 +38,10 @@
|
|||
<li class="first"><a href="/supporter/{{user.username}}"><span id="welcome">Welcome, {{ user.username }}</span></a></li>
|
||||
<li><a href="{% url auth_logout %}"><span>Sign Out</span></a></li>
|
||||
{% else %}
|
||||
<li class="first"><a href="{% url auth_login %}"><span>Sign In</span></a></li>
|
||||
<li class="first"><a href="{% url auth_login %}?next={% firstof request.path '/' %}"><span>Sign In</span></a></li>
|
||||
{% endif %}
|
||||
<li><a href="#"><span>Tour</span></a></li>
|
||||
<li><a href="#"><span>FAQ’s</span></a></li>
|
||||
<li><a href="#"><span>FAQs</span></a></li>
|
||||
<li><a href="#"><span>Help</span></a></li>
|
||||
{% if not user.is_authenticated %}
|
||||
<li class="last"><a href="{% url registration_register %}"><span>sign up</span></a></li>
|
||||
|
@ -60,6 +60,7 @@
|
|||
<p>
|
||||
<a href="{{privacyurl}}"><span>Privacy</span></a>
|
||||
<a href="{{rhtoolsurl}}"><span>Rightsholder tools</span></a>
|
||||
<a href="{% url api_help %}"<span>API</span></a>
|
||||
{% if user.is_authenticated %}
|
||||
<a href="{{editurl}}"><span>Settings</span></a>
|
||||
{% endif %}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
<br>
|
||||
<br>
|
||||
<h3>Or, login with:</h3>
|
||||
<a href="/socialauth/login/google"><img src="{{ STATIC_URL }}/images/auth/google_64.png"</a>
|
||||
<a href="/socialauth/login/google?next={% firstof request.GET.next '/' %}"><img src="{{ STATIC_URL }}/images/auth/google_64.png"</a>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
|
Loading…
Reference in New Issue