Exemplo n.º 1
0
    def create_external_system_record(self, request, driver, pds, subject, record_id=None, label=None):

        def rec_id_validator(new_record_id):
            return SubjectUtils.validate_new_record_id(
                pds, subject, new_record_id)

        grp = SubjectUtils.get_protocol_subject_record_group(pds.protocol, subject)
        if grp:
            rec_id_prefix = grp.ehb_key
        else:
            request.META['action'] = 'No subject record group found'
            request.META['error'] = True
            raise Exception('No subject record group found')
        rec_id = None

        if record_id:
            if record_id.startswith(rec_id_prefix):
                rec_id = driver.create(
                    record_id_prefix=None, record_id=record_id, record_id_validator=rec_id_validator)
            else:
                rec_id = driver.create(
                    record_id_prefix=rec_id_prefix, record_id=record_id)
        else:
            rec_id = driver.create(
                record_id_prefix=rec_id_prefix, record_id_validator=rec_id_validator)

        er = SubjectUtils.create_new_ehb_external_record(
            pds, request.user, subject, rec_id, label)
        return er.id
Exemplo n.º 2
0
    def create_external_system_record(self, request, driver, pds, subject, record_id=None, label=None):

        def rec_id_validator(new_record_id):
            return SubjectUtils.validate_new_record_id(
                pds, subject, new_record_id)

        grp = SubjectUtils.get_protocol_subject_record_group(pds.protocol, subject)
        if grp:
            rec_id_prefix = grp.ehb_key
        else:
            request.META['action'] = 'No subject record group found'
            request.META['error'] = True
            raise Exception('No subject record group found')
        rec_id = None

        if record_id:
            if record_id.startswith(rec_id_prefix):
                rec_id = driver.create(
                    record_id_prefix=None, record_id=record_id, record_id_validator=rec_id_validator)
            else:
                rec_id = driver.create(
                    record_id_prefix=rec_id_prefix, record_id=record_id)
        else:
            rec_id = driver.create(
                record_id_prefix=rec_id_prefix, record_id_validator=rec_id_validator)

        er = SubjectUtils.create_new_ehb_external_record(
            pds, request.user, subject, rec_id, label)
        return er.id
Exemplo n.º 3
0
    def delete(self, request, pk, subject, *args, **kwargs):
        try:
            subject = self.s_rh.get(id=subject)
            protocol = Protocol.objects.get(pk=pk)
            self.s_rh.delete(id=subject.id)
            SubjectUtils.delete_protocol_subject_record_group(protocol, subject)
        except:
            return Response({'error': 'Unable to delete subject'}, status=400)

        return Response({'info': 'Subject deleted'}, status=200)
Exemplo n.º 4
0
    def delete(self, request, pk, subject, *args, **kwargs):
        try:
            subject = self.s_rh.get(id=subject)
            protocol = Protocol.objects.get(pk=pk)
            self.s_rh.delete(id=subject.id)
            SubjectUtils.delete_protocol_subject_record_group(
                protocol, subject)
        except:
            return Response({'error': 'Unable to delete subject'}, status=400)

        return Response({'info': 'Subject deleted'}, status=200)
