Beispiel #1
0
def dnc_contact_import(request):
    """Import CSV file of DNC Contacts for the logged in user

    **Attributes**:

        * ``form`` - DNCContact_fileImport
        * ``template`` - frontend/dnc/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)
    csv_data = ''
    msg = ''
    error_msg = ''
    success_import_list = []
    type_error_import_list = []
    contact_cnt = 0
    dup_contact_cnt = 0
    bulk_record = []
    if request.method == 'POST':
        form = DNCContact_fileImport(request.user, request.POST, request.FILES)
        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 were 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) are 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,
            'module': current_view(request),
            'dialer_setting_msg': user_dialer_setting_msg(request.user),
        })
    template = 'frontend/dnc_contact/import_dnc_contact.html'
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
Beispiel #2
0
def dnc_contact_import(request):
    """Import CSV file of DNC Contacts for the logged in user

    **Attributes**:

        * ``form`` - DNCContact_fileImport
        * ``template`` - frontend/dnc/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)
    csv_data = ''
    msg = ''
    error_msg = ''
    success_import_list = []
    type_error_import_list = []
    contact_cnt = 0
    dup_contact_cnt = 0
    bulk_record = []
    if request.method == 'POST':
        form = DNCContact_fileImport(request.user, request.POST, request.FILES)
        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 were 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) are 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,
        'module': current_view(request),
        'dialer_setting_msg': user_dialer_setting_msg(request.user),
    })
    template = 'frontend/dnc_contact/import_dnc_contact.html'
    return render_to_response(template, data,
                              context_instance=RequestContext(request))
Beispiel #3
0
def import_survey(request):
    """Importing sections and branching of survey

    **Attributes**:

        * ``template`` - frontend/survey/import_survey.html
        * ``form`` - SurveyFileImport
    """
    form = SurveyFileImport()
    section_row = []
    branching_row = []
    type_error_import_list = []
    if request.method == 'POST':
        form = SurveyFileImport(request.POST, request.FILES)
        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 27, it's a section
                if len(row) == 27:
                    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 None,
                            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],
                            number_digits=int(row[19]) if row[19] else None,
                            min_number=row[20],
                            max_number=row[21],
                            phonenumber=row[22],
                            conference=row[23],
                            completed=True if row[24] == 'True' else False,
                            invalid_audiofile_id=int(row[25])
                            if row[25] else None,
                            survey=new_survey,
                        )

                        new_old_section[int(row[26])] = 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('/survey/')
        else:
            request.session["err_msg"] = True

    template = 'frontend/survey/import_survey.html'
    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(template,
                              data,
                              context_instance=RequestContext(request))
Beispiel #4
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)
Beispiel #5
0
def contact_import(request):
    """Import CSV file of Contacts for the logged in user

    **Attributes**:

        * ``form`` - Contact_fileImport
        * ``template`` - frontend/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 subscribers per campaign
        if check_dialer_setting(request, check_for="contact"):
            request.session['msg'] = \
                _("you have too many contacts per campaign. 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("/contact/")

    form = Contact_fileImport(request.user)
    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     - 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

                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],
                        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}

    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,
                          'module': current_view(request),
                          'dialer_setting_msg': user_dialer_setting_msg(request.user),
                          })
    template = 'frontend/contact/import_contact.html'
    return render_to_response(template, data,
                              context_instance=RequestContext(request))
Beispiel #6
0
def import_survey(request):
    """Importing sections and branching of survey

    **Attributes**:

        * ``template`` - frontend/survey/import_survey.html
        * ``form`` - SurveyFileImport
    """
    form = SurveyFileImport()
    section_row = []
    branching_row = []
    type_error_import_list = []
    if request.method == 'POST':
        form = SurveyFileImport(request.POST, request.FILES)
        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 27, it's a section
                if  len(row) == 27:
                    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 None,
                            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],
                            number_digits=int(row[19]) if row[19] else None,
                            min_number=row[20],
                            max_number=row[21],
                            phonenumber=row[22],
                            conference=row[23],
                            completed=True if row[24] == 'True' else False,
                            invalid_audiofile_id=int(row[25]) if row[25] else None,
                            survey=new_survey,
                        )

                        new_old_section[int(row[26])] = 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('/survey/')
        else:
            request.session["err_msg"] = True

    template = 'frontend/survey/import_survey.html'
    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(template, data,
        context_instance=RequestContext(request))
Beispiel #7
0
def contact_import(request):
    """Import CSV file of Contacts for the logged in user

    **Attributes**:

        * ``form`` - Contact_fileImport
        * ``template`` - frontend/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("/contact/")

    form = Contact_fileImport(request.user)
    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     - 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 (http://en.wikipedia.org/wiki/ISO_3166-1)"
                    )
                    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,
            "dialer_setting_msg": user_dialer_setting_msg(request.user),
        },
    )
    template = "frontend/contact/import_contact.html"
    return render_to_response(template, data, context_instance=RequestContext(request))
