Exemplo n.º 1
0
    def test_command_finds_commands(self):
        """Verify we find commands that we know about"""
        COMMANDLIST['!toread'] = lambda bmark: bmark

        bm = BmarkMock()
        bm.tags['!toread'] = True
        commander = Commander(bm)
        commander.build_commands()

        ok_('!toread' in commander.commands,
                "Our commander should find !toread command to run")
Exemplo n.º 2
0
    def test_command_tags_removed(self):
        """Test that the command tags are not left over in bmark object"""

        COMMANDLIST['!toread'] = CommandMock

        bm = BmarkMock()
        bm.tags['!toread'] = True
        commander = Commander(bm)
        updated = commander.process()

        ok_('!toread' not in updated.tags,
                "Our commander should find !toread command to run")
Exemplo n.º 3
0
    def test_toread_in_commandset(self):
        """Make sure we can process this command through the commander"""
        COMMANDLIST['!toread'] = ToRead

        bm = BmarkMock()
        bm.tags['!toread'] = True
        commander = Commander(bm)
        updated = commander.process()

        ok_('toread' in updated.tags,
                "Should have the toread tag in the updated bookmark")
        ok_('!toread' not in updated.tags,
                "Should not have the !toread tag in the updated bookmark")
Exemplo n.º 4
0
Arquivo: api.py Projeto: cambot/Bookie
def _update_mark(mark, params):
    """Update the bookmark found with settings passed in"""
    mark.description = params.get('description', mark.description)
    mark.extended = params.get('extended', mark.extended)

    new_tag_str = params.get('tags', None)

    # if the only new tags are commands, then don't erase the
    # existing tags
    # we need to process any commands associated as well
    new_tags = TagMgr.from_string(new_tag_str)
    found_cmds = Commander.check_commands(new_tags)

    if new_tag_str and len(new_tags) == len(found_cmds):
        # the all the new tags are command tags, just tack them on
        # for processing, but don't touch existing tags
        for command_tag in new_tags.values():
            mark.tags[command_tag.name] = command_tag
    else:
        if new_tag_str:
            # in this case, rewrite the tags wit the new ones
            mark.update_tags(new_tag_str)

    return mark
Exemplo n.º 5
0
def _update_mark(mark, params):
    """Update the bookmark found with settings passed in"""
    mark.description = params.get('description', mark.description)
    mark.extended = params.get('extended', mark.extended)

    new_tag_str = params.get('tags', None)

    # if the only new tags are commands, then don't erase the
    # existing tags
    # we need to process any commands associated as well
    new_tags = TagMgr.from_string(new_tag_str)
    found_cmds = Commander.check_commands(new_tags)

    if new_tag_str and len(new_tags) == len(found_cmds):
        # the all the new tags are command tags, just tack them on
        # for processing, but don't touch existing tags
        for command_tag in new_tags.values():
            mark.tags[command_tag.name] = command_tag
    else:
        if new_tag_str:
            # in this case, rewrite the tags wit the new ones
            mark.update_tags(new_tag_str)

    return mark
