Beispiel #1
0
def _get_page_in_book(page_uuid, page_version, book_uuid,
                      book_version, latest=False):
    book_ident_hash = join_ident_hash(book_uuid, book_version)
    coltree = _get_content_json(ident_hash=book_ident_hash)['tree']
    pages = list(flatten_tree_to_ident_hashes(coltree))
    page_ident_hash = join_ident_hash(page_uuid, page_version)
    if page_ident_hash in pages:
        return book_uuid, '{}:{}'.format(
            latest and book_uuid or book_ident_hash, page_uuid)
    # book not in page
    return page_uuid, page_ident_hash
Beispiel #2
0
def _get_page_in_book(page_uuid,
                      page_version,
                      book_uuid,
                      book_version,
                      latest=False):
    book_ident_hash = join_ident_hash(book_uuid, book_version)
    coltree = _get_content_json(ident_hash=book_ident_hash)['tree']
    pages = list(flatten_tree_to_ident_hashes(coltree))
    page_ident_hash = join_ident_hash(page_uuid, page_version)
    if page_ident_hash in pages:
        return book_uuid, '{}:{}'.format(
            latest and book_uuid or book_ident_hash, page_uuid)
    # book not in page
    return page_uuid, page_ident_hash
Beispiel #3
0
def get_book_info(cursor, real_dict_cursor, book_id, book_version, page_id,
                  page_version):
    """Return information about a given book.

    Return the book's title, id, shortId, authors and revised date.
    Raise HTTPNotFound if the page is not in the book.
    """
    book_ident_hash = join_ident_hash(book_id, book_version)
    page_ident_hash = join_ident_hash(page_id, page_version)
    tree = get_tree(book_ident_hash, cursor)

    # Check if the page appears in the book tree
    if not tree or page_ident_hash not in flatten_tree_to_ident_hashes(tree):
        # Return a 404 error if the page is not actually in the book tree
        raise httpexceptions.HTTPNotFound()

    sql_statement = """
    SELECT m.name as title,
           ident_hash(m.uuid, m.major_version, m.minor_version)
           as ident_hash,
           short_ident_hash(m.uuid, m.major_version, m.minor_version)
           as shortId, ARRAY(
               SELECT row_to_json(user_row)
               FROM (
                   SELECT u.username, u.first_name as firstname,
                        u.last_name as surname, u.full_name as fullname,
                        u.title, u.suffix
               ) as user_row
           ) as authors,
           m.revised
    FROM modules m
    JOIN users as u on u.username = ANY(m.authors)
    WHERE ident_hash(m.uuid, m.major_version, m.minor_version) = %s
    """
    real_dict_cursor.execute(sql_statement, vars=(book_ident_hash, ))
    return real_dict_cursor.fetchone()
