def create_sample_data(self):
        """ Add some data """
        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 email_confirmed_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        role_res1 = RoleEntity.create(name=ROLE_RESEARCHER_ONE, description='')
        role_res2 = RoleEntity.create(name=ROLE_RESEARCHER_TWO, description='')
        user.roles.extend([role_admin, role_tech, role_res1, role_res2])

        # == Create subject
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 day_offset=1,
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  day_offset=2.3,
                                  added_at=added_date)

        self.assertIsNotNone(evt.id)
        self.assertIsNotNone(evt2.id)

        files = [
            {'name': 'a.png', 'size': '123', 'event': evt.id},
            {'name': 'b.png', 'size': '1234', 'event': evt.id},
            {'name': 'c.png', 'size': '12345', 'event': evt2.id},
            {'name': 'd.png', 'size': '123456', 'event': evt2.id},
            {'name': 'e.png', 'size': '1234567', 'event': evt2.id},
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            self.assertIsNotNone(subject_file.id)
    def test_subject(self):
        """ verify save and find operations """

        added_date = datetime.today()
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=False)

        self.assertEquals(1, subject.id)
        self.assertEquals("001", subject.redcap_id)
        self.assertIsNotNone(subject.added_at)
        self.assertIsNotNone(subject.last_checked_at)
        self.assertFalse(subject.was_deleted)
        self.assertEqual(subject.added_at, subject.last_checked_at)

        subjects = SubjectEntity.query.all()
        self.assertEquals(1, len(subjects))

        # test usage of CRUDMixin.get_by_id() and create()
        subject = SubjectEntity.update(subject, redcap_id="002")
        self.assertEquals("002", subject.redcap_id)

        evt = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 1',
                                 added_at=added_date)

        fdata = {'name': 'a.png', 'size': '1MB', 'event': evt.id}
        user = UserEntity.create(email="*****@*****.**",
                                 first="",
                                 last="",
                                 minitial="",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=added_date)

        subject_file = SubjectFileEntity.create(
            subject_id=subject.id,
            event_id=fdata['event'],
            file_name=fdata['name'],
            file_check_sum=utils.compute_text_md5(fdata['name']),
            file_size=fdata['size'],
            uploaded_at=added_date,
            user_id=user.get_id())
        self.assertIsNotNone(subject_file.id)

        actual_count = SubjectFileEntity.query.count()
        self.assertEqual(1, actual_count)

        actual_count = SubjectFileEntity.query.filter_by(
            event_id=1).count()
        self.assertEqual(1, actual_count)

        sfile = SubjectFileEntity.query.first()
        self.assertEqual(user.id, sfile.user_id)
    def test_subject(self):
        """ verify save and find operations """

        added_date = datetime.today()
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=False)

        self.assertEquals(1, subject.id)
        self.assertEquals("001", subject.redcap_id)
        self.assertIsNotNone(subject.added_at)
        self.assertIsNotNone(subject.last_checked_at)
        self.assertFalse(subject.was_deleted)
        self.assertEqual(subject.added_at, subject.last_checked_at)

        subjects = SubjectEntity.query.all()
        self.assertEquals(1, len(subjects))

        # test usage of CRUDMixin.get_by_id() and create()
        subject = SubjectEntity.update(subject, redcap_id="002")
        self.assertEquals("002", subject.redcap_id)

        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 added_at=added_date)

        fdata = {'name': 'a.png', 'size': '1MB', 'event': evt.id}
        user = UserEntity.create(email="*****@*****.**",
                                 first="",
                                 last="",
                                 minitial="",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=added_date)

        subject_file = SubjectFileEntity.create(
            subject_id=subject.id,
            event_id=fdata['event'],
            file_name=fdata['name'],
            file_check_sum=utils.compute_text_md5(fdata['name']),
            file_size=fdata['size'],
            uploaded_at=added_date,
            user_id=user.get_id())
        self.assertIsNotNone(subject_file.id)

        actual_count = SubjectFileEntity.query.count()
        self.assertEqual(1, actual_count)

        actual_count = SubjectFileEntity.query.filter_by(event_id=1).count()
        self.assertEqual(1, actual_count)

        sfile = SubjectFileEntity.query.first()
        self.assertEqual(user.id, sfile.user_id)
    def create_sample_data(self):
        """ Add some data """

        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        user.roles.extend([role_admin, role_tech])

        # == Create subject
        subject = SubjectEntity.create(
            redcap_id="001",
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 1',
                          added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1', redcap_event='Event 2',
                           added_at=added_date)

        assert evt.id is not None

        files = [
            {'name': 'a.png', 'size': '1MB', 'event': evt.id},
            {'name': 'b.png', 'size': '2MB', 'event': evt.id},
            {'name': 'c.png', 'size': '3MB', 'event': evt2.id},
            {'name': 'd.png', 'size': '4MB', 'event': evt2.id},
            {'name': 'e.png', 'size': '5MB', 'event': evt2.id},
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            app.logger.debug("Init test case with: {}".format(subject_file))
Ejemplo n.º 5
0
def download_file():
    """ Download a file using the database id """

    if 'POST' == request.method:
        file_id = utils.get_safe_int(request.form['file_id'])
    else:
        file_id = utils.get_safe_int(request.args.get('file_id'))

    subject_file = SubjectFileEntity.get_by_id(file_id)
    file_path = subject_file.get_full_path(
        app.config['REDIDROPPER_UPLOAD_SAVED_DIR'])
    LogEntity.file_downloaded(session['uuid'], file_path)
    return send_file(file_path, as_attachment=True)
Ejemplo n.º 6
0
def download_file():
    """ Download a file using the database id """

    if 'POST' == request.method:
        file_id = utils.get_safe_int(request.form['file_id'])
    else:
        file_id = utils.get_safe_int(request.args.get('file_id'))

    subject_file = SubjectFileEntity.get_by_id(file_id)
    file_path = subject_file.get_full_path(
        app.config['REDIDROPPER_UPLOAD_SAVED_DIR'])
    LogEntity.file_downloaded(session['uuid'], file_path)
    return send_file(file_path, as_attachment=True)
Ejemplo n.º 7
0
def save_file_metadata(fchunk):
    """
    Insert a row in SubjectFile table to preserve file details.
    Called from api.py#api_upload() -> #save_uploaded_file() -> #merge_files()

    @return SubjectFileEntity
    """
    added_date = datetime.today()
    subject = SubjectEntity.get_by_redcap_id(fchunk.redcap_id)

    subject_file = SubjectFileEntity.create(subject_id=subject.id,
                                            event_id=fchunk.event_id,
                                            file_name=fchunk.file_name,
                                            file_check_sum='pending',
                                            file_size=fchunk.total_size,
                                            uploaded_at=added_date,
                                            user_id=current_user.id)
    logger.debug("Saved metadata to the db: ".format(subject_file))
    return subject_file
Ejemplo n.º 8
0
def save_file_metadata(fchunk):
    """
    Insert a row in SubjectFile table to preserve file details.
    Called from api.py#api_upload() -> #save_uploaded_file() -> #merge_files()

    @return SubjectFileEntity
    """
    added_date = datetime.today()
    subject = SubjectEntity.get_by_redcap_id(fchunk.redcap_id)

    subject_file = SubjectFileEntity.create(
        subject_id=subject.id,
        event_id=fchunk.event_id,
        file_name=fchunk.file_name,
        file_check_sum='pending',
        file_size=fchunk.total_size,
        uploaded_at=added_date,
        user_id=current_user.id)
    logger.debug("Saved metadata to the db: ".format(subject_file))
    return subject_file
Ejemplo n.º 9
0
    def create_sample_data(self):
        """ Add some data """
        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 email_confirmed_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        role_res1 = RoleEntity.create(name=ROLE_RESEARCHER_ONE, description='')
        role_res2 = RoleEntity.create(name=ROLE_RESEARCHER_TWO, description='')
        user.roles.extend([role_admin, role_tech, role_res1, role_res2])

        # == Create subject
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 day_offset=1,
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  day_offset=2.3,
                                  added_at=added_date)

        self.assertIsNotNone(evt.id)
        self.assertIsNotNone(evt2.id)

        files = [
            {
                'name': 'a.png',
                'size': '123',
                'event': evt.id
            },
            {
                'name': 'b.png',
                'size': '1234',
                'event': evt.id
            },
            {
                'name': 'c.png',
                'size': '12345',
                'event': evt2.id
            },
            {
                'name': 'd.png',
                'size': '123456',
                'event': evt2.id
            },
            {
                'name': 'e.png',
                'size': '1234567',
                'event': evt2.id
            },
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            self.assertIsNotNone(subject_file.id)
    def create_sample_data(self):
        """ Add some data """

        # == Create users
        added_date = datetime.today()
        access_end_date = utils.get_expiration_date(180)
        user = UserEntity.create(email="*****@*****.**",
                                 first="First",
                                 last="Last",
                                 minitial="M",
                                 added_at=added_date,
                                 modified_at=added_date,
                                 access_expires_at=access_end_date)

        # == Create roles
        role_admin = RoleEntity.create(name=ROLE_ADMIN, description='role')
        role_tech = RoleEntity.create(name=ROLE_TECHNICIAN, description='role')
        user.roles.extend([role_admin, role_tech])

        # == Create subject
        subject = SubjectEntity.create(redcap_id="001",
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=0)

        # == Create events
        evt = EventEntity.create(redcap_arm='Arm 1',
                                 redcap_event='Event 1',
                                 added_at=added_date)
        evt2 = EventEntity.create(redcap_arm='Arm 1',
                                  redcap_event='Event 2',
                                  added_at=added_date)

        assert evt.id is not None

        files = [
            {
                'name': 'a.png',
                'size': '1MB',
                'event': evt.id
            },
            {
                'name': 'b.png',
                'size': '2MB',
                'event': evt.id
            },
            {
                'name': 'c.png',
                'size': '3MB',
                'event': evt2.id
            },
            {
                'name': 'd.png',
                'size': '4MB',
                'event': evt2.id
            },
            {
                'name': 'e.png',
                'size': '5MB',
                'event': evt2.id
            },
        ]

        # Create subject files
        for fdata in files:
            subject_file = SubjectFileEntity.create(
                subject_id=subject.id,
                event_id=fdata['event'],
                file_name=fdata['name'],
                file_check_sum=utils.compute_text_md5(fdata['name']),
                file_size=fdata['size'],
                uploaded_at=added_date,
                user_id=user.id)
            app.logger.debug("Init test case with: {}".format(subject_file))