from dataserver.authentication import AnonymousApiKeyAuthentication
from datetime import datetime
from graffiti.api import TaggedItemResource
from projects.api import ProjectResource
from projectsheet.api import ProjectSheetResource

from taggit.models import Tag
from tastypie import fields
from tastypie.authorization import DjangoAuthorization
from tastypie.constants import ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

from .models import MakerScienceProject, MakerScienceResource
from projectsheet.models import ProjectSheet
from projects.models import ProjectTeam


class MakerScienceProjectResource(ModelResource):
    parent = fields.ToOneField(ProjectResource, 'parent', full=True)
    tags = fields.ToManyField(TaggedItemResource, 'tagged_items', full=True, null=True)

    base_projectsheet = fields.ToOneField(ProjectSheetResource, 'parent__projectsheet', null=True, full=True)

    linked_resources = fields.ToManyField('makerscience_catalog.api.MakerScienceResourceResource', 'linked_resources', full=True,null=True)

    class Meta:
        queryset = MakerScienceProject.objects.all()
        allowed_methods = ['get', 'post', 'put', 'patch']
        resource_name = 'makerscience/project'
        authentication = AnonymousApiKeyAuthentication()
        authorization = DjangoAuthorization()
        always_return_data = True
        filtering = {
            'parent' : ALL_WITH_RELATIONS,
            'featured' : ['exact'],
        }

    def obj_create(self, bundle, **kwargs):
        res = ModelResource.obj_create(self, bundle, **kwargs)
        team, _ = ProjectTeam.objects.get_or_create(project=res.obj.parent)
        team.members.add(bundle.request.user.get_profile())
        return res

    def hydrate(self, bundle):
        # bundle.data["tags"] = [Tag.objects.get_or_create(name=tag_name)[0] for tag_name in bundle.data["tags"]]
        bundle.data["modified"] = datetime.now()
        return bundle

class MakerScienceResourceResource(ModelResource):
    parent = fields.ToOneField(ProjectResource, 'parent', full=True)
    tags = fields.ToManyField(TaggedItemResource, 'tagged_items', full=True, null=True)

    base_resourcesheet = fields.ToOneField(ProjectSheetResource, 'parent__projectsheet', null=True, full=True)

    class Meta:
        queryset = MakerScienceResource.objects.all()
        allowed_methods = ['get', 'post', 'put', 'patch']
        resource_name = 'makerscience/resource'
        authentication = AnonymousApiKeyAuthentication()
        authorization = DjangoAuthorization()
        always_return_data = True
        filtering = {
            'parent' : ALL_WITH_RELATIONS,
            'featured' : ['exact'],
        }

    def obj_create(self, bundle, **kwargs):
        res = ModelResource.obj_create(self, bundle, **kwargs)
        team, _ = ProjectTeam.objects.get_or_create(project=res.obj.parent)
        team.members.add(bundle.request.user.get_profile())
        return res

    def hydrate(self, bundle):
        # bundle.data["tags"] = [Tag.objects.get_or_create(name=tag_name)[0] for tag_name in bundle.data["tags"]]
        bundle.data["modified"] = datetime.now()
        return bundle

@@ -1766,122 +1766,8 @@
 e):%0A
-        # bundle.data%5B%22tags%22%5D = %5BTag.objects.get_or_create(name=tag_name)%5B0%5D for tag_name in bundle.data%5B%22tags%22%5D%5D%0A
     
@@ -2878,122 +2878,8 @@
 e):%0A
-        # bundle.data%5B%22tags%22%5D = %5BTag.objects.get_or_create(name=tag_name)%5B0%5D for tag_name in bundle.data%5B%22tags%22%5D%5D%0A
     

Remove useless code