mirror of
https://github.com/pinry/pinry.git
synced 2025-11-14 00:55:43 +01:00
A major RESTful API rewrite
Rewritten API to handle creating pins for both urls and previously-uploaded images. Added some tests for it.
This commit is contained in:
@@ -1,73 +1,86 @@
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
from django_images.models import Thumbnail
|
||||
from tastypie.resources import ModelResource
|
||||
from tastypie import fields
|
||||
from tastypie.authorization import DjangoAuthorization
|
||||
from tastypie.resources import ModelResource
|
||||
from django_images.models import Thumbnail
|
||||
|
||||
from pinry.core.models import User
|
||||
|
||||
from pinry.pins.models import Pin
|
||||
from pinry.pins.models import Image, Pin
|
||||
|
||||
|
||||
class UserResource(ModelResource):
|
||||
gravatar = fields.CharField()
|
||||
gravatar = fields.CharField(readonly=True)
|
||||
|
||||
def dehydrate_gravatar(self, bundle):
|
||||
return bundle.obj.gravatar
|
||||
|
||||
class Meta:
|
||||
list_allowed_methods = ['get']
|
||||
queryset = User.objects.all()
|
||||
resource_name = 'user'
|
||||
excludes = ['password', 'is_superuser', 'first_name',
|
||||
'last_name', 'is_active', 'is_staff', 'last_login', 'date_joined']
|
||||
fields = ['username']
|
||||
include_resource_uri = False
|
||||
|
||||
|
||||
def filter_generator_for(size):
|
||||
def wrapped_func(bundle, **kwargs):
|
||||
return Thumbnail.objects.get_or_create_at_size(bundle.obj.pk, size, **kwargs)
|
||||
return wrapped_func
|
||||
|
||||
|
||||
class ThumbnailResource(ModelResource):
|
||||
class Meta:
|
||||
list_allowed_methods = ['get']
|
||||
fields = ['image', 'width', 'height']
|
||||
queryset = Thumbnail.objects.all()
|
||||
resource_name = 'thumbnail'
|
||||
include_resource_uri = False
|
||||
|
||||
|
||||
class ImageResource(ModelResource):
|
||||
standard = fields.ToOneField(ThumbnailResource, full=True,
|
||||
attribute=lambda bundle: filter_generator_for('standard')(bundle))
|
||||
thumbnail = fields.ToOneField(ThumbnailResource, full=True,
|
||||
attribute=lambda bundle: filter_generator_for('thumbnail')(bundle))
|
||||
|
||||
class Meta:
|
||||
fields = ['image', 'width', 'height']
|
||||
include_resource_uri = False
|
||||
resource_name = 'image'
|
||||
queryset = Image.objects.all()
|
||||
authorization = DjangoAuthorization()
|
||||
|
||||
|
||||
class PinResource(ModelResource):
|
||||
images = fields.DictField()
|
||||
submitter = fields.ToOneField(UserResource, 'submitter', full=True)
|
||||
image = fields.ToOneField(ImageResource, 'image', full=True)
|
||||
tags = fields.ListField()
|
||||
submitter = fields.ForeignKey(UserResource, 'submitter', full=True)
|
||||
|
||||
def dehydrate_images(self, bundle):
|
||||
original = bundle.obj.image
|
||||
images = {'original': {
|
||||
'url': original.get_absolute_url(), 'width': original.width, 'height': original.height}
|
||||
}
|
||||
for image in ['standard', 'thumbnail']:
|
||||
obj = Thumbnail.objects.get_or_create_at_size(original.pk, image)
|
||||
images[image] = {
|
||||
'url': obj.get_absolute_url(), 'width': obj.width, 'height': obj.height
|
||||
}
|
||||
return images
|
||||
def hydrate_image(self, bundle):
|
||||
url = bundle.data.get('url', None)
|
||||
if url:
|
||||
image = Image.objects.create_for_url(url)
|
||||
bundle.data['image'] = '/api/v1/image/{}/'.format(image.pk)
|
||||
return bundle
|
||||
|
||||
class Meta:
|
||||
queryset = Pin.objects.all()
|
||||
resource_name = 'pin'
|
||||
include_resource_uri = False
|
||||
filtering = {
|
||||
'published': ['gt'],
|
||||
'submitter': ['exact']
|
||||
}
|
||||
fields = ['submitter', 'tags', 'published', 'description', 'url']
|
||||
authorization = DjangoAuthorization()
|
||||
def dehydrate_tags(self, bundle):
|
||||
return map(str, bundle.obj.tags.all())
|
||||
|
||||
def build_filters(self, filters=None):
|
||||
if filters is None:
|
||||
filters = {}
|
||||
|
||||
orm_filters = super(PinResource, self).build_filters(filters)
|
||||
|
||||
if 'tag' in filters:
|
||||
orm_filters['tags__name__in'] = filters['tag'].split(',')
|
||||
|
||||
return orm_filters
|
||||
|
||||
def dehydrate_tags(self, bundle):
|
||||
return map(str, bundle.obj.tags.all())
|
||||
|
||||
def save_m2m(self, bundle):
|
||||
tags = bundle.data.get('tags', [])
|
||||
bundle.obj.tags.set(*tags)
|
||||
return super(PinResource, self).save_m2m(bundle)
|
||||
|
||||
class Meta:
|
||||
fields = ['id', 'url', 'description']
|
||||
queryset = Pin.objects.all()
|
||||
resource_name = 'pin'
|
||||
include_resource_uri = False
|
||||
authorization = DjangoAuthorization()
|
||||
|
||||
Reference in New Issue
Block a user