def voip_daily_report(self, request): opts = VoIPCall._meta kwargs = {} if request.method == 'POST': form = VoipSearchForm(request.user, request.POST) kwargs = voipcall_record_common_fun(request) else: kwargs = voipcall_record_common_fun(request) tday = datetime.today() form = VoipSearchForm(request.user, initial={ "from_date": tday.strftime("%Y-%m-%d"), "to_date": tday.strftime("%Y-%m-%d") }) if len(kwargs) == 0: kwargs['starting_date__gte'] = datetime( tday.year, tday.month, tday.day, 0, 0, 0, 0) select_data = { "starting_date": "SUBSTR(CAST(starting_date as CHAR(30)),1,10)" } # Get Total Records from VoIPCall Report table for Daily Call Report total_data = VoIPCall.objects.extra(select=select_data)\ .values('starting_date')\ .filter(**kwargs)\ .annotate(Count('starting_date'))\ .annotate(Sum('duration'))\ .annotate(Avg('duration'))\ .order_by('-starting_date') # Following code will count total voip calls, duration if total_data: max_duration = max([x['duration__sum'] for x in total_data]) total_duration = sum([x['duration__sum'] for x in total_data]) total_calls = sum([x['starting_date__count'] for x in total_data]) total_avg_duration = (sum([x['duration__avg'] for x in total_data])) / total_calls else: max_duration = 0 total_duration = 0 total_calls = 0 total_avg_duration = 0 ctx = RequestContext( request, { 'form': form, 'total_data': total_data, 'total_duration': total_duration, 'total_calls': total_calls, 'total_avg_duration': total_avg_duration, 'max_duration': max_duration, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': APP_LABEL, 'title': _('call aggregate report'), }) return render_to_response('admin/dialer_cdr/voipcall/voip_report.html', context_instance=ctx)
def setUp(self): self.user = User.objects.get(username='******') VoipSearchForm(self.user) try: content_type_id = ContentType.objects.get(model='survey').id except: content_type_id = 1 # Callrequest model self.callrequest = Callrequest( call_type=1, status=1, user=self.user, phone_number='123456', subscriber_id=1, campaign_id=1, aleg_gateway_id=1, content_type_id=content_type_id, object_id=1, ) self.callrequest.save() # VoIPCall model self.voipcall = VoIPCall( user=self.user, used_gateway_id=1, callrequest=self.callrequest, callid='Top Gun', phone_number='123456', leg_type=1, duration=20, ) self.voipcall.save() self.assertEqual(self.voipcall.__unicode__(), u'1 - Top Gun') # Test mgt command call_command("create_callrequest_cdr", "1|1") call_command("create_callrequest_cdr", "3|1")
def voip_report(self, request): opts = VoIPCall._meta app_label = opts.app_label kwargs = {} form = VoipSearchForm() if request.method == 'POST': form = VoipSearchForm(request.POST) kwargs = voipcall_record_common_fun(request) request.session['from_date'] = request.POST.get('from_date') request.session['to_date'] = request.POST.get('to_date') request.session['status'] = request.POST.get('status') else: kwargs = voipcall_record_common_fun(request) tday = datetime.today() if len(kwargs) == 0: kwargs['starting_date__gte'] = datetime( tday.year, tday.month, tday.day, 0, 0, 0, 0) select_data = \ {"starting_date": "SUBSTR(CAST(starting_date as CHAR(30)),1,10)"} total_data = '' # Get Total Records from VoIPCall Report table for Daily Call Report total_data = VoIPCall.objects.extra(select=select_data)\ .values('starting_date')\ .filter(**kwargs).annotate(Count('starting_date'))\ .annotate(Sum('duration'))\ .annotate(Avg('duration'))\ .order_by('-starting_date') # Following code will count total voip calls, duration if total_data.count() != 0: max_duration = \ max([x['duration__sum'] for x in total_data]) total_duration = \ sum([x['duration__sum'] for x in total_data]) total_calls = \ sum([x['starting_date__count'] for x in total_data]) total_avg_duration = \ (sum([x['duration__avg']\ for x in total_data])) / total_data.count() else: max_duration = 0 total_duration = 0 total_calls = 0 total_avg_duration = 0 ctx = RequestContext( request, { 'form': form, 'total_data': total_data.reverse(), 'total_duration': total_duration, 'total_calls': total_calls, 'total_avg_duration': total_avg_duration, 'max_duration': max_duration, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('VoIP Report'), 'title': _('Call Aggregate Report'), }) return render_to_response('admin/dialer_cdr/voipcall/voip_report.html', context_instance=ctx)
def changelist_view(self, request, extra_context=None): """Override changelist_view method of django-admin for search parameters **Attributes**: * ``form`` - VoipSearchForm * ``template`` - admin/dialer_cdr/voipcall/change_list.html **Logic Description**: * VoIP report Record Listing with search option & Daily Call Report search Parameters: by date, by status and by billed. """ opts = VoIPCall._meta app_label = opts.app_label query_string = '' form = VoipSearchForm() if request.method == 'POST': query_string = voipcall_search_admin_form_fun(request) return HttpResponseRedirect("/admin/" + opts.app_label + "/" + \ opts.object_name.lower() + "/?" + query_string) else: status = '' from_date = '' to_date = '' if request.GET.get('starting_date__gte'): from_date = variable_value(request, 'starting_date__gte') if request.GET.get('starting_date__lte'): to_date = variable_value(request, 'starting_date__lte')[0:10] if request.GET.get('disposition__exact'): status = variable_value(request, 'disposition__exact') form = VoipSearchForm(initial={ 'status': status, 'from_date': from_date, 'to_date': to_date }) ChangeList = self.get_changelist(request) try: cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) except IncorrectLookupParameters: if ERROR_FLAG in request.GET.keys(): return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') kwargs = {} if request.META['QUERY_STRING'] == '': tday = datetime.today() kwargs['starting_date__gte'] = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0) cl.root_query_set.filter(**kwargs) formset = cl.formset = None # Session variable is used to get record set with searched option into export file request.session['admin_voipcall_record_qs'] = cl.root_query_set selection_note_all = ungettext('%(total_count)s selected', 'All %(total_count)s selected', cl.result_count) ctx = { 'selection_note': _('0 of %(cnt)s selected') % { 'cnt': len(cl.result_list) }, 'selection_note_all': selection_note_all % { 'total_count': cl.result_count }, 'cl': cl, 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('VoIP Report'), 'title': _('Call Report'), } return super(VoIPCallAdmin, self)\ .changelist_view(request, extra_context=ctx)
def voipcall_report(request): """VoIP Call Report **Attributes**: * ``form`` - VoipSearchForm * ``template`` - frontend/report/voipcall_report.html **Logic Description**: * Get VoIP call list according to search parameters for loggedin user **Important variable**: * ``request.session['voipcall_record_qs']`` - stores voipcall query set """ kwargs = {} kwargs['user'] = request.user from_date = '' to_date = '' disposition = variable_value(request, 'status') form = VoipSearchForm() if request.method == 'POST': if request.POST['from_date'] != "": from_date = request.POST['from_date'] if request.POST['to_date'] != "": to_date = request.POST['to_date'] form = VoipSearchForm(request.POST) kwargs = voipcall_record_common_fun(request) else: tday = datetime.today() kwargs['starting_date__gte'] = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0) voipcall_list = \ VoIPCall.objects.values('user', 'callid', 'callerid', 'phone_number', 'starting_date', 'duration', 'billsec', 'disposition', 'hangup_cause', 'hangup_cause_q850', 'used_gateway').filter(**kwargs).order_by('-starting_date') # Session variable is used to get record set with searched option # into export file request.session['voipcall_record_qs'] = voipcall_list select_data = \ {"starting_date": "SUBSTR(CAST(starting_date as CHAR(30)),1,10)"} total_data = '' # Get Total Rrecords from VoIPCall Report table for Daily Call Report total_data = VoIPCall.objects.extra(select=select_data)\ .values('starting_date')\ .filter(**kwargs).annotate(Count('starting_date'))\ .annotate(Sum('duration'))\ .annotate(Avg('duration'))\ .order_by('-starting_date') # Following code will count total voip calls, duration if total_data.count() != 0: max_duration = \ max([x['duration__sum'] for x in total_data]) total_duration = \ sum([x['duration__sum'] for x in total_data]) total_calls = sum([x['starting_date__count'] for x in total_data]) total_avg_duration = \ (sum([x['duration__avg']\ for x in total_data])) / total_data.count() else: max_duration = 0 total_duration = 0 total_calls = 0 total_avg_duration = 0 template = 'frontend/report/voipcall_report.html' data = { 'form': form, 'from_date': from_date, 'to_date': to_date, 'disposition': disposition, 'total_data': total_data.reverse(), 'total_duration': total_duration, 'total_calls': total_calls, 'total_avg_duration': total_avg_duration, 'max_duration': max_duration, 'module': current_view(request), 'notice_count': notice_count(request), 'dialer_setting_msg': user_dialer_setting_msg(request.user), } return render_to_response(template, data, context_instance=RequestContext(request))
def voipcall_report(request): """VoIP Call Report **Attributes**: * ``form`` - VoipSearchForm * ``template`` - frontend/report/voipcall_report.html **Logic Description**: * Get VoIP call list according to search parameters for loggedin user **Important variable**: * ``request.session['voipcall_record_kwargs']`` - stores voipcall kwargs """ sort_col_field_list = ['starting_date', 'leg_type', 'disposition', 'used_gateway', 'callerid', 'callid', 'phone_number', 'duration', 'billsec', 'amd_status'] default_sort_field = 'starting_date' pagination_data =\ get_pagination_vars(request, sort_col_field_list, default_sort_field) PAGE_SIZE = pagination_data['PAGE_SIZE'] sort_order = pagination_data['sort_order'] start_page = pagination_data['start_page'] end_page = pagination_data['end_page'] search_tag = 1 action = 'tabs-1' if request.method == 'POST': form = VoipSearchForm(request.user, request.POST) if form.is_valid(): field_list = ['start_date', 'end_date', 'disposition', 'campaign_id', 'leg_type'] unset_session_var(request, field_list) if request.POST.get('from_date'): # From from_date = request.POST['from_date'] start_date = ceil_strdate(from_date, 'start') request.session['session_start_date'] = start_date if request.POST.get('to_date'): # To to_date = request.POST['to_date'] end_date = ceil_strdate(to_date, 'end') request.session['session_end_date'] = end_date disposition = request.POST.get('status') if disposition != 'all': request.session['session_disposition'] = disposition campaign_id = request.POST.get('campaign_id') if campaign_id and int(campaign_id) != 0: request.session['session_campaign_id'] = int(campaign_id) leg_type = request.POST.get('leg_type') if leg_type and leg_type != '': request.session['session_leg_type'] = leg_type post_var_with_page = 0 try: if request.GET.get('page') or request.GET.get('sort_by'): post_var_with_page = 1 start_date = request.session.get('session_start_date') end_date = request.session.get('session_end_date') disposition = request.session.get('session_disposition') campaign_id = request.session.get('session_campaign_id') leg_type = request.session.get('session_leg_type') form = VoipSearchForm(request.user, initial={'from_date': start_date.strftime('%Y-%m-%d'), 'to_date': end_date.strftime('%Y-%m-%d'), 'status': disposition, 'campaign_id': campaign_id, 'leg_type': leg_type}) else: post_var_with_page = 1 if request.method == 'GET': post_var_with_page = 0 except: pass if post_var_with_page == 0: # default tday = datetime.today() from_date = tday.strftime('%Y-%m-%d') to_date = tday.strftime('%Y-%m-%d') start_date = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0) end_date = datetime(tday.year, tday.month, tday.day, 23, 59, 59, 999999) disposition = 'all' campaign_id = 0 leg_type = '' form = VoipSearchForm(request.user, initial={'from_date': from_date, 'to_date': to_date, 'status': disposition, 'campaign_id': campaign_id, 'leg_type': leg_type}) # unset session var request.session['session_start_date'] = start_date request.session['session_end_date'] = end_date request.session['session_disposition'] = disposition request.session['session_campaign_id'] = '' request.session['session_leg_type'] = '' kwargs = {} if start_date and end_date: kwargs['starting_date__range'] = (start_date, end_date) if start_date and end_date == '': kwargs['starting_date__gte'] = start_date if start_date == '' and end_date: kwargs['starting_date__lte'] = end_date if disposition and disposition != 'all': kwargs['disposition__exact'] = disposition if campaign_id and int(campaign_id) != 0: kwargs['callrequest__campaign_id'] = campaign_id if leg_type and leg_type != '': kwargs['leg_type__exact'] = leg_type if not request.user.is_superuser: kwargs['user'] = request.user voipcall_list = VoIPCall.objects.filter(**kwargs) all_voipcall_list = voipcall_list.values_list('id', flat=True) # Session variable is used to get record set with searched option # into export file request.session['voipcall_record_kwargs'] = kwargs if request.GET.get('page') or request.GET.get('sort_by'): daily_data = request.session['voipcall_daily_data'] else: if not voipcall_list: request.session['voipcall_daily_data'] = '' daily_data = get_voipcall_daily_data(voipcall_list) request.session['voipcall_daily_data'] = daily_data voipcall_list = voipcall_list.order_by(sort_order)[start_page:end_page] template = 'frontend/report/voipcall_report.html' data = { 'form': form, 'total_data': daily_data['total_data'], 'total_duration': daily_data['total_duration'], 'total_calls': daily_data['total_calls'], 'total_avg_duration': daily_data['total_avg_duration'], 'max_duration': daily_data['max_duration'], 'module': current_view(request), 'dialer_setting_msg': user_dialer_setting_msg(request.user), 'all_voipcall_list': all_voipcall_list, 'voipcall_list': voipcall_list, 'PAGE_SIZE': PAGE_SIZE, 'CDR_REPORT_COLUMN_NAME': CDR_REPORT_COLUMN_NAME, 'col_name_with_order': pagination_data['col_name_with_order'], 'search_tag': search_tag, 'start_date': start_date, 'end_date': end_date, 'action': action, 'AMD': settings.AMD, } request.session['msg'] = '' request.session['error_msg'] = '' return render_to_response(template, data, context_instance=RequestContext(request))
def voipcall_report(request): """VoIP Call Report **Attributes**: * ``form`` - VoipSearchForm * ``template`` - dialer_cdr/voipcall_report.html **Logic Description**: * Get VoIP call list according to search parameters for loggedin user **Important variable**: * ``request.session['voipcall_record_kwargs']`` - stores voipcall kwargs """ sort_col_field_list = ['starting_date', 'leg_type', 'disposition', 'used_gateway', 'callerid', 'callid', 'phone_number', 'duration', 'billsec', 'amd_status'] pag_vars = get_pagination_vars(request, sort_col_field_list, default_sort_field='starting_date') action = 'tabs-1' form = VoipSearchForm(request.user, request.POST or None) if form.is_valid(): # Valid form field_list = ['start_date', 'end_date', 'disposition', 'campaign_id', 'leg_type'] unset_session_var(request, field_list) from_date = getvar(request, 'from_date') to_date = getvar(request, 'to_date') start_date = ceil_strdate(str(from_date), 'start') end_date = ceil_strdate(str(to_date), 'end') converted_start_date = start_date.strftime('%Y-%m-%d') converted_end_date = end_date.strftime('%Y-%m-%d') request.session['session_start_date'] = converted_start_date request.session['session_end_date'] = converted_end_date disposition = getvar(request, 'disposition', setsession=True) campaign_id = getvar(request, 'campaign_id', setsession=True) leg_type = getvar(request, 'leg_type', setsession=True) form = VoipSearchForm(request.user, initial={'from_date': start_date.strftime('%Y-%m-%d'), 'to_date': end_date.strftime('%Y-%m-%d'), 'disposition': disposition, 'campaign_id': campaign_id, 'leg_type': leg_type}) elif request.GET.get('page') or request.GET.get('sort_by'): # Pagination / Sort start_date = request.session.get('session_start_date') end_date = request.session.get('session_end_date') start_date = ceil_strdate(str(start_date), 'start') end_date = ceil_strdate(str(end_date), 'end') disposition = request.session.get('session_disposition') campaign_id = request.session.get('session_campaign_id') leg_type = request.session.get('session_leg_type') form = VoipSearchForm(request.user, initial={'from_date': start_date.strftime('%Y-%m-%d'), 'to_date': end_date.strftime('%Y-%m-%d'), 'disposition': disposition, 'campaign_id': campaign_id, 'leg_type': leg_type}) else: # Default tday = datetime.utcnow().replace(tzinfo=utc) from_date = tday.strftime('%Y-%m-%d') to_date = tday.strftime('%Y-%m-%d') start_date = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0).replace(tzinfo=utc) end_date = datetime(tday.year, tday.month, tday.day, 23, 59, 59, 999999).replace(tzinfo=utc) disposition = 'all' campaign_id = 0 leg_type = '' form = VoipSearchForm(request.user, initial={'from_date': from_date, 'to_date': to_date, 'disposition': disposition, 'campaign_id': campaign_id, 'leg_type': leg_type}) # unset session var request.session['session_start_date'] = start_date request.session['session_end_date'] = end_date request.session['session_disposition'] = disposition request.session['session_campaign_id'] = '' request.session['session_leg_type'] = '' kwargs = {} if start_date and end_date: kwargs['starting_date__range'] = (start_date, end_date) if start_date and end_date == '': kwargs['starting_date__gte'] = start_date if start_date == '' and end_date: kwargs['starting_date__lte'] = end_date if disposition and disposition != 'all': kwargs['disposition__exact'] = disposition if campaign_id and int(campaign_id) != 0: kwargs['callrequest__campaign_id'] = campaign_id if leg_type and leg_type != '': kwargs['leg_type__exact'] = leg_type if not request.user.is_superuser: kwargs['user_id'] = request.user.id voipcall_list = VoIPCall.objects.filter(**kwargs) all_voipcall_list = voipcall_list.values_list('id', flat=True) # Session variable is used to get record set with searched option # into export file request.session['voipcall_record_kwargs'] = kwargs if request.GET.get('page') or request.GET.get('sort_by'): daily_data = request.session['voipcall_daily_data'] else: if not voipcall_list: request.session['voipcall_daily_data'] = '' daily_data = get_voipcall_daily_data(voipcall_list) request.session['voipcall_daily_data'] = daily_data voipcall_list = voipcall_list.order_by(pag_vars['sort_order'])[pag_vars['start_page']:pag_vars['end_page']] data = { 'form': form, 'total_data': daily_data['total_data'], 'total_duration': daily_data['total_duration'], 'total_calls': daily_data['total_calls'], 'total_avg_duration': daily_data['total_avg_duration'], 'max_duration': daily_data['max_duration'], 'all_voipcall_list': all_voipcall_list, 'voipcall_list': voipcall_list, 'CDR_REPORT_COLUMN_NAME': CDR_REPORT_COLUMN_NAME, 'col_name_with_order': pag_vars['col_name_with_order'], 'start_date': start_date, 'end_date': end_date, 'action': action, } request.session['msg'] = '' request.session['error_msg'] = '' return render_to_response('dialer_cdr/voipcall_report.html', data, context_instance=RequestContext(request))
def changelist_view(self, request, extra_context=None): """ Override changelist_view method of django-admin for search parameters **Attributes**: * ``form`` - VoipSearchForm * ``template`` - admin/dialer_cdr/voipcall/change_list.html **Logic Description**: * VoIP report Record Listing with search option & Daily Call Report search Parameters: by date, by status and by billed. """ opts = VoIPCall._meta query_string = '' form = VoipSearchForm(request.user) if request.method == 'POST': # Session variable get record set with searched option into export file request.session[ 'admin_voipcall_record_kwargs'] = voipcall_record_common_fun( request) query_string = voipcall_search_admin_form_fun(request) return HttpResponseRedirect( "/admin/%s/%s/?%s" % (opts.app_label, opts.object_name.lower(), query_string)) else: status = '' from_date = '' to_date = '' campaign_id = '' leg_type = '' from_date = getvar(request, 'starting_date__gte') to_date = getvar(request, 'starting_date__lte')[0:10] status = getvar(request, 'disposition__exact') campaign_id = getvar(request, 'callrequest__campaign_id') leg_type = getvar(request, 'leg_type__exact') form = VoipSearchForm(request.user, initial={ 'status': status, 'from_date': from_date, 'to_date': to_date, 'campaign_id': campaign_id, 'leg_type': leg_type }) ChangeList = self.get_changelist(request) try: cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) except IncorrectLookupParameters: if ERROR_FLAG in request.GET.keys(): return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) return HttpResponseRedirect('%s?%s=1' % (request.path, ERROR_FLAG)) if request.META['QUERY_STRING'] == '': # Default # Session variable get record set with searched option into export file request.session[ 'admin_voipcall_record_kwargs'] = voipcall_record_common_fun( request) query_string = voipcall_search_admin_form_fun(request) return HttpResponseRedirect( "/admin/%s/%s/?%s" % (opts.app_label, opts.object_name.lower(), query_string)) cl.formset = None selection_note_all = ungettext('%(total_count)s selected', 'All %(total_count)s selected', cl.result_count) ctx = { 'selection_note': _('0 of %(cnt)s selected') % { 'cnt': len(cl.result_list) }, 'selection_note_all': selection_note_all % { 'total_count': cl.result_count }, 'cl': cl, 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': APP_LABEL, 'title': _('call report'), } return super(VoIPCallAdmin, self).changelist_view(request, extra_context=ctx)