Example #1
0
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=yield from User.findAll('email=?',[email])

    if len(users)>0:
        raise APIError('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.gravator.com/avatar/%s?d=mm&s=120'%hashlib.md5(email.encode('utf-8')).hexdigest())
    yield from 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
Example #2
0
async def api_signup_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 APIError('signup:failed', 'email', 'Email is already in use')
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)
    if name == 'sharon':
        admin = 1
    else:
        admin = 0
    user = User(id=uid,
                name=name.strip(),
                email=email,
                passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
                admin=admin)
    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 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 APIError("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
Example #4
0
def api_register_user(*, UserID, Phone, name, Pass):
    print(UserID)
    if not UserID:
        raise APIValueError('身份证号')
    if not name:
        raise APIValueError('姓名')
    if not Pass or not _RE_SHA1.match(Pass):
        raise APIValueError('密码')
    if not Phone:
        raise APIValueError('手机号')
    users = yield from User.findAll('Phone=?', [Phone])
    if len(users) > 0:
        raise APIError('register:failed', 'phone', 'Phone is already in use.')

    sha1_Pass = '******' % (Phone, Pass)
    user = User(UserID=UserID,
                User=name,
                Pass=hashlib.sha1(sha1_Pass.encode('utf-8')).hexdigest(),
                Phone=Phone)
    yield from user.save()

    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    user.Pass = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=True).encode('utf-8')
    return r
Example #5
0
async def authenticate(*, email, passwd):
    if not email:
        raise APIValueError('email', 'Invaild email.')
    if not passwd:
        raise APIValueError('passwd', 'Invaild password')
    users = await User.findAll('email=?', [email])  #通过email获得 user信息
    if len(users) == 0:
        raise APIValueError('email', 'Email not exist.')
    user = users[0]
    # check passwd
    sha1 = hashlib.sha1()
    sha1.update(user.id.encode('utf-8'))
    sha1.update(b':')
    sha1.update(
        passwd.encode('utf-8')
    )  #这里将通过email获取的的user.id + ':' + 输入的passwd 组合进行计算,接着与数据库的user.passwd密码摘要(参考register及api/users)比对判断密码是否正确,
    if user.passwd != sha1.hexdigest():
        raise APIValueError('passwd', 'Invaild password.')
    # authenticate ok set 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("passwd", "Invalid email.")
    users = await User.findAll("email=?", [email])
    if len(users) == 0:
        raise APIValueError("email", "Email not exist.")
    user = users[0]
    # check passwd
    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.")
    # authenticate ok, set 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
Example #7
0
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 APIError('register:failed', 'email', 'Email is already in use.')
    sha1_passwd = '%s:%s' % (email, passwd)
    user = User(name=name.strip(),
                email=email,
                passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest())
    await user.save()
    # make session cookie:
    print('---------------response -----------------')
    users = await User.findAll('email=?', [email])
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    result = ResultBean(0, users[0])
    r.body = json.dumps(result, ensure_ascii=False).encode('utf-8')
    return r
Example #8
0
def authenticate(*, email, passwd):
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid password.')
    users = yield from User.findAll('email=?', [email])
    if len(users) == 0:
        raise APIValueError('email', 'Email not exist.')
    user = users[0]
    # check passwd:
    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.')
    # authenticate ok, set 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
Example #9
0
def api_register_user(*, email, name, passwd):
    #判断name是否为空:
    if not name or not name.strip():
        raise APIValueError('name')
    #判断email是否为空及是否满足email格式:
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    #判断password首付为空及是否满足password格式:
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    #数据中查询对应的email信息:
    users = yield from User.findAll('email=?', [email])
    #判断查询结果是否存在,若存在则返回异常提示邮件已存在:
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    #生成唯一ID:
    uid = next_id()
    #重构唯一ID和password成新的字符串:
    sha1_passwd = '%s:%s' % (uid, passwd)
    #构建用户对象信息:
    #hashlib.sha1().hexdigest():取得SHA1哈希摘要算法的摘要值。
    user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www(first).gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
    #将用户信息存储到数据库:
    yield from user.save()
    # make session cookie:
    #构造session cookie信息:
    r = web.Response()
    #aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)   #max_age:定义cookie的有效期(秒);
    user.passwd = '******'
    r.content_type = 'application/json'
    #以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Example #10
