def taste(self, request, *args, **kwargs): data = request.data try: with transaction.atomic(): _level = data.get('level') print("MI LEVEL CTM:", _level) if _level is not None: grades_in_level = Level.objects.filter(id=_level) #gradeJson = serializers.serialize("json", grades_in_level) gradeJson = LevelSerializer(grades_in_level) print("grade serializaer:", gradeJson) return Response({"grades inLevel": gradeJson.data}, status=status.HTTP_200_OK) else: return Response({"detail": "You didnt send all the necessary data"}, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "Somenthing was wrong, its not possible to create grades"}, status=status.HTTP_400_BAD_REQUEST) except User.DoesNotExist: return Response({"detail": "User not found"}, status=status.HTTP_404_NOT_FOUND) except KeyError as e: return Response({"detail": "{} is a required field".format(str(e))}, status=status.HTTP_400_BAD_REQUEST)
def ask(request): user = request.session['user'] kuser, created = KryptosUser.objects.get_or_create(user_id=user) # TODO: Send message to websocket on new user. level = Level.objects.filter(level=kuser.level)[0] serializer = LevelSerializer(level) return Response(serializer.data)
def list(self, request, *args, **kwargs): level_dicts = [] for level in range(constants.LEVEL_MIN, constants.LEVEL_MAX + 1): level_dicts.append(self._serialize_level(level, request)) serializer = LevelSerializer(level_dicts, many=True, context={"request": request}) return Response(serializer.data)
def test_get_levels(self): resp = self.client.get(reverse(GetLevelsView.view_name)) data = resp.data self.assertEquals(resp.status_code, status.HTTP_200_OK) self.assertEquals(len(data), 1) level = self.public_level data = data[0] serializer = LevelSerializer(instance=level) self.assertEquals(data, serializer.data)
def ask(request): user = request.session['user'] kuser, created = CircuimstanceUser.objects.get_or_create(user_id=user) # TODO: Send message to websocket on new user. if created: rdb.add('circuimstance', user, 1) level = Level.objects.filter(level=kuser.level) if len(level) > 0: serializer = LevelSerializer(level[0]) return Response(serializer.data) else: return JsonResponse({'completed': True})
def test_serialize_create_level(self): serializer = LevelSerializer(data={ "level_config": None, "name": "level_one", "is_public": True }) serializer.context['request'] = MagicMock(user=self.default_user) serializer.is_valid(raise_exception=True) new_level = serializer.save() self.assertEquals(new_level.name, "level_one")
def setUpClass(cls): super().setUpClass() cls.default_user = common.create_user() cls.level = Level.objects.create(name='level_one', is_public=True, creator=cls.default_user) cls.level_2 = Level.objects.create(name='level_two', is_public=False, creator=cls.default_user) # userserializer should return only public levels serializer = LevelSerializer(instance=[cls.level], many=True) cls.user_data = { 'email': cls.default_user.email, 'levels': serializer.data, 'id': cls.default_user.id }
def test_serialize_one_level(self): serializer = LevelSerializer(instance=self.level) self.assertEquals(len(serializer.data), len(self.level_data)) for item in serializer.data.items(): self.assertEquals(self.level_data[item[0]], item[1])