Skip to content

Commit

Permalink
Now using tastypie for all our API needs.
Browse files Browse the repository at this point in the history
  • Loading branch information
overshard committed May 11, 2012
1 parent d50e7ce commit 10ffd90
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 30 deletions.
16 changes: 16 additions & 0 deletions pinry/api/api.py
@@ -0,0 +1,16 @@
from tastypie.resources import ModelResource
from tastypie import fields

from pinry.pins.models import Pin


class PinResource(ModelResource):
thumbnail = fields.CharField(readonly=True)

class Meta:
queryset = Pin.objects.all()
resource_name = 'pin'
include_resource_uri = False

def dehydrate_thumbnail(self, bundle):
return Pin.objects.only('image').get(pk=bundle.data['id']).image.url_200x1000
5 changes: 1 addition & 4 deletions pinry/api/tests.py
Expand Up @@ -6,10 +6,7 @@
class RecentPinsTest(TestCase):
def setUp(self):
self.client = Client()
self.url = reverse('api:pins-recent', args=[0])

def test_url(self):
self.assertEqual(self.url, '/api/pins/recent/0/')
self.url = '/api/pin/?format=json'

def test_status_code(self):
response = self.client.get(self.url)
Expand Down
4 changes: 3 additions & 1 deletion pinry/api/urls.py
@@ -1,6 +1,8 @@
from django.conf.urls import patterns, include, url
from .api import PinResource

pin_resource = PinResource()

urlpatterns = patterns('',
url(r'^pins/recent/(?P<page>\d*)/$', 'pinry.api.views.pins_recent', name='pins-recent'),
url(r'', include(pin_resource.urls)),
)
21 changes: 0 additions & 21 deletions pinry/api/views.py

This file was deleted.

9 changes: 5 additions & 4 deletions pinry/core/static/core/js/pinry.js
Expand Up @@ -2,8 +2,8 @@
* Based on Wookmark's endless scroll.
*/
$(window).ready(function () {
var apiURL = '/api/pins/recent/'
var page = 1;
var apiURL = '/api/pin/?format=json&offset='
var page = 0;
var handler = null;
var isLoading = false;

Expand Down Expand Up @@ -40,7 +40,7 @@ $(window).ready(function () {
$('#loader').show();

$.ajax({
url: apiURL+page,
url: apiURL+(page*20),
success: onLoadData
});
};
Expand All @@ -49,6 +49,7 @@ $(window).ready(function () {
* Receives data from the API, creates HTML for images and updates the layout
*/
function onLoadData(data) {
data = data.objects;
isLoading = false;
$('#loader').hide();

Expand All @@ -59,7 +60,7 @@ $(window).ready(function () {
for(; i<length; i++) {
image = data[i];
html += '<div class="pin">';
html += '<a class="fancybox" rel="pins" href="'+image.original+'">';
html += '<a class="fancybox" rel="pins" href="'+image.image+'">';
html += '<img src="'+image.thumbnail+'" width="200" height="'+Math.round(image.height/image.width*200)+'">';
html += '</a>';
html += '<p>'+image.description+'</p>';
Expand Down
3 changes: 3 additions & 0 deletions pinry/pins/models.py
Expand Up @@ -23,3 +23,6 @@ def save(self):
temp_img.flush()
self.image.save(self.url.split('/')[-1], File(temp_img))
super(Pin, self).save()

class Meta:
ordering = ['-id']
1 change: 1 addition & 0 deletions pinry/settings/__init__.py
Expand Up @@ -49,6 +49,7 @@
messages.SUCCESS: 'alert alert-success',
messages.INFO: 'alert alert-info',
}
API_LIMIT_PER_PAGE = 20

INSTALLED_APPS = (
'django.contrib.auth',
Expand Down
1 change: 1 addition & 0 deletions requirements/development.txt
@@ -1,4 +1,5 @@
Django==1.4
South==0.7.4
PIL==1.1.7
django-tastypie==0.9.11
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs
1 change: 1 addition & 0 deletions requirements/jenkins.txt
@@ -1,6 +1,7 @@
Django==1.4
South==0.7.4
PIL==1.1.7
django-tastypie==0.9.11
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs

django-jenkins==0.12.1
Expand Down

0 comments on commit 10ffd90

Please sign in to comment.