Beispiel #8
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_cdr.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        rdr = ''  # will contain CSV data
        msg = ''
        success_import_list = []
        error_import_list = []
        type_error_import_list = []

        if request.method == 'POST':
            form = CDR_FileImport(request.user, request.POST, request.FILES)
            if form.is_valid():
                field_list = {}
                field_notin_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        field_list[i] = int(request.POST[i])
                    else:
                        field_notin_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [(k, v) for k, v in field_list.iteritems() if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(field_notin_list):

                    # To count total rows of CSV file
                    records = csv.reader(
                        request.FILES['csv_file'], delimiter=',', quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(
                        request.FILES['csv_file'], delimiter=',', quotechar='"')
                    cdr_record_count = 0

                    #Store cdr in list to insert by bulk
                    cdr_bulk_record = []
                    local_count_import = 0
                    PAGE_SIZE = 1000

                    # Read each Row
                    for row in rdr:
                        if (row and str(row[0]) > 0):
                            row = striplist(row)
                            try:
                                accountcode = ''
                                # extra fields to import
                                caller_id_name = ''
                                direction = 'outbound'
                                remote_media_ip = ''
                                answer_uepoch = ''
                                end_uepoch = ''
                                mduration = ''
                                billmsec = ''
                                write_codec = ''
                                read_codec = ''
                                get_cdr_from_row = {}
                                row_counter = 0

                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1] - 1]
                                    #get_cdr_from_row[j[0]] = row[row_counter]
                                    caller_id_name = get_value_from_uni(j, row, 'caller_id_name')
                                    caller_id_number = get_value_from_uni(j, row, 'caller_id_number')
                                    direction = get_value_from_uni(j, row, 'direction')
                                    remote_media_ip = get_value_from_uni(j, row, 'remote_media_ip')
                                    answer_uepoch = get_value_from_uni(j, row, 'answer_uepoch')
                                    end_uepoch = get_value_from_uni(j, row, 'end_uepoch')
                                    mduration = get_value_from_uni(j, row, 'mduration')
                                    billmsec = get_value_from_uni(j, row, 'billmsec')
                                    read_codec = get_value_from_uni(j, row, 'read_codec')
                                    write_codec = get_value_from_uni(j, row, 'write_codec')
                                    row_counter = row_counter + 1

                                if len(field_notin_list) != 0:
                                    for i in field_notin_list:
                                        if i == 'accountcode' and request.POST.get("accountcode_csv"):
                                            accountcode = request.POST["accountcode_csv"]

                                if not accountcode and request.POST.get("accountcode") != '0':
                                    accountcode = get_cdr_from_row['accountcode']

                                # Mandatory fields to import
                                switch_id = int(request.POST['switch'])
                                caller_id_number = get_cdr_from_row['caller_id_number']
                                duration = int(get_cdr_from_row['duration'])
                                billsec = int(get_cdr_from_row['billsec'])

                                if request.POST.get('import_asterisk') \
                                    and request.POST['import_asterisk'] == 'on':
                                    hangup_cause_name = "_".join(get_cdr_from_row['hangup_cause_id'].upper().split(' '))
                                    hangup_cause_id =\
                                        get_hangupcause_id_from_name(hangup_cause_name)
                                else:
                                    hangup_cause_id =\
                                        get_hangupcause_id(int(get_cdr_from_row['hangup_cause_id']))

                                start_uepoch = \
                                    datetime.datetime.fromtimestamp(int(float(get_cdr_from_row['start_uepoch'])))

                                destination_number = get_cdr_from_row['destination_number']
                                uuid = get_cdr_from_row['uuid']

                                destination_data = chk_destination(destination_number)
                                authorized = destination_data['authorized']
                                country_id = destination_data['country_id']

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = \
                                        datetime.datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = \
                                        datetime.datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = generate_global_cdr_record(switch_id, caller_id_number,
                                    caller_id_name, destination_number, duration, billsec, hangup_cause_id,
                                    accountcode, direction, uuid, remote_media_ip, start_uepoch, answer_uepoch,
                                    end_uepoch, mduration, billmsec, read_codec, write_codec,
                                    'CSV_IMPORT', '', country_id, authorized)

                                # check if cdr is already existing in cdr_common
                                cdr_data = settings.DBCON[settings.MONGO_CDRSTATS['CDR_COMMON']]
                                query_var = {}
                                query_var['uuid'] = uuid
                                record_count = cdr_data.find(query_var).count()

                                if record_count >= 1:
                                    msg = _('CDR already exists !!')
                                    error_import_list.append(row)
                                else:
                                    # if not, insert record
                                    # record global CDR

                                    # Append cdr to bulk_cdr list
                                    cdr_bulk_record.append(cdr_record)

                                    local_count_import = local_count_import + 1
                                    if local_count_import == PAGE_SIZE:
                                        CDR_COMMON.insert(cdr_bulk_record)
                                        local_count_import = 0
                                        cdr_bulk_record = []

                                    date_start_uepoch = get_cdr_from_row['start_uepoch']
                                    common_function_to_create_analytic(date_start_uepoch, start_uepoch,
                                        switch_id, country_id, accountcode, hangup_cause_id, duration)

                                    cdr_record_count = cdr_record_count + 1

                                    msg =\
                                        _('%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!')\
                                            % {'cdr_record_count': cdr_record_count,
                                               'total_rows': total_rows}
                                    success_import_list.append(row)
                            except:
                                msg = _("Error : invalid value for import")
                                type_error_import_list.append(row)

                    # remaining record
                    if cdr_bulk_record:
                        CDR_COMMON.insert(cdr_bulk_record)
                        local_count_import = 0
                        cdr_bulk_record = []

                    if cdr_record_count > 0:
                        # Apply index
                        apply_index(shell=True)
                else:
                    msg = _("Error : importing several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(request, {
            'title': _('Import CDR'),
            'form': form,
            'opts': opts,
            'model_name': opts.object_name.lower(),
            'app_label': app_label,
            'rdr': rdr,
            'msg': msg,
            'success_import_list': success_import_list,
            'error_import_list': error_import_list,
            'type_error_import_list': type_error_import_list,
            'CDR_FIELD_LIST': list(CDR_FIELD_LIST),
            'CDR_FIELD_LIST_NUM': list(CDR_FIELD_LIST_NUM),
        })
        template = 'admin/cdr/switch/import_cdr.html'
        return render_to_response(template, context_instance=ctx)
