from geonode.base.models import ResourceBase
from tastypie import fields
from tastypie.constants import ALL
from tastypie.resources import ModelResource
from django.core.urlresolvers import reverse


class AllResoucesResource(ModelResource):
    type = fields.CharField(null=False, blank=False)
    urls = fields.DictField(null=False, blank=False)

    class Meta:
        queryset = ResourceBase.objects.distinct()
        fields = ['id', 'title', 'abstract',
                  'thumbnail_url', 'type', 'featured']
        filtering = {
            'id': ALL,
            'title': ALL,
            'abstract': ALL,
            'featured': ALL
        }

    def dehydrate_urls(self, bundle):
        item = bundle.obj
        urls = dict(details=item.detail_url)
        if hasattr(item, 'appinstance'):
            urls["view"] = reverse('%s.view' % item.appinstance.app.name,
                                   args=[str(item.appinstance.id)])
        elif hasattr(item, 'document'):
            urls["download"] = reverse(
                'document_download', None, [str(item.id)])
        elif hasattr(item, 'map'):
            urls["view"] = reverse('map_view', None, [str(item.id)])
        return urls

    def dehydrate_type(self, bundle):
        item = bundle.obj
        if hasattr(item, 'appinstance'):
            return "App"
        elif hasattr(item, 'document'):
            return "Doc"
        elif hasattr(item, 'map'):
            return "Map"
        else:
            return "Layer"

@@ -100,17 +100,37 @@
 port ALL
+, ALL_WITH_RELATIONS
 %0A
-
 from tas
@@ -211,16 +211,59 @@
 reverse%0A
+from geonode.api.api import OwnersResource%0A
 %0A%0Aclass 
@@ -403,16 +403,82 @@
 k=False)
+%0A    owner = fields.ToOneField(OwnersResource, 'owner', full=True)
 %0A%0A    cl
@@ -636,16 +636,35 @@
 eatured'
+, 'owner__username'
 %5D%0A      
@@ -788,18 +788,160 @@
  ALL
-%0A        %7D
+,%0A            'owner': ALL_WITH_RELATIONS%0A        %7D%0A        limit = 100%0A%0A    def dehydrate_owner(self, bundle):%0A        return bundle.obj.owner.username
 %0A%0A  

add owner to all resources api