From 0934fde0d96ec1e33c42017b096bd4ad816f7972 Mon Sep 17 00:00:00 2001 From: fabiobatalha Date: Wed, 18 Jun 2014 17:02:04 -0300 Subject: [PATCH 1/2] fix #850 --- scielomanager/api/resources.py | 23 +++++++++++++++++++++-- scielomanager/api/tests.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/scielomanager/api/resources.py b/scielomanager/api/resources.py index e89da33d..33a0fd46 100644 --- a/scielomanager/api/resources.py +++ b/scielomanager/api/resources.py @@ -26,6 +26,8 @@ Article, ) +from scielomanager.utils import usercontext + from articletrack.models import ( Checkin, Notice, @@ -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() @@ -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: + 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: + col = current_user_active_collection() + return bundle.obj.membership_info(col, 'reason') diff --git a/scielomanager/api/tests.py b/scielomanager/api/tests.py index 472c5a5a..0e9b8055 100644 --- a/scielomanager/api/tests.py +++ b/scielomanager/api/tests.py @@ -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): From 068b656c3bf820bab1cf0ad85ef2086c4d17bc5e Mon Sep 17 00:00:00 2001 From: fabiobatalha Date: Wed, 18 Jun 2014 17:42:47 -0300 Subject: [PATCH 2/2] especificando a exception --- scielomanager/api/resources.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scielomanager/api/resources.py b/scielomanager/api/resources.py index 33a0fd46..5216ae31 100644 --- a/scielomanager/api/resources.py +++ b/scielomanager/api/resources.py @@ -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 @@ -276,7 +276,7 @@ def dehydrate_collections(self, bundle): def dehydrate_pub_status(self, bundle): try: col = bundle.obj.collections.get() - except: + except MultipleObjectsReturned: col = current_user_active_collection() return bundle.obj.membership_info(col, 'status') @@ -284,7 +284,7 @@ def dehydrate_pub_status(self, bundle): def dehydrate_pub_status_reason(self, bundle): try: col = bundle.obj.collections.get() - except: + except MultipleObjectsReturned: col = current_user_active_collection() return bundle.obj.membership_info(col, 'reason')