Ejemplo n.º 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
Ejemplo n.º 2
0
async def test(loop):
    await create_pool(loop, user='******', password='******', db='awesome')
    u = User(name='Test01',
             email='*****@*****.**',
             passwd='1234567890',
             image='about:blank')
    await u.save()
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def api_register(*, name, email, password):
    logging.info("enter")
    check_string(name=name)
    check_email_passwd(email, name)
    users = yield from User.findAll('email = ?', [email])
    if users:
        raise APIValueError("email", "Email is already in used")
    uid = next_id()
    sha1_passwd = '%s:%s' % (email, password)
    user = User(name=name.strip(),
                email=email,
                password=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
                image="/static/img/user.png",
                admin=0)
    yield from user.save()

    #make session cooike
    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
Ejemplo n.º 6
0
def testdb(loop):
    'test class User - add a new user'
    print('Create db pool...')
    r = yield from orm.create_pool(loop=loop,
                                   host='127.0.0.1',
                                   port=3306,
                                   user='******',
                                   password='******',
                                   db='awesome')
    print('r : %s' % r)
    # 以下为测试:
    print('Create a new user...')

    "create a user with fixed <id> and <created_at>"
    #u = User(email='*****@*****.**', passwd='password', admin=True, name='testname2', image='blank', id='testid2',created_at='1.0')

    "create a user with fixed auto-generated <id> and <created_at>"
    u = User(email='*****@*****.**',
             passwd='password',
             admin=True,
             name='testname3',
             image='blank',
             id=None,
             created_at=None)  #
    print('Save the new user into database...')
    r = yield from u.save()
    print('r : %s' % r)
Ejemplo n.º 7
0
async def test():
    await orm.create_pool(loop=loop,user='******',password='******',db='awesome')
    u = User(name='zbf',email='*****@*****.**',passwd='123456',image='about:blank')
    try:
        await u.save()
    except BaseException as e:
        print(e)
Ejemplo n.º 8
0
def userregister():
    form = Userform(request.form)

    if request.method == 'POST' and form.validate():

        try:

            person_data = Person(name=form.name.data)
            DB.session.add(person_data)
            DB.session.commit()
            DB.session.flush()

            print("{0}".format(person_data.name))

        except Exception as e:
            DB.session.rollback()
            print(e)

        else:

            try:
                user_data = User(username=form.username.data,
                                 email=form.email.data)
                DB.session.add(user_data)
                DB.session.commit()
                DB.session.flush()

                print("{0}  {1}  {2}".format(user_data.username,
                                             user_data.email,
                                             user_data.user_join_date))
            except Exception as e:
                DB.session.rollback()
                print(e)

    return render_template('userregister.html', form=form)
Ejemplo n.º 9
0
def test(loop):
    #创建连接池
    yield from orm.create_pool(loop,user='******',password='******',database='awesome')
    #创建对象
    u=User(name='Test231',email='test123@wxample',passwd='123456',image='about:blank')
    #调用保存方法
    yield from u.save()
Ejemplo n.º 10
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
Ejemplo n.º 11
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
Ejemplo n.º 12
0
async def test(loop):
    await www.orm.create_pool(loop,
                              user='******',
                              password='******',
                              db='awesome')
    u = User(name='wtf', email='6', passwd='000', image='about:blank')
    await u.save()
Ejemplo n.º 13
0
def test(loop):
    yield from create_pool(loop=loop, user='******', password='******', db='test')
    u = User(name='aaa', email='*****@*****.**', passwd='aaa', image='about:blank')
    yield from u.save()
    #    r = yield from u.findAll()
    #    print(r)
    yield from destory_pool()
Ejemplo n.º 14
0
async def test(loop):
    await www.orm.create_pool(loop, user='******', password='******', db='awesome')
    u = User(name='Test',
             email='*****@*****.**' % random.randint(0, 10000000),
             passwd='123',
             image='about:blank')
    await u.save()
Ejemplo n.º 15
0
async def test(loop):
    await orm.create_pool(loop=loop, user='******', password='******', db='awesome')
    u = User(name='Test', email='*****@*****.**', passwd='12343243242', image='about:blank')
    await u.save()
    # 添加到数据库后需要关闭连接池,否则会报错:runtime error : event loop is closed
    orm.__pool.close()
    await orm.__pool.wait_closed()
def before_request():
    #print "REQUEST_HOST='%s'" % (request.host,)
    host_name = request.host.lower()
    # if ':' in host_name: host_name = host_name[:host_name.find(':')]

    if host_name in Site.domain_to_tag:
        # This is for DNS resolved versions
        site_tag = Site.domain_to_tag[host_name]
    else:
        # This is for subdomains of the main site : Just strip off the subdomain
        site_tag = host_name[:host_name.find('.')].lower()

    #print "REQUEST_HOST_TAG='%s'" % (site_tag,)

    flask.g.site = Site.query().filter_by(tag=site_tag).first()
    if flask.g.site is None:
        print("FAILED TO FIND SITE_TAG : DEFAULTING")
        # Default if all else fails
        flask.g.site = Site.query().filter_by(tag=Site.tag_main).first()

    if 'userid' in session:
        flask.g.user = User.get(session['userid'])
    else:
        flask.g.user = User(flask.g.site.tag, '*****@*****.**',
                            'Not logged in')  # Creates a user with id=None

    #session.pop('hash')
    if 'hash' not in session:
        session['hash'] = ''.join(['%02x' % ord(ch) for ch in urandom(4)])
        print("Created session hash '%s'" % (session['hash'], ))
    flask.g.hash = session['hash']

    flask.g.ip = request.remote_addr
