Beispiel #1
0
def edit(request):
    """Manual add a bookmark to the user account

    Can pass in params (say from a magic bookmarklet later)
    url
    description
    extended
    tags

    """
    rdict = request.matchdict
    params = request.params
    new = False

    with ReqAuthorize(request, username=rdict['username']):

        if 'hash_id' in rdict:
            hash_id = rdict['hash_id']
        elif 'hash_id' in params:
            hash_id = params['hash_id']
        else:
            hash_id = None

        if hash_id:
            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)

            if bmark is None:
                return HTTPNotFound()
        else:
            # hash the url and make sure that it doesn't exist
            url = params.get('url', u"")
            if url != u"":
                new_url_hash = generate_hash(url)

                test_exists = BmarkMgr.get_by_hash(new_url_hash,
                                                   request.user.username)

                if test_exists:
                    location = request.route_url(
                        'user_bmark_edit',
                        hash_id=new_url_hash,
                        username=request.user.username)
                    return HTTPFound(location)

            new = True
            desc = params.get('description', None)
            bmark = Bmark(url, request.user.username, desc=desc)

        tag_suggest = TagMgr.suggestions(
            url=bmark.hashed.url,
            username=request.user.username
        )

        return {
            'new': new,
            'bmark': bmark,
            'user': request.user,
            'tag_suggest': tag_suggest,
        }
Beispiel #2
0
def edit_error(request):
    rdict = request.matchdict
    params = request.params
    post = request.POST

    with ReqAuthorize(request, username=rdict['username']):
        if 'new' in request.url:
            BmarkMgr.store(post['url'], request.user.username,
                           post['description'], post['extended'], post['tags'])

        else:
            if 'hash_id' in rdict:
                hash_id = rdict['hash_id']
            elif 'hash_id' in params:
                hash_id = params['hash_id']

            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)
            if bmark is None:
                return HTTPNotFound()

            bmark.fromdict(post)
            bmark.update_tags(post['tags'])

        # if this is a new bookmark from a url, offer to go back to that url
        # for the user.
        if 'go_back' in params and params['comes_from'] != "":
            return HTTPFound(location=params['comes_from'])
        else:
            return HTTPFound(location=request.route_url(
                'user_bmark_recent', username=request.user.username))
Beispiel #3
0
def edit_error(request):
    rdict = request.matchdict
    params = request.params
    post = request.POST

    with ReqAuthorize(request, username=rdict['username']):
        if 'new' in request.url:
            BmarkMgr.store(post['url'],
                           request.user.username,
                           post['description'],
                           post['extended'],
                           post['tags'])

        else:
            if 'hash_id' in rdict:
                hash_id = rdict['hash_id']
            elif 'hash_id' in params:
                hash_id = params['hash_id']

            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)
            if bmark is None:
                return HTTPNotFound()

            bmark.fromdict(post)
            bmark.update_tags(post['tags'])

        # if this is a new bookmark from a url, offer to go back to that url
        # for the user.
        if 'go_back' in params and params['comes_from'] != "":
            return HTTPFound(location=params['comes_from'])
        else:
            return HTTPFound(
                location=request.route_url('user_bmark_recent',
                                           username=request.user.username))
Beispiel #4
0
def admin_bmark_remove(request):
    """Remove this bookmark from the system"""
    rdict = request.matchdict
    username = rdict.get('username')
    hash_id = rdict.get('hash_id')

    try:
        bmark = BmarkMgr.get_by_hash(hash_id, username=username)
        print bmark
        if bmark:
            DBSession.delete(bmark)
            return {
                'message': "done",
            }
        else:
            return {
                'error': 'Bookmark not found.',
            }

    except NoResultFound:
        request.response.status_code = 404
        return {
            'error':
            'Bookmark with hash id {0} not found.'.format(rdict['hash_id'])
        }
Beispiel #5
0
def admin_bmark_remove(request):
    """Remove this bookmark from the system"""
    rdict = request.matchdict
    username = rdict.get('username')
    hash_id = rdict.get('hash_id')

    try:
        bmark = BmarkMgr.get_by_hash(hash_id,
                                     username=username)
        print bmark
        if bmark:
            DBSession.delete(bmark)
            return _api_response(request, {
                'message': "done",
            })
        else:
            return _api_response(request, {
                'error': 'Bookmark not found.',
            })

    except NoResultFound:
        request.response.status_code = 404
        return _api_response(request, {
            'error': 'Bookmark with hash id {0} not found.'.format(
                rdict['hash_id'])
        })
