有没有办法根据用户实例的语言更改在 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)
有没有人有任何建议或已经这样做了?