Exemplo n.º 5
0
 def put(self, request, pk, subject, *args, **kwargs):
     subject_update = json.loads(request.body.decode('utf-8'))
     # See if subject exists
     try:
         ehb_sub = self.s_rh.get(id=subject)
         org = self.o_rh.get(id=subject_update['organization'])
         protocol = Protocol.objects.get(pk=pk)
         old_group_name = SubjectUtils.protocol_subject_record_group_name(
             protocol, ehb_sub)
         group = self.g_rh.get(name=old_group_name)
     except:
         return Response({'error': 'subject not found'}, status=404)
     ehb_sub.old_subject = deepcopy(ehb_sub)
     ehb_sub.first_name = subject_update['first_name']
     ehb_sub.last_name = subject_update['last_name']
     ehb_sub.organization_subject_id = subject_update[
         'organization_subject_id']
     ehb_sub.organization_id = org.id
     ehb_sub.dob = datetime.strptime(subject_update['dob'], '%Y-%m-%d')
     new_group_name = SubjectUtils.protocol_subject_record_group_name(
         protocol, ehb_sub)
     group.name = new_group_name
     group.client_key = protocol._settings_prop('CLIENT_KEY', 'key', '')
     group.current_client_key(group.client_key)
     update = self.s_rh.update(ehb_sub)[0]
     if update['errors']:
         return Response(json.dumps({'error': 'Unable to update subject'}),
                         status=400)
     # If the update is succesful, update the subject record group associated with this subject
     res = self.g_rh.update(group)[0]
     if not res['success']:
         return Response(json.dumps({'error': 'Unable to update group'}),
                         status=400)
     # If the update is succesful, update the cache.
     sub = json.loads(Subject.json_from_identity(update['subject']))
     sub['organization_name'] = org.name
     cache_key = 'protocol{0}_sub_data'.format(pk)
     cache_data = self.cache.get(cache_key)
     if cache_data:
         if 'external_ids' in list(subject_update.keys()):
             sub['external_ids'] = subject_update['external_ids']
         else:
             sub['external_ids'] = []
         sub['external_records'] = subject_update['external_records']
         sub['organization_name'] = org.name
         subjects = json.loads(cache_data)
         for i in range(0, len(subjects)):
             if subjects[i]['id'] == sub['id']:
                 subjects[i] = sub
         self.cache.set(cache_key, json.dumps(subjects))
         if hasattr(self.cache, 'persist'):
             self.cache.persist(cache_key)
     return Response(sub, headers={'Access-Control-Allow-Origin': '*'})
Exemplo n.º 6
0
 def put(self, request, pk, subject, *args, **kwargs):
     subject_update = json.loads(request.body.decode('utf-8'))
     # See if subject exists
     try:
         ehb_sub = self.s_rh.get(id=subject)
         org = self.o_rh.get(id=subject_update['organization'])
         protocol = Protocol.objects.get(pk=pk)
         old_group_name = SubjectUtils.protocol_subject_record_group_name(protocol, ehb_sub)
         group = self.g_rh.get(name=old_group_name)
     except:
         return Response({'error': 'subject not found'}, status=404)
     ehb_sub.old_subject = deepcopy(ehb_sub)
     ehb_sub.first_name = subject_update['first_name']
     ehb_sub.last_name = subject_update['last_name']
     ehb_sub.organization_subject_id = subject_update['organization_subject_id']
     ehb_sub.organization_id = org.id
     ehb_sub.dob = datetime.strptime(subject_update['dob'], '%Y-%m-%d')
     new_group_name = SubjectUtils.protocol_subject_record_group_name(protocol, ehb_sub)
     group.name = new_group_name
     group.client_key = protocol._settings_prop(
         'CLIENT_KEY', 'key', '')
     group.current_client_key(group.client_key)
     update = self.s_rh.update(ehb_sub)[0]
     if update['errors']:
         return Response(json.dumps({'error': 'Unable to update subject'}), status=400)
     # If the update is succesful, update the subject record group associated with this subject
     res = self.g_rh.update(group)[0]
     if not res['success']:
         return Response(json.dumps({'error': 'Unable to update group'}), status=400)
     # If the update is succesful, update the cache.
     sub = json.loads(Subject.json_from_identity(update['subject']))
     sub['organization_name'] = org.name
     cache_key = 'protocol{0}_sub_data'.format(pk)
     cache_data = self.cache.get(cache_key)
     if cache_data:
         if 'external_ids' in list(subject_update.keys()):
             sub['external_ids'] = subject_update['external_ids']
         else:
             sub['external_ids'] = []
         sub['external_records'] = subject_update['external_records']
         sub['organization_name'] = org.name
         subjects = json.loads(cache_data)
         for i in range(0, len(subjects)):
             if subjects[i]['id'] == sub['id']:
                 subjects[i] = sub
         self.cache.set(cache_key, json.dumps(subjects))
         if hasattr(self.cache, 'persist'):
             self.cache.persist(cache_key)
     return Response(
         sub,
         headers={'Access-Control-Allow-Origin': '*'}
     )
Exemplo n.º 7
0
 def update_subject_group(cls, protocol, subject_update, group):
     new_group_name = SubjectUtils.protocol_subject_record_group_name(
         protocol, subject_update)
     group.name = new_group_name
     group.client_key = protocol._settings_prop('CLIENT_KEY', 'key', '')
     group.current_client_key(group.client_key)
     return cls.g_rh.update(group)[0]
