class PublicAccess(models.Model): token = models.CharField("Token", default=generate_token(), unique=True, max_length=64) email = models.EmailField("Email", unique=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True, editable=False) def __str__(self): return self.email
def mutate( cls, root, info, ): """ """ user = get_user_model().objects.get(id=info.context.user.id) tokens = UserToken.objects.filter(user=user.id) tokens.delete() token = UserToken.objects.create(user=user, token=generate_token()) send_confirmation_email.delay(user.id, token.token) return ResendVerification(result=user, ok=True, errors=[])
def mutate(cls, root, info, email, first_name, last_name, password): user = get_user_model() existing_user = user.objects.filter(email=email) if existing_user.count(): return CreateUser(result=None, ok=False, errors=[{ "message": "Email already registered" }]) user = user.objects.create_user(email, first_name, last_name, password) token = UserToken.objects.create(user=user, token=generate_token()) send_confirmation_email.delay(user.id, token.token) return CreateUser(result=user, ok=True, errors=[])
def login(): username = request.json.get("username") password = request.json.get("password") db = get_db() cursor = db.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute("select * from user WHERE username= %s and password=%s", (username, password)) user = cursor.fetchone() if user: cache = get_cache() token = generate_token() cache.set("ID:%s" % (user["id"]), token, pick=True) cache.set("TOKEN:%s" % token, user["id"], pick=True) user["token"] = token user.pop("password") return Response({"status": 0, "data": user}) else: return Response({"status": 4003, "msg": "账号密码错误"}, 400)
def post(self, request, *args, **kwargs): email = request.data.get('email') name = request.data.get('name') password = request.data.get('password') try: validate_email(email) except ValidationError: raise APIException(detail='Wrong email format {}'.format(email), code=status.HTTP_500_INTERNAL_SERVER_ERROR) user = User.objects.create_user( email=email, password=password, username=name, ) serializer = self.serializer_class(data=self.request.data) serializer.is_valid() CustomUser.objects.create(**serializer.validated_data, basic_user=user) token = generate_token(user) return Response(data={'token': token}, status=status.HTTP_201_CREATED)
def get_token(self, admin): return generate_token()
def test_generate_token(self): self.assertTrue(generate_token())
def save(self, *args, **kwargs): if not self.pk: self.token = generate_token() super(Invite, self).save(*args, **kwargs)