Beispiel #9
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_contact.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        rdr = ''  # will contain CSV data
        msg = ''
        success_import_list = []
        error_import_list = []
        type_error_import_list = []

        #TODO : Too many indentation in the code, refact, less if, for

        #TODO : respect DRY principale, some of the code is duplicate
        #from import tasks

        if request.method == 'POST':
            form = CDR_FileImport(request.user, request.POST, request.FILES)

            if form.is_valid():

                field_list = {}
                field_notin_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        field_list[i] = int(request.POST[i])
                    else:
                        field_notin_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [(k, v) for k, v in field_list.iteritems() \
                            if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(field_notin_list):

                    # To count total rows of CSV file
                    records = csv.reader(request.FILES['csv_file'],
                                         delimiter=',',
                                         quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(request.FILES['csv_file'],
                                     delimiter=',',
                                     quotechar='"')
                    cdr_record_count = 0

                    # Read each Row
                    for row in rdr:
                        if (row and str(row[0]) > 0):
                            row = striplist(row)
                            try:
                                accountcode = ''
                                # extra fields to import
                                caller_id_name = ''
                                direction = 'outbound'
                                remote_media_ip = ''
                                answer_uepoch = ''
                                end_uepoch = ''
                                mduration = ''
                                billmsec = ''
                                write_codec = ''
                                read_codec = ''
                                get_cdr_from_row = {}
                                row_counter = 0
                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1] - 1]
                                    #get_cdr_from_row[j[0]] = row[row_counter]
                                    caller_id_name = \
                                        get_value_from_uni(j, row, 'caller_id_name')
                                    caller_id_number = \
                                        get_value_from_uni(j, row, 'caller_id_number')
                                    direction = \
                                        get_value_from_uni(j, row, 'direction')
                                    remote_media_ip = \
                                        get_value_from_uni(j, row, 'remote_media_ip')
                                    answer_uepoch = \
                                        get_value_from_uni(j, row, 'answer_uepoch')
                                    end_uepoch = \
                                        get_value_from_uni(j, row, 'end_uepoch')
                                    mduration = \
                                        get_value_from_uni(j, row, 'mduration')
                                    billmsec = \
                                        get_value_from_uni(j, row, 'billmsec')
                                    read_codec = \
                                        get_value_from_uni(j, row, 'read_codec')
                                    write_codec = \
                                        get_value_from_uni(j, row, 'write_codec')

                                    row_counter = row_counter + 1

                                if len(field_notin_list) != 0:
                                    for i in field_notin_list:
                                        if i == 'accountcode':
                                            accountcode = int(
                                                request.POST[i + "_csv"])

                                if not accountcode:
                                    accountcode = int(
                                        get_cdr_from_row['accountcode'])

                                # Mandatory fields to import
                                switch_id = int(request.POST['switch'])
                                caller_id_number = get_cdr_from_row[
                                    'caller_id_number']
                                duration = int(get_cdr_from_row['duration'])
                                billsec = int(get_cdr_from_row['billsec'])
                                hangup_cause_id = \
                                    get_hangupcause_id(int(get_cdr_from_row['hangup_cause_id']))
                                start_uepoch = \
                                    datetime.datetime.fromtimestamp(int(get_cdr_from_row['start_uepoch']))
                                destination_number = get_cdr_from_row[
                                    'destination_number']
                                uuid = get_cdr_from_row['uuid']

                                destination_data = chk_destination(
                                    destination_number)
                                authorized = destination_data['authorized']
                                country_id = destination_data['country_id']

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = \
                                        datetime.datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = \
                                        datetime.datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = {
                                    'switch_id': int(request.POST['switch']),
                                    'caller_id_number': caller_id_number,
                                    'caller_id_name': caller_id_name,
                                    'destination_number': destination_number,
                                    'duration': duration,
                                    'billsec': billsec,
                                    'hangup_cause_id': hangup_cause_id,
                                    'accountcode': accountcode,
                                    'direction': direction,
                                    'uuid': uuid,
                                    'remote_media_ip': remote_media_ip,
                                    'start_uepoch': start_uepoch,
                                    'answer_uepoch': answer_uepoch,
                                    'end_uepoch': end_uepoch,
                                    'mduration': mduration,
                                    'billmsec': billmsec,
                                    'read_codec': read_codec,
                                    'write_codec': write_codec,
                                    'cdr_type': 'CSV_IMPORT',
                                    'cdr_object_id': '',
                                    'country_id': country_id,
                                    'authorized': authorized,
                                }

                                try:
                                    # check if cdr is already existing in cdr_common
                                    cdr_data = settings.DBCON[
                                        settings.MG_CDR_COMMON]
                                    query_var = {}
                                    query_var['uuid'] = uuid
                                    record_count = cdr_data.find(
                                        query_var).count()
                                    if record_count >= 1:
                                        msg = _('CDR already exists !!')
                                        error_import_list.append(row)
                                    else:
                                        # if not, insert record
                                        # record global CDR
                                        CDR_COMMON.insert(cdr_record)

                                        # start_uepoch = get_cdr_from_row['start_uepoch']
                                        daily_date = datetime.datetime.\
                                            fromtimestamp(int(get_cdr_from_row['start_uepoch'][:10]))

                                        # insert daily analytic record
                                        create_daily_analytic(
                                            daily_date, switch.id, country_id,
                                            accountcode, hangup_cause_id,
                                            duration)

                                        # MONTHLY_ANALYTIC
                                        # insert monthly analytic record
                                        create_monthly_analytic(
                                            daily_date, start_uepoch,
                                            switch.id, country_id, accountcode,
                                            duration)

                                        cdr_record_count = cdr_record_count + 1

                                        msg =\
                                        _('%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!')\
                                        % {'cdr_record_count': cdr_record_count,
                                           'total_rows': total_rows}
                                        success_import_list.append(row)
                                except:
                                    msg = _("Error : invalid value for import")
                                    type_error_import_list.append(row)

                            except:
                                msg = _("Error : invalid value for import")
                                type_error_import_list.append(row)

                    if cdr_record_count > 0:
                        apply_index()
                        # Apply index
                        DAILY_ANALYTIC.ensure_index([("metadata.date", -1)])
                        CDR_COMMON.ensure_index([("start_uepoch", -1)])
                else:
                    msg = _("Error : importing several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(
            request, {
                'title': _('Import CDR'),
                'form': form,
                'opts': opts,
                'model_name': opts.object_name.lower(),
                'app_label': app_label,
                'rdr': rdr,
                'msg': msg,
                'success_import_list': success_import_list,
                'error_import_list': error_import_list,
                'type_error_import_list': type_error_import_list,
                'CDR_FIELD_LIST': list(CDR_FIELD_LIST),
                'CDR_FIELD_LIST_NUM': list(CDR_FIELD_LIST_NUM),
            })
        template = 'admin/cdr/switch/import_cdr.html'
        return render_to_response(template, context_instance=ctx)