Exemplo n.º 8
0
    def delete(self, request, pk, subject, *args, **kwargs):
        if request.user.is_superuser:
            try:
                subject = self.s_rh.get(id=subject)
                protocol = Protocol.objects.get(pk=pk)
                group_id = self.get_group_id(protocol.immutable_key.key)
                # instead of deleting a subject - we want to just remove them from the subject group
                # self.s_rh.delete(id=subject.id)
                self.remove_sub_from_ehb_group(subject.id, group_id)
                SubjectUtils.delete_protocol_subject_record_group(
                    protocol, subject)
            except:
                return Response({'error': 'Unable to delete subject'},
                                status=400)

            return Response({'info': 'Subject deleted'}, status=200)
        else:
            return Response(
                {
                    "detail":
                    "You are not authorized to view subjects in this protocol"
                },
                status=403)
Exemplo n.º 9
0
    def put(self, request, pk, subject, *args, **kwargs):
        subject_update = json.loads(request.body.decode('utf-8'))
        subject_api_url = "/api/subject/id/" + subject + "/"
        # See if subject exists
        try:
            ehb_sub = ServiceClient.ehb_api(subject_api_url, "GET").json()
            protocol = Protocol.objects.get(pk=pk)
            old_group_name = SubjectUtils.protocol_subject_record_group_name(
                protocol, ehb_sub)
            group = self.g_rh.get(name=old_group_name)
        except:
            return Response({'error': 'subject not found'}, status=404)

        update_subject_response = self.updateEhbSubject(
            subject, subject_update, ehb_sub)
        try:
            success = update_subject_response['success']
        except:
            success = (update_subject_response.status_code == 200)

        if (success is False):
            return Response(json.dumps({'error': 'Unable to update subject'}),
                            status=400)
        user_audit_payload = {
            'subject': ehb_sub['id'],
            'change_type': "Subject",
            'change_type_ehb_pk': ehb_sub['id'],
            'change_action': "Update",
            'user_name': request.user.username,
            'protocol_id': protocol.id
        }
        self.edit_subject_user_audit(subject_update, ehb_sub,
                                     user_audit_payload)
        # If the update is succesful, update the subject record group associated with this subject
        update_subj_gr_resp = self.update_subject_group(
            protocol, subject_update, group)
        if not update_subj_gr_resp['success']:
            return Response(json.dumps({'error': 'Unable to update group'}),
                            status=400)

        # If the update is succesful, update the cache.
        try:
            self.update_subject_cache(pk, subject_update, False)
        except:
            return Response(json.dumps({'error': 'Unable to update cache'}),
                            status=400)

        return Response(subject_update,
                        headers={'Access-Control-Allow-Origin': '*'})
Exemplo n.º 10
0
 def rec_id_validator(new_record_id, include_path):
     return SubjectUtils.validate_new_record_id(context['pds'],
                                                context['subject'],
                                                new_record_id,
                                                include_path)
