def index(): if auth.user is None: return all() current_user = cacher.get('auth_user', auth.user.id) response.title = 'Pinformation :: {0}'.replace('{0}', current_user['full_name']) boards = cacher.get_multi('board', current_user['boards']).values() result = logic.get_follow_boards_pins(auth.user.id, 0, page_size) api_url = URL('api', 'pins') # if we want to add myself # me = logic.get_user_pins(auth.user.id, 0, page_size) # result.extend(me) # result = sorted(all, key=lambda (id,t,s): t, reverse=True) pin_ids = [id for (id,t,s) in result] dic_pins = cacher.get_multi('pin', pin_ids) pins = [dic_pins[id] for id in pin_ids] if len(pins) == 0 and len(boards) == 0: redirect(URL('default','welcome')) return dict(show_pinner=True, categories=get_all_categories(), boards=boards, pins=pins, api_url=api_url)
def boards(): ##if empty "/boards" return to main if len(request.args)==0: redirect(URL('index')) id = long(request.args(0)) current_board = cacher.get('board', id) or redirect(URL('index')) response.title = 'Pinformation :: {0}'.replace('{0}', current_board['name']) api_url = URL('api', 'boards', args=[current_board['id'], 'pins']) top_pins = current_board['pins'][:page_size] dic_pins = cacher.get_multi('pin', top_pins) pins = [dic_pins[id] for id in top_pins] action = None boards = [] if auth.user: current_user = cacher.get('auth_user', auth.user.id) boards = cacher.get_multi('board', current_user['boards']).values() is_following = any(current_board['id'] == id for id in current_user['follow_boards']) #update the follow button if is_following: action = 'unfollow' else: action = 'follow' ##perform actions on user if request.args(1): action = request.args(1).lower() if action == 'follow' or action == 'unfollow': logic.toggle_follow_board(current_user, current_board, action) redirect(URL('boards', args=[current_board['id']])) return dict(current_board=current_board, pins=pins, boards=boards, show_pinner=False, action=action, api_url=api_url, categories=get_all_categories())
def articles(): ids = logic.search('article', request.vars.q, 0, page_size) result = cacher.get_multi('article', ids).values() pin_ids = [] for article in result: if len(article['pins']) > 0: pin_ids.append(article['pins'][0]) pins = cacher.get_multi('pin', pin_ids).values() pins = sorted(pins, key=lambda p: p['created_on'], reverse=True) api_url = URL('api', 'search', args=['articles', request.vars.q]) return dict(pins=pins, api_url=api_url)
def GET(kind,terms,page=0): if kind == 'articles': response.view = 'api/pins.html' page = max(0, int(page)-1) ids = logic.search('article', terms, page*page_size, (page+1)*page_size) result = cacher.get_multi('article', ids).values() pin_ids = [] for article in result: if len(article['pins']) > 0: pin_ids.append(article['pins'][0]) pins = cacher.get_multi('pin', pin_ids).values() pins = sorted(pins, key=lambda p: p['created_on'], reverse=True) return _serialize(dict(pins=pins))
def GET(page=0): page = max(0, int(page)-1) result = logic.get_follow_boards_pins(auth.user.id, page*page_size, (page+1)*page_size) pin_ids = [id for (id,t,s) in result] dic_pins = cacher.get_multi('pin', pin_ids) pins = [dic_pins[id] for id in pin_ids] return _serialize(dict(pins=pins, show_pinner=True))
def add_comment(): if len(request.vars.comment) > 0 and re.match('(^[\w\s.-]+$)', request.vars.comment): pin = db.pin(long(request.vars.pin_id)) user = cacher.get('auth_user', auth.user.id) if pin: comment = db.comment.insert( user=user['id'], user_name=user['full_name'], user_picture_url=user['picture_url'], pin=pin['id'], content=request.vars.comment ) pin.comments.append(comment.id) pin.update_record() cacher.delete(pin.id) try: users = [pin['user']] comments = cacher.get_multi('auth_user', pin['comments']).values() for c in comments: users.append(c['user']) users = cacher.get_multi('auth_user', users).values() if len(users) > 0: usernames = [] for u in users: if not u['id'] == user['id']: usernames.append(u['username']) subject = '{0} commented on "{1}"'.replace('{0}',user['full_name']).replace('{1}', pin['article_title']) msg_template = '{0}\n\n ---------------------------------------------------------------\nwww.pinformation.co is an innovative way to curate and share information on the web.\nWe help you become better at what you do.' msg = '{0} commented on "{1}"\n\nClick the link to continue the conversation: http://www.pinformation.co{2}' msg = msg.replace('{0}', user['full_name']).replace('{1}', pin['article_title']).replace('{2}', URL('default', 'pins', args=[pin['id']])) msg = msg_template.replace('{0}', msg) result = session.linkedin.send_message(subject, msg, usernames, False) except: pass return 'Success' else: return 'please use only alpha numeric characters'
def GET(id, param, page=0): response.view = 'api/pins.html' page = max(0, int(page)-1) id = long(id) user = cacher.get('auth_user', id) top_pins = user['pins'][min(page*page_size, len(user['pins'])):min((page+1)*page_size, len(user['pins']))] dic_pins = cacher.get_multi('pin', top_pins) pins = [dic_pins[id] for id in top_pins] return _serialize(dict(pins=pins, show_pinner=False))
def people(): ids = logic.search('auth_user', request.vars.q, 0, page_size) users = cacher.get_multi('auth_user', ids).values() if auth.user: current_user = cacher.get('auth_user', auth.user.id) for u in users: if any(u['id'] == id for id in current_user['follow_users']): u['action'] = 'unfollow' else: u['action'] = 'follow' return dict(results=users)
def GET(id): id = long(id) pin = cacher.get('pin', id) or redirect(URL('index')) article = logic.get_article(pin['article']) if auth.user: user = cacher.get('auth_user', auth.user.id) else: user = None; comments = cacher.get_multi('comment', pin['comments']).values() comments = sorted(comments, key=lambda c: c['created_on']) return dict(user=user, pin=pin, article=article, comments=comments)
def get_pins_by_category(cat, start, end): ids = [] if cat == None: ids = _get_all_pins() else: #get updated list from cache - db saves once a day cat = cacher.get('category', cat['id']) ids = cat['pins'] if ids and len(ids)>0: ids = ids[start:end] dic_pins = cacher.get_multi('pin', ids) pins = [dic_pins[id] for id in ids] return pins return []
def get_users_by_username(usernames): if usernames is None or len(usernames) == 0: return None result = {} #username -> id from cache, get -1 if we know its not in the db keys = ['user.username=%s' % un for un in usernames] cached = memcache.get_multi(keys) #find cached users if cached and len(cached) > 0: cached_ids = [] for username, id in cached.iteritems(): if id > 0: cached_ids.append(id) cached_users = cacher.get_multi('auth_user', cached_ids) for u in cached_users.values(): result[u['username']] = u # find all the usernames that were not in the cache, ignore if -1 exists in the cache db_usernames = [] for un in usernames: if not cached.has_key('user.username=%s' % un): db_usernames.append(un) #break it to lists that that are not bigger than 30, GAE can't do belongs with over 30 results db = current.db list_size = 30 lists = [db_usernames[i:i+list_size] for i in xrange(0, len(db_usernames), list_size)] to_cache = {} #query db per list for list in lists: users = db(db.auth_user.username.belongs(list)).select().as_list() for u in users: result[u['username']] = u to_cache['user.username=%s' % u['username']] = u['id'] #set all usernames that does not exist in db as -1 in cache for un in usernames: if not result.has_key(un) and not cached.has_key('user.username=%s' % un): to_cache['user.username=%s' % un] = -1 memcache.set_multi(to_cache) return result
def pins(): ##if empty "/pins" return to main if len(request.args)==0: redirect(URL('index')) if auth.user: user = cacher.get('auth_user', auth.user.id) else: user = {} id = long(request.args(0)) pin = cacher.get('pin', id) or redirect(URL('index')) article = logic.get_article(pin['article']) board = cacher.get('board', pin['board']) comments = cacher.get_multi('comment', pin['comments']).values() comments = sorted(comments, key=lambda c: c['created_on']) response.title = 'Pinformation :: {0}'.replace('{0}', pin['article_title']) return dict(user=user, pin=pin, article=article, comments=comments, board=board, categories=get_all_categories())
def all(): if request.vars.category: cat_name = request.vars.category category = db(db.category.name==cat_name.lower()).select().first() if category: api_url = URL('api', 'categories', args=[category['id'], 'pins']) else: api_url = URL('api', 'categories', args=['all', 'pins']) else: category = None api_url = URL('api', 'categories', args=['all', 'pins']) pins = logic.get_pins_by_category(category, 0, page_size) boards = [] if auth.user: current_user = cacher.get('auth_user', auth.user.id) boards = cacher.get_multi('board', current_user['boards']).values() return dict(show_pinner=True, current_category=category, categories=get_all_categories(), boards=boards, pins=pins, api_url=api_url)
def add_board(): name = request.vars.name if re.match('(^[\w\s.-]+$)', name): #sanitize the board name category = cacher.get('category', long(request.vars.category)) user = cacher.get('auth_user', auth.user.id) if category and user: boards = cacher.get_multi('board', user['boards']).values() already_exists = any(name == b['name'] for b in boards) if not already_exists: board = db.board.insert( user=user['id'], user_name = user['full_name'], category=category['id'], name=name ) board.user.boards.append(board) board.user.update_record() cacher.delete(board.user.id) ##add the board to every user that follow this one ##should be moved to async when we have async try: users = db(db.auth_user.follow_users.contains(board.user.id)).select() for u in users: logic.toggle_follow_board(u, board, 'follow') except: pass return 'Success' else: return 'You already have a board with that name' else: return 'category does not exist' return 'please use only alpha numeric characters'
def boards(): ids = logic.search('board', request.vars.q, 0, page_size) result = cacher.get_multi('board', ids).values() return dict(results=result)
def get_follow_boards_pins(id, start, end): user = cacher.get('auth_user', id) boards = cacher.get_multi('board', user['follow_boards']).values() return _get_boards_pins(boards, start, end)