Пример #1
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        event_id = self.kwargs.get("event_pk", None)
        event = Event.objects.get(id=event_id)
        if event.use_captcha and not verify_captcha(
                serializer.data.get("captcha_response", None)):
            raise ValidationError({"error": "Bad captcha"})

        current_user = request.user

        with transaction.atomic():
            registration = Registration.objects.get_or_create(
                event_id=event_id,
                user_id=current_user.id,
                defaults={
                    "updated_by": current_user,
                    "created_by": current_user
                },
            )[0]
            feedback = serializer.data.get("feedback", "")
            if registration.event.feedback_required and not feedback:
                raise ValidationError({"error": "Feedback is required"})
            registration.status = constants.PENDING_REGISTER
            registration.feedback = feedback
            registration.save(current_user=current_user)
            transaction.on_commit(
                lambda: async_register.delay(registration.id))
        registration.refresh_from_db()
        registration_serializer = RegistrationReadSerializer(
            registration, context={"user": registration.user})
        return Response(data=registration_serializer.data,
                        status=status.HTTP_202_ACCEPTED)
Пример #2
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        event_id = self.kwargs.get('event_pk', None)
        event = Event.objects.get(id=event_id)
        if event.use_captcha and not verify_captcha(serializer.data.get('captcha_response', None)):
            raise ValidationError({'error': 'Bad captcha'})

        user_id = request.user.id

        with transaction.atomic():
            registration = Registration.objects.get_or_create(event_id=event_id, user_id=user_id)[0]
            feedback = serializer.data.get('feedback', '')
            if registration.event.feedback_required and not feedback:
                raise ValidationError({'error': 'Feedback is required'})
            registration.status = constants.PENDING_REGISTER
            registration.feedback = feedback
            registration.save(update_fields=['status', 'feedback'])
            transaction.on_commit(lambda: async_register.delay(registration.id))
        registration_serializer = RegistrationReadSerializer(
            registration, context={'user': registration.user}
        )
        return Response(data=registration_serializer.data, status=status.HTTP_202_ACCEPTED)
Пример #3
0
 def validate_captcha_response(self, captcha_response):
     if not verify_captcha(captcha_response):
         raise exceptions.ValidationError("invalid_captcha")
     return captcha_response