Ejemplo n.º 1
0
def create_models_from_submission(submission):
    if submission.form.name != 'Medicines Form':
        return
    with transaction.commit_on_success():
        content = submission.content
        name = content.section_general.facility_name
        try:
            facility = submission.facilitysubmission_set.all()[0].facility
        except IndexError:
            raise ValueError('Submission is not linked to a facility.')
        timestamp = iso8601.parse_date(content.start_time)
        meds = [m for m in content.section_stocked.nodes() if not m.endswith('_comments')]
        for m in meds:
            if getattr(content.section_stocked, m).lower() == 'yes':
                medicine = Medicine.from_name(m)
                medicine_node = getattr(content, 'medicine-'+m)
                # Create MedicineStock object.
                if medicine_node.medicine_available:
                    amount = int(medicine_node.packs_available)
                else:
                    amount = 0
                MedicineStock.objects.create(
                    submission = submission,
                    facility = facility,
                    medicine = medicine,
                    timestamp = timestamp,
                    amount = amount
                    )
                # Create MedicineRestockExpected object.
                time = medicine_node.restock_date
                start, end = get_start_and_end_dates(time, timestamp)
                if start and end:
                    amount = int(medicine_node.restock_ordered)
                    if amount == 9999:
                        amount = None
                    MedicineRestockExpectation.objects.create(
                        submission = submission,
                        facility = facility,
                        medicine = medicine,
                        timestamp = timestamp,
                        start = start,
                        end = end,
                        amount = amount
                        )
                # Create MedicineRestock object.
                time = medicine_node.last_restock_date
                start, end = get_start_and_end_dates(time, timestamp, ago=True)
                if start and end:
                    amount = int(medicine_node.last_restock_amount)
                    if amount == 9999:
                        amount = None
                    MedicineRestock.objects.create(
                        submission = submission,
                        facility = facility,
                        medicine = medicine,
                        timestamp = timestamp,
                        start = start,
                        end = end,
                        amount = amount
                        )
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     created = 0
     errors = 0
     submissions = ORFormSubmission.objects.filter(
         form__name="Medicines Form",
         medicinestockout=None,
         submissionworkerdevice__valid=True,
         submissionworkerdevice__verified=True
     )
     for submission in submissions:
         content = submission.content
             
         try:
             meds_names = [m for m in content.nodes() if str(m).startswith('medicine-')] 
             meds = [(med, getattr(content, med)) for med in meds_names]
             for med_name, section in meds:
                 if section.medicine_available.lower() == "no":
                     try:
                         medicine = Medicine.from_name(med_name)
                         try:
                             facility = submission.facilitysubmission_set.all()[0].facility
                             MedicineStockout.objects.create(
                                 medicine=medicine,
                                 facility=facility,
                                 submission=submission
                             )
                             created += 1
                         except IndexError:
                             errors += 1
                             logger.warn("Could not find facility for: %s (%d)" % (submission.filename, submission.id))
                     except Exception, e:
                         errors += 1
                         logger.exception(e)
         except Exception, e:
             errors += 1
             logger.exception(e)