Skip to content

Commit

Permalink
Merge pull request #853 from scieloorg/beta
Browse files Browse the repository at this point in the history
Incorporação de códigos estáveis
  • Loading branch information
gustavofonseca committed Jun 23, 2014
2 parents 42d1ca3 + 97a8466 commit e158eca
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
25 changes: 22 additions & 3 deletions scielomanager/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.db.models import Q
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from tastypie.resources import ModelResource, Resource
from tastypie import fields
from tastypie.contrib.contenttypes.fields import GenericForeignKeyField
Expand All @@ -26,6 +26,8 @@
Article,
)

from scielomanager.utils import usercontext

from articletrack.models import (
Checkin,
Notice,
Expand All @@ -36,6 +38,15 @@

logger = logging.getLogger(__name__)


def current_user_active_collection():
return usercontext.get_finder().get_current_user_active_collection()


def current_user_collections():
return usercontext.get_finder().get_current_user_collections()


class ApiKeyAuthMeta:
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
Expand Down Expand Up @@ -263,11 +274,19 @@ def dehydrate_collections(self, bundle):
return ''

def dehydrate_pub_status(self, bundle):
col = bundle.obj.collections.get()
try:
col = bundle.obj.collections.get()
except MultipleObjectsReturned:
col = current_user_active_collection()

return bundle.obj.membership_info(col, 'status')

def dehydrate_pub_status_reason(self, bundle):
col = bundle.obj.collections.get()
try:
col = bundle.obj.collections.get()
except MultipleObjectsReturned:
col = current_user_active_collection()

return bundle.obj.membership_info(col, 'reason')


Expand Down
33 changes: 33 additions & 0 deletions scielomanager/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,39 @@ def test_filter_eletronic_issn(self):
self.assertEqual(len(json.loads(response.content)['objects']), 1)
self.assertEqual(json.loads(response.content)['objects'][0]['eletronic_issn'], '1234-1234')

def test_dehydrate_pub_status_with_one_collections(self):
col = modelfactories.CollectionFactory()

col.add_user(self.user)

journal = modelfactories.JournalFactory.create()
journal.join(col, self.user)

response = self.app.get('/api/v1/journals/',
extra_environ=self.extra_environ).json

self.assertEqual(response['objects'][0]['pub_status'], u'inprogress')

def test_dehydrate_pub_status_with_multiple_collections(self):
col = modelfactories.CollectionFactory()
col2 = modelfactories.CollectionFactory()

col.add_user(self.user)
col2.add_user(self.user)

col.make_default_to_user(self.user)

journal = modelfactories.JournalFactory.create()
journal.join(col, self.user)
journal.join(col2, self.user, )

journal.change_status(col, u'current', u'yeah', self.user)

response = self.app.get('/api/v1/journals/',
extra_environ=self.extra_environ).json

self.assertEqual(response['objects'][0]['pub_status'], u'current')


class CollectionRestAPITest(WebTest):

Expand Down

0 comments on commit e158eca

Please sign in to comment.