Beispiel #4
0
def _get_content_json(ident_hash=None):
    """Return a content as a dict from its ident-hash (uuid@version)."""
    request = get_current_request()
    routing_args = request and request.matchdict or {}
    settings = get_current_registry().settings
    if not ident_hash:
        ident_hash = routing_args['ident_hash']
    id, version = split_ident_hash(ident_hash)

    as_collated = asbool(request.GET.get('as_collated', True))
    page_ident_hash = routing_args.get('page_ident_hash', '')
    if page_ident_hash:
        try:
            p_id, p_version = split_ident_hash(page_ident_hash)
        except IdentHashShortId as e:
            p_id = get_uuid(e.id)
            p_version = e.version
        except IdentHashMissingVersion as e:
            # page ident hash doesn't need a version
            p_id = e.id
            p_version = None

    with psycopg2.connect(settings[config.CONNECTION_STRING]) as db_connection:
        with db_connection.cursor() as cursor:
            result = get_content_metadata(id, version, cursor)
            if result['mediaType'] == COLLECTION_MIMETYPE:
                # Grab the collection tree.
                result['tree'] = get_tree(ident_hash, cursor,
                                          as_collated=as_collated)
                result['collated'] = as_collated
                if not result['tree']:
                    # If collated tree is not available, get the uncollated
                    # tree.
                    result['tree'] = get_tree(ident_hash, cursor)
                    result['collated'] = False

                if page_ident_hash:
                    for id_ in flatten_tree_to_ident_hashes(result['tree']):
                        id, version = split_ident_hash(id_)
                        if id == p_id and (
                           version == p_version or not p_version):
                            content = None
                            if as_collated:
                                content = get_collated_content(
                                    id_, ident_hash, cursor)
                            if content:
                                result = get_content_metadata(
                                    id, version, cursor)
                                result['content'] = content[:]
                                return result
                            raise httpexceptions.HTTPFound(request.route_path(
                                request.matched_route.name,
                                ident_hash=join_ident_hash(id, version)))
                    raise httpexceptions.HTTPNotFound()
            else:
                # Grab the html content.
                args = dict(id=id, version=result['version'],
                            filename='index.cnxml.html')
                cursor.execute(SQL['get-resource-by-filename'], args)
                try:
                    content = cursor.fetchone()[0]
                except (TypeError, IndexError,):  # None returned
                    logger.debug("module found, but "
                                 "'index.cnxml.html' is missing.")
                    raise httpexceptions.HTTPNotFound()
                result['content'] = content[:]

    return result
Beispiel #5
0
def _get_content_json(ident_hash=None):
    """Return a content as a dict from its ident-hash (uuid@version)."""
    request = get_current_request()
    routing_args = request and request.matchdict or {}
    if not ident_hash:
        ident_hash = routing_args['ident_hash']
    id, version = split_ident_hash(ident_hash)

    as_collated = asbool(request.GET.get('as_collated', True))
    page_ident_hash = routing_args.get('page_ident_hash', '')
    if page_ident_hash:
        try:
            p_id, p_version = split_ident_hash(page_ident_hash)
        except IdentHashShortId as e:
            p_id = get_uuid(e.id)
            p_version = e.version
        except IdentHashMissingVersion as e:
            # page ident hash doesn't need a version
            p_id = e.id
            p_version = None

    with db_connect() as db_connection:
        with db_connection.cursor() as cursor:
            result = get_content_metadata(id, version, cursor)
            if result['mediaType'] == COLLECTION_MIMETYPE:
                # Grab the collection tree.
                result['tree'] = get_tree(ident_hash,
                                          cursor,
                                          as_collated=as_collated)
                result['collated'] = as_collated
                if not result['tree']:
                    # If collated tree is not available, get the uncollated
                    # tree.
                    result['tree'] = get_tree(ident_hash, cursor)
                    result['collated'] = False

                if page_ident_hash:
                    for id_ in flatten_tree_to_ident_hashes(result['tree']):
                        id, version = split_ident_hash(id_)
                        if id == p_id and (version == p_version
                                           or not p_version):
                            content = None
                            if as_collated:
                                content = get_collated_content(
                                    id_, ident_hash, cursor)
                            if content:
                                result = get_content_metadata(
                                    id, version, cursor)
                                result['content'] = content[:]
                                return result
                            # 302 'cause lack of baked content may be temporary
                            raise httpexceptions.HTTPFound(
                                request.route_path(request.matched_route.name,
                                                   _query=request.params,
                                                   ident_hash=join_ident_hash(
                                                       id, version),
                                                   ext=routing_args['ext']))
                    raise httpexceptions.HTTPNotFound()
            else:
                # Grab the html content.
                args = dict(id=id,
                            version=result['version'],
                            filename='index.cnxml.html')
                cursor.execute(SQL['get-resource-by-filename'], args)
                try:
                    content = cursor.fetchone()[0]
                except (
                        TypeError,
                        IndexError,
                ):  # None returned
                    logger.debug("module found, but "
                                 "'index.cnxml.html' is missing.")
                    raise httpexceptions.HTTPNotFound()
                result['content'] = content[:]

    return result