Exemplo n.º 1
0
def ls(rw):
    form = Form(rw)
    token = form['token']
    range = form.get('range')
    try:
        code = yun.runtime.tokenizer.unpack(token)
    except VerificationError:
        return DATA_INVALID_TOKEN
    else:
        phone = code.split('&')[0]
        mydb, user_id = get_userdb_by_phone(phone)

    filetype = form.get('type', '')
    if filetype:
        filetype = 'AND type={}'.format(filetype)

    ext = tuple(filter(None, set(form.get('ext', '').split(','))))
    if len(ext) > 1:
        ext_str = 'AND extension IN {}'.format(ext)
    elif len(ext) == 1:
        ext_str = 'AND extension IN ("{}")'.format(ext[0])
    else:
        ext_str = ''

    pid = rw.environ['locals.api_info'].lstrip('/') or ROOT
    if range == 'all':
        pid_str = ''
    else:
        pid_str = 'AND parent="{}"'.format(pid)

    page = (int(form.get('page', 1)) - 1) * SELECT_LIMIT
    files = mydb.cursor().execute(
        ('SELECT creator, phone, name, type, extension, size,'
         ' parent, path, uuid, create_time, modify_time, creator'
         ' FROM (SELECT * FROM fs WHERE state=0 AND creator=? '
         ' {} {} {} '
         ' ORDER BY create_time DESC)'
         ' LIMIT ? OFFSET ?').format(filetype, ext_str, pid_str),
        (user_id, SELECT_LIMIT, page)).fetchall()
    return {'code': 0, 'msg': 'ok', 'data': files}
Exemplo n.º 2
0
def headline(rw):
    form = Form(rw)
    uuid = form.get('uuid')
    mydb, user_id = get_userspace_by_permission(1)
    if uuid is None:
        article = mydb.cursor().execute(
            ('SELECT uuid, title FROM article WHERE state=0 '
             'ORDER BY create_time DESC '
             'LIMIT 5 OFFSET 0')).fetchall()
    else:
        article = mydb.cursor().execute((
            'SELECT title, desc, create_time, content, img, creator FROM article WHERE state=0 AND uuid=?'
        ), (uuid, )).fetchone()
        article = [article]
    return {'code': 0, 'msg': 'ok', 'data': article}
Exemplo n.º 3
0
def create(rw):
    form = Form(rw)
    name = form['name']
    title = form['title']
    desc = form['desc']
    img = form['img']
    content = form['content']
    token = form['token']
    pid = form['pid'] or ROOT
    if not (name and title and desc and content and img):
        return {'code': 111, 'msg': '缺少必要参数', 'data': []}
    pid = form.get('pid') or ROOT
    path = '/'
    try:
        data = yun.runtime.tokenizer.unpack(token)
    except VerificationError:
        return DATA_INVALID_TOKEN
    else:
        phone = data.split('&')[0]
        mydb, user_id = get_userdb_by_phone(phone)

    cr = mydb.cursor()
    exsit = cr.execute(
        'SELECT COUNT(*) FROM fs WHERE state=0 AND creator=? AND name=? AND parent=?',
        (user_id, name, pid)).fetchone()[0]
    if exsit > 0:
        return {'code': 112, 'msg': '卡片已存在', 'data': []}
    if pid != ROOT:
        path = cr.execute('SELECT path FROM fs WHERE state=0 AND uuid=?',
                          (pid, )).fetchone()[0]
        if path == '/':
            path = '{}{}'.format(path, pid)
        else:
            path = '{}/{}'.format(path, pid)
    create_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')
    uuid_ = uuid4().hex
    cr.execute(('INSERT INTO fs (creator, phone, state, type, name, '
                ' uuid, create_time, modify_time, parent, path) '
                ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'),
               (user_id, phone, 0, 4, name, uuid_, create_time, create_time,
                pid, path))
    cr.execute(('INSERT INTO article (creator, uuid, phone, state, type, '
                ' name, title, desc, img, content, create_time, modify_time) '
                ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'),
               (user_id, uuid_, phone, 0, 1, name, title, desc, img, content,
                create_time, create_time))
    return DATA_OK
Exemplo n.º 4
0
def ls(rw):
    form = Form(rw)
    token = form['token']
    try:
        code = yun.runtime.tokenizer.unpack(token)
    except VerificationError:
        return DATA_INVALID_TOKEN
    else:
        phone = code.split('&')[0]
        mydb, user_id = get_userdb_by_phone(phone)

    page = (int(form.get('page', 1)) - 1) * SELECT_LIMIT
    articles = mydb.cursor().execute(
        ('SELECT * FROM (SELECT * FROM article WHERE state=0 AND creator=? '
         ' ORDER BY create_time DESC)'
         ' LIMIT ? OFFSET ?'), (user_id, SELECT_LIMIT, page)).fetchall()
    return {'code': 0, 'msg': 'ok', 'data': articles}
Exemplo n.º 5
0
def mkdir(rw):
    form = Form(rw)
    name = form['name']
    if not name:
        return {'code': 41, 'msg': '文件名错误', 'data': []}
    token = form['token']
    pid = form.get('pid') or ROOT
    path = '/'
    try:
        data = yun.runtime.tokenizer.unpack(token)
    except VerificationError:
        return DATA_INVALID_TOKEN
    else:
        phone = data.split('&')[0]
        mydb, user_id = get_userdb_by_phone(phone)

    cr = mydb.cursor()
    exsit = cr.execute(
        'SELECT COUNT(*) FROM fs WHERE type=1 AND state=0 AND creator=? AND name=? AND parent=?',
        (user_id, name, pid)).fetchone()[0]
    if exsit > 0:
        return {'code': 42, 'msg': '目录已存在', 'data': []}
    if pid != ROOT:
        path = cr.execute('SELECT path FROM fs WHERE state=0 AND uuid=?',
                          (pid, )).fetchone()[0]
        if path == '/':
            path = '{}{}'.format(path, pid)
        else:
            path = '{}/{}'.format(path, pid)
    create_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')
    cr.execute(('INSERT INTO fs (creator, phone, state, type, name '
                ', uuid, create_time, modify_time, parent, path) '
                ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'),
               (user_id, phone, 0, 1, name, uuid4().hex, create_time,
                create_time, pid, path))
    return DATA_OK