def set_tag(): """ { name: ['tagname'], tab: 'tabname' } """ try: data = json.loads(request.data) tags = data['name'] tabname = data['tab'] assert isinstance(tabname, basestring) assert isinstance(tags, list) for tag in tags: assert isinstance(tag, basestring) except: return {'error': 'illegal format'} u = get_user(current_user.username) for tab in u['tab']: if tabname == tab['name']: tab['tags'] = tags get_mongo('user').save(u) log_info('user {0} set tag to {1} on tab \ {2}'.format(current_user.username, tags, tabname)) return {'tabs': u['tab']} return {'error': 'no such tab'}
def del_tag(): """ GET /del_tag?name=xxx&tab=xxx """ try: data = request.args tagname = data['name'] tabname = data['tab'] assert isinstance(tagname, basestring) \ and isinstance(tabname, basestring) except: return {'error': 'illegal format'} u = get_user(current_user.username) for tab in u['tab']: if tabname == tab['name']: try: tab['tags'].remove(tagname) except ValueError: return {'error': 'tag {0} not in tab {1}'.format(tagname, tabname)} get_mongo('user').save(u) log_info('user {0} del tag {1} in tab \ {2}'.format(current_user.username, tagname, tabname)) return {'tabs': u['tab']} return {'error': 'no such tab'}
def add_tag(): """ { name: 'tagname', tab: 'tabname' } """ try: data = json.loads(request.data) tagname = data['name'] tabname = data['tab'] assert isinstance(tagname, basestring) \ and isinstance(tabname, basestring) except: return {'error': 'illegal format'} u = get_user(current_user.username) for tab in u['tab']: if tabname == tab['name']: l = tab['tags'] l.append(tagname) l = list(set(l)) tab['tags'] = l get_mongo('user').save(u) log_info('user {0} add tag {1} to tab \ {2}'.format(current_user.username, tagname, tabname)) return {'tabs': u['tab']} return {'error': 'no such tab'}
def get_tab_article(): """get all articles under a tab GET /get_tab_article?tab=tabname """ try: tabname = request.args['tab'] assert isinstance(tabname, basestring) except: return {'error': 'illegal format'} db = get_user(current_user.username) tab = filter(lambda x: x['name'] == tabname, db['tab']) if len(tab) == 0: return {'error': 'no such tab'} tags = tab[0]['tags'] itemdb = get_mongo('item') rst = list(itemdb.find({'tag': {'$in': tags}, '$or': [ {'fetcher_type': FETCHER_TYPE_GENERAL}, { 'fetcher_type': FETCHER_TYPE_USER, 'other.user_id': current_user.username } ]}, {'_id': 0})) rst = parse_article(rst) return {'data': rst}
def add_tab(): """add a new tab to current user { name: 'string' # name of the tab priority: integer # bigger, better. default to be 0 } """ try: data = json.loads(request.data) name = data['name'] priority = int(data.get('priority', 0)) assert isinstance(name, basestring) except: return {'error': 'illegal format'} username = current_user.username doc = get_user(username) for tab in doc['tab']: if tab['name'] == name: return {'error': 'tab with this name already exists!'} doc['tab'].append({ 'name': name, 'priority': priority, 'tags': [] }) get_mongo('user').save(doc) log_info('user {0} add tab {1}'.format(username, name)) return {'success': 1}
def register(): """user registration api. username: string password: string XXX TODO """ try: username = request.values['username'] password = request.values['password'] assert isinstance(username, basestring) assert isinstance(password, basestring) except: return {'error': 'illegal format'} if len(username) < 3 or len(password) < 3: return {'error': 'length of username and password must' + ' be at least 3 characters'} exist = get_user(username) if exist: return {"error": "user {0} already exists".format(username)} db = get_mongo('user') db.insert({ 'username': username, 'password': password, 'tab': [] }) log_info('new user: {0}:{1}'.format(username, password)) return {'success': 1}
def get_all_tabs(): tabs = get_user(current_user.username)['tab'] return {'tabs': tabs}