当前位置: 首页>>代码示例>>Python>>正文


Python Part.content_type方法代码示例

本文整理汇总了Python中inbox.models.Part.content_type方法的典型用法代码示例。如果您正苦于以下问题:Python Part.content_type方法的具体用法?Python Part.content_type怎么用?Python Part.content_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在inbox.models.Part的用法示例。


在下文中一共展示了Part.content_type方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: update_draft

# 需要导入模块: from inbox.models import Part [as 别名]
# 或者: from inbox.models.Part import content_type [as 别名]
def update_draft(db_session, account, original_draft, to=None, subject=None,
                 body=None, blocks=None, cc=None, bcc=None, tags=None):
    """
    Update draft.

    To maintain our messages are immutable invariant, we create a new draft
    message object.


    Returns
    -------
    Message
        The new draft message object.

    Notes
    -----
    Messages, including draft messages, are immutable in Inbox.
    So to update a draft, we create a new draft message object and
    return its public_id (which is different than the original's).

    """

    def update(attr, value=None):
        if value is not None:
            setattr(original_draft, attr, value)

            if attr == 'sanitized_body':
                # Update size, snippet too
                original_draft.size = len(value)
                original_draft.calculate_html_snippet(value)

    update('to_addr', _parse_recipients(to) if to else None)
    update('cc_addr', _parse_recipients(cc) if cc else None)
    update('bcc_addr', _parse_recipients(bcc) if bcc else None)
    update('subject', subject if subject else None)
    update('sanitized_body', body if body else None)
    update('received_date', datetime.utcnow())

    # Parts, tags require special handling
    for block in blocks:
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        part.filename = block.filename
        original_draft.parts.append(part)

        db_session.add(part)

    thread = original_draft.thread
    if tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = tags | tags_to_keep

    # Delete previous version on remote
    schedule_action('delete_draft', original_draft,
                    original_draft.namespace.id, db_session,
                    inbox_uid=original_draft.inbox_uid)

    # Update version  + inbox_uid, sync to remote
    version = generate_public_id()
    update('version', version)
    update('inbox_uid', version)

    schedule_action('save_draft', original_draft, original_draft.namespace.id,
                    db_session)

    db_session.commit()

    return original_draft
开发者ID:nixon1333,项目名称:inbox,代码行数:74,代码来源:base.py

示例2: create_and_save_draft

# 需要导入模块: from inbox.models import Part [as 别名]
# 或者: from inbox.models.Part import content_type [as 别名]
def create_and_save_draft(
    db_session,
    account,
    to_addr=None,
    subject=None,
    body=None,
    blocks=None,
    cc_addr=None,
    bcc_addr=None,
    new_tags=None,
    thread=None,
    is_reply=False,
    parent_draft=None,
):
    """
    Create a draft object and commit it to the database.
    """
    dt = datetime.utcnow()
    uid = generate_public_id()
    to_addr = to_addr or []
    cc_addr = cc_addr or []
    bcc_addr = bcc_addr or []
    blocks = blocks or []
    body = body or ""
    if subject is None and thread is not None:
        # Set subject from thread by default.
        subject = thread.subject
    subject = subject or ""
    message = SpoolMessage()
    message.from_addr = [(account.sender_name, account.email_address)]
    message.created_date = dt
    # TODO(emfree): we should maybe make received_date nullable, so its value
    # doesn't change in the case of a drafted-and-later-reconciled message.
    message.received_date = dt
    message.is_sent = False
    message.state = "draft"
    if parent_draft is not None:
        message.parent_draft_id = parent_draft.id
    message.subject = subject
    message.sanitized_body = body
    message.to_addr = to_addr
    message.cc_addr = cc_addr
    message.bcc_addr = bcc_addr
    # TODO(emfree): this is different from the normal 'size' value of a
    # message, which is the size of the entire MIME message.
    message.size = len(body)
    message.is_draft = True
    message.is_read = True
    message.inbox_uid = uid
    message.public_id = uid

    # Set the snippet
    message.calculate_html_snippet(body)

    # Associate attachments to the draft message
    for block in blocks:
        # Create a new Part object to associate to the message object.
        # (You can't just set block.message, because if block is an attachment
        # on an existing message, that would dissociate it from the existing
        # message.)
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = "attachment"
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        message.parts.append(part)
        db_session.add(part)

    # TODO(emfree) Update contact data here.

    if is_reply:
        message.is_reply = True
        # If we're updating a draft, copy the in-reply-to and references
        # headers from the parent. Otherwise, construct them from the last
        # message currently in the thread.
        if parent_draft is not None:
            message.in_reply_to = parent_draft.in_reply_to
            message.references = parent_draft.references
        else:
            # Make sure that the headers are constructed from an actual
            # previous message on the thread, not another draft
            non_draft_messages = [m for m in thread.messages if not m.is_draft]
            if non_draft_messages:
                last_message = non_draft_messages[-1]
                message.in_reply_to = last_message.message_id_header
                message.references = last_message.references + "\t" + last_message.message_id_header
    if thread is None:
        # Create a new thread object for the draft.
        thread = Thread(
            subject=message.subject,
            recentdate=message.received_date,
            namespace=account.namespace,
            subjectdate=message.received_date,
        )
        db_session.add(thread)

    message.thread = thread
    # This triggers an autoflush, so we need to execute it after setting
    # message.thread
