mirror of
https://github.com/pinry/pinry.git
synced 2025-11-13 16:45:41 +01:00
Now using tastypie for all our API needs.
This commit is contained in:
16
pinry/api/api.py
Normal file
16
pinry/api/api.py
Normal file
@@ -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
|
||||||
@@ -6,10 +6,7 @@ from django.core.urlresolvers import reverse
|
|||||||
class RecentPinsTest(TestCase):
|
class RecentPinsTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.client = Client()
|
||||||
self.url = reverse('api:pins-recent', args=[0])
|
self.url = '/api/pin/?format=json'
|
||||||
|
|
||||||
def test_url(self):
|
|
||||||
self.assertEqual(self.url, '/api/pins/recent/0/')
|
|
||||||
|
|
||||||
def test_status_code(self):
|
def test_status_code(self):
|
||||||
response = self.client.get(self.url)
|
response = self.client.get(self.url)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
from django.conf.urls import patterns, include, url
|
from django.conf.urls import patterns, include, url
|
||||||
|
from .api import PinResource
|
||||||
|
|
||||||
|
pin_resource = PinResource()
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^pins/recent/(?P<page>\d*)/$', 'pinry.api.views.pins_recent', name='pins-recent'),
|
url(r'', include(pin_resource.urls)),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
from django.utils import simplejson
|
|
||||||
from django.http import HttpResponse
|
|
||||||
|
|
||||||
from pinry.pins.models import Pin
|
|
||||||
|
|
||||||
|
|
||||||
def pins_recent(request, page=1):
|
|
||||||
start_pin = abs(int(page) - 1) * 25
|
|
||||||
end_pin = int(page) * 25
|
|
||||||
|
|
||||||
pins = Pin.objects.order_by('-id')[start_pin:end_pin]
|
|
||||||
recent_pins = []
|
|
||||||
for pin in pins:
|
|
||||||
recent_pins.append({
|
|
||||||
'id': pin.id,
|
|
||||||
'thumbnail': pin.image.url_200x1000,
|
|
||||||
'original': pin.image.url,
|
|
||||||
'description': pin.description,
|
|
||||||
})
|
|
||||||
|
|
||||||
return HttpResponse(simplejson.dumps(recent_pins), mimetype="application/json")
|
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
* Based on Wookmark's endless scroll.
|
* Based on Wookmark's endless scroll.
|
||||||
*/
|
*/
|
||||||
$(window).ready(function () {
|
$(window).ready(function () {
|
||||||
var apiURL = '/api/pins/recent/'
|
var apiURL = '/api/pin/?format=json&offset='
|
||||||
var page = 1;
|
var page = 0;
|
||||||
var handler = null;
|
var handler = null;
|
||||||
var isLoading = false;
|
var isLoading = false;
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ $(window).ready(function () {
|
|||||||
$('#loader').show();
|
$('#loader').show();
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: apiURL+page,
|
url: apiURL+(page*20),
|
||||||
success: onLoadData
|
success: onLoadData
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -49,6 +49,7 @@ $(window).ready(function () {
|
|||||||
* Receives data from the API, creates HTML for images and updates the layout
|
* Receives data from the API, creates HTML for images and updates the layout
|
||||||
*/
|
*/
|
||||||
function onLoadData(data) {
|
function onLoadData(data) {
|
||||||
|
data = data.objects;
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
$('#loader').hide();
|
$('#loader').hide();
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ $(window).ready(function () {
|
|||||||
for(; i<length; i++) {
|
for(; i<length; i++) {
|
||||||
image = data[i];
|
image = data[i];
|
||||||
html += '<div class="pin">';
|
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 += '<img src="'+image.thumbnail+'" width="200" height="'+Math.round(image.height/image.width*200)+'">';
|
||||||
html += '</a>';
|
html += '</a>';
|
||||||
html += '<p>'+image.description+'</p>';
|
html += '<p>'+image.description+'</p>';
|
||||||
|
|||||||
@@ -23,3 +23,6 @@ class Pin(models.Model):
|
|||||||
temp_img.flush()
|
temp_img.flush()
|
||||||
self.image.save(self.url.split('/')[-1], File(temp_img))
|
self.image.save(self.url.split('/')[-1], File(temp_img))
|
||||||
super(Pin, self).save()
|
super(Pin, self).save()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ['-id']
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ MESSAGE_TAGS = {
|
|||||||
messages.SUCCESS: 'alert alert-success',
|
messages.SUCCESS: 'alert alert-success',
|
||||||
messages.INFO: 'alert alert-info',
|
messages.INFO: 'alert alert-info',
|
||||||
}
|
}
|
||||||
|
API_LIMIT_PER_PAGE = 20
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
Django==1.4
|
Django==1.4
|
||||||
South==0.7.4
|
South==0.7.4
|
||||||
PIL==1.1.7
|
PIL==1.1.7
|
||||||
|
django-tastypie==0.9.11
|
||||||
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs
|
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
Django==1.4
|
Django==1.4
|
||||||
South==0.7.4
|
South==0.7.4
|
||||||
PIL==1.1.7
|
PIL==1.1.7
|
||||||
|
django-tastypie==0.9.11
|
||||||
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs
|
git+git://github.com/vannoppen/django-thumbs.git@v1.0.0#egg=django-thumbs
|
||||||
|
|
||||||
django-jenkins==0.12.1
|
django-jenkins==0.12.1
|
||||||
|
|||||||
Reference in New Issue
Block a user