Exemplo n.º 1
0
    def _public_register(self, request):
        if not request.domain.public_register:
            raise exc.BadRequest(_("Public register is disabled for this domain."))

        serializer = PublicRegisterSerializer(data=request.DATA)
        if not serializer.is_valid():
            raise exc.BadRequest(serializer.errors)

        data = serializer.data

        if User.objects.filter(Q(username=data["username"]) | Q(email=data["email"])).exists():
            raise exc.BadRequest(_("This username or email is already in use."))

        user = User(username=data["username"],
                    first_name=data["first_name"],
                    last_name=data["last_name"],
                    email=data["email"])
        user.set_password(data["password"])
        user.save()

        self._create_domain_member(user)
        #self._send_public_register_email(user)

        response_data = self._create_response(user)
        return Response(response_data, status=status.HTTP_201_CREATED)
Exemplo n.º 2
0
    def _private_register(self, request):
        base_serializer = PrivateGenericRegisterSerializer(data=request.DATA)
        if not base_serializer.is_valid():
            raise exc.BadRequest(base_serializer.errors)

        membership_model = get_model("projects", "Membership")
        try:
            membership = membership_model.objects.get(token=base_serializer.data["token"])
        except membership_model.DoesNotExist as e:
            raise exc.BadRequest(_("Invalid token")) from e

        if base_serializer.data["existing"]:
            serializer = PrivateRegisterExistingSerializer(data=request.DATA)
            if not serializer.is_valid():
                raise exc.BadRequest(serializer.errors)

            user = get_object_or_404(User, username=serializer.data["username"])
            if not user.check_password(serializer.data["password"]):
                raise exc.BadRequest({"password": _("Incorrect password")})

        else:
            serializer = PrivateRegisterSerializer(data=request.DATA)
            if not serializer.is_valid():
                raise exc.BadRequest(serializer.errors)

            data = serializer.data

            if User.objects.filter(Q(username=data["username"]) | Q(email=data["email"])).exists():
                raise exc.BadRequest(_("This username or email is already in use."))

            user = User(username=data["username"],
                        first_name=data["first_name"],
                        last_name=data["last_name"],
                        email=data["email"])
            user.set_password(data["password"])
            user.save()

        self._create_domain_member(user)

        membership.user = user
        membership.save()

        #self._send_private_register_email(user, membership=membership)

        response_data = self._create_response(user)
        return Response(response_data, status=status.HTTP_201_CREATED)