Beispiel #10
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)
Beispiel #11
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
                common_send_notification(request, "3")
                return HttpResponseRedirect(reverse("admin:dialer_campaign_contact_changelist"))

        opts = Contact._meta
        rdr = ""  # will contain CSV data
        msg = ""
        success_import_list = []
        error_import_list = []
        type_error_import_list = []
        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     - additional_vars
                # To count total rows of CSV file
                records = csv.reader(request.FILES["csv_file"], delimiter=",", quotechar='"')
                total_rows = len(list(records))

                rdr = csv.reader(request.FILES["csv_file"], delimiter=",", quotechar='"')
                contact_record_count = 0
                # Read each Row
                for row in rdr:
                    if row and str(row[0]) > 0:
                        row = striplist(row)
                        try:
                            # check field type
                            int(row[5])

                            phonebook = Phonebook.objects.get(pk=request.POST["phonebook"])
                            try:
                                # check if prefix is already
                                # existing in the retail plan or not
                                contact = Contact.objects.get(phonebook_id=phonebook.id, contact=row[0])
                                msg = _("Contact already exists !!")
                                error_import_list.append(row)
                            except:
                                # if not, insert record
                                Contact.objects.create(
                                    phonebook=phonebook,
                                    contact=row[0],
                                    last_name=row[1],
                                    first_name=row[2],
                                    email=row[3],
                                    description=row[4],
                                    status=int(row[5]),
                                    additional_vars=row[6],
                                )
                                contact_record_count = contact_record_count + 1
                                msg = _(
                                    "%(contact_record_count)s Contact(s) are uploaded, out of %(total_rows)s row(s) !!"
                                ) % {"contact_record_count": contact_record_count, "total_rows": total_rows}
                                success_import_list.append(row)
                        except:
                            msg = _("Error : invalid value for import! Check import samples.")
                            type_error_import_list.append(row)
        else:
            form = Contact_fileImport(request.user)

        ctx = RequestContext(
            request,
            {
                "title": _("Import Contact"),
                "form": form,
                "opts": opts,
                "model_name": opts.object_name.lower(),
                "app_label": _("Dialer_campaign"),
                "rdr": rdr,
                "msg": msg,
                "success_import_list": success_import_list,
                "error_import_list": error_import_list,
                "type_error_import_list": type_error_import_list,
            },
        )
        return render_to_response("admin/dialer_campaign/contact/import_contact.html", context_instance=ctx)
Beispiel #12
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_cdr.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        rdr = ""  # will contain CSV data
        msg = ""
        success_import_list = []
        error_import_list = []
        type_error_import_list = []

        # TODO : Too many indentation in the code, refact, less if, for
        # TODO : respect DRY principale, some of the code is duplicate
        if request.method == "POST":
            form = CDR_FileImport(request.user, request.POST, request.FILES)
            if form.is_valid():
                field_list = {}
                field_notin_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        field_list[i] = int(request.POST[i])
                    else:
                        field_notin_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [(k, v) for k, v in field_list.iteritems() if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(field_notin_list):

                    # To count total rows of CSV file
                    records = csv.reader(request.FILES["csv_file"], delimiter=",", quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(request.FILES["csv_file"], delimiter=",", quotechar='"')
                    cdr_record_count = 0

                    # Read each Row
                    for row in rdr:
                        if row and str(row[0]) > 0:
                            row = striplist(row)
                            try:
                                accountcode = ""
                                # extra fields to import
                                caller_id_name = ""
                                direction = "outbound"
                                remote_media_ip = ""
                                answer_uepoch = ""
                                end_uepoch = ""
                                mduration = ""
                                billmsec = ""
                                write_codec = ""
                                read_codec = ""
                                get_cdr_from_row = {}
                                row_counter = 0
                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1] - 1]
                                    # get_cdr_from_row[j[0]] = row[row_counter]
                                    caller_id_name = get_value_from_uni(j, row, "caller_id_name")
                                    caller_id_number = get_value_from_uni(j, row, "caller_id_number")
                                    direction = get_value_from_uni(j, row, "direction")
                                    remote_media_ip = get_value_from_uni(j, row, "remote_media_ip")
                                    answer_uepoch = get_value_from_uni(j, row, "answer_uepoch")
                                    end_uepoch = get_value_from_uni(j, row, "end_uepoch")
                                    mduration = get_value_from_uni(j, row, "mduration")
                                    billmsec = get_value_from_uni(j, row, "billmsec")
                                    read_codec = get_value_from_uni(j, row, "read_codec")
                                    write_codec = get_value_from_uni(j, row, "write_codec")
                                    row_counter = row_counter + 1

                                if len(field_notin_list) != 0:
                                    for i in field_notin_list:
                                        if i == "accountcode":
                                            accountcode = request.POST[i + "_csv"]

                                if not accountcode:
                                    accountcode = get_cdr_from_row["accountcode"]

                                # Mandatory fields to import
                                switch_id = int(request.POST["switch"])
                                caller_id_number = get_cdr_from_row["caller_id_number"]
                                duration = int(get_cdr_from_row["duration"])
                                billsec = int(get_cdr_from_row["billsec"])
                                hangup_cause_id = get_hangupcause_id(int(get_cdr_from_row["hangup_cause_id"]))
                                start_uepoch = datetime.datetime.fromtimestamp(int(get_cdr_from_row["start_uepoch"]))
                                destination_number = get_cdr_from_row["destination_number"]
                                uuid = get_cdr_from_row["uuid"]

                                destination_data = chk_destination(destination_number)
                                authorized = destination_data["authorized"]
                                country_id = destination_data["country_id"]

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = datetime.datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = datetime.datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = {
                                    "switch_id": int(request.POST["switch"]),
                                    "caller_id_number": caller_id_number,
                                    "caller_id_name": caller_id_name,
                                    "destination_number": destination_number,
                                    "duration": duration,
                                    "billsec": billsec,
                                    "hangup_cause_id": hangup_cause_id,
                                    "accountcode": accountcode,
                                    "direction": direction,
                                    "uuid": uuid,
                                    "remote_media_ip": remote_media_ip,
                                    "start_uepoch": start_uepoch,
                                    "answer_uepoch": answer_uepoch,
                                    "end_uepoch": end_uepoch,
                                    "mduration": mduration,
                                    "billmsec": billmsec,
                                    "read_codec": read_codec,
                                    "write_codec": write_codec,
                                    "cdr_type": "CSV_IMPORT",
                                    "cdr_object_id": "",
                                    "country_id": country_id,
                                    "authorized": authorized,
                                }

                                try:
                                    # check if cdr is already existing in cdr_common
                                    cdr_data = settings.DBCON[settings.MONGO_CDRSTATS["CDR_COMMON"]]
                                    query_var = {}
                                    query_var["uuid"] = uuid
                                    record_count = cdr_data.find(query_var).count()
                                    if record_count >= 1:
                                        msg = _("CDR already exists !!")
                                        error_import_list.append(row)
                                    else:
                                        # if not, insert record
                                        # record global CDR
                                        CDR_COMMON.insert(cdr_record)

                                        # start_uepoch = get_cdr_from_row['start_uepoch']
                                        daily_date = datetime.datetime.fromtimestamp(
                                            int(get_cdr_from_row["start_uepoch"][:10])
                                        )

                                        # insert daily analytic record
                                        create_daily_analytic(
                                            daily_date, switch_id, country_id, accountcode, hangup_cause_id, duration
                                        )

                                        # MONTHLY_ANALYTIC
                                        # insert monthly analytic record
                                        create_monthly_analytic(
                                            daily_date, start_uepoch, switch_id, country_id, accountcode, duration
                                        )

                                        cdr_record_count = cdr_record_count + 1

                                        msg = _(
                                            "%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!"
                                        ) % {"cdr_record_count": cdr_record_count, "total_rows": total_rows}
                                        success_import_list.append(row)
                                except:
                                    msg = _("Error : invalid value for import")
                                    type_error_import_list.append(row)

                            except:
                                msg = _("Error : invalid value for import")
                                type_error_import_list.append(row)

                    if cdr_record_count > 0:
                        apply_index()
                        # Apply index
                        DAILY_ANALYTIC.ensure_index([("metadata.date", -1)])
                        CDR_COMMON.ensure_index([("start_uepoch", -1)])
                else:
                    msg = _("Error : importing several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(
            request,
            {
                "title": _("Import CDR"),
                "form": form,
                "opts": opts,
                "model_name": opts.object_name.lower(),
                "app_label": app_label,
                "rdr": rdr,
                "msg": msg,
                "success_import_list": success_import_list,
                "error_import_list": error_import_list,
                "type_error_import_list": type_error_import_list,
                "CDR_FIELD_LIST": list(CDR_FIELD_LIST),
                "CDR_FIELD_LIST_NUM": list(CDR_FIELD_LIST_NUM),
            },
        )
        template = "admin/cdr/switch/import_cdr.html"
        return render_to_response(template, context_instance=ctx)