#.........这里部分代码省略.........
开发者ID:raoulwissink,项目名称:inbox,代码行数:103,代码来源:base.py

示例3: create_and_save_draft

# 需要导入模块: from inbox.models import Part [as 别名]
# 或者: from inbox.models.Part import content_type [as 别名]
def create_and_save_draft(db_session, account, to_addr=None, subject=None,
                          body=None, blocks=None, cc_addr=None, bcc_addr=None,
                          new_tags=None, thread=None, is_reply=False,
                          syncback=True):
    """Create a draft object and commit it to the database."""
    dt = datetime.utcnow()
    uid = generate_public_id()
    version = generate_public_id()
    to_addr = to_addr or []
    cc_addr = cc_addr or []
    bcc_addr = bcc_addr or []
    blocks = blocks or []
    body = body or ''
    if subject is None and thread is not None:
        # Set subject from thread by default.
        subject = thread.subject
    subject = subject or ''

    # Sets is_draft = True, state = 'draft'
    message = Message.create_draft_message()

    message.from_addr = [(account.sender_name, account.email_address)]
    # TODO(emfree): we should maybe make received_date nullable, so its value
    # doesn't change in the case of a drafted-and-later-reconciled message.
    message.received_date = dt
    message.subject = subject
    message.sanitized_body = body
    message.to_addr = to_addr
    message.cc_addr = cc_addr
    message.bcc_addr = bcc_addr
    # TODO(emfree): this is different from the normal 'size' value of a
    # message, which is the size of the entire MIME message.
    message.size = len(body)
    message.is_read = True
    message.is_sent = False
    message.is_reply = is_reply
    message.public_id = uid
    message.version = version
    message.inbox_uid = version

    # Set the snippet
    message.calculate_html_snippet(body)

    # Associate attachments to the draft message
    for block in blocks:
        # Create a new Part object to associate to the message object.
        # (You can't just set block.message, because if block is an attachment
        # on an existing message, that would dissociate it from the existing
        # message.)
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        part.filename = block.filename
        message.parts.append(part)
        db_session.add(part)

    # TODO(emfree) Update contact data here.

    if is_reply:
        message.is_reply = True
        # Construct the in-reply-to and references headers from the last
        # message currently in the thread.
        _set_reply_headers(message, thread)
    if thread is None:
        # Create a new thread object for the draft.
        thread = Thread(
            subject=message.subject,
            recentdate=message.received_date,
            namespace=account.namespace,
            subjectdate=message.received_date)
        db_session.add(thread)

    message.thread = thread
    # This triggers an autoflush, so we need to execute it after setting
    # message.thread
    thread.apply_tag(account.namespace.tags['drafts'])

    if new_tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = new_tags | tags_to_keep

    if syncback:
        schedule_action('save_draft', message, message.namespace.id,
                        db_session)

    db_session.add(message)
    db_session.commit()
    return message
开发者ID:nixon1333,项目名称:inbox,代码行数:93,代码来源:base.py


注:本文中的inbox.models.Part.content_type方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。