2012-05-11 02:08:46 +00:00
|
|
|
from tastypie.resources import ModelResource
|
|
|
|
|
from tastypie import fields
|
2013-02-21 06:26:06 +00:00
|
|
|
from tastypie.authorization import DjangoAuthorization
|
2012-07-06 20:01:33 +00:00
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2012-05-11 02:08:46 +00:00
|
|
|
|
|
|
|
|
from pinry.pins.models import Pin
|
|
|
|
|
|
|
|
|
|
|
2013-02-20 07:59:45 +00:00
|
|
|
class UserResource(ModelResource):
|
|
|
|
|
class Meta:
|
|
|
|
|
queryset = User.objects.all()
|
|
|
|
|
resource_name = 'user'
|
|
|
|
|
excludes = ['email', 'password', 'is_superuser', 'first_name',
|
|
|
|
|
'last_name', 'is_active', 'is_staff', 'last_login', 'date_joined']
|
|
|
|
|
include_resource_uri = False
|
|
|
|
|
|
|
|
|
|
|
2013-02-21 06:26:06 +00:00
|
|
|
class PinResource(ModelResource):
|
2012-09-28 04:42:13 +00:00
|
|
|
tags = fields.ListField()
|
2013-02-20 07:59:45 +00:00
|
|
|
submitter = fields.ForeignKey(UserResource, 'submitter', full=True)
|
2012-09-28 04:42:13 +00:00
|
|
|
|
2012-05-11 02:08:46 +00:00
|
|
|
class Meta:
|
|
|
|
|
queryset = Pin.objects.all()
|
|
|
|
|
resource_name = 'pin'
|
|
|
|
|
include_resource_uri = False
|
2012-07-16 17:12:59 +02:00
|
|
|
filtering = {
|
|
|
|
|
'published': ['gt'],
|
2013-02-20 07:59:45 +00:00
|
|
|
'submitter': ['exact']
|
2012-07-16 17:12:59 +02:00
|
|
|
}
|
2013-02-21 06:26:06 +00:00
|
|
|
authorization = DjangoAuthorization()
|
2012-05-11 02:08:46 +00:00
|
|
|
|
2012-09-28 04:42:13 +00:00
|
|
|
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(',')
|
2012-09-28 05:28:31 +00:00
|
|
|
|
2012-09-28 04:42:13 +00:00
|
|
|
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)
|