Beispiel #13
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_contact.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        file_exts = ('.csv', )
        rdr = ''  # will contain CSV data
        msg = ''
        success_import_list = []
        error_import_list = []
        type_error_import_list = []
        if request.method == 'POST':
            form = CDR_FileImport(request.user, request.POST, request.FILES)

            if form.is_valid():

                cdr_field_list = {}
                cdr_field_not_in_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        cdr_field_list[i] = int(request.POST[i])
                    else:
                        cdr_field_not_in_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in cdr_field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [ (k, v) for k, v in cdr_field_list.iteritems() if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(cdr_field_not_in_list):

                    # To count total rows of CSV file
                    records = csv.reader(request.FILES['csv_file'],
                                         delimiter=',', quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(request.FILES['csv_file'],
                                     delimiter=',', quotechar='"')
                    cdr_record_count = 0

                    # Read each Row
                    for row in rdr:
                        if (row and str(row[0]) > 0):
                            row = striplist(row)
                            try:
                                accountcode = ''
                                # extra fields to import
                                caller_id_name = ''
                                direction = 'outbound'
                                remote_media_ip = ''
                                answer_uepoch = ''
                                end_uepoch = ''
                                mduration = ''
                                billmsec = ''
                                write_codec = ''
                                read_codec = ''
                                get_cdr_from_row = {}
                                row_counter = 0
                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1]-1]
                                    #get_cdr_from_row[j[0]] = row[row_counter]
                                    if j[0] == 'caller_id_name':
                                        caller_id_name = row[j[1]-1]
                                    if j[0] == 'caller_id_name':
                                        caller_id_name = row[j[1]-1]
                                    if j[0] == 'direction':
                                        direction = row[j[1]-1]
                                    if j[0] == 'remote_media_ip':
                                        remote_media_ip = row[j[1]-1]
                                    if j[0] == 'answer_uepoch':
                                        answer_uepoch = row[j[1]-1]
                                    if j[0] == 'end_uepoch':
                                        end_uepoch = row[j[1]-1]
                                    if j[0] == 'mduration':
                                        mduration = row[j[1]-1]
                                    if j[0] == 'billmsec':
                                        billmsec = row[j[1]-1]
                                    if j[0] == 'read_codec':
                                        read_codec = row[j[1]-1]
                                    if j[0] == 'write_codec':
                                        write_codec = row[j[1]-1]

                                    row_counter = row_counter + 1

                                get_cdr_not_from_row = {}
                                if len(cdr_field_not_in_list) != 0:
                                    for i in cdr_field_not_in_list:
                                        if i == 'accountcode':
                                            accountcode = int(request.POST[i+"_csv"])

                                if not accountcode:
                                    accountcode = int(get_cdr_from_row['accountcode'])


                                # Mandatory fields to import
                                switch_id = int(request.POST['switch'])
                                caller_id_number = get_cdr_from_row['caller_id_number']
                                duration = int(get_cdr_from_row['duration'])
                                billsec = int(get_cdr_from_row['billsec'])
                                hangup_cause_id = get_hangupcause_id(int(get_cdr_from_row['hangup_cause_id']))
                                start_uepoch = datetime.fromtimestamp(int(get_cdr_from_row['start_uepoch']))
                                destination_number = get_cdr_from_row['destination_number']
                                uuid = get_cdr_from_row['uuid']

                                # number startswith 0 or `+` sign
                                #remove leading +
                                sanitized_destination = re.sub("^\++", "", destination_number)
                                #remove leading 011
                                sanitized_destination = re.sub("^011+", "", sanitized_destination)
                                #remove leading 00
                                sanitized_destination = re.sub("^0+", "", sanitized_destination)

                                prefix_list = prefix_list_string(sanitized_destination)

                                authorized = 1 # default
                                #check desti against whiltelist
                                authorized = chk_prefix_in_whitelist(prefix_list)
                                if authorized:
                                    authorized = 1 # allowed destination
                                else:
                                    #check desti against blacklist
                                    authorized = chk_prefix_in_blacklist(prefix_list)
                                    if not authorized:
                                        authorized = 0 # not allowed destination

                                country_id = get_country_id(prefix_list)

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = {
                                    'switch_id': int(request.POST['switch']),
                                    'caller_id_number': caller_id_number,
                                    'caller_id_name': caller_id_name,
                                    'destination_number': destination_number,
                                    'duration': duration,
                                    'billsec': billsec,
                                    'hangup_cause_id': hangup_cause_id,
                                    'accountcode': accountcode,
                                    'direction': direction,
                                    'uuid': uuid,
                                    'remote_media_ip': remote_media_ip,
                                    'start_uepoch': start_uepoch,
                                    'answer_uepoch': answer_uepoch,
                                    'end_uepoch': end_uepoch,
                                    'mduration': mduration,
                                    'billmsec': billmsec,
                                    'read_codec': read_codec,
                                    'write_codec': write_codec,
                                    'cdr_type': 'CSV_IMPORT',
                                    'cdr_object_id': '',
                                    'country_id': country_id,
                                    'authorized': authorized,
                                    }

                                try:
                                    # check if cdr is already existing in cdr_common
                                    cdr_data = settings.DB_CONNECTION[settings.CDR_MONGO_CDR_COMMON]
                                    query_var = {}
                                    query_var['uuid'] = uuid
                                    record_count = cdr_data.find(query_var).count()
                                    if record_count >= 1:
                                        msg = _('CDR already exists !!')
                                        error_import_list.append(row)
                                    else:
                                        # if not, insert record
                                        # record global CDR
                                        CDR_COMMON.insert(cdr_record)

                                        # monthly collection
                                        current_y_m = datetime.strptime(str(start_uepoch)[:7], "%Y-%m")
                                        CDR_MONTHLY.update(
                                                {
                                                'start_uepoch': current_y_m,
                                                'destination_number': destination_number,
                                                'hangup_cause_id': hangup_cause_id,
                                                'accountcode': accountcode,
                                                'switch_id': switch_id,
                                                },
                                                {
                                                '$inc':
                                                        {'calls': 1,
                                                         'duration': duration }
                                            }, upsert=True)

                                        # daily collection
                                        current_y_m_d = datetime.strptime(str(start_uepoch)[:10], "%Y-%m-%d")
                                        CDR_DAILY.update(
                                                {
                                                'start_uepoch': current_y_m_d,
                                                'destination_number': destination_number,
                                                'hangup_cause_id': hangup_cause_id,
                                                'accountcode': accountcode,
                                                'switch_id': switch_id,
                                                },
                                                {
                                                '$inc':
                                                        {'calls': 1,
                                                         'duration': duration }
                                            },upsert=True)

                                        # hourly collection
                                        current_y_m_d_h = datetime.strptime(str(start_uepoch)[:13], "%Y-%m-%d %H")
                                        CDR_HOURLY.update(
                                                {
                                                'start_uepoch': current_y_m_d_h,
                                                'destination_number': destination_number,
                                                'hangup_cause_id': hangup_cause_id,
                                                'accountcode': accountcode,
                                                'switch_id': switch_id,},
                                                {
                                                '$inc': {'calls': 1,
                                                         'duration': duration }
                                            },upsert=True)

                                        # Country report collection
                                        current_y_m_d_h_m = datetime.strptime(str(start_uepoch)[:16], "%Y-%m-%d %H:%M")
                                        CDR_COUNTRY_REPORT.update(
                                                {
                                                'start_uepoch': current_y_m_d_h_m,
                                                'country_id': country_id,
                                                'accountcode': accountcode,
                                                'switch_id': switch_id,},
                                                {
                                                '$inc': {'calls': 1,
                                                         'duration': duration }
                                            },upsert=True)

                                        cdr_record_count = cdr_record_count + 1
                                        msg =\
                                        _('%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!')\
                                        % {'cdr_record_count': cdr_record_count,
                                           'total_rows': total_rows}
                                        success_import_list.append(row)
                                except:
                                    msg = _("Error : invalid value for import! Check import samples.")
                                    type_error_import_list.append(row)

                            except:
                                msg = _("Error : invalid value for import! Check import samples.")
                                type_error_import_list.append(row)
                else:
                    msg = _("Error : You selected to import several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(request, {
            'title': _('Import CDR'),
            'form': form,
            'opts': opts,
            'model_name': opts.object_name.lower(),
            'app_label': _('Switch'),
            'rdr': rdr,
            'msg': msg,
            'success_import_list': success_import_list,
            'error_import_list': error_import_list,
            'type_error_import_list': type_error_import_list,
            'CDR_FIELD_LIST': list(CDR_FIELD_LIST),
            'CDR_FIELD_LIST_NUM': list(CDR_FIELD_LIST_NUM),
            })
        template = 'admin/cdr/switch/import_cdr.html'
        return render_to_response(template, context_instance=ctx)
