Working on private API
parent
0e5fdb6e2d
commit
2754f48d40
|
@ -52,12 +52,12 @@ USE_L10N = True
|
|||
|
||||
# Absolute filesystem path to the directory that will hold user-uploaded files.
|
||||
# Example: "/home/media/media.lawrence.com/media/"
|
||||
MEDIA_ROOT = os.path.join(BASE_PATH, 'media')
|
||||
MEDIA_ROOT = os.path.join(BASE_PATH, 'media/uploads')
|
||||
|
||||
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
|
||||
# trailing slash.
|
||||
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_URL = '/media/uploads/'
|
||||
|
||||
# Absolute path to the directory static files should be collected to.
|
||||
# Don't put anything in this directory yourself; store your static files
|
||||
|
|
|
@ -2,7 +2,7 @@ from tastypie.authentication import BasicAuthentication
|
|||
from tastypie.authorization import DjangoAuthorization
|
||||
from tastypie.resources import ModelResource
|
||||
from django.contrib.auth.models import User
|
||||
from snipts.models import Comment, Snipt
|
||||
from snipts.models import Snipt
|
||||
from tastypie.cache import SimpleCache
|
||||
from django.db.models import Count
|
||||
from tastypie import fields
|
||||
|
@ -40,23 +40,9 @@ class PublicTagResource(ModelResource):
|
|||
snipt__public=True).count()
|
||||
return bundle
|
||||
|
||||
class PublicCommentResource(ModelResource):
|
||||
user = fields.ForeignKey(PublicUserResource, 'user')
|
||||
snipt = fields.ForeignKey('snipts.api.PublicSniptResource', 'snipt')
|
||||
|
||||
class Meta:
|
||||
queryset = Comment.objects.filter(snipt__public=True).order_by('-created')
|
||||
resource_name = 'comment'
|
||||
fields = ['user', 'snipt', 'comment', 'created', 'modified',]
|
||||
include_absolute_url = True
|
||||
allowed_methods = ['get']
|
||||
cache = SimpleCache()
|
||||
|
||||
class PublicSniptResource(ModelResource):
|
||||
user = fields.ForeignKey(PublicUserResource, 'user', full=True)
|
||||
tags = fields.ToManyField(PublicTagResource, 'tags', related_name='tag', full=True)
|
||||
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
|
||||
related_name='comment', full=True)
|
||||
|
||||
class Meta:
|
||||
queryset = Snipt.objects.filter(public=True).order_by('-created')
|
||||
|
@ -87,14 +73,55 @@ class PublicSniptResource(ModelResource):
|
|||
return orm_filters
|
||||
|
||||
|
||||
class PrivateUserResource(ModelResource):
|
||||
class Meta:
|
||||
queryset = User.objects.all()
|
||||
resource_name = 'user'
|
||||
fields = ['username', 'email',]
|
||||
include_absolute_url = True
|
||||
allowed_methods = ['get']
|
||||
list_allowed_methods = []
|
||||
authentication = BasicAuthentication()
|
||||
authorization = DjangoAuthorization()
|
||||
cache = SimpleCache()
|
||||
|
||||
def apply_authorization_limits(self, request, object_list):
|
||||
return object_list.filter(username=request.user.username)
|
||||
|
||||
class PrivateTagResource(ModelResource):
|
||||
class Meta:
|
||||
queryset = Tag.objects.all()
|
||||
resource_name = 'tag'
|
||||
fields = ['name',]
|
||||
allowed_methods = ['get']
|
||||
authentication = BasicAuthentication()
|
||||
authorization = DjangoAuthorization()
|
||||
cache = SimpleCache()
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
bundle.data['absolute_url'] = '/%s/tag/%s/' % (bundle.request.user.username,
|
||||
bundle.obj.slug)
|
||||
bundle.data['snipts'] = '/api/private/snipt/?tag=%d' % bundle.obj.id
|
||||
|
||||
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(
|
||||
snipt__user=bundle.request.user).count()
|
||||
|
||||
return bundle
|
||||
|
||||
def apply_authorization_limits(self, request, object_list):
|
||||
return object_list.filter(snipt__user=request.user)
|
||||
|
||||
class PrivateSniptResource(ModelResource):
|
||||
user = fields.ForeignKey(PrivateUserResource, 'user', full=True)
|
||||
tags = fields.ToManyField(PrivateTagResource, 'tags', related_name='tag', full=True)
|
||||
|
||||
class Meta:
|
||||
queryset = Snipt.objects.all().order_by('-created')
|
||||
resource_name = 'snipt'
|
||||
fields = ['title', 'description', 'slug', 'lexer', 'code', 'line_count',
|
||||
'key', 'public', 'created', 'modified',]
|
||||
include_absolute_url = True
|
||||
allowed_methods = ['delete', 'get', 'post', 'put',]
|
||||
allowed_methods = ['get',]
|
||||
authentication = BasicAuthentication()
|
||||
authorization = DjangoAuthorization()
|
||||
cache = SimpleCache()
|
||||
|
@ -108,5 +135,18 @@ class PrivateSniptResource(ModelResource):
|
|||
return super(PrivateSniptResource, self).obj_create(bundle, request,
|
||||
user=request.user)
|
||||
|
||||
def build_filters(self, filters=None):
|
||||
if filters is None:
|
||||
filters = {}
|
||||
|
||||
orm_filters = super(PrivateSniptResource, self).build_filters(filters)
|
||||
|
||||
if 'tag' in filters:
|
||||
tag = Tag.objects.get(pk=filters['tag'])
|
||||
tagged_items = tag.taggit_taggeditem_items.all()
|
||||
orm_filters['pk__in'] = [i.object_id for i in tagged_items]
|
||||
|
||||
return orm_filters
|
||||
|
||||
def apply_authorization_limits(self, request, object_list):
|
||||
return object_list.filter(user=request.user)
|
||||
|
|
|
@ -56,15 +56,6 @@
|
|||
<ul class="attrs">
|
||||
<li class="author"><a href="{{ snipt.user.get_absolute_url }}">{{ snipt.user.username }}</a></li>
|
||||
<li class="created">{{ snipt.created|naturalday }}</li>
|
||||
<li class="comments">
|
||||
<a href="{{ snipt.get_absolute_url }}#comments">
|
||||
{% if snipt.comment_set.all|length > 0 %}
|
||||
{{ snipt.comment_set.all|length }} Comment{{ snipt.comment_set.all|pluralize }}
|
||||
{% else %}
|
||||
No comments
|
||||
{% endif %}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</footer>
|
||||
</article>
|
||||
|
|
|
@ -8,13 +8,14 @@ from snipts.api import *
|
|||
admin.autodiscover()
|
||||
|
||||
public_api = Api(api_name='public')
|
||||
public_api.register(PublicUserResource())
|
||||
public_api.register(PublicTagResource())
|
||||
public_api.register(PublicCommentResource())
|
||||
public_api.register(PublicSniptResource())
|
||||
public_api.register(PublicTagResource())
|
||||
public_api.register(PublicUserResource())
|
||||
|
||||
private_api = Api(api_name='private')
|
||||
private_api.register(PrivateSniptResource())
|
||||
private_api.register(PrivateTagResource())
|
||||
private_api.register(PrivateUserResource())
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
|
|
Loading…
Reference in New Issue