Esempio n. 1
0
def toggle_follow_user(follower, followee, action):
    db = current.db
    is_following = any(followee['id'] == id for id in follower['follow_users'])

    if action == 'follow' and not is_following:
        #update users list
        db_user = db.auth_user(follower['id'])
        db_user.follow_users.append(followee['id'])

        #update boards list
        for b in followee['boards']:
            if not b in db_user['follow_boards']:
                db_user['follow_boards'].append(b)

        db_user.update_record()
        cacher.delete(db_user['id'])

    elif action == 'unfollow' and is_following:
        #update users list
        db_user = db.auth_user(follower['id'])
        db_user.follow_users.remove(followee['id'])

        #update boards list
        for b in followee['boards']:
            if b in db_user['follow_boards']:
                db_user['follow_boards'].remove(b)

        db_user.update_record()
        cacher.delete(db_user['id'])
Esempio n. 2
0
def fix():
    users = db().select(db.auth_user.ALL)
    for u in users:
        boards = db(db.board.user == u.id).select()
        u.boards = []
        for b in boards:
            u.boards.insert(0,b.id)

        u.update_record()
        cacher.delete(u)

    return 'success'
Esempio n. 3
0
def toggle_follow_board(user, board, action):
    db = current.db
    is_following = any(board['id'] == id for id in user['follow_boards'])

    if action == 'follow' and not is_following:
        db_user = db.auth_user(user['id'])
        db_user.follow_boards.append(board['id'])
        db_user.update_record()
        cacher.delete(db_user['id'])

    elif action == 'unfollow' and is_following:
        db_user = db.auth_user(user['id'])
        db_user.follow_boards.remove(board['id'])
        db_user.update_record()
        cacher.delete(db_user['id'])
Esempio n. 4
0
def add_repin():
    pin = cacher.get('pin', long(request.vars.id))
    board = cacher.get('board', long(request.vars.repin_board))

    if pin and board:
        repin = db((db.pin.article==pin['article'])&(db.pin.board==board['id'])).select().first()
        if repin is None:
            article = cacher.get('article', pin['article'])
            repin = logic.add_pin(article, board)
            repin.original_pin = pin['id']
            repin.update_record()
            cacher.delete(repin.id)
            if request.vars.linkedin and request.env.http_host != '127.0.0.1:8080':
                logic.share_on_linkedin(session.linkedin, repin)

    return 'Success'
Esempio n. 5
0
def test():
    articles = db().select(db.article.ALL)

    for a in articles:
        if len(a.pins) == 0:
            pins = db(db.pin.article == a.id).select()
            pins = sorted(pins, key=lambda p: p.created_on)

            if len(pins) > 0:
                for p in pins:
                    a.pins.insert(0, p.id)
                    a.pins_created_on.insert(0, p.created_on)

                a.update_record()
                cacher.delete(a.id)

    return 'success'
Esempio n. 6
0
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'
Esempio n. 7
0
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'
Esempio n. 8
0
def add_pin(article, board):
    db = current.db

    pin = db((db.pin.article==article['id'])&(db.pin.board==board['id'])).select().first()
    if pin == None:
        pin = db.pin.insert(
            article=article['id'],
            article_title = article['title'],
            article_domain = article['domain'],
            article_excerpt = article['excerpt'],
            board=board['id'],
            board_name=board['name'],
            user = board['user'],
            user_name = board['user_name'],
        )

        #put it in the cache
        cacher.get('pin', pin.id)

        #get from db as board might be from cache
        db_board = db.board(board['id'])
        db_board.pins.insert(0, pin.id)
        db_board.pins_created_on.insert(0, pin.created_on)
        db_board.update_record()
        cacher.delete(board['id'])

        #update article from db
        db_article = db.article(article['id'])
        db_article.pins.insert(0, pin.id)
        db_article.pins_created_on.insert(0, pin.created_on)
        db_article.update_record()
        cacher.delete(article['id'])

        #update user pins
        result = get_user_pins(db_board.user.id, 0, 500)
        ids = []
        ts = []
        for (i,t,s) in result:
            ids.append(i)
            ts.append(s)

        db_board.user.pins = ids
        db_board.user.pins_created_on = ts
        db_board.user.update_record()
        cacher.delete(db_board.user.id)

        _add_pin_to_category(pin)

        return pin

    return None
Esempio n. 9
0
def flush():
    id = long(request.vars.id)
    return cacher.delete(id)