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,
                                 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)
예제 #4
0
def api_import_redcap_subjects():
    """
    Refresh the list of subjects
    """
    local_subjects = SubjectEntity.query.all()
    url = app.config['REDCAP_API_URL']
    redcap_subjects = utils.retrieve_redcap_subjects(
        url=url,
        token=app.config['REDCAP_API_TOKEN'],
        fields=app.config['REDCAP_DEMOGRAPHICS_SUBJECT_ID'],
        max_time=app.config['REDCAP_CURL_API_MAX_TIME'])
    new_subjects = find_new_subjects(local_subjects, redcap_subjects)

    added_date = datetime.today()
    inserted_subjects = []

    for id, redcap_subject in new_subjects.iteritems():
        subject = SubjectEntity.create(
            redcap_id=id,
            added_at=added_date,
            last_checked_at=added_date,
            was_deleted=False)
        inserted_subjects.append(subject)

    details = [i for i in new_subjects]
    LogEntity.redcap_subjects_imported(session['uuid'],
                                       "Total: {} \n {}"
                                       .format(len(details), details))

    return utils.jsonify_success({
        'local_subjects': [i.redcap_id for i in local_subjects],
        'redcap_subjects': redcap_subjects,
        'inserted_subjects': [i.redcap_id for i in inserted_subjects],
        'api_url': url
    })
예제 #5
0
def api_import_redcap_subjects():
    """
    Refresh the list of subjects
    """
    local_subjects = SubjectEntity.query.all()
    url = app.config['REDCAP_API_URL']
    redcap_subjects = utils.retrieve_redcap_subjects(
        url=url,
        token=app.config['REDCAP_API_TOKEN'],
        fields=app.config['REDCAP_DEMOGRAPHICS_SUBJECT_ID'],
        max_time=app.config['REDCAP_CURL_API_MAX_TIME'])
    new_subjects = find_new_subjects(local_subjects, redcap_subjects)

    added_date = datetime.today()
    inserted_subjects = []

    for id, redcap_subject in new_subjects.iteritems():
        subject = SubjectEntity.create(redcap_id=id,
                                       added_at=added_date,
                                       last_checked_at=added_date,
                                       was_deleted=False)
        inserted_subjects.append(subject)

    details = [i for i in new_subjects]
    LogEntity.redcap_subjects_imported(
        session['uuid'], "Total: {} \n {}".format(len(details), details))

    return utils.jsonify_success({
        'local_subjects': [i.redcap_id for i in local_subjects],
        'redcap_subjects':
        redcap_subjects,
        'inserted_subjects': [i.redcap_id for i in inserted_subjects],
        'api_url':
        url
    })
    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))
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
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
예제 #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))