Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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 per campaign. 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 = []
        if request.method == 'POST':
            form = Contact_fileImport(
                request.user, request.POST, request.FILES)
            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

                    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) are uploaded successfully out of %(total_rows)s row(s) !!')\
                        % {'contact_cnt': contact_cnt, 'total_rows': total_rows}
        else:
            form = Contact_fileImport(request.user)

        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)
Ejemplo n.º 4
0
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))