Exemplo n.º 11
0
    def post(self, request, **kwargs):
        context = self.get_context_data(**kwargs)

        def rec_id_validator(new_record_id, include_path):
            return SubjectUtils.validate_new_record_id(context['pds'],
                                                       context['subject'],
                                                       new_record_id,
                                                       include_path)

        try:
            grp = SubjectUtils.get_protocol_subject_record_group(
                self.pds.protocol, self.subject)
            grp.client_key = self.pds.protocol._settings_prop(
                'CLIENT_KEY', 'key', '')
            rec_id_prefix = ''

            label_id = request.POST.get('label_id', 1)
            if grp:
                rec_id_prefix = grp.ehb_key
            else:
                request.META[
                    'action'] = 'Subject record group not found for {0}'.format(
                        context['subject'].id)
                request.META['error'] = True
                raise Exception('No subject record group found')
            # Try to process the new record form
            try:
                rec_id = self.driver.process_new_record_form(
                    request=request,
                    record_id_prefix=rec_id_prefix,
                    record_id_validator=rec_id_validator)
                # If we have successfully created the record. Make sure it is in the eHB.
                try:
                    self.record_id = SubjectUtils.create_new_ehb_external_record(
                        self.pds, request.user, self.subject, rec_id,
                        label_id).id
                    if self.check_cache():
                        self.update_cache()
                    return HttpResponseRedirect(self.start_path)
                except RecordCreationError as rce:  # exception from the eHB
                    request.META['action'] = rce.errmsg
                    request.META['error'] = True
                    context['errors'].append(
                        ('The record could not be created on the '
                         'electronic Honest Broker. Please contact a '
                         'system administrator. There could be a '
                         'connection problem.'))
            except IgnoreEhbExceptions as iee:
                # TODO this is a hack until BRP can create Nautilus records
                rec_id = iee.record_id
                ehb_rec_id = ''
                try:
                    # this will create the ehb external_record entry
                    # and add that record to the subject's record group
                    self.record_id = SubjectUtils.create_new_ehb_external_record(
                        self.pds, request.user, self.subject, rec_id,
                        label_id).id

                    if self.check_cache():
                        self.update_cache()
                    self.start_path = '{0}/dataentry/protocoldatasource/{1}/subject/{2}/record/{3}/start/'.format(
                        self.service_client.self_root_path, self.pds.id,
                        self.subject.id, self.record_id)
                    return HttpResponseRedirect(self.start_path)

                except RecordCreationError as rce:  # exception from the eHB
                    request.META['action'] = rce.errmsg
                    request.META['error'] = True
                    record_already_exists = 6
                    cause = rce.raw_cause
                    if cause and len(
                            cause) == 1 and record_already_exists == cause[0]:
                        er_rh = ServiceClient.get_rh_for(
                            record_type=ServiceClient.EXTERNAL_RECORD)
                        ehb_recs = er_rh.get(
                            external_system_url=self.pds.data_source.url,
                            subject_id=self.subject.id,
                            path=self.pds.path)
                        ehb_rec_id = None
                        for record in ehb_recs:
                            if record.record_id == rec_id:
                                ehb_rec_id = record.id
                        if ehb_rec_id:
                            self.start_path = '%s/dataentry/protocoldatasource/%s/subject/%s/record/%s/start' % (
                                ServiceClient.self_root_path, self.pds.id,
                                self.subject.id, ehb_rec_id)
                        else:
                            context['errors'].append(
                                'This ID has already been assigned to another subject.'
                            )
                            return render_to_response(
                                'pds_dataentry_rec_create.html',
                                context,
                                context_instance=RequestContext(request))
                    else:
                        request.META['action'] = 'Record could not be created'
                        request.META['error'] = True
                        context['errors'].append(
                            ('The record could not be created on the '
                             'electronic Honest Broker. Please contact '
                             'a system administrator. There could be a'
                             ' connection problem.'))
                except:
                    return HttpResponse('Unknown Error')
                return HttpResponseRedirect(self.start_path)

        except RecordCreationError as rce:
            # Handle errors in the form.
            request.META['action'] = rce.errmsg
            request.META['error'] = True
            context['errors'].append(rce.cause)
            return render_to_response('pds_dataentry_rec_create.html',
                                      context,
                                      context_instance=RequestContext(request))
Exemplo n.º 12
0
 def rec_id_validator(new_record_id):
     return SubjectUtils.validate_new_record_id(pds, subject,
                                                new_record_id)
Exemplo n.º 13
0
 def rec_id_validator(new_record_id, include_path):
     return SubjectUtils.validate_new_record_id(
         context['pds'], context['subject'], new_record_id, include_path)
Exemplo n.º 14
0
 def rec_id_validator(new_record_id):
     return SubjectUtils.validate_new_record_id(
         pds, subject, new_record_id)
