Djoser 更改电子邮件语言

Djoser change email language

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

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

当然可以!

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

    @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)

所以在 subclassing 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)

Djoser 正在使用 templated-mail 创建邮件内容。

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

在你的 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"

并按照说明在 djoser 设置中设置 CountryActivationMail in the docs.