Beispiel #1
0
def authenticate(*, email, passwd):
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid passwd.')
    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():
        logging.info('passwd:%s,sha1:%s' % (user.passwd, sha1.hexdigest()))
        raise APIValueError('passwd', 'password error.')
    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
Beispiel #2
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.gravatar.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
Beispiel #3
0
def manage_blogs(*, page='1'):
    page_index = get_page_index(page)
    blogCount = yield from Blog.findNumber('count(id)')
    page = PageManager(blogCount, page_index)
    blogs = yield from Blog.findAll(orderBy='created_at desc',
                                    limit=(page.offset, page.limit))
    admins = yield from User.findAll('admin = 1')
    admin = None
    if len(admins):
        admin = admins[0]
    if admin:
        admin.tagLen = yield from getTagLen()
    return {
        '__template__': 'manage_blogs.html',
        'blogCount': blogCount,
        'page_index': page_index,
        'page': page,
        'blogs': blogs,
        'admin': admin
    }
Beispiel #4
0
def upload_icon(request):
    reader = yield from request.multipart()
    image_data = yield from reader.next()
    filename = image_data.filename
    extention = os.path.splitext(filename)[1]
    filename = img_name(extention)
    logging.info(image_data)
    size = 0
    upload_path = '/upload/my/icon/'
    if not os.path.exists(upload_path):
        os.makedirs(upload_path)
    with open(os.path.join(upload_path, filename), 'wb') as f:
        while True:
            chunk = yield from image_data.read_chunk(
            )  # 8192 bytes by default.
            if not chunk:
                break
            size += len(chunk)
            f.write(chunk)
    admins = yield from User.findAll('admin = 1')
    admin = admins[0]
    admin.image = os.path.join('../upload/my/icon/', filename)
    yield from admin.update()
def index(request):
    users = yield from User.findAll()
    return {
        '__template__' : 'test.html',
        'users': users
    }
Beispiel #6
0
def getAdmin():
    admins = yield from User.findAll('admin = 1')
    admin = None
    if len(admins):
        admin = admins[0]
    return admin