Beispiel #1
0
def user_delete():
    try:
        id = request.form['id'].split(',')
    except KeyError:
        return error(400, u'参数错误')
    if not Category.delete(id):
        return error(10020, 'delete failed')
    return 'delete ok'
Beispiel #2
0
def category_create():
    try:
        name = request.form['name']
    except KeyError:
        return error(400, u'参数错误')
    category = Category.create(**locals())
    if not category:
        return error(100021, 'create category failed')
    return category
Beispiel #3
0
def category_update():
    try:
        name = request.form['name']
        id = request.form['id']
    except KeyError:
        return error(400, u'参数错误')
    category = Category.update(**locals())
    if not category:
        return error(10022, 'update category failed')
    return category
Beispiel #4
0
def news_delete():
    print request.form.getlist('id')

    try:
        id = request.form['id'].split(',')
    except KeyError:
        return error(400, u'参数错误')
    if not News.delete(id):
        return error(10020, 'delete failed')
    return 'delete ok'
Beispiel #5
0
def news_create():
    try:
        title = request.form['title']
        alias_title = request.form['alias_title']
        content = request.form['content']
        cover_url = request.form['cover_url']
        category_id = request.form['category_id']
        auther_id = request.form['auther_id']
    except KeyError:
        return error(400, u'参数错误')
    news = News.create(**locals())
    if not news:
        return error(100021, 'create news failed')
    return news
Beispiel #6
0
 def wrapper():
     if not current_user.is_authenticated:
         return error(10111, 'require login')
     args = func.__name__.split('_')
     mod, action = args[0], args[1]
     group_id = User.get(current_user.get_id()).group_id
     if not group_id:
         return error(10112, 'user cannot find group id')
     auth_value = Group.get(group_id).__getattribute__(mod)
     # print "auth_value=", auth_value
     index = action_arr.index(action)
     index = len(action_arr) - index - 1
     if not (auth_value >> index & 1):
         return error(10113, 'cannot access this action')
     return func()
Beispiel #7
0
def get_news(id):
    if id.isdigit():
        news = News.get(id=id)
    else:
        news = News.get_by_alias(alias=id)
    if not news:
        return error(10003, 'news id not found')
    return news
Beispiel #8
0
def user_update():
    try:
        id = request.form['id']
        username = request.form['username']
        passwd = request.form['passwd']
        nickname = request.form['nickname']
        gender = request.form['gender']
        birthday = request.form['birthday']
        avatar_url = request.form['avatar_url']
        group_id = request.form['group_id']
        if len(username)==0 or len(passwd)==0:
            raise(KeyError)
    except KeyError:
        return error(400, u'参数错误')
    user = User.update(**locals())
    if not user:
        return error(10022, 'update user failed')
    return user
Beispiel #9
0
def login():
    '''
    username: string 
    passwd: 32bit md5 code
    '''
    try:
        username = request.form['username']
        passwd = request.form['passwd']
    except KeyError:
        return error(400, u'参数错误')
    if len(passwd) == 0 or len(username) == 0:
        return error(10005, u'用户名密码不能为空')
    user = User.validate(username, passwd)
    if not user:
        return error(10006, u'用户名或密码错误')
    login_user(user)
    # login_user(user, remember = remember_me)
    return user.dict()
Beispiel #10
0
def news_update():
    keys = []
    values = []
    for k, v in request.form.iteritems():
        if k != 'id':
            keys.append(k)
            values.append(v)
    print keys, values

    try:
        id = request.form['id']
        if len(keys) == 0:
            raise KeyError
    except KeyError:
        return error(400, u'参数错误')
    # clause.decode('gb2312').encode('utf-8')
    news = News.update(id, keys, values)
    return news if news else error(10022, 'update news failed')
Beispiel #11
0
def register():
    try:
        username = request.form['username']
        passwd = request.form['passwd']
        nickname = request.form['nickname']
        gender = request.form['gender']
        birthday = request.form['birthday']
        avatar_url = request.form['avatar_url']
        if len(username)==0 or len(passwd)==0:
            raise(KeyError)
    except KeyError:
        return error(400, u'参数错误')
    if len(gender) == 0:
        gender = 0
    if len(birthday) == 0:
        birthday = '1990-1-1'
    if User.get_by_username(username):
        return error(10008, u'用户已存在')
    passwd = md5.new(passwd).hexdigest()
    user = User.create(username, passwd, nickname, gender, birthday, avatar_url)
    if not user:
        return error(10007, u'创建失败')
    return user
Beispiel #12
0
def category_retrieve():
    id = request.args.get('id', 0)
    if id:
        category = Category.get(id)
        if not category:
            return error(404, 'category not exist')
        return category

    # start = request.args.get('start', 0)
    # limit = int(request.args.get('limit', PAGE_LIMIT))
    # category还要start??, 
    data = {}
    data['start'] = 0
    data['data'] = Category.get_all()
    data['count'] = len(data['data'])

    return data
Beispiel #13
0
def news_retrieve():
    id = request.args.get('id', 0)
    if id:
        news = News.get(id=id)
        if not news:
            return error(404, 'news not exist')
        return news

    start = request.args.get('start', 0)
    limit = int(request.args.get('limit', PAGE_LIMIT))
    if limit > PAGE_MAX:
        limit = PAGE_MAX
    data = {}
    data['start'] = start
    data['data'] = News.get_all('create_time desc', int(start), int(limit))
    data['count'] = len(data['data'])
    data['total'] = News.get_total()
    return data
Beispiel #14
0
def user_retrieve():
    id = request.args.get('id', 0)
    if id:
        user = User.get(id)
        if not user:
            return error(404, 'user not exist')
        return user
    start = request.args.get('start', 0)
    limit = int(request.args.get('limit', PAGE_LIMIT))

    if limit > PAGE_MAX:
        limit = PAGE_MAX

    data = {}
    data['start'] = start
    data['data'] = User.get_all(start, limit)
    data['count'] = len(data['data'])
    data['total'] = User.get_total()
    return data