def dnc_contact_import(request): """Import CSV file of DNC Contacts for the logged in user **Attributes**: * ``form`` - DNCContact_fileImport * ``template`` - dnc/dnc_contact/import_contact.html **Logic Description**: * Add new dnc contacts which will belong to the logged in user via csv file & get the result (upload success and failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records imported from the CSV file """ form = DNCContact_fileImport(request.user, request.POST or None, request.FILES or None) csv_data = '' msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 dup_contact_cnt = 0 bulk_record = [] if form.is_valid(): # col_no - field name # 0 - contact # To count total rows of CSV file # Get DNC Obj dnc = get_object_or_404(DNC, pk=request.POST['dnc_list'], user=request.user) records = csv.reader(request.FILES['csv_file']) total_rows = len(list(records)) BULK_SIZE = 1000 csv_data = csv.reader(request.FILES['csv_file']) # Read each Row for row in csv_data: row = striplist(row) if not row or str(row[0]) == 0: continue # Check field type try: int(row[0]) except ValueError: error_msg = _("Some of the imported data was invalid!") type_error_import_list.append(row) continue bulk_record.append(DNCContact(dnc_id=dnc.id, phone_number=row[0])) contact_cnt = contact_cnt + 1 if contact_cnt < 100: # We want to display only 100 lines of the success import success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: # Bulk insert DNCContact.objects.bulk_create(bulk_record) bulk_record = [] if bulk_record: # Remaining record DNCContact.objects.bulk_create(bulk_record) bulk_record = [] # check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s DNC contact(s) have been uploaded successfully out of %(total_rows)s row(s)!') \ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} if dup_contact_cnt > 0: error_msg = _('Duplicate DNC contact(s) %(dup_contact_cnt)s are not inserted!!') \ % {'dup_contact_cnt': dup_contact_cnt} data = { 'form': form, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, } return render_to_response('dnc/dnc_contact/import_dnc_contact.html', data, context_instance=RequestContext(request))
def contact_import(request): """Import CSV file of Contacts for the logged in user **Attributes**: * ``form`` - Contact_fileImport * ``template`` - dialer_contact/contact/import_contact.html **Logic Description**: * Before adding contacts, check dialer setting limit if applicable to the user. * Add new contacts which will belong to the logged in user via csv file & get the result (upload success and failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records imported from the CSV file """ # Check dialer setting limit if request.user and request.method == 'POST': # check Max Number of contacts if check_dialer_setting(request, check_for="contact"): request.session['msg'] = _("you have too many contacts. you are allowed a maximum of %(limit)s") % \ {'limit': dialer_setting_limit(request, limit_for="contact")} # contact limit reached frontend_send_notification(request, NOTIFICATION_NAME.contact_limit_reached) return HttpResponseRedirect(redirect_url_to_contact_list) form = Contact_fileImport(request.user, request.POST or None, request.FILES or None) csv_data = '' msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 bulk_record = [] if form.is_valid(): # col_no - field name # 0 - contact # 1 - last_name # 2 - first_name # 3 - email # 4 - description # 5 - status # 6 - address # 7 - city # 8 - country # 9 - country # 10 - unit_number # 11 - additional_vars # To count total rows of CSV file records = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') total_rows = len(list(records)) BULK_SIZE = 1000 csv_data = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') #Get Phonebook Obj phonebook = get_object_or_404(Phonebook, pk=request.POST['phonebook'], user=request.user) #Read each Row for row in csv_data: row = striplist(row) if not row or str(row[0]) == 0: continue #Check field type if not int(row[5]): error_msg = _("invalid value for import! please check the import samples or phonebook is not valid") type_error_import_list.append(row) break if len(row[9]) > 2: error_msg = _("invalid value for country code, it needs to be a valid ISO 3166-1 alpha-2 codes") type_error_import_list.append(row) break row_11 = '' if row[11]: try: row_11 = json.loads(row[11]) except: row_11 = '' bulk_record.append( Contact( phonebook=phonebook, contact=row[0], last_name=row[1], first_name=row[2], email=row[3], description=row[4], status=int(row[5]), address=row[6], city=row[7], state=row[8], country=row[9], # Note: country needs to be a country code (CA, ES) unit_number=row[10], additional_vars=row_11) ) contact_cnt = contact_cnt + 1 if contact_cnt < 100: #We want to display only 100 lines of the success import success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: #Bulk insert Contact.objects.bulk_create(bulk_record) bulk_record = [] # remaining record Contact.objects.bulk_create(bulk_record) bulk_record = [] #check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s contact(s) have been uploaded successfully out of %(total_rows)s row(s)!') \ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} data = RequestContext(request, { 'form': form, 'csv_data': csv_data, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, }) return render_to_response('dialer_contact/contact/import_contact.html', data, context_instance=RequestContext(request))
def dnc_contact_import(request): """Import CSV file of DNC Contacts for the logged in user **Attributes**: * ``form`` - DNCContact_fileImport * ``template`` - dnc/dnc_contact/import_contact.html **Logic Description**: * Add new dnc contacts which will belong to the logged in user via csv file & get the result (upload success and failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records imported from the CSV file """ form = DNCContact_fileImport(request.user, request.POST or None, request.FILES or None) csv_data = '' msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 dup_contact_cnt = 0 bulk_record = [] if form.is_valid(): # col_no - field name # 0 - contact # To count total rows of CSV file #Get DNC Obj dnc = get_object_or_404(DNC, pk=request.POST['dnc_list'], user=request.user) records = csv.reader(request.FILES['csv_file']) total_rows = len(list(records)) BULK_SIZE = 1000 csv_data = csv.reader(request.FILES['csv_file']) #Read each Row for row in csv_data: row = striplist(row) if not row or str(row[0]) == 0: continue #Check field type try: int(row[0]) except ValueError: error_msg = _("Some of the imported data was invalid!") type_error_import_list.append(row) continue bulk_record.append( DNCContact( dnc_id=dnc.id, phone_number=row[0]) ) contact_cnt = contact_cnt + 1 if contact_cnt < 100: #We want to display only 100 lines of the success import success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: #Bulk insert DNCContact.objects.bulk_create(bulk_record) bulk_record = [] if bulk_record: #Remaining record DNCContact.objects.bulk_create(bulk_record) bulk_record = [] #check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s DNC contact(s) have been uploaded successfully out of %(total_rows)s row(s)!') \ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} if dup_contact_cnt > 0: error_msg = _('Duplicate DNC contact(s) %(dup_contact_cnt)s are not inserted!!') \ % {'dup_contact_cnt': dup_contact_cnt} data = RequestContext(request, { 'form': form, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, }) return render_to_response('dnc/dnc_contact/import_dnc_contact.html', data, context_instance=RequestContext(request))
def import_contact(self, request): """Add custom method in django admin view to import CSV file of Contacts **Attributes**: * ``form`` - Contact_fileImport * ``template`` - admin/dialer_campaign/contact/import_contact.html **Logic Description**: * Before adding contact, check the dialer setting limit if applicable to the user. * Add a new contact which will belong to the logged in user via csv file & get the result (Upload success & failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records which are imported from The CSV file """ # Check dialer setting limit if request.user and request.method == 'POST': # check Max Number of subscribers per campaign if check_dialer_setting(request, check_for="contact"): msg = _("you have too many contacts. you are allowed a maximum of %(limit)s")\ % {'limit': dialer_setting_limit(request, limit_for="contact")} messages.error(request, msg) # campaign limit reached frontend_send_notification( request, NOTIFICATION_NAME.campaign_limit_reached) return HttpResponseRedirect( reverse("admin:dialer_campaign_contact_changelist")) opts = Contact._meta rdr = '' # will contain CSV data msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 bulk_record = [] form = Contact_fileImport(request.user, request.POST or None, request.FILES or None) if form.is_valid(): # col_no - field name # 0 - contact # 1 - last_name # 2 - first_name # 3 - email # 4 - description # 5 - status # 6 - address # 7 - city # 8 - state # 9 - country # 10 - unit_number # 11 - additional_vars # To count total rows of CSV file records = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') total_rows = len(list(records)) BULK_SIZE = 1000 rdr = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') #Get Phonebook Obj phonebook = Phonebook.objects.get(pk=request.POST['phonebook']) contact_cnt = 0 # Read each Row for row in rdr: row = striplist(row) if not row or str(row[0]) == 0: continue # check field type if not int(row[5]): error_msg = _( "invalid value for import! please check the import samples or phonebook is not valid" ) type_error_import_list.append(row) break if len(row[9]) > 2: error_msg = _( "invalid value for country code, it needs to be a valid ISO 3166-1 alpha-2 codes (http://en.wikipedia.org/wiki/ISO_3166-1)" ) type_error_import_list.append(row) break row_11 = '' if row[11]: row_11 = json.loads(row[11]) bulk_record.append( Contact(phonebook=phonebook, contact=row[0], last_name=row[1], first_name=row[2], email=row[3], description=row[4], status=int(row[5]), address=row[6], city=row[7], state=row[8], country=row[9], unit_number=row[10], additional_vars=row_11)) contact_cnt = contact_cnt + 1 if contact_cnt < 100: success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: # Bulk insert Contact.objects.bulk_create(bulk_record) bulk_record = [] # remaining record Contact.objects.bulk_create(bulk_record) bulk_record = [] #check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s contact(s) have been uploaded successfully out of %(total_rows)s row(s)!')\ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} ctx = RequestContext( request, { 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('dialer contact'), 'title': _('import contact'), 'rdr': rdr, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, }) return render_to_response( 'admin/dialer_contact/contact/import_contact.html', context_instance=ctx)
def import_contact(self, request): """Add custom method in django admin view to import CSV file of Contacts **Attributes**: * ``form`` - Contact_fileImport * ``template`` - admin/dialer_campaign/contact/import_contact.html **Logic Description**: * Before adding contact, check the dialer setting limit if applicable to the user. * Add a new contact which will belong to the logged in user via csv file & get the result (Upload success & failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records which are imported from The CSV file """ # Check dialer setting limit if request.user and request.method == 'POST': # check Max Number of subscribers per campaign if check_dialer_setting(request, check_for="contact"): msg = _("you have too many contacts. you are allowed a maximum of %(limit)s")\ % {'limit': dialer_setting_limit(request, limit_for="contact")} messages.error(request, msg) # campaign limit reached frontend_send_notification(request, NOTIFICATION_NAME.campaign_limit_reached) return HttpResponseRedirect(reverse("admin:dialer_campaign_contact_changelist")) opts = Contact._meta rdr = '' # will contain CSV data msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 bulk_record = [] form = Contact_fileImport(request.user, request.POST or None, request.FILES or None) if form.is_valid(): # col_no - field name # 0 - contact # 1 - last_name # 2 - first_name # 3 - email # 4 - description # 5 - status # 6 - address # 7 - city # 8 - state # 9 - country # 10 - unit_number # 11 - additional_vars # To count total rows of CSV file records = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') total_rows = len(list(records)) BULK_SIZE = 1000 rdr = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') #Get Phonebook Obj phonebook = Phonebook.objects.get(pk=request.POST['phonebook']) contact_cnt = 0 # Read each Row for row in rdr: row = striplist(row) if not row or str(row[0]) == 0: continue # check field type if not int(row[5]): error_msg = _("invalid value for import! please check the import samples or phonebook is not valid") type_error_import_list.append(row) break if len(row[9]) > 2: error_msg = _("invalid value for country code, it needs to be a valid ISO 3166-1 alpha-2 codes (http://en.wikipedia.org/wiki/ISO_3166-1)") type_error_import_list.append(row) break row_11 = '' if row[11]: row_11 = json.loads(row[11]) bulk_record.append( Contact( phonebook=phonebook, contact=row[0], last_name=row[1], first_name=row[2], email=row[3], description=row[4], status=int(row[5]), address=row[6], city=row[7], state=row[8], country=row[9], unit_number=row[10], additional_vars=row_11) ) contact_cnt = contact_cnt + 1 if contact_cnt < 100: success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: # Bulk insert Contact.objects.bulk_create(bulk_record) bulk_record = [] # remaining record Contact.objects.bulk_create(bulk_record) bulk_record = [] #check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s contact(s) have been uploaded successfully out of %(total_rows)s row(s)!')\ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} ctx = RequestContext(request, { 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('dialer contact'), 'title': _('import contact'), 'rdr': rdr, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, }) return render_to_response('admin/dialer_contact/contact/import_contact.html', context_instance=ctx)
def contact_import(request): """Import CSV file of Contacts for the logged in user **Attributes**: * ``form`` - Contact_fileImport * ``template`` - dialer_contact/contact/import_contact.html **Logic Description**: * Before adding contacts, check dialer setting limit if applicable to the user. * Add new contacts which will belong to the logged in user via csv file & get the result (upload success and failure statistics) **Important variable**: * total_rows - Total no. of records in the CSV file * retail_record_count - No. of records imported from the CSV file """ # Check dialer setting limit if request.user and request.method == 'POST': # check Max Number of contacts if check_dialer_setting(request, check_for="contact"): request.session['msg'] = _("you have too many contacts. you are allowed a maximum of %(limit)s") % \ {'limit': dialer_setting_limit(request, limit_for="contact")} # contact limit reached frontend_send_notification(request, NOTIFICATION_NAME.contact_limit_reached) return HttpResponseRedirect(redirect_url_to_contact_list) form = Contact_fileImport(request.user, request.POST or None, request.FILES or None) csv_data = '' msg = '' error_msg = '' success_import_list = [] type_error_import_list = [] contact_cnt = 0 bulk_record = [] if form.is_valid(): # col_no - field name # 0 - contact # 1 - last_name # 2 - first_name # 3 - email # 4 - description # 5 - status # 6 - address # 7 - city # 8 - country # 9 - country # 10 - unit_number # 11 - additional_vars # To count total rows of CSV file records = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') total_rows = len(list(records)) BULK_SIZE = 1000 csv_data = csv.reader(request.FILES['csv_file'], delimiter='|', quotechar='"') #Get Phonebook Obj phonebook = get_object_or_404(Phonebook, pk=request.POST['phonebook'], user=request.user) #Read each Row for row in csv_data: row = striplist(row) if not row or str(row[0]) == 0: continue #Check field type if not int(row[5]): error_msg = _( "invalid value for import! please check the import samples or phonebook is not valid" ) type_error_import_list.append(row) break if len(row[9]) > 2: error_msg = _( "invalid value for country code, it needs to be a valid ISO 3166-1 alpha-2 codes" ) type_error_import_list.append(row) break row_11 = '' if row[11]: try: row_11 = json.loads(row[11]) except: row_11 = '' bulk_record.append( Contact( phonebook=phonebook, contact=row[0], last_name=row[1], first_name=row[2], email=row[3], description=row[4], status=int(row[5]), address=row[6], city=row[7], state=row[8], country=row[ 9], # Note: country needs to be a country code (CA, ES) unit_number=row[10], additional_vars=row_11)) contact_cnt = contact_cnt + 1 if contact_cnt < 100: #We want to display only 100 lines of the success import success_import_list.append(row) if contact_cnt % BULK_SIZE == 0: #Bulk insert Contact.objects.bulk_create(bulk_record) bulk_record = [] # remaining record Contact.objects.bulk_create(bulk_record) bulk_record = [] #check if there is contact imported if contact_cnt > 0: msg = _('%(contact_cnt)s contact(s) have been uploaded successfully out of %(total_rows)s row(s)!') \ % {'contact_cnt': contact_cnt, 'total_rows': total_rows} data = RequestContext( request, { 'form': form, 'csv_data': csv_data, 'msg': msg, 'error_msg': error_msg, 'success_import_list': success_import_list, 'type_error_import_list': type_error_import_list, }) return render_to_response('dialer_contact/contact/import_contact.html', data, context_instance=RequestContext(request))
def import_survey(request): """Importing sections and branching of survey **Attributes**: * ``template`` - survey/import_survey.html * ``form`` - SurveyFileImport """ form = SurveyFileImport(request.POST or None, request.FILES or None) section_row = [] branching_row = [] type_error_import_list = [] if request.method == 'POST': if form.is_valid(): new_survey = Survey_template.objects.create(name=request.POST['name'], user=request.user) records = csv.reader(request.FILES['survey_file'], delimiter='|', quotechar='"') new_old_section = {} # disconnect post_save_add_script signal from Section_template post_save.disconnect(post_save_add_script, sender=Section_template) # Read each row for row in records: row = striplist(row) if not row or str(row[0]) == 0: continue # if length of row is 30, it's a section if len(row) == 30: try: # for section section_template_obj = Section_template.objects.create( order=int(row[0]), type=int(row[1]) if row[1] else 1, question=row[2], script=row[3], audiofile_id=int(row[4]) if row[4] else None, retries=int(row[5]) if row[5] else 0, timeout=int(row[6]) if row[6] else 0, key_0=row[7] if row[7] else '', key_1=row[8] if row[8] else '', key_2=row[9] if row[9] else '', key_3=row[10] if row[10] else '', key_4=row[11] if row[11] else '', key_5=row[12] if row[12] else '', key_6=row[13] if row[13] else '', key_7=row[14] if row[14] else '', key_8=row[15] if row[15] else '', key_9=row[16] if row[16] else '', rating_laps=int(row[17]) if row[17] else None, validate_number=row[18] if row[18] == 'True' else False, number_digits=int(row[19]) if row[19] else None, min_number=row[20] if row[20] else None, max_number=row[21] if row[21] else None, phonenumber=row[22] if row[22] else None, confirm_script=row[23] if row[23] else None, confirm_key=row[24] if row[24] else None, conference=row[25] if row[25] else None, sms_text=row[26] if row[26] else None, completed=True if row[27] == 'True' else False, invalid_audiofile_id=int(row[28]) if row[28] else None, survey=new_survey, ) new_old_section[int(row[29])] = section_template_obj.id section_row.append(row) except: type_error_import_list.append(row) # if length of row is 3, it's a branching if len(row) == 3: new_section_id = '' new_goto_section_id = '' if row[1]: new_section_id = new_old_section[int(row[1])] if row[2]: new_goto_section_id = new_old_section[int(row[2])] duplicate_count = Branching_template.objects.filter( keys=row[0], section_id=new_section_id).count() if duplicate_count == 0: try: Branching_template.objects.create( keys=row[0], section_id=new_section_id, goto_id=int(new_goto_section_id) if new_goto_section_id else None, ) except: type_error_import_list.append(row) # connect post_save_add_script signal with Section_template post_save.connect(post_save_add_script, sender=Section_template) return HttpResponseRedirect(redirect_url_to_survey_list) else: request.session["err_msg"] = True data = { 'form': form, 'section_row': section_row, 'branching_row': branching_row, 'type_error_import_list': type_error_import_list, 'err_msg': request.session.get('err_msg'), } request.session['err_msg'] = '' return render_to_response('survey/import_survey.html', data, context_instance=RequestContext(request))