def create_customer(self): customer = Customers() customer.partner_id = self.partner.id customer.company_name = self.partner.company_name customer.address = self.partner.address_1 + '' + self.partner.address_2 customer.city = self.partner.city customer.state = self.partner.state customer.Pincode = self.partner.pin_code customer.country = self.partner.state customer.pan_number = '' customer.deleted = False customer.customer_vertical = None customer.delivery_sequence = '000' customer.save() print('Customer %s created' % customer.company_name) self.customer = customer ''' Creating customer contacts1 ''' self.create_customer_contacts() ''' Creating customer contacts2 ''' self.create_customer_contacts()
def handle(self, *args, **options): migration_csvs = os.path.join(settings.BASE_DIR, 'migrations') partner_map = {} print("Creating Partners...") with open(os.path.join(migration_csvs, 'partners.csv'), 'r', encoding='latin1') as csvfile: reader = csv.reader(csvfile) for row in reader: if not row[0]: print("Cannot find partner identifier for ", row[0]) continue partner = Partner() partner.company_name = row[3] if row[1] == 'Y': partner.status = True else: partner.status = False if row[28] == 'Y': partner.existing_status = True else: partner.existing_status = False partner.jba_code = str.strip(row[23]) partner.credits = row[24] partner.address_1 = row[4] partner.address_2 = row[5] partner.address_3 = row[6] partner.city = row[7] partner.state = row[8] partner.pin_code = row[9] partner.partner_type = 'R' partner.business_type = 'S' partner.focused_customer = '' partner.business_type = '' partner.interested_workload = '' original_date = row[21].split(' ') try: val = datetime.datetime.strptime(original_date[0], '%m/%d/%y') except ValueError: val = datetime.datetime.strptime(original_date[0], '%m/%d/%Y') partner.created_at = val partner.activated_at = val for field in partner._meta.fields: if field.name == 'created_at' or field.name == 'activated_at': field.auto_now_add = False partner.created_by = 1 partner.save() partner_id = partner.id partner_map[row[0].strip()] = partner_id c = ContactDetails() c.partner = partner c.type = 'P' c.name = str.format('{} {}', row[10], row[11]) c.email = row[13] c.mobile = row[12] c.save() contact_types = ['D/O', 'A&O', 'S'] for type in contact_types: c = ContactDetails() c.partner = partner c.type = type c.name = str.format('{} {}', row[10], row[11]) c.email = row[13] c.mobile = row[12] c.save() customer_map = {} print("Creating Customers...") with open(os.path.join(migration_csvs, 'customers.csv'), 'r', encoding='latin1') as csvfile: reader = csv.reader(csvfile) for row in reader: customer = Customers() if not row[1].strip() in partner_map.keys(): import pdb pdb.set_trace() print("Cannot find partner for customer: ", row[1]) continue customer.partner_id = partner_map[row[1].strip()] customer.company_name = row[2] customer.address = row[3] customer.city = row[6] customer.state = row[7] customer.country = row[8] customer.Pincode = row[9] customer.pan_number = row[22] customer.deleted = False original_date = row[24].split(' ') try: val = datetime.datetime.strptime(original_date[0], '%m/%d/%y') except ValueError: val = datetime.datetime.strptime(original_date[0], '%m/%d/%Y') customer.created_at = val for field in customer._meta.fields: if field.name == 'created_at': field.auto_now_add = False customer.save() customer_map[row[0]] = customer.id customer_contact = CustomerContacts() customer_contact.customer = customer customer_contact.name = str.format('{} {} {}', row[10], row[11], row[12]) customer_contact.position = row[13] customer_contact.email = row[15] customer_contact.mobile = row[14] customer_contact.save() customer_contact2 = CustomerContacts() customer_contact2.customer = customer customer_contact2.name = str.format('{} {} {}', row[16], row[17], row[18]) customer_contact2.position = row[19] customer_contact2.email = row[21] customer_contact2.mobile = row[20] customer_contact2.save() import json customer_contents = json.dumps(customer_map) f = open('/tmp/customer_map.json', 'w') f.write(customer_contents) f.close() print("Creating Users...") with open(os.path.join(migration_csvs, 'users.csv'), 'r', encoding='latin1') as csvfile: reader = csv.reader(csvfile) for row in reader: if not row[3].strip() in partner_map.keys(): print("Cannot find partner for user: "******"Found Redington user: {}, Please create manually", row[3]) continue user = RedUser() user.username = row[3] user.first_name = row[4] user.last_name = row[5] user.email = row[6] user.is_active = True user.is_staff = False user.save() permission_group_name = AppDefaults.get_predefined_roles() if Group.objects.filter( name=permission_group_name['Partner']).exists(): user.groups = Group.objects.filter( name=permission_group_name['Partner']) user.save() PartnerUserDetails.objects.create( user=user, partner=Partner.objects.get( pk=partner_map[row[3].strip()])) UserProfile.objects.create( user=user, user_type='P', description=str.format('Partner user for {}', row[3]), created_by=RedUser.objects.get(username='******'), role_id=2)
def create(self, request, *args, **kwargs): data = request.data tenant_id = None is_valid_domain = None if 'logo' in request.FILES: logo = request.FILES['logo'] customer_data = json.loads(data['data']) else: logo = None customer_data = json.loads(data['data']) # initial_partner_obj = InitialPartner.objects.filter(id=1) customer_obj = Customers() if logo != None: customer_obj.logo = logo if not request.user.is_superuser: if request.user.profile.user_type == 'P': customer_data['partner_id'] = request.user.partner.first( ).partner.id if 'cloud_account' in customer_data.keys(): cloud_account = customer_data['cloud_account'] if cloud_account['type'] == 'MS': domain = cloud_account['details']['domain_name'] if domain and domain != '': ms_api = MicrosoftApi() domain_exist = ms_api.check_domain_exists(domain) if not domain_exist: return Response('Invalid_domain') else: customer_info = ms_api.get_customer_from_domain(domain) if 'companyProfile' in customer_info: tenant_id = customer_info['companyProfile'][ 'tenantId'] is_valid_domain = True customer_obj.company_name = customer_data['company_name'] customer_obj.address = customer_data['address'] customer_obj.city = customer_data['city'] customer_obj.state = customer_data['state'] customer_obj.Pincode = customer_data['postcode'] customer_obj.country = customer_data['country'] customer_obj.pan_number = customer_data['pan_number'] customer_obj.customer_vertical = customer_data['customer_vertical'][0][ 'id'] customer_obj.delivery_sequence = customer_data['delivery_sequence'] customer_obj.segment = customer_data['segment'][0]['id'] if 'partner_id' in customer_data: partnerId = customer_data['partner_id'] customer_obj.partner_id = partnerId customer_obj.save() try: # Inserting primary contact details of Customer self.saveContactDetails(customer_data['primary_contact'], customer_obj.id) except Exception: return Response(Exception) try: # Inserting secondary contact details of Customer self.saveContactDetails(customer_data['secondary_contact'], customer_obj.id) except Exception: return Response(Exception) """ Validating and Storing customer's cloud account requirements """ if 'cloud_account' in customer_data.keys(): try: cloud_account_data = customer_data['cloud_account'] if tenant_id is not None: cloud_account_data['details']['tenant_id'] = tenant_id cloud_account_data['details']['active'] = is_valid_domain if is_valid_domain: cloud_account_data['active'] = True cloud_account_data['customer'] = customer_obj.id cloud_account_serializer = CloudAccountsSerializer( data=cloud_account_data, context={'request': request}) cloud_account_serializer.is_valid(raise_exception=True) cloud_account_serializer.save() except Exception: customer_obj.delete() return Response(Exception) if cloud_account['type'] == 'MS': domain = cloud_account_data['details']['domain_name'] if tenant_id is None and domain != '': data['customer'] = customer_obj.id CloudAccountsViewSet.store_ms_domain( data, domain, cloud_account_serializer.data['id']) return Response(customer_obj.id)