Esempio n. 1
0
 def create(cls, note_id, name, byte_stream, uploaded_by):
     return NoteAttachment(
         note_id=note_id,
         path_to_attachment=put_attachment_to_s3(name=name,
                                                 byte_stream=byte_stream),
         uploaded_by_uid=uploaded_by,
     )
Esempio n. 2
0
def _add_attachments_to_notes(attachments, template_attachment_ids, author_uid, note_ids):
    now = utc_now().strftime('%Y-%m-%d %H:%M:%S')

    def _add_attachment(_s3_path):
        count_per_chunk = 10000
        for chunk in range(0, len(note_ids), count_per_chunk):
            query = """
                INSERT INTO note_attachments (created_at, note_id, path_to_attachment, uploaded_by_uid)
                SELECT created_at, note_id, path_to_attachment, uploaded_by_uid
                FROM json_populate_recordset(null::note_attachments, :json_dumps);
            """
            note_ids_subset = note_ids[chunk:chunk + count_per_chunk]
            data = [
                {
                    'created_at': now,
                    'note_id': note_id,
                    'path_to_attachment': _s3_path,
                    'uploaded_by_uid': author_uid,
                } for note_id in note_ids_subset
            ]
            db.session.execute(query, {'json_dumps': json.dumps(data)})

    for byte_stream_bundle in attachments:
        s3_path = put_attachment_to_s3(name=byte_stream_bundle['name'], byte_stream=byte_stream_bundle['byte_stream'])
        _add_attachment(s3_path)
    if template_attachment_ids:
        for template_attachment in NoteTemplateAttachment.get_attachments(template_attachment_ids):
            _add_attachment(template_attachment.path_to_attachment)