Esempio n. 1
0
 def test_partial_update(self):
     serializer = UserSerializer(self.user,
                                 data={'email': '*****@*****.**'},
                                 partial=True)
     self.assertTrue(serializer.is_valid())
     serializer.save()
     self.assertEqual(self.user.email, '*****@*****.**')
Esempio n. 2
0
 def test_full_update_all_data(self):
     serializer = UserSerializer(self.user,
                                 data={
                                     'email': '*****@*****.**',
                                     'password': '******'
                                 })
     self.assertTrue(serializer.is_valid())
Esempio n. 3
0
 def post(self, request):
     if not User.is_exists(request.data['email']):
         serializer = UserSerializer(data=request.data)
         if serializer.is_valid():
             user = serializer.save()
             user.generate_code_for_user()
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
         raise InvalidInputDataException(str(serializer.errors))
     raise UserExistsException()
Esempio n. 4
0
 def test_serializer_updates_profile(self):
     serializer = UserSerializer(self.user,
                                 data={
                                     'email': '*****@*****.**',
                                     'about': 'my about',
                                     'pic': 'https://my.pic.com/my.png',
                                     'password': '******'
                                 })
     self.assertTrue(serializer.is_valid())
     serializer.save()
     self.assertEqual(self.user.profile.pic, 'https://my.pic.com/my.png')
     self.assertEqual(self.user.profile.about, 'my about')
Esempio n. 5
0
    def test_get_one_user(self):
        url = reverse('user', args=[
            self.t_user.id,
        ])

        response = self.client.get(url)

        serializer = UserSerializer(self.t_user)

        nt.assert_equal(response.data, NO_SUCH_PERM)
Esempio n. 6
0
    def test_get_all_users(self):
        url = reverse('users_list')

        response = self.client.get(url)

        users = StdUser.objects.all()
        serializer = UserSerializer(users, many=True)

        nt.assert_equal(response.data, serializer.data)
        nt.assert_equal(response.status_code, status.HTTP_200_OK)
Esempio n. 7
0
 def test_display_user(self):
     serializer = UserSerializer(self.user)
     expected_data = {
         'username': self.user.username,
         'email': '',
         'pic': '',
         'about': ''
     }
     data = serializer.data
     token = data.pop('token', None)
     self.assertNotEqual(token, None)
     self.assertEqual(expected_data, data)
Esempio n. 8
0
class AttendeeSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Attendee
        fields = (
            'id',
            'event',
            'user',
            'attended',
            'timestamp',
        )
Esempio n. 9
0
class TrainerUserSerializer(serializers.ModelSerializer):
    user = UserSerializer(required=True)

    class Meta:
        model = Trainer
        fields = ('name', 'team', 'home_location', 'current_location', 'user')

    def create(self, validated_data):
        user_data = validated_data.pop('user')
        user = User.objects.create_user(**user_data)
        trainer = Trainer.objects.create(user=user, **validated_data)
        return trainer
Esempio n. 10
0
class PlenarySessionSerializer(serializers.ModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = PlenarySession
        exclude = ['modified', 'created']

    def validate(self, data):
        if not self.instance:
            if not data['enable'] and data['resume']:
                raise serializers.ValidationError(
                    _('Only session enable can have resume'))
        return data
Esempio n. 11
0
class ArticleSerializer(TaggitSerializer, serializers.ModelSerializer):
    created_by = UserSerializer()
    image = ResponsiveImageSerializer()
    tags = TagListSerializerField()
    absolute_url = serializers.CharField(source='get_absolute_url',
                                         read_only=True)

    class Meta(object):
        model = Article
        fields = ('absolute_url', 'authors', 'created_by', 'changed_date',
                  'content', 'created_date', 'featured', 'heading', 'id',
                  'ingress', 'ingress_short', 'published_date', 'slug', 'tags',
                  'video', 'image')
Esempio n. 12
0
    def post(self, request):
        user_serializer = UserSerializer(data=request.data)

        user_serializer.is_valid(raise_exception=True)
        team = user_serializer.get_team()
        user = user_serializer.save()

        bind_user_to_team(user, team)

        return Response({"result": "success"})
Esempio n. 13
0
class CommitteeApplicationSerializer(serializers.ModelSerializer):
    committees = CommitteeSerializer(many=True, source='committeepriority_set')
    applicant = UserSerializer(read_only=True)

    class Meta(object):
        model = CommitteeApplication
        fields = ('name', 'email', 'applicant', 'application_text', 'prioritized', 'committees')

    def create(self, validated_data):
        committees = validated_data.pop('committeepriority_set')
        application = CommitteeApplication(**validated_data)
        try:
            application.clean()
        except DjangoValidationError as django_error:
            raise serializers.ValidationError(django_error.message)
        application.save()

        for committee in committees:
            CommitteePriority.objects.create(committee_application=application, **committee)

        return CommitteeApplication.objects.get(pk=application.pk)
Esempio n. 14
0
class PublicationSerializer(serializers.ModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = Publication
        exclude = ['modified']

    def validate(self, data):
        if not self.instance:
            try:
                if (bool(data['content']) is False and
                    bool(data['tweet_id']) is False and
                        bool(data['image']) is False):
                    raise serializers.ValidationError(
                        _('Content or tweet_id or image are required'))
            except KeyError as e:
                raise serializers.ValidationError(
                    _('{} are required in json object'.format(e)))
            session = data['session']
            if not session.enable:
                raise serializers.ValidationError(
                    _('Only session enable can have publication'))
        return data
Esempio n. 15
0
class EventUserReactionSerializer(serializers.ModelSerializer):
    event_id = serializers.PrimaryKeyRelatedField(source='event',
                                                  write_only=True,
                                                  queryset=Event.objects.all())

    user_id = serializers.PrimaryKeyRelatedField(source='user',
                                                 write_only=True,
                                                 queryset=User.objects.all())
    user = UserSerializer(read_only=True)

    class Meta:
        model = models.EventUserReaction
        fields = ('user_id', 'user', 'interested', 'going', 'event_id')

    def validate(self, attrs):
        interested = attrs.get('interested', False)
        going = attrs.get('going', False)

        reactions_are_equal = interested == going
        if reactions_are_equal:
            raise serializers.ValidationError(
                _('Interested and going cannot be equal!'))

        return attrs
Esempio n. 16
0
 def get(self, request):
     user = UserSerializer(request.user).data
     return Response(data={"user": user})
Esempio n. 17
0
 def test_update_empty_data(self):
     serializer = UserSerializer(self.user, data={}, partial=True)
     self.assertFalse(serializer.is_valid())
 def post(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 19
0
class TrainerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Trainer
        fields = ('name', 'team', 'home_location', 'current_location', 'user')

    user = UserSerializer(required=True)
Esempio n. 20
0
 def test_full_update_partial_data(self):
     serializer = UserSerializer(self.user,
                                 data={'email': '*****@*****.**'})
     self.assertFalse(serializer.is_valid())