Пример #1
0
async def api_register_user(*, email, name, passwd, key):
    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")
    if key != _INVITATION_KEY:
        raise APIError("register:failed", "invitation-code",
                       "Invalid invitation code")
    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
Пример #2
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])  #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
Пример #3
0
async def api_register_user(*, name, email, passwd):
    if not name or not name.strip():
        raise APIValueError("name")
    if not email or not _RE_SHA1.match(passwd):
        raise APIValueError("email")
    if not passwd and not _RE_SHA1.match(passwd):
        raise APIValueError("password")
    users = await User.findAll(where="email=?", args=[email])
    # check whether the email has been registered
    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="http://www.gravatar.com/avatar/{}?d=mm&s=120".format(
                    hashlib.md5(email.encode("utf-8")).hexdigest()))
    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
Пример #4
0
async def api_signin(*, email, name, password):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not RE_SHA1.match(password):
        raise APIValueError('password')

    users = await User.findAll(where='email=?', args=[email])
    if len(users) > 0:
        raise APIError('signup:failed', 'email', 'Email is already in use.')
    uid = next_id()
    sha1_password = '******' % (uid, password)
    user = User(id=uid,
                name=name.strip(),
                email=email,
                password=hashlib.sha1(
                    sha1_password.encode('utf-8')).hexdigest(),
                image=configs.web_meta.user_image)
    await user.save()
    # 设置cookie
    r = web.Response()
    r.set_cookie(configs.cookie.name,
                 user2cookie(user, configs.cookie.max_age),
                 max_age=configs.cookie.max_age,
                 httponly=True)
    user.password = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Пример #5
0
async def api_register_user(*, email, name, password):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not _RE_SHA1.match(password):
        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计算后的40位Hash字符串,所以服务器端并不知道用户的原始口令。

    sha1_password = '******' % (uid, password)
    #user=User(id=uid,name=name.strip(),email=email,password=hashlib.sha1(sha1_password.encode('utf-8')).hexdigest(),image='/static/img/a.bmp'%hashlib.md5(email.encode('utf-8')).hexdigest())
    user = User(id=uid,
                name=name.strip(),
                email=email,
                password=hashlib.sha1(
                    sha1_password.encode('utf-8')).hexdigest(),
                image='static/img/a.bmp')
    await user.save()

    #make session cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    user.password = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Пример #6
0
def api_register_user(*,email,name,passwd):
	logging.info('comming into register post users')
	if not name or not name.strip():
		raise APIValueError('name')
	if not name 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.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
	yield from user.save()

	#make session cookie from here:
	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 
Пример #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')
    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()
    # 登陆的时候要创建cookie信息
    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
Пример #8
0
def create_users(loop):
    yield from orm.create_pool(loop=loop, user='******', password='******', db='check_in_system')
    user_dict = {
        'u1': {
            'name': 'tonyyeti',
            'passwd': 'Tonyalston911',
            'admin': True},
        'u2': {
            'name': 'feifei',
            'passwd': '123456',
            'admin': False},
        'u3': {
            'name': 'maomao',
            'passwd': '123456',
            'admin': False}
            }
    for user in user_dict.values():
        uid = next_id()
        sha1_passwd = '%s:%s' % (uid, user['passwd'])
        u = User(id=uid, name=user['name'], passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='about:blank', admin=user['admin'])
        yield from u.save()
        print(user['name'] + 'saved.')
    feifei = yield from User.findAll('name=?', ['feifei'])
    maomao = yield from User.findAll('name=?', ['maomao'])
    r1 = Relation(active_user_id=feifei[0].id, active_user_name=feifei[0].name, passive_user_id=maomao[0].id, passive_user_name=maomao[0].name)
    r2 = Relation(active_user_id=maomao[0].id, active_user_name=maomao[0].name, passive_user_id=feifei[0].id, passive_user_name=feifei[0].name)
    yield from r1.save()
    print('r1 saved.')
    yield from r2.save()
    print('r2 saved')
Пример #9
0
async def api_create_comments(request, *, id, content):
    uid = next_id()
    comment = Comment(id=uid,
                      blog_id=id,
                      user_id=request.__user__.id,
                      user_name=request.__user__.name,
                      user_image=request.__user__.image,
                      content=content)
    await comment.save()
    return comment
Пример #10
0
def create_drg(loop):
    yield from orm.create_pool(loop=loop,
                               user='******',
                               password='******',
                               db='check_in_system')
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, '123')
    drg = User(id=uid,
               name='drg',
               passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
               image='about:blank',
               admin=False)
    yield from drg.save()
    print('drg saved')
