Ejemplo n.º 1
0
            tests = [None] * 100 # TODO: Initialize the tests (multiply wit some big number)
            for para in parameters:
                test = "physchem_test_testresults_" + para.test_name_short
                if sample_data.get(test) == None: # a simple check to get temperature and weather assessments
                    test =  "physchem_test_assessment_" + para.test_name_short
                index = int(para.pk)
                tests.insert(index, test)
            
            # TODO: ['physchem_test_assessment_temperature'] ['physchem_test_assessment_weather']
            # should this be in test parameter or in the notes??

            # empty list for measured values
            
            for some in tests:
                if sample_data.get(some) != None:
                    para_id = tests.index(some)
                    # this test is present in the xform, hence store it's value.
                    value = MeasuredValue()
                    value.value = sample_data[some]

                    # TODO: get a parameter for the value. according to the test done.
                    value.parameter = Parameter.objects.get(id = para_id)
                    value.sample = sample
                    value.save()
                    vals.append(value)            
        send_sms_notifications(sample,vals,form_xmlns)

# Register to receive signals each time a Metadata is saved
post_save.connect(check_and_add_sample, sender=Metadata)

Ejemplo n.º 2
0
def check_and_add_sample(sender, instance, created, **kwargs):
    try:
        sample_data = instance.formdefmodel.row_as_dict(instance.raw_data)
    except:
        logging.debug("no data")
        return
    
    reporter = check_and_add_reporter(sample_data, instance.formdefmodel.domain)    
    
    if reporter is None:
        logging.debug("no reporter")
        return
    
    sampling_point = check_and_add_samplingpoint(sample_data, instance.formdefmodel.domain)
    
    if sampling_point is None:
        logging.debug("no sample point")
        return
         
    # only process newly created forms, not all of them
    if not created:
        logging.debug("no created")
        return    

    # update reporter location (latest seen location)
    update_reporter_location(reporter, sampling_point)

    form_xmlns = instance.formdefmodel.target_namespace
    prefix = form_xmlns.split("/")[-1] + '_test_'

    assessment_date = None
    
    if True or form_xmlns in WQR_XMLNS:
        sample = Sample()
        sample.created = datetime.utcnow()        
        sample.date_received = instance.attachment.submission.submit_time
        sample.date_taken = datetime.utcnow().strftime("%Y-%m-%d") # default date for now (string)
        sample.taken_by = reporter    
        sample.xform = instance.formdefmodel
        sample.sampling_point = sampling_point
        sample.meta_uid = instance.uid
        
        assessment_fields = {}

        # extract some key fields
        for key in sample_data.keys():
            # hack to find correct column prefix
            if key.find("meta_chw_id") != -1:
                prefix = key.replace('meta_chw_id', '')
            
            if key.find("datacapture_enteredby") != -1:
                sample.reporter_name = sample_data[key]

            if key.find("datacapture_comments") != -1:
                sample.notes = sample_data[key]
                
            if key.find("assessment_assessmentdate") != -1:                
                assessment_date = sample_data[key]
                
            if key.find("assessment_h2sbatch") != -1: # should change forms to be test agnostic
                sample.batch_number = sample_data[key]
            elif key.find("assessment_aquatestcode") != -1:
                sample.batch_number = sample_data[key]
       
        # collect all assessment values (looping again because prefix might not have been correct till now)
        for key in sample_data.keys():            
            if key.find("assessment") != -1:                
                short_key = key.replace(prefix + 'assessment_', '')
                
                if short_key not in ['pointcode', 'assessmentdate', 'pointname', 'areaname']:
                    if sample_data[key] is not None:
                        assessment_fields[short_key] = unicode(sample_data[key])
                    else:  
                        assessment_fields[short_key] = ''
     
        sample.assessment_fields = json.dumps(assessment_fields)               
        sample.date_raw = str(assessment_date)
        skip_date_check = False

        if type(assessment_date) == datetime:
            sample.date_taken = assessment_date.date()
        elif type(assessment_date) == date:
            sample.date_taken = assessment_date
        else:
            try:                
                sample.date_taken = parser.parse(assessment_date, dayfirst=True).date()
                skip_date_check = True
            except ValueError:
                sample.date_taken = sample.date_received.date()
                
        if not skip_date_check:
            # check if date_taken is sane
            diff = (sample.date_received.date() - sample.date_taken).days
        
            # in the future or too old
            if diff < 0 or diff > 30:
                sample.date_taken = sample.date_received.strftime("%Y-%m-%d 00:00:00")
        
        sample.save()
                    
        parameters = Parameter.objects.filter(meta=False)
        
        # hmm        
        tests = {}

        for p in parameters:
            test = prefix + "testresults_" + p.test_name_short
            
            if not sample_data.has_key(test): # physchem hack
                test = prefix + "assessment_" + p.test_name_short
                       
            if sample_data.has_key(test):
                tests[int(p.pk)] = test
        
        for parameter_pk, test in tests.iteritems():
            if sample_data[test] is not None:
                value = MeasuredValue()
                
                if sample_data[test] == "negative":
                    value.value = 0
                elif sample_data[test] == "positive":
                    value.value = 1
                else:
                    if sample_data[test] is not None:
                        value.value = sample_data[test]
                    else:
                        value.value = ''
                    
                value.parameter = Parameter.objects.get(id=parameter_pk)
                value.sample = sample
                value.save()

        # check sms notifications
        send_sms_notifications(sample)
    else:
        logging.debug("no xmlns")