from datetime import datetime
from django.conf.urls import url
from django.core.urlresolvers import reverse

from haystack.query import SearchQuerySet
from tastypie.utils import trailing_slash
from tastypie.resources import ModelResource
from tastypie.authorization import DjangoAuthorization
from tastypie import fields
from tastypie.constants import ALL_WITH_RELATIONS

from dataserver.authentication import AnonymousApiKeyAuthentication
from accounts.api import ProfileResource
from scout.api import PlaceResource
from graffiti.api import TaggedItemResource

from makerscience_admin.api import SearchableMakerScienceResource
from .models import MakerScienceProfile, MakerScienceProfileTaggedItem

class MakerScienceProfileResource(ModelResource, SearchableMakerScienceResource):
    parent = fields.OneToOneField(ProfileResource, 'parent', full=True)
    location = fields.ToOneField(PlaceResource, 'location', null=True, blank=True, full=True)

    tags = fields.ToManyField('makerscience_profile.api.MakerScienceProfileTaggedItemResource', 'tagged_items', full=True, null=True, readonly=True)

    class Meta:
        queryset = MakerScienceProfile.objects.all()
        allowed_methods = ['get', 'post', 'put', 'patch', 'delete']
        resource_name = 'makerscience/profile'
        authentication = AnonymousApiKeyAuthentication()
        authorization = DjangoAuthorization()
        always_return_data = True
        detail_uri_name = 'slug'
        filtering = {
            'parent' : ALL_WITH_RELATIONS,
            "location" : ['isnull', ],
            'id' : ['exact',],
            'slug' : ['exact',]
        }
        limit = 6

    def dehydrate(self, bundle):
        bundle.data["full_name"] = "%s %s" % (bundle.obj.parent.user.first_name, bundle.obj.parent.user.last_name)
        return bundle

    def hydrate(self, bundle):
        bundle.data["modified"] = datetime.now()
        return bundle

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/search%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('ms_search'), name="api_ms_search"),
        ]

class MakerScienceProfileTaggedItemResource(TaggedItemResource):

    class Meta:
        queryset = MakerScienceProfileTaggedItem.objects.all()
        resource_name = 'makerscience/profiletaggeditem'
        # authentication = AnonymousApiKeyAuthentication()
        # authorization = DjangoAuthorization()
        default_format = "application/json"
        filtering = {
            "tag" : ALL_WITH_RELATIONS,
            "object_id" : ['exact', ],
            'tag_type' : ['exact', ]
        }
        always_return_data = True

@@ -2329,26 +2329,24 @@
 tem'%0A       
- #
  authenticat
@@ -2394,10 +2394,8 @@
     
- #
  aut
@@ -2613,32 +2613,32 @@
 t', %5D%0A        %7D%0A
-
         always_r
@@ -2634,29 +2634,563 @@
    always_return_data = True
+%5C%0A%0A    def prepend_urls(self):%0A        return %5B%0A           url(r%22%5E(?P%3Cresource_name%3E%25s)/(?P%3Ccontent_type%3E%5Cw+?)/(?P%3Cobject_id%3E%5Cd+?)/(?P%3Ctag_type%3E%5Cw+?)%25s$%22 %25 (self._meta.resource_name, trailing_slash()),%0A               self.wrap_view('dispatch_list'),%0A               name=%22api_dispatch_list%22),%0A            url(r%22%5E(?P%3Cresource_name%3E%25s)/(?P%3Ccontent_type%3E%5Cw+?)/(?P%3Cobject_id%3E%5Cd+?)/similars%25s$%22 %25 (self._meta.resource_name, trailing_slash()),%0A               self.wrap_view('get_similars'),%0A               name=%22api_get_similars%22),%0A        %5D
 %0A

Fix missing rights and end-point