示例#1
0
 def delete(self, id):
   schema = MenuSchema()
   sub_menu = schema.dump(Menu.query.filter_by(pid=id).first())
   if sub_menu.data is None  or len(sub_menu.data) == 0:
     Menu.query.filter(Menu.id==id).delete()
     db.session.commit()
     return success()
   else:
     return error(500,'has some submenu of this menu, cat not delete!')
示例#2
0
 def post(self):
   name = request.json.get('name')
   pid = request.json.get('pid')
   schema = MenuSchema()
   res = schema.dump(Menu.query.filter_by(name=name).first())
   if res.data is None or len(res.data) == 0:
     db.session.add(Menu(name, pid))
     db.session.commit()
   return success()
示例#3
0
 def get(self, id):
   if id is not None:
     schema = PostsSchema()
     menu_schema = MenuSchema()
     tag_schema = TagSchema(many=True)
     ret = schema.dump(Posts.query.get(id))
     info = ret.data
     if info is not None and len(info) > 0:
       info['menu'] = menu_schema.dump(Menu.query.get(info['menu_id'])).data
       info['selectedTags'] = tag_schema.dump(Tag.query.filter(Tag.id.in_(info['tags'].split(','))).all()).data
       info['selectedTagIds'] = str(info['tags'])[:-1].split(',')
     return success(ret.data)
   else:
     menu_id = request.args.get('menu_id')
     if menu_id is None:
       schema = PostsSchema(many=True)
       menu_schema = MenuSchema()
       tag_schema = TagSchema(many=True)
       lists = schema.dump(Posts.query.order_by('push_at desc').all()).data
       for i in lists:
         i['menu'] = menu_schema.dump(Menu.query.get(i['menu_id'])).data
         i['tagList'] = tag_schema.dump(Tag.query.filter(Tag.id.in_(i['tags'].split(','))).all()).data
       return success(lists)
     else:
       schema = PostsSchema(many=True)
       menu_schema = MenuSchema()
       tag_schema = TagSchema(many=True)
       lists = schema.dump(Posts.query.filter(Posts.menu_id==menu_id).order_by('push_at desc').all()).data
       for i in lists:
         i['menu'] = menu_schema.dump(Menu.query.get(i['menu_id'])).data
         i['tagList'] = tag_schema.dump(Tag.query.filter(Tag.id.in_(i['tags'].split(','))).all()).data
       return success(lists)
示例#4
0
 def put(self, id):
   schema = MenuSchema()
   name = request.json.get('name')
   pid = request.json.get('pid')
   m = schema.dump(Menu.query.filter(Menu.name==name).filter(Menu.id!=id).first())
   if m.data is None or len(m.data) == 0:
     n = Menu.query.filter(Menu.id==id).first()
     n.name = name
     n.pid = pid
     db.session.commit()
   return success()
示例#5
0
 def get(self, id):
   if id is None:
     schema = MenuSchema(many=True)
     res = schema.dump(Menu.query.filter_by(pid=0).all())     
     r_menu = res.data
     for s in r_menu:
       sub = schema.dump(Menu.query.filter_by(pid=s['id']).all())
       s['sub'] = sub.data
     return success(r_menu)
   else:
     schema = MenuSchema()
     res = schema.dump(Menu.query.filter_by(id=id).first())
     return success(res.data)
示例#6
0
def menu(id=0):
    schema = MenuSchema(many=True)
    res = schema.dump(Menu.query.filter_by(pid=id).all())
    return success(res.data)
示例#7
0
def get_posts(id=None):
    top = request.args.get('top')
    start_id = request.args.get('start_id')
    if id is not None:
        schema = PostsSchema()
        menu_schema = MenuSchema()
        tag_schema = TagSchema(many=True)
        p = Posts.query.get(id)
        ret = schema.dump(p)
        info = ret.data
        if info is not None and len(info) > 0:
            info['menu'] = menu_schema.dump(Menu.query.get(
                info['menu_id'])).data
            info['next'] = schema.dump(
                Posts.query.filter(Posts.id > id).first()).data
            info['prev'] = schema.dump(
                Posts.query.filter(
                    Posts.id < id).order_by('id desc').first()).data
            info['tagList'] = tag_schema.dump(
                Tag.query.filter(Tag.id.in_(
                    info['tags'].split(','))).all()).data
        p.click_num = p.click_num + 1
        db.session.commit()
        return success(info)
    elif top is None:
        menu_id = request.args.get('menu_id')
        if menu_id is None:
            schema = PostsSchema(many=True)
            one_schema = PostsSchema()
            menu_schema = MenuSchema()
            tag_schema = TagSchema(many=True)
            if start_id is not None:
                lists = schema.dump(
                    Posts.query.filter(Posts.id <= int(start_id)).order_by(
                        'push_at desc').limit(9).all()).data
            else:
                lists = schema.dump(
                    Posts.query.order_by('push_at desc').limit(9).all()).data
            last_id = 0
            for i in lists:
                i['menu'] = menu_schema.dump(Menu.query.get(i['menu_id'])).data
                i['tagList'] = tag_schema.dump(
                    Tag.query.filter(Tag.id.in_(
                        i['tags'].split(','))).all()).data
            if len(lists) > 0:
                lastInfo = one_schema.dump(
                    Posts.query.filter(Posts.id < lists[len(lists) - 1]['id']).
                    order_by('push_at desc').first()).data
                if lastInfo is not None and len(lastInfo) > 0:
                    last_id = lastInfo['id']
            ret = {'lists': lists, 'last_id': last_id}
            return success(ret)
        else:
            schema = PostsSchema(many=True)
            one_schema = PostsSchema()
            menu_schema = MenuSchema()
            tag_schema = TagSchema(many=True)

            some_menu_schema = MenuSchema(many=True)
            menus = some_menu_schema.dump(
                Menu.query.filter(Menu.pid == menu_id)).data
            menuids = [menu_id]
            for m in menus:
                menuids.append(m['id'])

            if start_id is not None:
                lists = schema.dump(
                    Posts.query.filter(Posts.menu_id.in_(menuids)).filter(
                        Posts.id <= int(start_id)).order_by(
                            'push_at desc').limit(9).all()).data
            else:
                lists = schema.dump(
                    Posts.query.filter(Posts.menu_id.in_(menuids)).order_by(
                        'push_at desc').limit(9).all()).data
            last_id = 0
            for i in lists:
                i['menu'] = menu_schema.dump(Menu.query.get(i['menu_id'])).data
                i['tagList'] = tag_schema.dump(
                    Tag.query.filter(Tag.id.in_(
                        i['tags'].split(','))).all()).data
            if len(lists) > 0:
                lastInfo = one_schema.dump(
                    Posts.query.filter(Posts.id < lists[len(lists) - 1]['id']).
                    order_by('push_at desc').first()).data
                if lastInfo is not None and len(lastInfo) > 0:
                    last_id = lastInfo['id']
            ret = {'lists': lists, 'last_id': last_id}
            return success(ret)
    else:
        if top is not None:
            schema = PostsSchema(many=True)
            menu_schema = MenuSchema()
            tag_schema = TagSchema(many=True)
            alists = schema.dump(
                Posts.query.order_by('click_num desc').limit(top).all()).data
            for info in alists:
                if info is not None and len(info) > 0:
                    info['menu'] = menu_schema.dump(
                        Menu.query.get(info['menu_id'])).data
                    info['tagList'] = tag_schema.dump(
                        Tag.query.filter(Tag.id.in_(
                            info['tags'].split(','))).all()).data
            return success(alists)