Esempio n. 1
0
File: utils.py Progetto: akn/Bookie
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    with_content
        is always GET and specifies if we're searching the fulltext of pages

    Ajax Requests:
        We also use this method to serve ajax requests
        If we have _ajax in the name of the route it's an ajax match
        Make sure we sent out the proper MorJSON response

    """
    route_name = request.matched_route.name

    mdict = request.matchdict
    rdict = request.GET

    if 'terms' in mdict:
        phrase = " ".join(mdict['terms'])
    else:
        phrase = rdict.get('search', '')

    # with content is always in the get string
    with_content = asbool(rdict.get('content', False))
    LOG.debug('with_content')
    LOG.debug(with_content)

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    res_list = searcher.search(phrase, content=with_content)

    # if the route name is search_ajax we want a json response
    # else we just want to return the payload data to the mako template
    if 'ajax' in route_name:
        html = render('bookie:templates/utils/results.mako',
                    { 'search_results': res_list,
                      'result_count': len(res_list),
                      'phrase': phrase,
                      'with_content': with_content,
                    },
                  request=request)
        return {
            'success': True,
            'message': "",
            'payload': {
                'html': html,
            }
        }

    else:
        return {
            'search_results': res_list,
            'result_count': len(res_list),
            'phrase': phrase,
            'with_content': with_content,
        }
Esempio n. 2
0
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    """
    route_name = request.matched_route.name
    mdict = request.matchdict
    rdict = request.GET

    username = rdict.get('username', None)

    if 'terms' in mdict:
        phrase = " ".join(mdict['terms'])
    else:
        phrase = rdict.get('search', '')

    # Always search the fulltext content
    with_content = True

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    # check if we have a page count submitted
    params = request.params
    page = params.get('page', 0)
    count = params.get('count', 50)

    res_list = searcher.search(phrase,
                               content=with_content,
                               username=username,
                               ct=count,
                               page=page,
                               )

    # if the route name is search_ajax we want a json response
    # else we just want to return the payload data to the mako template
    if 'ajax' in route_name or 'api' in route_name:
        return {
            'success': True,
            'message': "",
            'payload': {
                'search_results': [dict(res) for res in res_list],
                'result_count': len(res_list),
                'phrase': phrase,
                'page': page,
                'username': username,
            }
        }
    else:
        return {
            'search_results': res_list,
            'count': len(res_list),
            'max_count': 50,
            'phrase': phrase,
            'page': page,
            'username': username,
        }
Esempio n. 3
0
    def test_get_handler(self):
        """Verify we get the right type of full text store object"""
        sqlite_str = "sqlite:///somedb.db"

        handler = get_fulltext_handler(sqlite_str)

        ok_(isinstance(handler, SqliteFulltext),
            "Should get a sqlite fulltext handler")
Esempio n. 4
0
    def test_get_handler(self):
        """Verify we get the right type of full text store object"""
        sqlite_str = "sqlite:///somedb.db"

        handler = get_fulltext_handler(sqlite_str)

        ok_(isinstance(handler, SqliteFulltext),
                "Should get a sqlite fulltext handler")
Esempio n. 5
0
def posts_add(request):
    """Add a new bmark into the system given request params

    For example usage make sure to check out the unit tests in the
    test_delicious directory

    """
    params = request.GET

    with Authorize(request.registry.settings.get('api_key', ''),
                   params.get('api_key', None)):

        request.response_content_type = 'text/xml'
        if 'url' in params and params['url']:
            # check if we already have this
            try:
                mark = BmarkMgr.get_by_url(params['url'])

                mark.description = params.get('description', mark.description)
                mark.extended = params.get('extended', mark.extended)

                new_tags = params.get('tags', None)
                if new_tags:
                    mark.update_tags(new_tags)

            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

                # we want to store fulltext info so send that along to the
                # import processor
                conn_str = request.registry.settings.get(
                    'sqlalchemy.url', False)
                fulltext = get_fulltext_handler(conn_str)

                BmarkMgr.store(
                    params['url'],
                    params.get('description', ''),
                    params.get('extended', ''),
                    params.get('tags', ''),
                    dt=stored_time,
                    fulltext=fulltext,
                )

            return '<result code="done" />'
        else:
            return '<result code="Bad Request: missing url" />'
