# -*- coding: utf-8 -*-

import logging

from tastypie_mongoengine.resources import MongoEngineResource
from tastypie_mongoengine.fields import ReferencedListField, ReferenceField

from tastypie.resources import ALL
from tastypie.fields import CharField

from .models.nonrel import Subscription, Article, Read
from ..base.api import (UserResource,
                        common_authentication,
                        UserObjectsOnlyAuthorization, )

LOGGER = logging.getLogger(__name__)


class SubscriptionResource(MongoEngineResource):
    class Meta:
        queryset = Subscription.objects.all()

        # Ember-data expect the following 2 directives
        always_return_data = True
        allowed_methods    = ('get', 'post', 'put', 'delete')

        # These are specific to 1flow functionnals.
        authentication     = common_authentication
        authorization      = UserObjectsOnlyAuthorization()


class AuthorResource(MongoEngineResource):

    class Meta:
        queryset = Author.objects.all()

        # Ember-data expect the following 2 directives
        always_return_data = True
        allowed_methods    = ('get', 'post', 'put', 'delete')
        collection_name    = 'objects'
        resource_name      = 'author'
        filtering          = {'id': ALL, }
        ordering           = ALL
        # These are specific to 1flow functionnals.
        authentication     = common_authentication
        #authorization      = UserObjectsOnlyAuthorization()


class ArticleResource(MongoEngineResource):

    author_ids = ReferencedListField(AuthorResource, 'authors', null=True)

    # Gosh. "url" is reserved in EmberJS… We need to map our
    # internal `Article.url` to Ember's `Article.public_url`.
    public_url = CharField(attribute='url', readonly=True)

    class Meta:
        queryset = Article.objects.all()

        # Ember-data expect the following 2 directives
        always_return_data = True
        allowed_methods    = ('get', 'post', 'put', 'delete', )
        collection_name    = 'objects'
        resource_name      = 'article'
        filtering          = {'id': ALL, }
        ordering           = ALL
        excludes           = ('pages_urls', 'authors', 'url', )

        # These are specific to 1flow functionnals.
        authentication     = common_authentication
        #authorization      = UserObjectsOnlyAuthorization()


class ReadResource(MongoEngineResource):
    article_id = ReferenceField(ArticleResource, 'article')
    user_id    = ReferenceField(UserResource, 'user')

    class Meta:
        queryset = Read.objects.all()

        # Ember-data expect the following 2 directives
        always_return_data = True
        allowed_methods    = ('get', 'post', 'put', 'delete')
        collection_name    = 'objects'
        resource_name      = 'read'
        filtering          = {'id': ALL, }
        ordering           = ALL
        excludes           = ('article', 'user', )

        # These are specific to 1flow functionnals.
        authentication     = common_authentication
        authorization      = UserObjectsOnlyAuthorization()


__all__ = ('SubscriptionResource',
           'ReadResource', 'ArticleResource',
           'AuthorResource', )

@@ -288,16 +288,22 @@
 ription,
+ Feed,
  Article
@@ -308,16 +308,64 @@
 le, Read
+, Author%0A%0Afrom ..base.utils.dateutils import now
 %0Afrom ..
@@ -548,50 +548,516 @@
 ass 
-SubscriptionResource(MongoEngineResource):
+FeedResource(MongoEngineResource):%0A%0A    class Meta:%0A        queryset = Feed.objects.all()%0A%0A        # Ember-data expect the following 2 directives%0A        always_return_data = True%0A        allowed_methods    = ('get', 'post', 'put', 'delete')%0A%0A        # These are specific to 1flow functionnals.%0A        authentication     = common_authentication%0A        authorization      = UserObjectsOnlyAuthorization()%0A%0A%0Aclass SubscriptionResource(MongoEngineResource):%0A    feed_id = ReferenceField(FeedResource, 'feed')%0A
 %0A   

Add the feed resource.