Example #1
0
def send_message(user, args):
    room_id = args['room_id']

    participant = db.session.query(RoomParticipant).filter_by(
        room_id=args['room_id'], user_id=user.id).first()
    if participant.status != ParticipantStatus.IN:
        raise Error(StatusCode.FORBIDDEN, message='Invalid room access')

    message = Message(user_id=user.id, **args)
    db.session.add(message)
    db.session.commit()

    data = {
        'user_id': user.id,
        'username': user.name,
        'room_id': room_id,
        'content': args['content']
    }
    pusher.trigger(room_id, PusherEvent.NEW_MESSAGE, data)

    return jsonify({
        'message': 'Message added successfully',
        'data': MessageSchema().dump(message).data
    }), 200
Example #2
0
def upload_complete(request):
    try:
        identifier = request.POST['identifier']
        case_id = request.POST['case_id']
        
        directory = settings.MEDIA_ROOT + 'dicom/' + identifier
        file_names = [ f for f in listdir(directory) if isfile(join(directory,f)) ]
        file_names.sort()
        
        #Generate zip file
        with ZipFile(directory + '/' + identifier + '.zip', 'w') as myzip:
            for fn in file_names:
                f_path = join(directory,fn)
                myzip.write(f_path)
            myzip.close()
        
        #Generate images
        image_count = 0
        for fn in file_names:
            f_path = join(directory,fn)
            new_path = join(directory,str(image_count+1)+'.dcm')
            rename(f_path, new_path)
            #Convert images
            LINUX = sys.platform.startswith('linux')
            MAC = sys.platform.startswith('darwin')
            WINDOWS = sys.platform.startswith('win32')
            if LINUX or MAC:
                call([settings.PROJECT_ROOT + 'main/utils/dicom2-unix', new_path, 
                     '-w', '--to=' + directory,  '--rename=cur_nm'])
            else:
                call(settings.PROJECT_ROOT + 'main/utils/dicom2-windows ' + new_path +
                         ' -w --to=' + directory + ' --rename=cur_nm')
            image_count += 1
        
        #Update database
        data = MRIData(name=identifier)
        data.image_count = image_count 
        data.broker_id = request.user.pk
        data.save()
        
        case_id = request.POST['case_id']
        case = Case.objects.get(pk=case_id)
        case.data = data
        case.status = 2
        case.save()
        
        #Save start and end image index
        count = 1
        sequences = {} #key:different part, key:(start, end)
        for fn in file_names:
            parts = fn.split('.')
            if parts[8] in sequences:
                start, end = sequences[parts[8]]
                if count < start:
                    start = count
                if count > end:
                    end = count
                sequences[parts[8]] = (start, end)
            else:
                sequences[parts[8]] = (count, count)
            count += 1
        
        for key in sequences:
            ds = DataSequence(data=data, name=key, 
                              image_start=sequences[key][0], image_end = sequences[key][1])
            ds.save()

        apt = Appointment.objects.get(case=case_id, is_current=True)
        
        #Send message to referring doctor to notify him that scan is complete and uploaded
        message = Message(is_sys=True, type=3, receiver=apt.doctor, case=case)
        message.title = 'MRI scan complete for ' + case_id
        message.content = 'The MRI scan is complete for case ' + case_id \
                          + '. Please wait for cardiologist to upload report' 
        message.save()
        
        return HttpResponse('{"code":"0", "msg":"Completed!"}')
    except Exception, e:
        return HttpResponse('{"code":"1", "msg":"Error while processing dicom files."}')
Example #3
0
def submit_report(request):
    profile = Profile.objects.get(pk=request.user.pk)
    diagnosis = request.POST['diagnosis']
    
    
    case_id = request.POST['case_id']
    case = Case.objects.get(pk=case_id)
    data = case.data
    
    image_list = []
    comments_list = [] #list of string
    comments_str = request.POST['comments']
    if comments_str:
        comments = comments_str.split('!#!')
        for c in comments:
            split_index = c.find(':')
            image_index = int(c[:split_index])        
            content = c[split_index+1:] if split_index != len(c) - 1 else ''
            cinstance = Comment(data=data, image_index=image_index, content=content)
            cinstance.save()
            comments_list.append(content)
            image_list.append(str(image_index)+'.dcm.bmp')
    
    
    #Generate PDF report
    identifier = data.name
    directory = settings.MEDIA_ROOT + 'dicom/' + identifier
    pdf_path = settings.MEDIA_ROOT + 'dicom/' + identifier + '/' + identifier + '.pdf'
    patient_dob_str = case.patient.birthday.strftime("%m/%d/%Y")
    patient_gender = 'Male' if case.patient.gender else 'Female'
    Report2PDF(profile.first_name, profile.last_name, case.patient.first_name, case.patient.last_name,
               patient_gender, patient_dob_str, directory, image_list, comments_list, pdf_path, diagnosis)
    report = Report(content=diagnosis)
    report.file = 'dicom/' + identifier + '/' + identifier + '.pdf'  
    report.save()
    
    case.status = 5
    case.report = report
    case.save()
    
    apt = Appointment.objects.get(case=case, is_current=True)
    
    message = Message(is_sys=False, receiver=apt.doctor, case=case)
    message.sender_id = request.user.pk
    message.title = 'Report uploaded for Case #' + case_id
    message.content = 'Report is uploaded for Case #' + case_id + \
                      'from cardiologist ' + profile.first_name + ' ' + profile.last_name + ': '\
                      + diagnosis
    message.save()
    
    
    '''
    send system message
    '''
    message = Message(is_sys=True, receiver=apt.doctor, case=case, type=4)
    message.title = 'Report Available for Case #' + case_id
    message.content = 'Report is available for Case #' + case_id + \
                      'from cardiologist ' + profile.first_name + ' ' + profile.last_name + ': '\

    message.save()
    
    return HttpResponse('{"code":"0", "msg":"Success"}')