async def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise ApiValueError('name') if not email or not _RE_EMAIL.match(email): raise ApiValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise ApiValueError('passwd') users = await User.findAll('email=?', [email]) if len(users) > 0: raise ApiValueError('register:failed', 'email', 'Email is already in use.') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) await user.save() # make session cookie: r = web.Response() r.set_cookie(Cookie_Name, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
async def authenticate(*, email, passwd): if not email: raise ApiValueError('email', 'invalid email.') if not passwd: raise ApiValueError('password', 'invalid password.') users = await User.findAll('email=?', [email]) if len(users) == 0: raise ApiValueError('email', 'email not exist.') user = users[0] # 验证密码 sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise ApiValueError('passwd', 'Invalid password.') r = web.Response() r.set_cookie(Cookie_Name, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
async def api_update_blog(id, request, *, name, summary, content): check_admin(request) blog = await Blog.find(id) if not name or not name.strip(): raise ApiValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise ApiValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise ApiValueError('content', 'content cannot be empty.') blog.name = name.strip() blog.summary = summary.strip() blog.content = content.strip() await blog.save() return blog
async def api_create_blog(request, *, name, summary, content): check_admin(request) if not name or not name.strip(): raise ApiValueError('name', 'name must be not empty') if not summary or not summary.strip(): raise ApiValueError('summary', 'summary must be not empty') if not content or not content.strip(): raise ApiValueError('content', 'content must be not empty') blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip()) await blog.save() return blog
async def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise ApiValueError('name') if not email or not _RE_EMAIL.match(email): raise ApiValueError("email") if not passwd or not _RE_SHA1.match(passwd): raise ApiValueError('password') users = await User.findAll('email=?', email) if len(users) > 0: raise APIError("register:failed", "email", "Email is already in use.") uid = next_id() sha1_passwd = '{}:{}'.format(uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='null') await user.save() r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '********' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
async def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') if not content or not content.strip(): raise ApiValueError('content') blog = await Blog.find(id) if blog is None: raise ApiResourceNotFoundError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment