def process_fixes(self, fix_file, fixes):
        with open(fix_file) as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                if row['Station1'] != '':
                    if row['New Location'] == 'TRUE':
                        fixes['new_location'].append({
                            'station':
                            row['Station1'],
                            'location':
                            row['Location']
                        })
                    elif row['Matching SL Name'] == '':
                        print('__Other for', row['Location'])
                        fixes['mapping'][row['Location']] = "__Other"
                    else:
                        fixes['mapping'][
                            row['Location']] = row['Matching SL Name']

                if row['New Location Name'] != '':
                    fixes['rename'][row['Current Location']] = {
                        'station': row['Station2'],
                        'new_location': row['New Location Name']
                    }

                if row['Currently Monitoring'] == 'FALSE':
                    fixes['not_monitoring'][
                        row['Current Location']] = row['Station2']

            for new_location in fixes['new_location']:
                try:
                    existing = Location.objects.get(
                        border_station__station_name=new_location['station'],
                        name=new_location['location'])
                    print('location', new_location['location'],
                          'already exists for station',
                          new_location['station'])
                except ObjectDoesNotExist:
                    location = Location()
                    print('new_location, border_station',
                          new_location['station'])
                    location.border_station = BorderStation.objects.get(
                        station_name=new_location['station'])
                    location.name = new_location['location']
                    location.save()

            for key, value in fixes['rename'].items():
                try:
                    location = Location.objects.get(
                        border_station__station_name=value['station'],
                        name=key)
                    location.name = value['new_location']
                    location.save()
                except ObjectDoesNotExist:
                    print('rename location', key, value, 'not found')

            for key, value in fixes['not_monitoring'].items():
                try:
                    location = Location.objects.get(
                        border_station__station_name=value, name=key)
                except ObjectDoesNotExist:
                    print('Unable to find location for station', value,
                          'location', key)

                location.active = False
                location.save()

            for key, value in fixes['mapping'].items():
                if value in fixes['rename']:
                    fixes['mapping'][key] = fixes['rename'][value][
                        'new_location']
예제 #2
0
 def process_row(self, in_row, renum):
     irf = IrfBangladesh()
     row = dict(in_row)
     
     irf_number = row['IRF #'].strip()
     if irf_number in self.cleaned:
         cleaned_addresses = self.cleaned[irf_number]
     else:
         print('Unable to find new address for IRF # ', irf_number)
         return
     
     if irf_number == renum['old'].strip():
         row['IRF #'] = renum['new']
     else:
         print('renumber fail ',row['IRF #'],renum['old'])
         return
         
     
     station_code = renum['new'][:3]
     if station_code is None:
         print ('Unable to process row with Station Code of None')
         return
     try:
         station = BorderStation.objects.get(station_code=station_code)
     except Exception:
         print('Unable to locate station for code ' + station_code)
         return
     
     location_name = row['Location']
     if location_name is not None and location_name.strip() != '':
         existing = Location.objects.filter(name=location_name, border_station = station)
         if len(existing) < 1:
             location = Location()
             location.name = location_name
             location.border_station = station
             location.save()
     
     irf.entered_by = Account.objects.get(id=10022)
     
     irf.station = station
     irf.status = 'in-progress'
     for question_id in ImportBangladesh.main_questions.keys():
         entry = ImportBangladesh.main_questions[question_id]
         if 'columns' not in entry:
             continue
         
         if len(entry['columns']) > 1:
             pass
         else:
             column = entry['columns'][0]
             value = row[column]
             question = Question.objects.get(id=question_id)
             #print('<<<<',question.export_name, '>>>>', value)
             if 'contains' in entry:
                 if value is not None and entry['contains'] in value:
                     result = True
                 else:
                     result = False
             elif 'map' in entry:
                 if value in entry['map']:
                     result = entry['map'][value]
                 else:
                     result = value
             elif question.answer_type.name == 'Checkbox' and (question.params is None or 'textbox' not in question.params):
                 if value is None or value.upper() == 'NO':
                     result = False
                 elif value.upper() == 'YES':
                     result = True
             elif question.answer_type.name == 'DateTime':
                 local_time = parse(value)
                 tz = pytz.timezone(station.time_zone)
                 result = tz.localize(local_time) 
             elif ((question.answer_type.name == 'Integer' or question.answer_type.name == 'Float') and 
                   (value is None or value.strip() == '')):
                 result = 0
             else:
                 result = value
         
         if (result is None or result == '') and 'default' in entry:
             result = entry['default'] 
         
         storage = QuestionStorage.objects.get(question_id = question_id)
         setattr(irf, storage.field_name, result)
         
     if irf.irf_number is None or irf.irf_number == '':
         return
     try:
         irf.save()
     except Exception as exc:
         print (irf.irf_number, exc.args[0])
         return
     
     for idx in range(1,3):
         prefix = 'Victim ' + str(idx) + ' '
         addr_prefix = 'V' + str(idx) + ' '
         self.create_interceptee(row, 'v', prefix, addr_prefix, irf, cleaned_addresses)
         
     
     for idx in range(1,3):
         prefix = 'Trafficker ' + str(idx) + ' '
         addr_prefix = 'T' + str(idx) + ' '
         self.create_interceptee(row, 't', prefix, addr_prefix, irf, cleaned_addresses)
     
     irf.status = 'approved'
     form_data = FormData(irf, self.form)
     validate = ValidateForm(self.form, form_data, True)
     validate.validate()
     if len(validate.errors) < 1:
         irf.save()