def get(self, request, format=None):

        offset = int(request.query_params.get('offset', 0))
        if offset == 0:
            random_tale = random.random() * tales_models.Tale.objects.count()
            tale_sentences = tales_models.TaleSentence.objects.filter(
                tale__id=random_tale)
            offset = tale_sentences[0].id - 1
        sentences = tales_models.TaleSentence.objects.all()[offset:offset + 10]
        sentences_serialized = tales_serializers.TaleSentenceSerializer(
            data=sentences, many=True)
        sentences_serialized.is_valid()
        print(sentences_serialized.data)

        if request.query_params.get('new_user', False):
            print("New user requested")
            anonymous_user = authentication_models.AnonymousUserProfile(
                anonymous_name='anonymous_{}'.format(
                    str(authentication_models.AnonymousUserProfile.objects.all(
                    ).count())))
            anonymous_user.save()
            print(anonymous_user)
            anonymous_user_sentence_serialized = mobile_api_serializer.AnonymousUserSentenceSerializer(
                data={
                    'anonymous_user': anonymous_user.id,
                    'sentences': sentences_serialized.data
                })
            print(anonymous_user_sentence_serialized.is_valid())

            print(anonymous_user_sentence_serialized.data)
            return Response(anonymous_user_sentence_serialized.data)
        else:

            return Response(sentences_serialized.data)
    def get(self, request, format=None):
        print(tales_models.TaleSentence.objects.count())
        random_tale = random.random() * tales_models.Tale.objects.count()
        tale_sentences = tales_models.TaleSentence.objects.filter(
            tale__id=random_tale)
        random_sentence_id = tale_sentences[0].id
        sentence = tales_models.TaleSentence.objects.get(pk=random_sentence_id)
        serializer = tales_serializers.TaleSentenceSerializer(sentence)

        return Response(serializer.data)
    def get(self, request, format=None):
        print(tales_models.TaleSentence.objects.count())
        print(request.query_params)
        all_tales = tales_models.Tale.objects.all()
        random_tale = int(random.random() *
                          tales_models.Tale.objects.all().count())
        tale_sentences = tales_models.TaleSentence.objects.filter(
            tale=all_tales[random_tale])
        if tale_sentences.count() > 0:
            random_sentence_id = tale_sentences[0].id
        else:
            random_sentence_id = 0
        sentence = tales_models.TaleSentence.objects.get(
            pk=int(request.query_params.get('id', random_sentence_id)) + 1)
        serializer = tales_serializers.TaleSentenceSerializer(sentence)

        return Response(serializer.data)
    def get(self, request, *args, **kwargs):

        tale_id = self.kwargs.get("tale_id", None)
        if tale_id is not None:
            try:
                tale = tales_models.Tale.objects.get(pk=tale_id)
                sentences = tales_models.TaleSentence.objects.filter(tale=tale)
                serializer = tales_serializers.TaleSentenceSerializer(
                    sentences, many=True)
                return Response({
                    'state': 'Success',
                    'error': 0,
                    'sentences': serializer.data
                })
            except tales_models.Tale.DoesNotExist:
                return Response({'state': 'Tale does not exists', 'error': 1})
        else:
            return Response({'state': 'No tale id sended', 'error': 1})
Beispiel #5
0
class AnonymousUserSentenceSerializer(serializers.Serializer):
    anonymous_user = serializers.IntegerField()
    sentences = tales_serializers.TaleSentenceSerializer()