def clean_device_list(self): csv_data = read_csv_file(self.cleaned_data['device_list'], ('meid', 'mpn', 'ip', 'status_changed_on'), 1) errors = [] self.devices_to_update = [] for row_num, row in enumerate(csv_data, 1): parsed_status_changed = None device = None try: device = GDrive.objects.get(meid=row['meid']) except GDrive.DoesNotExist: errors.append( "[{0}] Could not find device with MEID: {1}".format( row_num, row['meid'])) try: parsed_status_changed = parse(row['status_changed_on']) except ValueError: errors.append("[{0}] Could not parse date/time: {1}".format( row_num, row['status_changed_on'])) if errors: continue details = { 'phone_number': row['mpn'], 'ip_address': row['ip'], 'datetime_network_status_changed': parsed_status_changed, 'network_status': self.cleaned_data['new_status'] } if (self.cleaned_data['new_status'] == GDrive.DEVICE_NETWORK_STATUS_ACTIVE): details['datetime_network_status_activated'] = \ parsed_status_changed self.devices_to_update.append((device, details)) if errors: raise forms.ValidationError("\n".join(errors))
def clean_csv(self): self.forms = [] names = set() csv_data = read_csv_file(self.cleaned_data['csv'], self.CSV_FIELD_NAMES) for count, data in enumerate(csv_data, 1): data['phone_0_phone'] = data['phone'] ptype = data['phone_type'].lower() if ptype == 'contact': data['phone_0_is_contact'] = True elif ptype == 'cell': data['phone_0_is_cell'] = True del data['phone'], data['phone_type'] if data['name'] in names: raise forms.ValidationError("Line {0} is invalid: {1}".format( count, "* name * Genesis group with this name already exists in " "CSV file.")) names.add(data['name']) g_form = GroupForm(data) if not g_form.is_valid(): raise forms.ValidationError("Line {0} is invalid: {1}".format( count, g_form.errors.as_text().replace('\n', ' '))) self.forms.append(g_form)
def clean_csv(self): self.forms = [] emails = set() csv_data = read_csv_file( self.cleaned_data['csv'], self.CSV_FIELD_NAMES) for count, data in enumerate(csv_data, 1): data['phone_0_phone'] = data['phone'] ptype = data['phone_type'].lower() if ptype == 'contact': data['phone_0_is_contact'] = True elif ptype == 'cell': data['phone_0_is_cell'] = True del data['phone'], data['phone_type'] data['confirm_email'] = data['email'] if data['email'] in emails: raise forms.ValidationError("Line {0} is invalid: {1}".format( count, "* email * Professional with this Email already exists " "in CSV file.")) emails.add(data['email']) p_form = ProfessionalForm( data, requester=self.requester, initial_group=self.initial_group) if not p_form.is_valid(): raise forms.ValidationError("Line {0} is invalid: {1}".format( count, p_form.errors.as_text().replace('\n', ' '))) self.forms.append(p_form)
def clean(self): self.forms = [] self.datas = [] csv_data = read_csv_file(self.cleaned_data['csv'], self.CSV_FIELD_NAMES) patients = User.objects.filter(patient_profile__isnull=False) taken_usernames = list( map(lambda x: x['username'], patients.values('username'))) for count, data in enumerate(csv_data, 1): if not data['first_name']: continue data['phone_0_phone'] = data['phone'] ptype = data['phone_type'].lower() if ptype == 'contact': data['phone_0_is_contact'] = True elif ptype == 'cell': data['phone_0_is_cell'] = True del data['phone'], data['phone_type'] data['email_confirm'] = data['email'] try: month, day, year = data['date_of_birth'].split('/') except ValueError: pass else: if len(year) == 2: full_year = expand_birthday_year(year) data['date_of_birth'] = '/'.join( [month, day, str(full_year)]) raw_data = data.copy() line_form_kwargs = {'initial_group': self.initial_group} username = data.pop('username', None) if username: try: user = patients.get(username=username) except User.DoesNotExist: raise forms.ValidationError( "Invalid username: {}".format(username)) line_form_kwargs['instance'] = user else: new_username = PatientProfile.generate_username( data['email'], data['first_name'], data['last_name'], also_skip=taken_usernames, skip_db_check=True) taken_usernames.append(new_username) line_form_kwargs['supplied_username'] = new_username raw_data['username'] = new_username data['company'] = self.cleaned_data['company'].id p_form = ImportPatientLineForm(data, **line_form_kwargs) if not p_form.is_valid(): raise forms.ValidationError("Line {0} is invalid: {1}".format( count, p_form.errors.as_text().replace('\n', ' '))) self.datas.append(raw_data) self.forms.append(p_form) return self.cleaned_data
def get_row_forms(self): if not hasattr(self, '_forms'): forms = [] rows = read_csv_file( self.cleaned_data['import_file'], self.csv_headers, skip_lines=self.skip_lines) for row in rows: forms.append(self.line_form_class(row)) self._forms = forms return self._forms
def clean_csv(self): data = read_csv_file(self.cleaned_data['csv'], self.CSV_FIELD_NAMES) self.successful_orders = [] errors = [] for idx, row in enumerate(data): try: order = Order.objects.get(pk=row['order_id']) except Order.DoesNotExist: errors.append("Row {0}: Invalid order id {1}".format( idx, row['order_id'])) continue if not order.can_be_fulfilled(): errors.append( "Row {0}: Invalid order {1} - order cannot be fulfilled.") if errors: continue if row['success'].lower() == 'y': self.successful_orders.append(order) if errors: raise forms.ValidationError("\n".join(errors)) return self.cleaned_data['csv']
def clean_csv(self): names = set() self.forms = [] csv_data = read_csv_file( self.cleaned_data['csv'], self.CSV_FIELD_NAMES) for count, data in enumerate(csv_data, 1): data['phone_0_phone'] = data['phone'] ptype = data['phone_type'].lower() if ptype == 'contact': data['phone_0_is_contact'] = True elif ptype == 'cell': data['phone_0_is_cell'] = True del data['phone'], data['phone_type'] payor_name = data['payor'] try: data['payor'] = Payor.objects.get( name=payor_name, group=self.initial_group).id except Payor.DoesNotExist: msg = '* payor * A payor with the name {0} does not exist ' \ 'in this Group.'.format(payor_name) raise forms.ValidationError("Line {0} is invalid: {1}".format( count, msg)) if data['name'] in names: raise forms.ValidationError( "Line {0} is invalid: {1}".format( count, "* name * Company with this name already exists in " "CSV file.")) names.add(data['name']) p_form = CompanyForm(data, initial_group=self.initial_group) if not p_form.is_valid(): raise forms.ValidationError("Line {0} is invalid: {1}".format( count, p_form.errors.as_text().replace('\n', ' '))) self.forms.append(p_form)
def clean_csv(self): data = read_csv_file(self.cleaned_data['csv'], self.CSV_FIELD_NAMES) self.order_data = {} products = {} errors = [] for idx, row in enumerate(data): try: patient = User.objects.filter( patient_profile__isnull=False).get(pk=row['patient_id']) except User.DoesNotExist: errors.append("Row {0}: Invalid patient id {1}".format( idx, row['patient_id'])) if row['product_id'] not in products: try: product = ProductType.objects.get(id=row['product_id']) except ProductType.DoesNotExist: errors.append("Row {0}: Invalid product id {1}".format( idx, row['product_id'])) else: products[row['product_id']] = product try: quantity = int(row['quantity']) except ValueError: errors.append("Row {0}: Invalid quantity {1}".format( idx, row['quantity'])) if errors: continue if patient.id not in self.order_data: self.order_data[patient.id] = { 'patient': patient, 'products': [], } self.order_data[patient.id]['products'].append((product, quantity)) if errors: raise forms.ValidationError("\n".join(errors)) return self.cleaned_data['csv']