Beispiel #14
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_contact.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        rdr = ''  # will contain CSV data
        msg = ''
        success_import_list = []
        error_import_list = []
        type_error_import_list = []

        #TODO : Too many indentation in the code, refact, less if, for

        #TODO : respect DRY principale, some of the code is duplicate
        #from import tasks

        if request.method == 'POST':
            form = CDR_FileImport(request.user, request.POST, request.FILES)

            if form.is_valid():

                field_list = {}
                field_notin_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        field_list[i] = int(request.POST[i])
                    else:
                        field_notin_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [(k, v) for k, v in field_list.iteritems() \
                            if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(field_notin_list):

                    # To count total rows of CSV file
                    records = csv.reader(request.FILES['csv_file'],
                                         delimiter=',', quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(request.FILES['csv_file'],
                                     delimiter=',', quotechar='"')
                    cdr_record_count = 0

                    # Read each Row
                    for row in rdr:
                        if (row and str(row[0]) > 0):
                            row = striplist(row)
                            try:
                                accountcode = ''
                                # extra fields to import
                                caller_id_name = ''
                                direction = 'outbound'
                                remote_media_ip = ''
                                answer_uepoch = ''
                                end_uepoch = ''
                                mduration = ''
                                billmsec = ''
                                write_codec = ''
                                read_codec = ''
                                get_cdr_from_row = {}
                                row_counter = 0
                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1] - 1]
                                    #get_cdr_from_row[j[0]] = row[row_counter]
                                    caller_id_name = \
                                        get_value_from_uni(j, row, 'caller_id_name')
                                    caller_id_number = \
                                        get_value_from_uni(j, row, 'caller_id_number')
                                    direction = \
                                        get_value_from_uni(j, row, 'direction')
                                    remote_media_ip = \
                                        get_value_from_uni(j, row, 'remote_media_ip')
                                    answer_uepoch = \
                                        get_value_from_uni(j, row, 'answer_uepoch')
                                    end_uepoch = \
                                        get_value_from_uni(j, row, 'end_uepoch')
                                    mduration = \
                                        get_value_from_uni(j, row, 'mduration')
                                    billmsec = \
                                        get_value_from_uni(j, row, 'billmsec')
                                    read_codec = \
                                        get_value_from_uni(j, row, 'read_codec')
                                    write_codec = \
                                        get_value_from_uni(j, row, 'write_codec')

                                    row_counter = row_counter + 1

                                if len(field_notin_list) != 0:
                                    for i in field_notin_list:
                                        if i == 'accountcode':
                                            accountcode = int(request.POST[i + "_csv"])

                                if not accountcode:
                                    accountcode = int(get_cdr_from_row['accountcode'])


                                # Mandatory fields to import
                                switch_id = int(request.POST['switch'])
                                caller_id_number = get_cdr_from_row['caller_id_number']
                                duration = int(get_cdr_from_row['duration'])
                                billsec = int(get_cdr_from_row['billsec'])
                                hangup_cause_id = \
                                    get_hangupcause_id(int(get_cdr_from_row['hangup_cause_id']))
                                start_uepoch = \
                                    datetime.datetime.fromtimestamp(int(get_cdr_from_row['start_uepoch']))
                                destination_number = get_cdr_from_row['destination_number']
                                uuid = get_cdr_from_row['uuid']

                                destination_data = chk_destination(destination_number)
                                authorized = destination_data['authorized']
                                country_id = destination_data['country_id']

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = \
                                        datetime.datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = \
                                        datetime.datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = {
                                    'switch_id': int(request.POST['switch']),
                                    'caller_id_number': caller_id_number,
                                    'caller_id_name': caller_id_name,
                                    'destination_number': destination_number,
                                    'duration': duration,
                                    'billsec': billsec,
                                    'hangup_cause_id': hangup_cause_id,
                                    'accountcode': accountcode,
                                    'direction': direction,
                                    'uuid': uuid,
                                    'remote_media_ip': remote_media_ip,
                                    'start_uepoch': start_uepoch,
                                    'answer_uepoch': answer_uepoch,
                                    'end_uepoch': end_uepoch,
                                    'mduration': mduration,
                                    'billmsec': billmsec,
                                    'read_codec': read_codec,
                                    'write_codec': write_codec,
                                    'cdr_type': 'CSV_IMPORT',
                                    'cdr_object_id': '',
                                    'country_id': country_id,
                                    'authorized': authorized,
                                    }

                                try:
                                    # check if cdr is already existing in cdr_common
                                    cdr_data = settings.DBCON[settings.MG_CDR_COMMON]
                                    query_var = {}
                                    query_var['uuid'] = uuid
                                    record_count = cdr_data.find(query_var).count()
                                    if record_count >= 1:
                                        msg = _('CDR already exists !!')
                                        error_import_list.append(row)
                                    else:
                                        # if not, insert record
                                        # record global CDR
                                        CDR_COMMON.insert(cdr_record)

                                        # start_uepoch = get_cdr_from_row['start_uepoch']
                                        daily_date = datetime.datetime.\
                                            fromtimestamp(int(get_cdr_from_row['start_uepoch'][:10]))

                                        # insert daily analytic record
                                        create_daily_analytic(daily_date, switch.id, country_id,
                                                              accountcode, hangup_cause_id,
                                                              duration)

                                        # MONTHLY_ANALYTIC
                                        # insert monthly analytic record
                                        create_monthly_analytic(daily_date, start_uepoch, switch.id,
                                                                country_id, accountcode, duration)

                                        cdr_record_count = cdr_record_count + 1

                                        msg =\
                                        _('%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!')\
                                        % {'cdr_record_count': cdr_record_count,
                                           'total_rows': total_rows}
                                        success_import_list.append(row)
                                except:
                                    msg = _("Error : invalid value for import")
                                    type_error_import_list.append(row)

                            except:
                                msg = _("Error : invalid value for import")
                                type_error_import_list.append(row)

                    if cdr_record_count > 0:
                        apply_index()
                        # Apply index
                        DAILY_ANALYTIC.ensure_index([("metadata.date", -1)])
                        CDR_COMMON.ensure_index([("start_uepoch", -1)])
                else:
                    msg = _("Error : importing several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(request, {
            'title': _('Import CDR'),
            'form': form,
            'opts': opts,
            'model_name': opts.object_name.lower(),
            'app_label': app_label,
            'rdr': rdr,
            'msg': msg,
            'success_import_list': success_import_list,
            'error_import_list': error_import_list,
            'type_error_import_list': type_error_import_list,
            'CDR_FIELD_LIST': list(CDR_FIELD_LIST),
            'CDR_FIELD_LIST_NUM': list(CDR_FIELD_LIST_NUM),
            })
        template = 'admin/cdr/switch/import_cdr.html'
        return render_to_response(template, context_instance=ctx)