Esempio n. 6
0
def posts_add(request):
    """Add a new bmark into the system given request params

    For example usage make sure to check out the unit tests in the
    test_delicious directory

    """
    params = request.GET

    with Authorize(request.registry.settings.get("api_key", ""), params.get("api_key", None)):

        request.response_content_type = "text/xml"
        if "url" in params and params["url"]:
            # check if we already have this
            try:
                mark = BmarkMgr.get_by_url(params["url"])

                mark.description = params.get("description", mark.description)
                mark.extended = params.get("extended", mark.extended)

                new_tags = params.get("tags", None)
                if new_tags:
                    mark.update_tags(new_tags)

            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

                # we want to store fulltext info so send that along to the
                # import processor
                conn_str = request.registry.settings.get("sqlalchemy.url", False)
                fulltext = get_fulltext_handler(conn_str)

                BmarkMgr.store(
                    params["url"],
                    params.get("description", ""),
                    params.get("extended", ""),
                    params.get("tags", ""),
                    dt=stored_time,
                    fulltext=fulltext,
                )

            return '<result code="done" />'
        else:
            return '<result code="Bad Request: missing url" />'
Esempio n. 7
0
def search(request):
    """Search for the content in the matchdict"""
    rdict = request.GET

    # check if we have a page count submitted
    phrase = rdict.get('search', '')

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    res_list = searcher.search(phrase)

    return {
        'search_results': res_list,
        'result_count': len(res_list),
        'phrase': phrase,
    }
Esempio n. 8
0
def search(request):
    """Search for the content in the matchdict"""
    rdict = request.GET

    # check if we have a page count submitted
    phrase = rdict.get('search', '')

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    res_list = searcher.search(phrase)

    return {
        'search_results': res_list,
        'result_count': len(res_list),
        'phrase': phrase,
    }
Esempio n. 9
0
def import_bmarks(request):
    """Allow users to upload a delicious bookmark export"""
    data = {}
    post = request.POST
    LOG.error(request.registry.settings.get('api_key', ''))
    LOG.error(post.get('api_key'))
    if post:
        # we have some posted values
        with Authorize(request.registry.settings.get('api_key', ''),
                       post.get('api_key', None)):

            # if auth fails, it'll raise an HTTPForbidden exception
            files = post.get('import_file', None)

            if files is not None:
                # upload is there for use
                # process the file using the import script
                importer = Importer(files.file)

                # we want to store fulltext info so send that along to the
                # import processor
                conn_str = request.registry.settings.get(
                    'sqlalchemy.url', False)
                searcher = get_fulltext_handler(conn_str)
                importer.process(fulltext=searcher)

                # @todo get a count of the imported bookmarks and setup a flash
                # message. Forward to / and display the import message

                # request.session.flash("Error something")
                return HTTPFound(location=request.route_url('home'))
            else:
                msg = request.session.pop_flash()

                if msg:
                    data['error'] = msg
                else:
                    data['error'] = None

            return data
    else:
        # just display the form
        return {}
Esempio n. 10
0
File: utils.py Progetto: akn/Bookie
def import_bmarks(request):
    """Allow users to upload a delicious bookmark export"""
    data = {}
    post = request.POST
    LOG.error(request.registry.settings.get('api_key', ''))
    LOG.error(post.get('api_key'))
    if post:
        # we have some posted values
        with Authorize(request.registry.settings.get('api_key', ''),
                       post.get('api_key', None)):

            # if auth fails, it'll raise an HTTPForbidden exception
            files = post.get('import_file', None)

            if files is not None:
                # upload is there for use
                # process the file using the import script
                importer = Importer(files.file)

                # we want to store fulltext info so send that along to the
                # import processor
                conn_str = request.registry.settings.get('sqlalchemy.url',
                                                         False)
                searcher = get_fulltext_handler(conn_str)
                importer.process(fulltext=searcher)

                # @todo get a count of the imported bookmarks and setup a flash
                # message. Forward to / and display the import message

                # request.session.flash("Error something")
                return HTTPFound(location=request.route_url('home'))
            else:
                msg = request.session.pop_flash()

                if msg:
                    data['error'] = msg
                else:
                    data['error'] = None

            return data
    else:
        # just display the form
        return {}
