def create(self, **query): query['user'] = self.user logger.debug(query) query = self._check_fields(query) if isinstance(query, Message): logger.debug("I'm a message!") return query query['action_list'] = [self._create_action(query)] if isinstance(query['action_list'], Message): return query['action_list'] query['id_patient'] = query['id_examination'][0].id_patient.id_patient logger.debug(query) report = Report(**query) # report = db_wrapper.insert('report', **query) try: report.save() except (FieldDoesNotExist, NotUniqueError, SaveConditionError) as err: txt_message = "Report cannot be saved: %s " % (err.message) logger.error(txt_message) message = Message(ReportErrorHeader(message=txt_message)) return message # Porto gli esami a reported e_controller = ExaminationController(user=self.user) for ex_ in report.id_examination: _ = e_controller.eject(id=str(ex_.id)) self._currentReport = report message = "Report correctly created" report = Report.objects(id=report.id) # query = {'id':report.id} # report = db_wrapper.query('report', **query) message = Message(ReportCorrectHeader(message=message), data=report) return message
def run(database_config, n_loaded=50): db_config = parse_db_config(database_config) _ = easyris_connect(**db_config) Report.drop_collection() user = User.objects(username='******').first() examination_list = Examination.objects(status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) # Trovo i pazienti con piu esami id_patient_list = [e.id_patient.id_patient for e in examination_list] counter = collections.Counter(id_patient_list) if len(counter) < n_loaded: n_loaded = len(counter) / 2 print n_loaded for i in range(n_loaded): id_patient = counter.keys()[i] patient = Patient.objects(id_patient=id_patient).first() patient_examination = Examination.objects(id_patient=patient.id, status_name='completed', data_inserimento=datetime.datetime(year=2016, month=02, day=02)) dates_ = np.unique([e.data_inserimento for e in patient_examination]) for d_ in dates_: data_ = datetime.datetime(d_.year, d_.month, d_.day) same_date_examination = Examination.objects(id_patient=patient.id, data_inserimento=data_, status_name='completed') report_log = ReportAction( user=user, action='create' ) report = Report( id_examination = [e for e in same_date_examination], report_text = "Il paziente bla bla bla", action_list = [report_log], id_patient = id_patient, status=SuspendedReportStatus(), status_name='suspended' ) report.save() for e in same_date_examination: e.modify(status=ClosedExaminationStatus(), status_name='closed') e.save()