Ejemplo n.º 17
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.")
    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
Ejemplo n.º 18
0
def registerUser(*, 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 password.strip():
        raise APIValueError('password')
    users = yield from User.findAll('email=?', email)
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    uid = nextId()
    sha1Password = '******' % (uid, password)
    sha1 = hashlib.sha1(sha1Password.encode('utf-8')).hexdigest()
    user = User(
        id=uid,
        name=name.strip(),
        email=email,
        password=sha1,
        image=
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542105917178&di=d1f6b6a11859ff9a2436460ed3c691dd&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fbba1cd11728b47104c5c00e9c9cec3fdfc0323a0.jpg'
    )
    yield from user.save()
    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
Ejemplo n.º 19
0
async def test(loop):
    await www.orm.create_pool(loop, user='******', passwd='pass', db='awesome')
    u = User(name='Test5',
             email='*****@*****.**',
             passwd='123456780',
             image='about:blank')
    await u.save()
    a = await u.findAll()  # 这个要打印才显示出来
    print(a)
Ejemplo n.º 20
0
async def test(loop):
    await orm.create_pool(loop=loop,
                          user='******',
                          password='******',
                          db='awesome')
    u = User(name='Test2',
             email='*****@*****.**',
             passwd='1234567890',
             image='about:blank')
    await u.save()
Ejemplo n.º 21
0
def test(loop):
    yield from orm.create_pool(loop=loop,
                               user='******',
                               password='******',
                               database='awesome')
    u = User(name='Yao',
             email='*****@*****.**',
             passwd="sas",
             image='about:blank')
    yield from u.save()
Ejemplo n.º 22
0
def find_test(loop):
    yield from orm.create_pool(loop=loop,
                               host='123.206.178.243',
                               user='******',
                               password='******',
                               db='awesome')

    u = yield from User().find(
        '001502421565775576b9c8a80e847e3b6ca4f45e5573712000')
    print(u)
Ejemplo n.º 23
0
async def delete(loop, email=None):

    await www.orm.create_pool(loop,
                              user='******',
                              password='******',
                              db='awesome')
    id = await User.findNumber('id', where='email="' + email + '"')
    u = User(id=id)
    await u.remove()
    print('删除测试: %s' % id)
Ejemplo n.º 24
0
async def test(loop):
    await www.orm.create_pool(loop=loop,
                              user='******',
                              password='******',
                              db='awesome')
    u = User(name='Test',
             email='*****@*****.**',
             passwd='12345',
             image='about:blank')
    await u.save()
Ejemplo n.º 25
0
def test():
    try:
        print('program start')
        yield from www.orm.create_pool(loop, user='******', password='******', db='chen')
        from www.models import User
        u = User(name='Test', email='*****@*****.**', passwd='111111', image='about:blank')
        yield from u.save()
        print('program end')
    except BaseException as e:
        logging.exception(e)
Ejemplo n.º 26
0
async def test(loop):
    await create_pool(loop=loop,
                      user='******',
                      password='******',
                      database='iwblog')
    u = User(name='iwfans',
             email='*****@*****.**',
             passwd='123456',
             image='about:blank')
    await u.save()
Ejemplo n.º 27
0
async def test(loop):
    await www.orm.create_pool(loop=loop,
                              user='******',
                              password='******',
                              db='awesome')
    u = User(name="Test1",
             email="*****@*****.**",
             passwd='1234567890',
             image="about:blank")
    await u.save()
Ejemplo n.º 28
0
async def test(loop):
    await www.web.orm.create_pool(loop,
                                  user='******',
                                  password='******',
                                  db='test')
    u = User(name='Test',
             email='*****@*****.**',
             passwd='123456',
             image='about.blank')
    await u.save()
Ejemplo n.º 29
0
async def test(loop):
    #创建连接池,里面的host,port,user,password需要替换为自己数据库的信息
    __pool = await orm_demo.create_pool(loop,
                                        host='127.0.0.1',
                                        user='******',
                                        password='******',
                                        database='test')
    #没有设置默认值的一个都不能少
    u = User(id='123', name='Test')
    await u.save(__pool)
Ejemplo n.º 30
0
def test():
    yield from orm.create_pool(user='******',
                               password='******',
                               database='awesome')

    u = User(name='Test',
             email='*****@*****.**',
             passwd='1234567890',
             image='about:blank')

    yield from u.save()