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, '*****@*****.**')
def test_full_update_all_data(self): serializer = UserSerializer(self.user, data={ 'email': '*****@*****.**', 'password': '******' }) self.assertTrue(serializer.is_valid())
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()
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')
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)
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)
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)
class AttendeeSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Attendee fields = ( 'id', 'event', 'user', 'attended', 'timestamp', )
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
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
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')
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"})
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)
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
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
def get(self, request): user = UserSerializer(request.user).data return Response(data={"user": user})
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)
class TrainerSerializer(serializers.ModelSerializer): class Meta: model = Trainer fields = ('name', 'team', 'home_location', 'current_location', 'user') user = UserSerializer(required=True)
def test_full_update_partial_data(self): serializer = UserSerializer(self.user, data={'email': '*****@*****.**'}) self.assertFalse(serializer.is_valid())