Beispiel #1
0
def save_attachments(instance, media_files):
    """
    Returns `True` if any new attachment was saved, `False` if all attachments
    were duplicates or none were provided
    """
    any_new_attachment = False
    for f in media_files:
        attachment_filename = generate_attachment_filename(instance, f.name)
        existing_attachment = Attachment.objects.filter(
            instance=instance,
            media_file=attachment_filename,
            mimetype=f.content_type,
        ).first()
        if existing_attachment and (existing_attachment.file_hash
                                    == hash_attachment_contents(f.read())):
            # We already have this attachment!
            continue
        f.seek(0)
        # This is a new attachment; save it!
        Attachment.objects.create(instance=instance,
                                  media_file=f,
                                  mimetype=f.content_type)
        any_new_attachment = True

    return any_new_attachment
Beispiel #2
0
def save_attachments(instance, media_files):
    '''
    Returns `True` if any new attachment was saved, `False` if all attachments
    were duplicates or none were provided
    '''
    any_new_attachment = False
    for f in media_files:
        attachment_filename = generate_attachment_filename(instance, f.name)
        existing_attachment = Attachment.objects.filter(
            instance=instance,
            media_file=attachment_filename,
            mimetype=f.content_type,
        ).first()
        if existing_attachment and (existing_attachment.file_hash ==
                                    hash_attachment_contents(f.read())):
            # We already have this attachment!
            continue
        f.seek(0)
        # This is a new attachment; save it!
        Attachment.objects.create(
            instance=instance,
            media_file=f, mimetype=f.content_type)
        any_new_attachment = True
    return any_new_attachment