Beispiel #6
0
def readable(request):
    """Display a readable version of this url if we can"""
    rdict = request.matchdict
    bid = rdict.get('hash_id', None)
    username = rdict.get('username', None)

    if bid:
        found = BmarkMgr.get_by_hash(bid, username=username)
        if found:
            return {
                'bmark': found,
                'username': username,
            }
        else:
            return HTTPNotFound()
Beispiel #7
0
def bmark_remove(request):
    """Remove this bookmark from the system"""
    rdict = request.matchdict
    user = request.user

    try:
        bmark = BmarkMgr.get_by_hash(rdict['hash_id'], username=user.username)
        DBSession.delete(bmark)
        return {
            'message': "done",
        }

    except NoResultFound:
        request.response.status_code = 404
        return {
            'error':
            'Bookmark with hash id {0} not found.'.format(rdict['hash_id'])
        }
Beispiel #8
0
def bmark_remove(request):
    """Remove this bookmark from the system"""
    rdict = request.matchdict
    user = request.user

    try:
        bmark = BmarkMgr.get_by_hash(rdict['hash_id'],
                                    username=user.username)
        DBSession.delete(bmark)
        return {
            'message': "done",
        }

    except NoResultFound:
        request.response.status_code = 404
        return {
            'error': 'Bookmark with hash id {0} not found.'.format(
                        rdict['hash_id'])
        }
Beispiel #9
0
def bmark_get(request):
    """Return a bookmark requested via hash_id

    We need to return a nested object with parts
        bmark
            - readable
    """
    rdict = request.matchdict
    params = request.params

    hash_id = rdict.get('hash_id', None)
    username = request.user.username

    # the hash id will always be there or the route won't match
    bookmark = BmarkMgr.get_by_hash(hash_id,
                                    username=username)

    last_bmark = {}
    if 'last_bmark' in params and params['last_bmark'] != "false":
        last = BmarkMgr.get_recent_bmark(username=username)
        if last is not None:
            last_bmark = {'last':  dict(last)}

    if bookmark is None:
        request.response.status_int = 404
        ret = {'error': "Bookmark for hash id {0} not found".format(hash_id)}
        ret.update(last_bmark)
        return ret
    else:
        return_obj = dict(bookmark)

        if 'with_content' in params and params['with_content'] != 'false':
            if bookmark.readable:
                return_obj['readable'] = dict(bookmark.readable)

        return_obj['tags'] = [dict(tag[1]) for tag in bookmark.tags.items()]

        ret = {'bmark': return_obj}
        ret.update(last_bmark)
        return ret
Beispiel #10
0
def bmark_get(request):
    """Return a bookmark requested via hash_id

    We need to return a nested object with parts
        bmark
            - readable
    """
    rdict = request.matchdict
    params = request.params

    hash_id = rdict.get('hash_id', None)
    username = request.user.username

    # the hash id will always be there or the route won't match
    bookmark = BmarkMgr.get_by_hash(hash_id, username=username)

    last_bmark = {}
    if 'last_bmark' in params and params['last_bmark'] != "false":
        last = BmarkMgr.get_recent_bmark(username=username)
        if last is not None:
            last_bmark = {'last': dict(last)}

    if bookmark is None:
        request.response.status_int = 404
        ret = {'error': "Bookmark for hash id {0} not found".format(hash_id)}
        ret.update(last_bmark)
        return ret
    else:
        return_obj = dict(bookmark)

        if 'with_content' in params and params['with_content'] != 'false':
            if bookmark.readable:
                return_obj['readable'] = dict(bookmark.readable)

        return_obj['tags'] = [dict(tag[1]) for tag in bookmark.tags.items()]

        ret = {'bmark': return_obj}
        ret.update(last_bmark)
        return ret
Beispiel #11
0
                    tags=post['tags'])

                return {
                    'new': True,
                    'bmark': bmark,
                    'message': exc.message,
                    'user': request.user,
                }

        else:
            if 'hash_id' in rdict:
                hash_id = rdict['hash_id']
            elif 'hash_id' in params:
                hash_id = params['hash_id']

            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)
            if bmark is None:
                return HTTPNotFound()

            bmark.fromdict(post)
            bmark.update_tags(post['tags'])

        # if this is a new bookmark from a url, offer to go back to that url
        # for the user.
        if 'go_back' in params and params['comes_from'] != "":
            return HTTPFound(location=params['comes_from'])
        else:
            return HTTPFound(
                location=request.route_url('user_bmark_recent',
                                           username=request.user.username))
Beispiel #12
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),
        }
Beispiel #13
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),
        }