Пример #11
0
def sign_up_check(*, name, passwd, passwdag):
    # 输入有问题:
    message = ''
    inputError = False
    if not name or not name.strip():
        if len(message) > 0:
            message = message + ';\n请输入用户名'
        else:
            message = message + '请输入用户名'
        inputError = True
    if not passwd or not _RE_SHA1.match(
            passwd) or not passwdag or not _RE_SHA1.match(passwdag):
        if len(message) > 0:
            message = message + ';\n密码只能由6-20位的字母与数字构成'
        else:
            message = message + '密码只能由6-20位的字母与数字构成'
        inputError = True
    if passwd != passwdag:
        if len(message) > 0:
            message = message + ';\n两次密码不匹配'
        else:
            message = message + '两次密码不匹配'
        inputError = True
    if inputError == True:
        return {
            '__template__': 'signup.html',
            'message': message,
            'username': name
        }
    # 用户名已被注册
    users = yield from User.findAll('name=?', [name])
    if len(users) > 0:
        message = '用户名已被注册'
        return {'__template__': 'signup.html', 'message': message}
    # 注册用户
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)
    user = User(id=uid,
                name=name.strip(),
                passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
                image='about:blank')
    yield from user.save()
    # 分配cookie并转至首页
    r = web.HTTPFound('/')
    cookie = user2cookie(user, 86400)
    r.set_cookie(COOKIE_NAME, cookie, max_age=86400, httponly=True)
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    user.passwd = '******'
    return r
Пример #12
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.')
    uid = next_id()
    blog = Blog(id=uid,
                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
Пример #13
0
def api_register_user(*, email, name, passwd):
    if not name or not name.strip():
        # wrong user name format
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        # wrong email format
        raise APIValueError('email')
    if not passwd or not _RE_SHA1.match(passwd):
        # wrong password format
        raise APIValueError('passwd')
    users = yield from User.findAll('email=?', [email])
    # test if user already registered (from mySQL database)
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use')

    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)

    # admin = False
    # if email == _ADMIN_EMAIL:
    #     admin = True

    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())
    # save user info to database user table
    yield from user.save()
    logging.info('save user OK')
    # response info that will display on browser
    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
Пример #14
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('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
Пример #15
0
async def api_blogs(request):
    if request.method == 'POST':
        if not request.content_type:
            return web.HTTPBadRequest('Missing Content-Type')
        ct = request.content_type.lower()
        if ct.startswith('application/json'):
            params = await request.json()
        if not isinstance(params, dict):
            return web.HTTPBadRequest('json is not dict')
    kw = params

    name = kw.get('name')
    summary = kw.get('summary')
    content = kw.get('content')
    if not name:
        raise Exception("not find title")
    if not summary:
        raise Exception("not find summary")
    if not content:
        raise Exception("not find content")

    user = check_user(request)
    user_id = user.id
    user_name = user.name
    user_image = user.image
    blog = Blog(id=next_id(),
                user_id=user_id,
                user_name=user_name,
                user_image=user_image,
                name=name,
                summary=summary,
                content=content)

    logging.info('blog save before')
    await blog.save()
    logging.info('blog save com')
    r = web.Response()
    r.body = json.dumps(blog).encode('utf-8')
    logging.info('blog wwww')
    return r
Пример #16
0
async def api_signin(*, email, name, password):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not RE_SHA1.match(password):
        raise APIValueError('password')

    users = await User.findAll(where='email=?', args=[email])
    if len(users) > 0:
        raise APIError('signup:failed', 'email', 'Email is already in use.')
    uid = next_id()
    sha1_password = '******' % (uid, password)
    user = User(id=uid, name=name.strip(), email=email, password=hashlib.sha1(sha1_password.encode('utf-8')).hexdigest(), image=configs.web_meta.user_image)
    await user.save()
    # 设置cookie
    r = web.Response()
    r.set_cookie(configs.cookie.name, user2cookie(user, configs.cookie.max_age), max_age=configs.cookie.max_age, httponly=True)
    user.password = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Пример #17
0
async def api_register_user(request):
    if request.method == 'POST':
        if not request.content_type:
            return web.HTTPBadRequest('Missing Content-Type')
        ct = request.content_type.lower()
        if ct.startswith('application/json'):
            params = await request.json()
        if not isinstance(params, dict):
            return web.HTTPBadRequest('json is not dict')
    kw = params

    name = kw.get('name')
    email = kw.get('email')
    password = kw.get('password')
    if not name:
        raise Exception("not find name")
    if not email:
        raise Exception("not find email")
    if not password:
        raise Exception("not find password")
    users = await User.findall('email', email)
    if len(users) > 0:
        raise Exception("email is already in use")
    password = hashlib.sha1((email + password).encode('utf-8')).hexdigest()
    user = User(id=next_id(),
                name=name,
                email=email,
                passwd=password,
                image='xxxx')
    await user.save()

    r = web.Response()

    r.set_cookie(_COOKIE_NAME,
                 __user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    r.body = json.dumps(user).encode('utf-8')
    return r
Пример #18
0
def register_user(email, name, password, image=_DEFAULT_IMAGE):
	if not name or not name.strip():
		raise APIValueError('name')
	if not email or not _RE_EMAIL.match(email):
		raise APIValueError('email')
	if not password or _RE_SHA1.match(password):
		raise APIValueError('password')

	user = yield from Users.find('email=?',[email])
	if user:
		raise APIError('register:failed','email','Email is already used.')

	uid = next_id()
	sha1_password = '******' % (uid, password)
	user = Users(id=uid, name=name, email=email, password=hashlib.sha1(sha1_password.encode('utf-8')).hexdigest(),image=image)
	yield from user.save()

	r = aiohttp.web.Response()
	r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
	user.password = '******'
	r.content_type = 'application/json'
	r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
	return r