def list(self, request, *args, **kwargs): mine = self.request.query_params.get('mine') search = self.request.query_params.get('search') if search: data = cache.get('all_tags_search_'+str(search)) if not data: data = serializers.TagSerializer( self.filter_queryset(self.queryset), many=True).data cache.set('all_tags_search_' + str(search), json.dumps(data), 60*60*2) else: data = json.loads(data) return Response(data) if mine: if not self.request.user.is_authenticated: return Response(data="Not authorized", status=status.HTTP_401_UNAUTHORIZED) data = cache.get('all_tags_user_'+str(self.request.user.id)) if not data: data = serializers.TagSerializer( Tag.objects.all(), many=True).data cache.set('all_tags_user_' + str(self.request.user.id), json.dumps(data), 60*60*2) else: data = json.loads(data) return Response(data) data = cache.get('all_tags') if not data: data = serializers.TagSerializer(Tag.objects.all(), many=True).data cache.set('all_tags', json.dumps(data), 60*60*2) else: data = json.loads(data) return Response(data)
def test_retrieve_tags_assigned_to_recipes(self): tag1 = Tag.objects.create(user=self.user, name='Breakfast') tag2 = Tag.objects.create(user=self.user, name='Lunch') recipe = Recipe.objects.create( title='Coriander eggs on toast', time_minutes=10, price=5.00, user=self.user ) recipe.tags.add(tag1) response = self.client.get(TAGS_URL, {'assigned_only': 1}) serializer1 = serializers.TagSerializer(tag1) serializer2 = serializers.TagSerializer(tag2) self.assertIn(serializer1.data, response.data) self.assertNotIn(serializer2.data, response.data)
def test_retrieve_tags_assigned_to_recipes(self): """Test retrieving tags asasigned to particular recipe""" tag1 = models.Tag.objects.create(user=self.user, name='Tag 1') tag2 = models.Tag.objects.create(user=self.user, name='Tag 2') recipe = models.Recipe.objects.create(title='sample recipe', price=3, time=4, user=self.user) recipe.tags.add(tag1) res = self.client.get(TAG_URL, {'assigned_only': 1}) serializer1 = serializers.TagSerializer(tag1) serializer2 = serializers.TagSerializer(tag2) self.assertIn(serializer1.data, res.data) self.assertNotIn(serializer2.data, res.data)
def test_retrieve_tags(self): """ test that authorized user can retrieve his tags successful. """ Tag.objects.create(user=self.user, name='name1') Tag.objects.create(user=self.user, name='name2') tags = Tag.objects.all().order_by('-name') serializer = serializers.TagSerializer(tags, many=True) res = self.client.get(TAGS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_retrieve_tags(self): Tag.objects.create(user=self.user, name='Vegan') Tag.objects.create(user=self.user, name='Desert') response = self.client.get(TAGS_URL) tags = Tag.objects.all().order_by('-name') serializer = serializers.TagSerializer(tags, many=True) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def test_retrieve_tags(self): """Test retrieving tags""" models.Tag.objects.create(user=self.user, name='Vegan') models.Tag.objects.create(user=self.user, name='Dessert') res = self.client.get(TAG_URL) tags = models.Tag.objects.all().order_by("-name") serializer = serializers.TagSerializer(tags, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)