Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        '''
        校验注册邀请码是否有效
        '''
        key = request.data.get('key', '')
        if not key:
            raise ValidationError({'key': ['this field is requied']})

        mobile = request.data.get('mobile', '')
        if not mobile:
            raise ValidationError({'mobile': ['this field is requied']})

        invitation = Invitation.parse(key)
        if invitation is None:
            return Response({'key': ['invalid']}, status=status.HTTP_400_BAD_REQUEST)

        if invitation.is_expired:
            return Response({'key': ['expired']}, status=status.HTTP_400_BAD_REQUEST)

        if invitation.invitee.mobile != mobile:
            return Response({'mobile': ['invalid']}, status=status.HTTP_400_BAD_REQUEST)

        user = invitation.invitee
        return Response({
            'token': user.token,
            **UserWithPermSerializer(user).data,
        })
Esempio n. 2
0
    def validate(self, attrs):
        validated_data = super().validate(attrs)
        key = validated_data.pop('key')
        invitation = Invitation.parse(key)
        if not invitation:
            raise ValidationError({'key': ['invalid']})

        user = invitation.invitee
        user_validated = False

        username = validated_data.get('username', '')
        if username and User.valid_objects.filter(username=username).exclude(pk=user.pk).exists():
            raise ValidationError({'username': ['existed']})

        sms_token = validated_data.pop('sms_token', '')
        if sms_token:
            mobile = UserActivateSMSClaimSerializer.check_sms_token(sms_token)['mobile']
            UserActivateSMSClaimSerializer.clear_sms_token(sms_token)
            if mobile != user.mobile:
                raise ValidationError({'sms_token': ['invalid']})
            user_validated = True

        email_token = validated_data.pop('email_token', '')
        if email_token:
            email = UserActivateEmailClaimSerializer.check_email_token(email_token)['email']
            UserActivateEmailClaimSerializer.clear_email_token(email_token)
            if email != user.private_email:
                raise ValidationError({'email_token': ['invalid']})
            user_validated = True

        if not user_validated:
            raise ValidationError({'auth_token': ['must provide "email_token" or "sms_token"']})

        validated_data.update(user=user)
        return validated_data
Esempio n. 3
0
    def test_invite_user(self):
        res = self.client.json_post(reverse('siteapi:invite_user', args=(self.invitee.username, )))
        self.assertEqual(res.status_code, 200)
        self.assertIn('key', res.json())

        key = res.json().get('key')
        self.assertIsNotNone(Invitation.parse(key))

        res = self.client.json_post(reverse('siteapi:invite_user', args=(self.invitee.username, )),
                                    data={
                                        'duration_minutes': 60 * 24 * 10,
                                    })
        self.assertEqual(res.status_code, 200)
        invitation = Invitation.active_objects.order_by('-id').first()
        self.assertEqual(invitation.duration, timedelta(minutes=60 * 24 * 10))
Esempio n. 4
0
    def get(self, request, *args, **kwargs):
        key = request.query_params.get('key', '')
        if not key:
            raise ValidationError({'key': ['this field is requied']})

        invitation = Invitation.parse(key)
        if invitation is None:
            return Response({'key': ['invalid']}, status=status.HTTP_400_BAD_REQUEST)

        if invitation.is_expired:
            return Response({'key': ['expired']}, status=status.HTTP_400_BAD_REQUEST)

        user = invitation.invitee
        return Response({
        # 'token': user.token,
            **UserWithPermSerializer(user).data,
        })
Esempio n. 5
0
    def validate(self, attrs):
        validated_data = super().validate(attrs)
        key = validated_data['key']

        invitation = Invitation.parse(key)
        if invitation is None:
            raise ValidationError({'key': ['invalid']})

        if invitation.is_expired:
            raise ValidationError({'key': ['expired']})

        if not invitation.invitee.private_email:
            raise ValidationError({'key': ['invalid']})

        return {
            'email': invitation.invitee.private_email,
            'key': key,
        }
Esempio n. 6
0
    def validate(self, attrs):
        key = attrs.get('key', '')
        if not key:
            raise ValidationError({'key': ['this field is required']})

        invitation = Invitation.parse(key)
        if invitation is None:
            raise ValidationError({'key': ['invalid']})

        if invitation.is_expired:
            raise ValidationError({'key': ['expired']})

        if not invitation.invitee.mobile:
            raise ValidationError({'key': ['invalid']})

        return {
            'key': key,
            'mobile': invitation.invitee.mobile,
        }