示例#1
0
def page_as_json_dict( page, view_name, only_api_url = False):

    """ Helper function to transform a page into a dictionary, suitable
        for transliteration into JSON.

    """

    if only_api_url:
        return { 'url' : get_url('api_page_info', pk = page.pk) }

    return {
        'id'       : page.pk,
        'url'      : get_url('api_page_info', pk = page.pk),
        'owner'    : page.owner.pk,
        'document' : get_url('api_document_info', pk = page.document.pk),
        'position' : page.position,
        'pdf_url'  : get_url('api_page_as_pdf', pk = page.pk ),
        'view'     : get_url('api_page_view', pk = page.pk, view_name = view_name),
        }
示例#2
0
def tag_as_json_dict(
    tag,
    date = None,
    show_doc_count = False,
    show_documents = False,
    show_contacts  = False,
    show_conversations = False,
    show_message_bodies = False,
    show_url       = False,
    relative_time  = False,
    view_name      = models.AssetClass.PAGE_THUMBNAIL,
    show_id        = True ):

    """
    Helper function to express a tag as a dictionary suitable for
    conversion to JSON.

    """
    if relative_time and tag.tag_class == models.Tag.UPLOAD_AGGREGATE:
        name = humanize_date(tag.label.split('.')[0])
    else:
        name = tag.label

    out_dict  = {'name' : name,
                 'label' : tag.label,
                 'date' : str(date),
                 'tag_class' : tag.tag_class }

    if show_url:
        out_dict.update( url = get_url('api_tag_info', label = tag.label) )

    if show_conversations:
        conversations = tag.conversations.all()
        out_dict.update(
                conversations = [
                    conversation_as_json_dict(conversation, show_message_bodies)
                    for conversation in conversations ] )

    if show_doc_count or show_documents:
        documents = tag.documents.all()

        if show_documents:
            out_dict.update(
                documents = [
                    document_as_json_dict(document, view_name)
                    for document in documents ] )

        if show_doc_count:
            out_dict.update(document_count = documents.count())

    if show_id:
        out_dict.update(guid = '%s.tag' % tag.pk)

    return out_dict
示例#3
0
def document_as_json_dict( document, page_view_name, page_num_list = None ):
    """
    Helper function to transform a document into a dictionary.  Handy for
    being further transformed into JSON.
    """

    if page_num_list is None:
        page_set = []
    elif page_num_list == 'all':
        page_set = document.pages.order_by('position').all()
    else:
        page_set = document.pages.order_by('position').filter(
            position__in = page_num_list)

    tags = [ tag.label for tag in document.tags.all() ]
    json = {
        'owner'  : document.owner.pk,
        'url'    : get_url('api_document_info', pk = document.pk),
        'title'  : document.title,
        'tags'   : tags,
        'tags_string' : ' '.join(tags),
        'length' : document.num_pages,
        'thumbnail' : '',
        'pdf'    : get_url('api_document_as_pdf', pk = document.pk),
        'pages'  : [ page_as_json_dict(
                                       page,
                                       page_view_name) for page in page_set ],
        }

    if document.pages.count() > 0:
        json['thumbnail'] = get_url(
            'api_page_view',
            pk = document.pages.get(position=1).pk,
            view_name = page_view_name)

    return json