Exemplo n.º 15
0
    def post(self, request, **kwargs):
        context = self.get_context_data(**kwargs)

        def rec_id_validator(new_record_id, include_path):
            return SubjectUtils.validate_new_record_id(
                context['pds'], context['subject'], new_record_id, include_path)

        try:
            grp = SubjectUtils.get_protocol_subject_record_group(
                self.pds.protocol, self.subject)
            grp.client_key = self.pds.protocol._settings_prop(
                'CLIENT_KEY', 'key', '')
            rec_id_prefix = ''

            label_id = request.POST.get('label_id', 1)
            if grp:
                rec_id_prefix = grp.ehb_key
            else:
                request.META['action'] = 'Subject record group not found for {0}'.format(context['subject'].id)
                request.META['error'] = True
                raise Exception('No subject record group found')
            # Try to process the new record form
            try:
                rec_id = self.driver.process_new_record_form(
                    request=request,
                    record_id_prefix=rec_id_prefix,
                    record_id_validator=rec_id_validator
                )
                # If we have successfully created the record. Make sure it is in the eHB.
                try:
                    self.record_id = SubjectUtils.create_new_ehb_external_record(
                        self.pds, request.user, self.subject, rec_id, label_id).id
                    if self.check_cache():
                        self.update_cache()
                    return HttpResponseRedirect(self.start_path)
                except RecordCreationError as rce:  # exception from the eHB
                    request.META['action'] = rce.errmsg
                    request.META['error'] = True
                    context['errors'].append((
                        'The record could not be created on the '
                        'electronic Honest Broker. Please contact a '
                        'system administrator. There could be a '
                        'connection problem.'))
            except IgnoreEhbExceptions as iee:
                # TODO this is a hack until BRP can create Nautilus records
                rec_id = iee.record_id
                ehb_rec_id = ''
                try:
                    # this will create the ehb external_record entry
                    # and add that record to the subject's record group
                    self.record_id = SubjectUtils.create_new_ehb_external_record(
                        self.pds, request.user, self.subject, rec_id, label_id).id

                    if self.check_cache():
                        self.update_cache()
                    self.start_path = '{0}/dataentry/protocoldatasource/{1}/subject/{2}/record/{3}/start/'.format(
                        self.service_client.self_root_path,
                        self.pds.id,
                        self.subject.id,
                        self.record_id)
                    return HttpResponseRedirect(self.start_path)

                except RecordCreationError as rce:  # exception from the eHB
                    request.META['action'] = rce.errmsg
                    request.META['error'] = True
                    record_already_exists = 6
                    cause = rce.raw_cause
                    if cause and len(cause) == 1 and record_already_exists == cause[0]:
                        er_rh = ServiceClient.get_rh_for(
                            record_type=ServiceClient.EXTERNAL_RECORD)
                        ehb_recs = er_rh.get(
                            external_system_url=self.pds.data_source.url, subject_id=self.subject.id, path=self.pds.path)
                        ehb_rec_id = None
                        for record in ehb_recs:
                            if record.record_id == rec_id:
                                ehb_rec_id = record.id
                        if ehb_rec_id:
                            self.start_path = '%s/dataentry/protocoldatasource/%s/subject/%s/record/%s/start' % (
                                ServiceClient.self_root_path,
                                self.pds.id,
                                self.subject.id,
                                ehb_rec_id)
                        else:
                            context['errors'].append(
                                'This ID has already been assigned to another subject.')
                            return render_to_response(
                                'pds_dataentry_rec_create.html',
                                context,
                                context_instance=RequestContext(request)
                            )
                    else:
                        request.META['action'] = 'Record could not be created'
                        request.META['error'] = True
                        context['errors'].append((
                            'The record could not be created on the '
                            'electronic Honest Broker. Please contact '
                            'a system administrator. There could be a'
                            ' connection problem.'))
                except:
                    return HttpResponse('Unknown Error')
                return HttpResponseRedirect(self.start_path)

        except RecordCreationError as rce:
            # Handle errors in the form.
            request.META['action'] = rce.errmsg
            request.META['error'] = True
            context['errors'].append(rce.cause)
            return render_to_response(
                'pds_dataentry_rec_create.html',
                context,
                context_instance=RequestContext(request)
            )
