Esempio n. 1
0
 def create(cls,
            creator_id,
            subject,
            title,
            attachments=(),
            body='',
            is_private=False,
            topics=()):
     creator = AuthorizedUser.find_by_id(creator_id)
     if creator:
         note_template = cls(body=body,
                             creator_id=creator_id,
                             is_private=is_private,
                             subject=subject,
                             title=title)
         for topic in topics:
             note_template.topics.append(
                 NoteTemplateTopic.create(
                     note_template.id,
                     titleize(vacuum_whitespace(topic))), )
         for byte_stream_bundle in attachments:
             note_template.attachments.append(
                 NoteTemplateAttachment.create(
                     note_template_id=note_template.id,
                     name=byte_stream_bundle['name'],
                     byte_stream=byte_stream_bundle['byte_stream'],
                     uploaded_by=creator.uid,
                 ), )
         db.session.add(note_template)
         std_commit()
         return note_template
Esempio n. 2
0
 def test_update_note_template_attachments(self, app, client, fake_auth,
                                           mock_note_template):
     """Update note attachments."""
     user = AuthorizedUser.find_by_id(mock_note_template.creator_id)
     fake_auth.login(user.uid)
     base_dir = app.config['BASE_DIR']
     attachment_id = mock_note_template.attachments[0].id
     filename = 'mock_advising_note_attachment_2.txt'
     path_to_new_attachment = f'{base_dir}/fixtures/{filename}'
     updated_note = self._api_note_template_update(
         app,
         client,
         note_template_id=mock_note_template.id,
         title=mock_note_template.title,
         subject=mock_note_template.subject,
         body=mock_note_template.body,
         attachments=[path_to_new_attachment],
         delete_attachment_ids=[attachment_id],
     )
     assert mock_note_template.id == updated_note['attachments'][0][
         'noteTemplateId']
     assert len(updated_note['attachments']) == 1
     assert filename == updated_note['attachments'][0]['displayName']
     assert filename == updated_note['attachments'][0]['filename']
     assert updated_note['attachments'][0]['id'] != attachment_id
     # Verify db
     attachments = NoteTemplateAttachment.query.filter(
         and_(
             NoteTemplateAttachment.note_template_id ==
             mock_note_template.id,
             NoteTemplateAttachment.deleted_at == None,  # noqa: E711
         ), ).all()
     assert len(attachments) == 1
     assert filename in attachments[0].path_to_attachment
     assert not NoteTemplateAttachment.find_by_id(attachment_id)
Esempio n. 3
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)
Esempio n. 4
0
 def _add_attachment(cls, note_template, attachment, uploaded_by_uid):
     note_template.attachments.append(
         NoteTemplateAttachment.create(
             note_template_id=note_template.id,
             name=attachment['name'],
             byte_stream=attachment['byte_stream'],
             uploaded_by=uploaded_by_uid,
         ), )
     note_template.updated_at = utc_now()
Esempio n. 5
0
    def test_delete_note_template_with_attachments(self, app, client,
                                                   fake_auth):
        """Delete note template that has an attachment."""
        fake_auth.login(l_s_major_advisor_uid)
        base_dir = app.config['BASE_DIR']
        note_template = _api_create_note_template(
            app,
            client,
            title='Delete me!',
            subject='I want to be free',
            attachments=[
                f'{base_dir}/fixtures/mock_advising_note_attachment_1.txt'
            ],
        )
        assert len(note_template.get('attachments')) == 1
        attachment_id = note_template.get('attachments')[0]['id']
        assert NoteTemplateAttachment.find_by_id(attachment_id)

        note_template_id = note_template['id']
        response = client.delete(
            f'/api/note_template/delete/{note_template_id}')
        assert response.status_code == 200
        assert not NoteTemplateAttachment.find_by_id(attachment_id)
Esempio n. 6
0
 def create(
         cls,
         author_uid,
         author_name,
         author_role,
         author_dept_codes,
         sid,
         subject,
         body,
         topics=(),
         attachments=(),
         template_attachment_ids=(),
 ):
     note = cls(author_uid, author_name, author_role, author_dept_codes,
                sid, subject, body)
     for topic in topics:
         note.topics.append(
             NoteTopic.create(note, titleize(vacuum_whitespace(topic)),
                              author_uid), )
     for byte_stream_bundle in attachments:
         note.attachments.append(
             NoteAttachment.create(
                 note_id=note.id,
                 name=byte_stream_bundle['name'],
                 byte_stream=byte_stream_bundle['byte_stream'],
                 uploaded_by=author_uid,
             ), )
     for template_attachment in NoteTemplateAttachment.get_attachments(
             template_attachment_ids):
         note.attachments.append(
             NoteAttachment.create_using_template_attachment(
                 note_id=note.id,
                 template_attachment=template_attachment,
                 uploaded_by=author_uid,
             ), )
     db.session.add(note)
     std_commit()
     cls.refresh_search_index()
     return note