Example #1
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
Example #2
0
def aggregate_as_json_dict(aggregate,
                              message_body = True):
    messages = aggregate.messages.all().order_by('-date')
    latest_message_date = messages[len(messages)-1].date
    latest_message_date = latest_message_date and latest_message_date.ctime() or None

    latest_message = aggregate.latest_message
    
    if aggregate.status == models.MessageAggregate.STATUS_READY:
        json = {
            'status'    : str(aggregate.status),
            'guid'      : '%s.aggregate' % aggregate.pk,
            'subject'   : aggregate.name,
            'summary'   : aggregate.summary,
            'date'      : str(latest_message_date),
            'humanized_age' : humanize_date(latest_message_date),
            'tags'     : [ '%s.tag' % tag.pk
                           for tag in aggregate.tags.all() ],
            'latest_sender' : aggregate.latest_sender.email
        }
        if message_body:
            json.update(messages = [ message_as_json_dict(message, message_body)
                       for message in messages])
        else:
            json.update(messages = [ '%s' % message.pk
                       for message in messages])
    elif aggregate.status == models.MessageAggregate.STATUS_DELETED:
        json = {
            'status'    : str(aggregate.status),
            'guid'      : '%s.aggregate' % aggregate.pk
        }
        json.update(messages = [ '%s' % message.pk
           for message in messages])

        
    return json