1

有没有办法根据用户实例的语言更改在 djoser 中发送的电子邮件?我发现了这个问题如何明确指定要发送的电子邮件的语言?,但在 djoser 的最新版本中似乎不是一个选项,因为每个操作(激活、重置密码等)不再有视图,或者我还没有弄清楚它是如何工作的。另外,我尝试继承 UserViewSet 类并应用翻译,但我不知道这是否是这样做的方法以及如何指定 djoser 的所有 url。

class UserViewSet(djoser_view.UserViewSet):
    def perform_create(self, serializer):
        user = serializer.save()
        signals.user_registered.send(
            sender=self.__class__, user=user, request=self.request
        )

        context = {"user": user}
        to = [get_user_email(user)]
        language = user.language or 'en-us'
        with translation.override(language):
            if settings.SEND_ACTIVATION_EMAIL:
                settings.EMAIL.activation(self.request, context).send(to)
            elif settings.SEND_CONFIRMATION_EMAIL:
                settings.EMAIL.confirmation(self.request, context).send(to)

    def perform_update(self, serializer):
        super().perform_update(serializer)
        user = serializer.instance
        language = user.language or 'en-us'
        with translation.override(language):
            if settings.SEND_ACTIVATION_EMAIL:
                context = {"user": user}
                to = [get_user_email(user)]
                settings.EMAIL.activation(self.request, context).send(to)


    @action(["post"], detail=False)
    def activation(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.user
        user.is_active = True
        user.save()

        signals.user_activated.send(
            sender=self.__class__, user=user, request=self.request
        )

        language = user.language or 'en-us'
        with translation.override(language):
            if settings.SEND_CONFIRMATION_EMAIL:
                context = {"user": user}
                to = [get_user_email(user)]
                settings.EMAIL.confirmation(self.request, context).send(to)

        return Response(status=status.HTTP_204_NO_CONTENT)

有没有人有任何建议或已经这样做了?

4

1 回答 1

0

你当然可以!

您正确识别了发送邮件的操作。

    @action(["post"], detail=False)
    def activation(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.user
        user.is_active = True
        user.save()

        signals.user_activated.send(
            sender=self.__class__, user=user, request=self.request
        )

        if settings.SEND_CONFIRMATION_EMAIL:
            context = {"user": user}
            to = [get_user_email(user)]
            settings.EMAIL.confirmation(self.request, context).send(to)

因此,在子类化之后,UserViewSet您可以提供一个activation实际的模板名称。

        if settings.SEND_CONFIRMATION_EMAIL:
            context = {"user": user}
            to = [get_user_email(user)]
            kwargs = {"request": self.request, "context": context}
            if user.country == "Country1":
                kwargs["template_name"] = "email/country1/activation.html"
            settings.EMAIL.confirmation(**kwargs)).send(to)

Djosertemplated-mail用于创建邮件内容。

但是,如您所见,user实体是在上下文中传递的。所以我宁愿建议稍微调整你的设置,并在负责生成这封电子邮件的班级中进行。

在你的app/email.py

class CountryActivationMail(ActivationEmail):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        if self.context.user.country == "Country1":
            self.template_name = "email/country1/activation.html"

并按照文档中的CountryActivationMail说明在 djoser 设置中进行设置

于 2020-12-02T11:30:37.760 回答