Esempio n. 11
0
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    with_content
        is always GET and specifies if we're searching the fulltext of pages

    """
    mdict = request.matchdict
    rdict = request.GET

    if 'terms' in mdict:
        phrase = " ".join(mdict['terms'])
    else:
        phrase = rdict.get('search', '')

    if request.user and request.user.username:
        username = request.user.username
    else:
        username = None

    # with content is always in the get string
    search_content = asbool(rdict.get('search_content', False))

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    # check if we have a page count submitted
    page = rdict.get('page', 0)
    count = rdict.get('count', 10)

    if 'username' in mdict:
        with_user = True
    else:
        with_user = False

    res_list = searcher.search(phrase,
                               content=search_content,
                               username=username if with_user else None,
                               ct=count,
                               page=page)

    constructed_results = []
    for res in res_list:
        return_obj = dict(res)
        return_obj['tags'] = [dict(tag[1]) for tag in res.tags.items()]

        # the hashed object is there as well, we need to pull the url and
        # clicks from it as total_clicks
        return_obj['url'] = res.hashed.url
        return_obj['total_clicks'] = res.hashed.clicks

        constructed_results.append(return_obj)

    return {
        'search_results': constructed_results,
        'result_count': len(constructed_results),
        'phrase': phrase,
        'page': page,
        'with_content': search_content,
        'username': username,
    }
Esempio n. 12
0
    def test_get_handler(self):
        """Verify we get the right type of full text store object"""
        handler = get_fulltext_handler("")

        ok_(isinstance(handler, WhooshFulltext),
            "Should get a whoosh fulltext by default")
Esempio n. 13
0
    def test_get_handler(self):
        """Verify we get the right type of full text store object"""
        handler = get_fulltext_handler("")

        self.assertTrue(isinstance(handler, WhooshFulltext), "Should get a whoosh fulltext by default")
Esempio n. 14
0
File: api.py Progetto: cambot/Bookie
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    with_content
        is always GET and specifies if we're searching the fulltext of pages

    """
    mdict = request.matchdict
    rdict = request.GET

    if 'terms' in mdict:
        phrase = " ".join(mdict['terms'])
    else:
        phrase = rdict.get('search', '')

    if request.user and request.user.username:
        username = request.user.username
    else:
        username = None

    # with content is always in the get string
    search_content = asbool(rdict.get('search_content', False))

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    # check if we have a page count submitted
    page = rdict.get('page', 0)
    count = rdict.get('count', 10)

    if 'username' in mdict:
        with_user = True
    else:
        with_user = False

    res_list = searcher.search(phrase,
                               content=search_content,
                               username=username if with_user else None,
                               ct=count,
                               page=page)

    constructed_results = []
    for res in res_list:
        return_obj = dict(res)
        return_obj['tags'] = [dict(tag[1]) for tag in res.tags.items()]

        # the hashed object is there as well, we need to pull the url and
        # clicks from it as total_clicks
        return_obj['url'] = res.hashed.url
        return_obj['total_clicks'] = res.hashed.clicks

        constructed_results.append(return_obj)

    return {
        'search_results': constructed_results,
        'result_count': len(constructed_results),
        'phrase': phrase,
        'page': page,
        'with_content': search_content,
        'username': username,
    }
