from taggit.models import Tag, TaggedItem
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from tastypie import fields
from django.conf.urls import url
from tastypie.utils.urls import trailing_slash
from django.contrib.contenttypes.models import ContentType
from tastypie.constants import ALL_WITH_RELATIONS

class ContentTypeResource(ModelResource):
    class Meta:
        queryset = ContentType.objects.all()
        resource_name = 'contenttype' 
        allowed_methods = ['get']
        authorization = Authorization()
        always_return_data = True     

class TagResource(ModelResource):
    class Meta:
        queryset = Tag.objects.all()
        resource_name = 'tag' 
        filtering = {
            "name":"exact",
        }
        allowed_methods = ['get']
        authorization = Authorization()
        always_return_data = True 
        
class TaggedItemResource(ModelResource):
    tag = fields.ToOneField(TagResource, 'tag')
    object_type = fields.CharField()
    
    class Meta:
        queryset = TaggedItem.objects.all()
        resource_name = 'taggeditem'
        authorization = Authorization()
        filtering = {
            "object_id":"exact",
            "tag" : ALL_WITH_RELATIONS
        }
    
    def prepend_urls(self):
        return [
           url(r"^(?P<resource_name>%s)/(?P<object_type>\w+?)/(?P<object_id>\w+?)%s$" % (self._meta.resource_name, trailing_slash()),
               self.wrap_view('dispatch_list'), 
               name="api_dispatch_list"),
            url(r"^(?P<resource_name>%s)/(?P<object_type>\w+?)/(?P<object_id>\w+?)/(?P<tag_id>\w+?)%s$" % (self._meta.resource_name, trailing_slash()),
               self.wrap_view('dispatch_detail'), 
               name="api_dispatch_detail"),
        ]
      
    def dispatch_list(self, request, **kwargs):
        if 'object_type' in kwargs :
            kwargs["content_type"] = ContentType.objects.get(model=kwargs.pop('object_type'))
        if 'tag_id' in kwargs:
            kwargs["tag"] = Tag.objects.get(id=kwargs.pop('tag_id'))
        return ModelResource.dispatch_list(self, request, **kwargs)
    
    def dispatch_detail(self, request, **kwargs):
        if 'object_type' in kwargs :
            kwargs["content_type"] = ContentType.objects.get(model=kwargs.pop('object_type'))
        if 'tag_id' in kwargs:
            kwargs["tag"] = Tag.objects.get(id=kwargs.pop('tag_id'))
        return ModelResource.dispatch_detail(self, request, **kwargs)
    
    def dehydrate(self, bundle):
        bundle.data["object_type"] = "%s" % bundle.obj.content_type.model
        return bundle
@@ -128,16 +128,37 @@
 rization
+, DjangoAuthorization
 %0Afrom ta
@@ -366,16 +366,84 @@
 ELATIONS
+%0Afrom dataserver.authentication import AnonymousApiKeyAuthentication
 %0A%0Aclass 
@@ -625,73 +625,136 @@
    a
-uthorization = Authorization()%0A        always_return_data = True 
+lways_return_data = True %0A        authentication = AnonymousApiKeyAuthentication()%0A        authorization = DjangoAuthorization()
     
@@ -980,73 +980,136 @@
    a
-uthorization = Authorization()%0A        always_return_data = True 
+lways_return_data = True %0A        authentication = AnonymousApiKeyAuthentication()%0A        authorization = DjangoAuthorization()
 %0A   
@@ -1240,16 +1240,111 @@
 Field()%0A
+    authentication = AnonymousApiKeyAuthentication()%0A    authorization = DjangoAuthorization()%0A
     %0A   

Set authorization