DRF将新激活码发送给未激活的已注册用户

时间:2018-06-14 13:02:47

标签: python django python-3.x django-rest-framework django-rest-framework-jwt

这是django rest框架项目。这次我想重新发送帐户激活码给注册用户(目前不活跃)我写了这样的视图,但我得到的服务器响应说:用户帐户被禁用...我知道用户现在被禁用但我想根据他/她的电子邮件和身份证件向他发送新的激活码。

注意:我在我的应用程序中使用jwt进行身份验证

这是我的观点:

我有什么建议可以解决这个问题吗? 关于我的看法是正确的方法吗?

class ResendActivationCode(APIView):
    permission_classes = [AllowAny]
    activation_code = code_generator()

    def get(self, request):
        print(request.user)
        capname = str('re' + request.user.id)
        iscached = cache.get(capname)
        if not iscached:
            cache.set(capname, 1, 120)
            email = request.user.email
            Profile.objects.filter(user_id=request.user.id).update(**{'activation_key': self.activation_code})
            subject = "complete you registration"
            message = "Thankyou. please enter this code to done your registration progress {}".format(self.activation_code)
            html_message = "http://localhost:4200/accounts/activation/{}".format(self.activation_code)
            froms = "mail@gmail.com"
            # html_template = render_to_string('mail/reg_mail.html', {})
            send_mail(
                subject,
                message,
                froms,
                [email],
                html_message=html_message,
                # fail_silently=False,
            )
            return Response("ok")
        else:
            return Response("time limit")

这是相关的模型:(这是与User表有关系的profile模型,并有用于存储代码的activation_code字段)

class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='profile')
    activation_key = models.CharField(max_length=200)
    bio = models.CharField(max_length=250, null=True, blank=True)
    email = models.EmailField(null=True, blank=True)
    job = models.CharField(default="",max_length=250, null=True)

服务器响应:

detail":"User account is disabled."

0 个答案:

没有答案