0
def authenticate(*, email, passwd):
    #判断email(用户名)及password是否为空;为空则抛出异常:
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid password.')
    #数据中查询对应的email信息:
    users = yield from User.findAll('email=?', [email])
    #判断查询结果是否存在,若不存在则抛出异常:
    if len(users) == 0:
        raise APIValueError('email', 'Email not exist.')
    #获取查询结果集的第一条数据:
    user = users[0]
    # check passwd:
    #调用摘要算法SHA1组装登陆信息;计算摘要值同数据库中的信息进行比配:
    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.')
    # authenticate ok, set cookie:
    #构造session cookie信息:
    r = web.Response()
    #aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    #以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Example #11
0
async def api_register_user(*, email, name, passwd):
    if not name or not name.strip():
        raise APIValueError('name', 'Name must not be empty.')
    if not email or not _RE_EAMIL.match(email.lower()):
        raise APIValueError('email', 'Illegal email.')
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd', 'Illegal passwd.')
    uid = next_id()
    passwd_solted = '%s:%s' % (uid, passwd)
    passwd_sha1 = hashlib.sha1(passwd_solted.encode('utf-8')).hexdigest()
    image_url = 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(
        email.encode('utf-8')).hexdigest()
    user = User(id=uid,
                name=name.strip(),
                email=email,
                passwd=passwd_sha1,
                image=image_url)
    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
Example #12
0
def authenticate(*, Phone, Pass):
    print('进来验证了。。。')
    if not Phone:
        raise APIValueError('Phone', 'Invalid PhoneNumber')
    if not Pass:
        raise APIValueError('Pass', 'Invalid password')
    users = yield from User.findAll('Phone=?', [Phone])
    if len(users) == 0:
        raise APIValueError('Phone', 'Phone not exists')
    user = users[0]
    # 检查密码
    sha1 = hashlib.sha1()
    sha1.update(user.Phone.encode('utf-8'))
    sha1.update(b':')
    sha1.update(Pass.encode('utf-8'))
    if user.Pass != sha1.hexdigest():
        raise APIValueError('password', 'invalid password')
    # 验证通过,设置cookie
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    user.Pass = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Example #13
0
async def api_create_blog(request, *, name, summary, content):
    check_admin(request)
    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 = 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_users(*, 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 APIError('register:failed', 'email', 'Email is already in use')
    send_user_email(email, name, passwd)
    return dict(r='yes')
Example #15
0
def api_create_blog(request, *, name, summary, content):
    # @copy_current_request_context
    # def do_some_work():
    check_admin(request)
    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 = 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())
    yield from blog.save()
    return blog
Example #16
0
def api_query_buses(*, BusFrom, BusTo, BusDate):
    print('查询列车')
    if not BusDate:
        raise APIValueError('发车时间')
    if not BusFrom:
        raise APIValueError('始发地')
    buses1 = yield from Bus.findAll('BusFrom=?', [BusFrom])
    buses2 = yield from Bus.findAll('BusTo=?', [BusTo])
    buses = [i for i in buses1 if i in buses2]
    #    retB = list(set(buses1).intersection(set(buses2)))
    r = web.Response()
    r.content_type = 'application/json'
    r.body = json.dumps(buses, ensure_ascii=True).encode('utf-8')
    return r
Example #17
0
def api_update_blog(request, *, id, name, summary, content):  # 将id置为命名关键字参数
    check_admin(request)
    blog = yield from 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()
    yield from blog.update()
    return blog
Example #18
0
async def api_update_blog(id,request,*,name,summary,content):
    check_admin(request)
    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 = await Blog.find(id)
    blog.name=name.strip()
    blog.summary=summary.strip()
    blog.content=content.strip()
    await blog.update()
    return blog
Example #19
0
def get_a():
    error = ''
    if error:
        infos = {'name': 'fta', 'age': 12}
        return dict(use=infos)
    else:
        raise APIValueError('nameerror', message='test error')