Esempio n. 15
0
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    with_content
        is always GET and specifies if we're searching the fulltext of pages

    """
    route_name = request.matched_route.name
    mdict = request.matchdict
    rdict = request.GET

    username = rdict.get("username", None)

    if "terms" in mdict:
        phrase = " ".join(mdict["terms"])
    else:
        phrase = rdict.get("search", "")

    # with content is always in the get string
    with_content = asbool(rdict.get("content", False))
    LOG.debug("with_content")
    LOG.debug(with_content)

    conn_str = request.registry.settings.get("sqlalchemy.url", False)
    searcher = get_fulltext_handler(conn_str)

    # check if we have a page count submitted
    params = request.params
    page = params.get("page", 0)
    count = params.get("count", 50)

    res_list = searcher.search(phrase, content=with_content, username=username, ct=count, page=page)

    # if the route name is search_ajax we want a json response
    # else we just want to return the payload data to the mako template
    if "ajax" in route_name or "api" in route_name:
        return {
            "success": True,
            "message": "",
            "payload": {
                "search_results": [dict(res) for res in res_list],
                "result_count": len(res_list),
                "phrase": phrase,
                "page": page,
                "with_content": with_content,
                "username": username,
            },
        }
    else:
        return {
            "search_results": res_list,
            "count": len(res_list),
            "max_count": 50,
            "phrase": phrase,
            "page": page,
            "with_content": with_content,
            "username": username,
        }
Esempio n. 16
0
def search_results(request):
    """Search for the query terms in the matchdict/GET params

    The ones in the matchdict win in the case that they both exist
    but we'll fall back to query string search=XXX

    with_content
        is always GET and specifies if we're searching the fulltext of pages

    """
    route_name = request.matched_route.name

    mdict = request.matchdict
    rdict = request.GET

    if 'terms' in mdict:
        phrase = " ".join(mdict['terms'])
    else:
        phrase = rdict.get('search', '')

    username = rdict.get('username', None)

    # with content is always in the get string
    with_content = asbool(rdict.get('content', False))
    LOG.debug('with_content')
    LOG.debug(with_content)

    conn_str = request.registry.settings.get('sqlalchemy.url', False)
    searcher = get_fulltext_handler(conn_str)

    # check if we have a page count submitted
    params = request.params
    page = params.get('page', None)
    count = params.get('count', None)

    res_list = searcher.search(phrase, content=with_content, username=username)

    # we're going to fake this since we dont' have a good way to do this query
    # side
    if page is not None and count is not None:
        page = int(page)
        count = int(count)
        start = count * page
        end = start + count
        LOG.debug('counts')
        LOG.debug(start)
        LOG.debug(end)
        res_list = res_list[start:end]

    # if the route name is search_ajax we want a json response
    # else we just want to return the payload data to the mako template
    if 'ajax' in route_name or 'api' in route_name:
        return {
            'success': True,
            'message': "",
            'payload': {
                'search_results': [dict(res) for res in res_list],
                'result_count': len(res_list),
                'phrase': phrase,
                'page': page,
                'with_content': with_content,
                'username': username,
            }
        }
    else:
        return {
            'search_results': res_list,
            'result_count': len(res_list),
            'phrase': phrase,
            'page': page,
            'with_content': with_content,
            'username': username,
        }
Esempio n. 17
0
def posts_add(request):
    """Add a new bmark into the system given request params

    For example usage make sure to check out the unit tests in the
    test_delicious directory

    """
    params = request.params

    request.response.content_type = 'text/xml'
    if 'url' in params and params['url']:
        # check if we already have this
        try:
            mark = BmarkMgr.get_by_url(params['url'],
                                       username=request.user.username)

            mark.description = params.get('description', mark.description)
            mark.extended = params.get('extended', mark.extended)

            new_tags = params.get('tags', None)
            if new_tags:
                mark.update_tags(new_tags)

        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

            # we want to store fulltext info so send that along to the
            # import processor
            conn_str = request.registry.settings.get('sqlalchemy.url',
                                                     False)
            fulltext = get_fulltext_handler(conn_str)

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

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

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

        return '<result code="done" />'
    else:
        return '<result code="Bad Request: missing url" />'
Esempio n. 18
0
def bmark_add(request):
    """Add a new bookmark to the system"""
    rdict = request.matchdict
    params = request.params
    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

            # we want to store fulltext info so send that along to the
            # import processor
            conn_str = request.registry.settings.get('sqlalchemy.url',
                                                     False)
            fulltext = get_fulltext_handler(conn_str)

            # 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,
                         fulltext=fulltext,
                         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 request.params:
            content = StringIO(request.params['content'])
            content.seek(0)
            parsed = ReadContent.parse(content, content_type="text/html")

            mark.hashed.readable = Readable()
            mark.hashed.readable.content = parsed.content
            mark.hashed.readable.content_type = parsed.content_type
            mark.hashed.readable.status_code = parsed.status
            mark.hashed.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),
        }