Beispiel #15
0
def contact_import(request):
    """Import CSV file of Contacts for the logged in user

    **Attributes**:

        * ``form`` - Contact_fileImport
        * ``template`` - frontend/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("/contact/")

    form = Contact_fileImport(request.user)
    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     - 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 (http://en.wikipedia.org/wiki/ISO_3166-1)"
                    )
                    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) are 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,
            'module': current_view(request),
            'dialer_setting_msg': user_dialer_setting_msg(request.user),
        })
    template = 'frontend/contact/import_contact.html'
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
Beispiel #16
0
    def import_cdr(self, request):
        """Add custom method in django admin view to import CSV file of
        cdr

        **Attributes**:

            * ``form`` - CDR_FileImport
            * ``template`` - admin/cdr/switch/import_cdr.html

        **Logic Description**:


        **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
        """
        opts = Switch._meta
        app_label = opts.app_label
        rdr = ''  # will contain CSV data
        msg = ''
        success_import_list = []
        error_import_list = []
        type_error_import_list = []

        if request.method == 'POST':
            form = CDR_FileImport(request.user, request.POST, request.FILES)
            if form.is_valid():
                field_list = {}
                field_notin_list = []
                for i in CDR_FIELD_LIST:
                    if int(request.POST[i]) != 0:
                        field_list[i] = int(request.POST[i])
                    else:
                        field_notin_list.append((i))

                # perform sorting & get unique order list
                countMap = {}
                for v in field_list.itervalues():
                    countMap[v] = countMap.get(v, 0) + 1
                uni = [(k, v) for k, v in field_list.iteritems()
                       if countMap[v] == 1]
                uni = sorted(uni, key=lambda uni: uni[1])

                # if order list matched with CDR_FIELD_LIST count
                if len(uni) == len(CDR_FIELD_LIST) - len(field_notin_list):

                    # To count total rows of CSV file
                    records = csv.reader(request.FILES['csv_file'],
                                         delimiter=',',
                                         quotechar='"')
                    total_rows = len(list(records))

                    rdr = csv.reader(request.FILES['csv_file'],
                                     delimiter=',',
                                     quotechar='"')
                    cdr_record_count = 0

                    #Store cdr in list to insert by bulk
                    cdr_bulk_record = []
                    local_count_import = 0
                    PAGE_SIZE = 1000

                    # Read each Row
                    for row in rdr:
                        if (row and str(row[0]) > 0):
                            row = striplist(row)
                            try:
                                accountcode = ''
                                # extra fields to import
                                caller_id_name = ''
                                direction = 'outbound'
                                remote_media_ip = ''
                                answer_uepoch = ''
                                end_uepoch = ''
                                mduration = ''
                                billmsec = ''
                                write_codec = ''
                                read_codec = ''
                                get_cdr_from_row = {}
                                row_counter = 0

                                for j in uni:
                                    get_cdr_from_row[j[0]] = row[j[1] - 1]
                                    #get_cdr_from_row[j[0]] = row[row_counter]
                                    caller_id_name = get_value_from_uni(
                                        j, row, 'caller_id_name')
                                    caller_id_number = get_value_from_uni(
                                        j, row, 'caller_id_number')
                                    direction = get_value_from_uni(
                                        j, row, 'direction')
                                    remote_media_ip = get_value_from_uni(
                                        j, row, 'remote_media_ip')
                                    answer_uepoch = get_value_from_uni(
                                        j, row, 'answer_uepoch')
                                    end_uepoch = get_value_from_uni(
                                        j, row, 'end_uepoch')
                                    mduration = get_value_from_uni(
                                        j, row, 'mduration')
                                    billmsec = get_value_from_uni(
                                        j, row, 'billmsec')
                                    read_codec = get_value_from_uni(
                                        j, row, 'read_codec')
                                    write_codec = get_value_from_uni(
                                        j, row, 'write_codec')
                                    row_counter = row_counter + 1

                                if len(field_notin_list) != 0:
                                    for i in field_notin_list:
                                        if i == 'accountcode' and request.POST.get(
                                                "accountcode_csv"):
                                            accountcode = request.POST[
                                                "accountcode_csv"]

                                if not accountcode and request.POST.get(
                                        "accountcode") != '0':
                                    accountcode = get_cdr_from_row[
                                        'accountcode']

                                # Mandatory fields to import
                                switch_id = int(request.POST['switch'])
                                caller_id_number = get_cdr_from_row[
                                    'caller_id_number']
                                duration = int(get_cdr_from_row['duration'])
                                billsec = int(get_cdr_from_row['billsec'])

                                if request.POST.get('import_asterisk') \
                                    and request.POST['import_asterisk'] == 'on':
                                    hangup_cause_name = "_".join(
                                        get_cdr_from_row['hangup_cause_id'].
                                        upper().split(' '))
                                    hangup_cause_id =\
                                        get_hangupcause_id_from_name(hangup_cause_name)
                                else:
                                    hangup_cause_id =\
                                        get_hangupcause_id(int(get_cdr_from_row['hangup_cause_id']))

                                start_uepoch = \
                                    datetime.datetime.fromtimestamp(int(float(get_cdr_from_row['start_uepoch'])))

                                destination_number = get_cdr_from_row[
                                    'destination_number']
                                uuid = get_cdr_from_row['uuid']

                                destination_data = chk_destination(
                                    destination_number)
                                authorized = destination_data['authorized']
                                country_id = destination_data['country_id']

                                # Extra fields to import
                                if answer_uepoch:
                                    answer_uepoch = \
                                        datetime.datetime.fromtimestamp(int(answer_uepoch[:10]))
                                if end_uepoch:
                                    end_uepoch = \
                                        datetime.datetime.fromtimestamp(int(end_uepoch[:10]))

                                # Prepare global CDR
                                cdr_record = generate_global_cdr_record(
                                    switch_id, caller_id_number,
                                    caller_id_name, destination_number,
                                    duration, billsec, hangup_cause_id,
                                    accountcode, direction, uuid,
                                    remote_media_ip, start_uepoch,
                                    answer_uepoch, end_uepoch, mduration,
                                    billmsec, read_codec, write_codec,
                                    'CSV_IMPORT', '', country_id, authorized)

                                # check if cdr is already existing in cdr_common
                                cdr_data = settings.DBCON[
                                    settings.MONGO_CDRSTATS['CDR_COMMON']]
                                query_var = {}
                                query_var['uuid'] = uuid
                                record_count = cdr_data.find(query_var).count()

                                if record_count >= 1:
                                    msg = _('CDR already exists !!')
                                    error_import_list.append(row)
                                else:
                                    # if not, insert record
                                    # record global CDR

                                    # Append cdr to bulk_cdr list
                                    cdr_bulk_record.append(cdr_record)

                                    local_count_import = local_count_import + 1
                                    if local_count_import == PAGE_SIZE:
                                        CDR_COMMON.insert(cdr_bulk_record)
                                        local_count_import = 0
                                        cdr_bulk_record = []

                                    date_start_uepoch = get_cdr_from_row[
                                        'start_uepoch']
                                    common_function_to_create_analytic(
                                        date_start_uepoch, start_uepoch,
                                        switch_id, country_id, accountcode,
                                        hangup_cause_id, duration)

                                    cdr_record_count = cdr_record_count + 1

                                    msg =\
                                        _('%(cdr_record_count)s Cdr(s) are uploaded, out of %(total_rows)s row(s) !!')\
                                            % {'cdr_record_count': cdr_record_count,
                                               'total_rows': total_rows}
                                    success_import_list.append(row)
                            except:
                                msg = _("Error : invalid value for import")
                                type_error_import_list.append(row)

                    # remaining record
                    if cdr_bulk_record:
                        CDR_COMMON.insert(cdr_bulk_record)
                        local_count_import = 0
                        cdr_bulk_record = []

                    if cdr_record_count > 0:
                        # Apply index
                        apply_index(shell=True)
                else:
                    msg = _("Error : importing several times the same column")
        else:
            form = CDR_FileImport(request.user)

        ctx = RequestContext(
            request, {
                'title': _('Import CDR'),
                'form': form,
                'opts': opts,
                'model_name': opts.object_name.lower(),
                'app_label': app_label,
                'rdr': rdr,
                'msg': msg,
                'success_import_list': success_import_list,
                'error_import_list': error_import_list,
                'type_error_import_list': type_error_import_list,
                'CDR_FIELD_LIST': list(CDR_FIELD_LIST),
                'CDR_FIELD_LIST_NUM': list(CDR_FIELD_LIST_NUM),
            })
        template = 'admin/cdr/switch/import_cdr.html'
        return render_to_response(template, context_instance=ctx)