async def api_create_comments(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 APIValueError('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
async def api_delete_comments(id, request):
    check_admin(request)
    c = await Comment.find(id)
    if c is None:
        raise APIValueError('Comment')
    await c.remove()
    return dict(id=id)
async def api_update_(id, request, *, name, summary, content):
    check_admin(request)
    blog = await Blog.find(id)
    # if not id or not id.strip():
    #     raise APIValueError("uid", "id cannot be empty.")
    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.update()
    return blog
Example #23
0
async def api_update_quote(id, request, *, content):
    check_admin(request)
    quote = await Quote.find(id)
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty.')
    quote.content = content.strip()
    await quote.update()
    return quote
Example #24
0
async def api_create_blog(request, *, name, summary, content):
    check_admin(request)
    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 = 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()
    logging.info('blog %s' % (json.dumps(blog).encode('utf-8')))
    return blog
Example #25
0
def api_create_blog(request, *, name, summary, content):
    #校验当前用户权限:
    check_admin(request)
    #校验传递值中参数‘name’是否为空或空串,为空则抛出异常:
    if not name or not name.strip():
        #参数‘name’为空则抛出异常:
        raise APIValueError('name', 'name cannot be empty.')
    #校验传递值中参数‘summary’是否为空或空串,为空则抛出异常:
    if not summary or not summary.strip():
        raise APIValueError('summary', 'summary cannot be empty.')
    #校验传递值中参数‘content’是否为空或空串,为空则抛出异常:
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty.')
    #创建Blog实例:
    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())
    #将Blog信息存储到数据库:
    yield from blog.save()
    return blog
Example #26
0
async def devicesDetails(*, id):
    if not id:
        raise APIValueError('kind')
    device = await Device.findAll("id=?", [id])
    r = web.Response()
    r.content_type = 'application/json'
    result = ResultBean(0, device[0])
    r.body = json.dumps(result, ensure_ascii=False).encode('utf-8')
    return r
Example #27
0
async def api_create_quote(request, *, content):
    check_admin(request)
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty')
    quote = Quote(user_id=request.__user__.id,
                  user_name=request.__user__.name,
                  user_image=request.__user__.image,
                  content=content.strip())
    await quote.save()
    return quote
Example #28
0
async def addDevice(*, name, kind, permissLevel=_PERMISS_CHILD):
    if not name or not name.strip():
        raise APIValueError('name')
    if not kind:
        raise APIValueError('kind')
    device = await Device.findAll('name=?', [name])
    if len(device) > 0:
        raise APIError('add:failed', 'device', 'device is already in use.')
    device = Device(name=name,
                    kind=kind,
                    status=_SWITCH_OFF,
                    permissLevel=permissLevel)

    await device.save()
    device = await Device.findAll('name=?', [name])
    r = web.Response()
    r.content_type = 'application/json'
    result = ResultBean(0, device[0])
    r.body = json.dumps(result, ensure_ascii=False).encode('utf-8')
    return r
Example #29
0
def api_update_blog(id, request, *, name, summary, content):
    #校验当前用户权限:
    check_admin(request)
    #数据库Blog表中查询指定文章信息:
    blog = yield from Blog.find(id)
    #校验传递值中参数‘name’是否为空或空串,为空则抛出异常:
    if not name or not name.strip():
        raise APIValueError('name', 'name cannot be empty.')
    #校验传递值中参数‘summary’是否为空或空串,为空则抛出异常:
    if not summary or not summary.strip():
        raise APIValueError('summary', 'summary cannot be empty.')
    #校验传递值中参数‘content’是否为空或空串,为空则抛出异常:
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty.')
    #将传递值中的信息赋值到blog实例中:
    blog.name = name.strip()
    blog.summary = summary.strip()
    blog.content = content.strip()
    #将Blog信息更新到数据库:
    yield from blog.update()
    return blog
Example #30
0
async def deleteDevice(*, id):
    if not id or not id.strip():
        raise APIValueError('name')
    device = await Device.findAll('id=?', [id])
    if len(device) == 0:
        raise APIError('delete:failed', 'id', 'do not has id.')
    await device[0].remove()
    r = web.Response()
    r.content_type = 'application/json'
    result = ResultBean(0, "remove finish")
    r.body = json.dumps(result, ensure_ascii=False).encode('utf-8')
    return r