Exemplo n.º 16
0
    def handle(self, *args, **options):

        if options['protocol']:
            if options['continue']:
                protocols = Protocol.objects.filter(
                    id__gte=options['protocol']).order_by('id').all()
            else:
                try:
                    protocols = Protocol.objects.filter(
                        id=options['protocol']).all()
                except Protocol.DoesNotExist:
                    print('Protocol specified does not exist')
                    return
        else:
            protocols = Protocol.objects.order_by('id').all()
        g_rh = ServiceClient.get_rh_for(record_type=ServiceClient.GROUP)
        errors = []
        msg = None
        for protocol in protocols:
            print('Checking {0} (ID:{1}) Group: {2}'.format(
                protocol.name, protocol.id, protocol.ehb_group_name()))
            try:
                g_rh.get(name=protocol.ehb_group_name())
            except RequestedRangeNotSatisfiable:
                msg = ('Unable to find expected group: {0}'.format(
                    protocol.ehb_group_name()))
                errors.append(msg)
                print(msg)
                msg = None
            # Check for pds groups
            subjects = protocol.getSubjects()
            if subjects:
                for subject in subjects:
                    try:
                        SubjectUtils.get_protocol_subject_record_group(
                            protocol, subject)
                    except RequestedRangeNotSatisfiable as e:
                        msg = (
                            'Warning: Group not found under Protocol: {0} Group name expected: {1}'
                            .format(protocol.name, e.errmsg))
                    except Organization.DoesNotExist:
                        msg = (
                            'Warning: Could not find associated Organization for protocol: {0}, eHB Org ID: {1} Likely eHB/BRP organizaiton mismatch'
                            .format(protocol.name, subject.organization_id))

                    except BadStatusLine:
                        msg = (
                            'Warning: Malformed Group Detected for Protocol: {0} Subject ID: {1} Likely Bad Organization ID'
                            .format(protocol.name, subject.id))
                    if msg:
                        errors.append(msg)
                        print(msg)
                        msg = None

        if csv:
            with open('group_report_{0}.csv'.format(
                    datetime.now().strftime('%Y%m%d.%H:%M.%S')),
                      'w',
                      newline='') as csvfile:
                csv_writer = csv.writer(csvfile,
                                        delimiter=' ',
                                        quotechar='"',
                                        quoting=csv.QUOTE_MINIMAL)
                for error in errors:
                    csv_writer.writerow([error])
Exemplo n.º 17
0
    def handle(self, *args, **options):

        if options['protocol']:
            if options['continue']:
                protocols = Protocol.objects.filter(id__gte=options['protocol']).order_by('id').all()
            else:
                try:
                    protocols = Protocol.objects.filter(id=options['protocol']).all()
                except Protocol.DoesNotExist:
                    print('Protocol specified does not exist')
                    return
        else:
            protocols = Protocol.objects.order_by('id').all()
        g_rh = ServiceClient.get_rh_for(record_type=ServiceClient.GROUP)
        errors = []
        msg = None
        for protocol in protocols:
            print('Checking {0} (ID:{1}) Group: {2}'.format(
                protocol.name,
                protocol.id,
                protocol.ehb_group_name()))
            try:
                g_rh.get(name=protocol.ehb_group_name())
            except RequestedRangeNotSatisfiable:
                msg = ('Unable to find expected group: {0}'.format(protocol.ehb_group_name()))
                errors.append(msg)
                print(msg)
                msg = None
            # Check for pds groups
            subjects = protocol.getSubjects()
            if subjects:
                for subject in subjects:
                    try:
                        SubjectUtils.get_protocol_subject_record_group(protocol, subject)
                    except RequestedRangeNotSatisfiable as e:
                        msg = ('Warning: Group not found under Protocol: {0} Group name expected: {1}'.format(
                            protocol.name,
                            e.errmsg
                        ))
                    except Organization.DoesNotExist:
                        msg = ('Warning: Could not find associated Organization for protocol: {0}, eHB Org ID: {1} Likely eHB/BRP organizaiton mismatch'.format(
                            protocol.name,
                            subject.organization_id
                        ))

                    except BadStatusLine:
                        msg = ('Warning: Malformed Group Detected for Protocol: {0} Subject ID: {1} Likely Bad Organization ID'.format(
                            protocol.name,
                            subject.id
                        ))
                    if msg:
                        errors.append(msg)
                        print(msg)
                        msg = None

        if csv:
            with open('group_report_{0}.csv'.format(datetime.now().strftime('%Y%m%d.%H:%M.%S')), 'w', newline='') as csvfile:
                csv_writer = csv.writer(csvfile, delimiter=' ',
                                        quotechar='"', quoting=csv.QUOTE_MINIMAL)
                for error in errors:
                    csv_writer.writerow([error])