Beispiel #1
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': '*'})
Beispiel #2
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': '*'}
     )
 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]
    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': '*'})