Exemplo n.º 6
0
Arquivo: api.py Projeto: cambot/Bookie
def bmark_add(request):
    """Add a new bookmark to the system"""
    rdict = request.matchdict
    if 'url' in request.params or 'hash_id' in request.params:
        params = request.params
    elif 'url' in request.json_body or 'hash_id' in request.json_body:
        params = request.json_body
    else:
        request.response.status_int = 400
        return {
            'error': 'Bad Request: missing url',
         }

    user = request.user

    if 'url' not in params and 'hash_id' not in rdict:
        request.response.status_int = 400
        return {
            'error': 'Bad Request: missing url',
         }

    elif 'hash_id' in rdict:
        try:
            mark = BmarkMgr.get_by_hash(rdict['hash_id'],
                                       username=user.username)
            mark = _update_mark(mark, params)

        except NoResultFound:
            request.response.status_code = 404
            return {
                'error': 'Bookmark with hash id {0} not found.'.format(
                            rdict['hash_id'])
            }

    else:
        # check if we already have this
        try:
            mark = BmarkMgr.get_by_url(params['url'],
                                       username=user.username)
            mark = _update_mark(mark, params)

        except NoResultFound:
            # then let's store this thing
            # if we have a dt param then set the date to be that manual
            # date
            if 'dt' in request.params:
                # date format by delapi specs:
                # CCYY-MM-DDThh:mm:ssZ
                fmt = "%Y-%m-%dT%H:%M:%SZ"
                stored_time = datetime.strptime(request.params['dt'], fmt)
            else:
                stored_time = None

            # check to see if we know where this is coming from
            inserted_by = params.get('inserted_by', 'unknown_api')

            mark = BmarkMgr.store(params['url'],
                         user.username,
                         params.get('description', ''),
                         params.get('extended', ''),
                         params.get('tags', ''),
                         dt=stored_time,
                         inserted_by=inserted_by,
                   )

        # we need to process any commands associated as well
        commander = Commander(mark)
        mark = commander.process()

        # if we have content, stick it on the object here
        if 'content' in params:
            content = StringIO(params['content'])
            content.seek(0)
            parsed = ReadContent.parse(content,
                                       content_type="text/html",
                                       url=mark.hashed.url)

            mark.readable = Readable()
            mark.readable.content = parsed.content
            mark.readable.content_type = parsed.content_type
            mark.readable.status_code = parsed.status
            mark.readable.status_message = parsed.status_message

        # we need to flush here for new tag ids, etc
        DBSession.flush()

        mark_data = dict(mark)
        mark_data['tags'] = [dict(mark.tags[tag]) for tag in mark.tags.keys()]

        return {
            'bmark': mark_data,
            'location': request.route_url('bmark_readable',
                                          hash_id=mark.hash_id,
                                          username=user.username),
        }
Exemplo n.º 7
0
def bmark_add(request):
    """Add a new bookmark to the system"""
    rdict = request.matchdict
    if 'url' in request.params or 'hash_id' in request.params:
        params = request.params
    elif 'url' in request.json_body or 'hash_id' in request.json_body:
        params = request.json_body
    else:
        request.response.status_int = 400
        return {
            'error': 'Bad Request: missing url',
        }

    user = request.user

    if 'url' not in params and 'hash_id' not in rdict:
        request.response.status_int = 400
        return {
            'error': 'Bad Request: missing url',
        }

    elif 'hash_id' in rdict:
        try:
            mark = BmarkMgr.get_by_hash(rdict['hash_id'],
                                        username=user.username)
            mark = _update_mark(mark, params)

        except NoResultFound:
            request.response.status_code = 404
            return {
                'error':
                'Bookmark with hash id {0} not found.'.format(rdict['hash_id'])
            }

    else:
        # check if we already have this
        try:
            mark = BmarkMgr.get_by_url(params['url'], username=user.username)
            mark = _update_mark(mark, params)

        except NoResultFound:
            # then let's store this thing
            # if we have a dt param then set the date to be that manual
            # date
            if 'dt' in request.params:
                # date format by delapi specs:
                # CCYY-MM-DDThh:mm:ssZ
                fmt = "%Y-%m-%dT%H:%M:%SZ"
                stored_time = datetime.strptime(request.params['dt'], fmt)
            else:
                stored_time = None

            # check to see if we know where this is coming from
            inserted_by = params.get('inserted_by', 'unknown_api')

            mark = BmarkMgr.store(
                params['url'],
                user.username,
                params.get('description', ''),
                params.get('extended', ''),
                params.get('tags', ''),
                dt=stored_time,
                inserted_by=inserted_by,
            )

        # we need to process any commands associated as well
        commander = Commander(mark)
        mark = commander.process()

        # if we have content, stick it on the object here
        if 'content' in params:
            content = StringIO(params['content'])
            content.seek(0)
            parsed = ReadContent.parse(content,
                                       content_type="text/html",
                                       url=mark.hashed.url)

            mark.readable = Readable()
            mark.readable.content = parsed.content
            mark.readable.content_type = parsed.content_type
            mark.readable.status_code = parsed.status
            mark.readable.status_message = parsed.status_message

        # we need to flush here for new tag ids, etc
        DBSession.flush()

        mark_data = dict(mark)
        mark_data['tags'] = [dict(mark.tags[tag]) for tag in mark.tags.keys()]

        return {
            'bmark':
            mark_data,
            'location':
            request.route_url('bmark_readable',
                              hash_id=mark.hash_id,
                              username=user.username),
        }