def home(request): if not request.GET.has_key('csrfmiddlewaretoken'): form = CdrSearchForm() registros = Cdr.objects.filter().using(request.database) else: form = CdrSearchForm(request.GET) if form.is_valid(): date_from = form.cleaned_data.get('calldate_from') date_to = form.cleaned_data.get('calldate_to') if date_to: date_to = date_to + timedelta(days=1) registros = Cdr.objects.all().using(request.database) if date_from: registros = Cdr.objects.filter(calldate__gte=date_from).using( request.database) if date_to: registros = Cdr.objects.filter(calldate__lte=date_to).using( request.database) disposition = form.cleaned_data.get('disposition', 'ALL') if disposition != 'ALL': registros = registros.filter(disposition=disposition).using( request.database) return render_to_response("cdr/index.html", { 'registros': registros, 'form': form }, context_instance=RequestContext(request))
def home(request): if not request.GET.has_key("csrfmiddlewaretoken"): form = CdrSearchForm() registros = Cdr.objects.filter().using(request.database) else: form = CdrSearchForm(request.GET) if form.is_valid(): date_from = form.cleaned_data.get("calldate_from") date_to = form.cleaned_data.get("calldate_to") if date_to: date_to = date_to + timedelta(days=1) registros = Cdr.objects.all().using(request.database) if date_from: registros = Cdr.objects.filter(calldate__gte=date_from).using(request.database) if date_to: registros = Cdr.objects.filter(calldate__lte=date_to).using(request.database) disposition = form.cleaned_data.get("disposition", "ALL") if disposition != "ALL": registros = registros.filter(disposition=disposition).using(request.database) return render_to_response( "cdr/index.html", {"registros": registros, "form": form}, context_instance=RequestContext(request) )
def test_cdr_view(self): """Test Function to check cdr_view""" response = self.client.get('/cdr_view/') self.assertTrue(response.context['form'], CdrSearchForm()) self.assertTemplateUsed(response, 'frontend/cdr_view.html') self.assertEqual(response.status_code, 200) request = self.factory.get('/cdr_view/') request.user = self.user request.session = {} response = cdr_view(request) self.assertEqual(response.status_code, 200) data = { 'switch_id': 1, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'to_date': datetime.now().strftime("%Y-%m-%d"), 'destination': '91', 'destination_type': 1, 'accountcode': '123', 'caller': 'abc', 'duration': '30', 'duration_type': '>', 'direction': 'INBOUND', 'hangup_cause': 1, 'result': 1, 'records_per_page': 10 } response = self.client.post('/cdr_view/', data) self.assertTrue(response.context['form'], CdrSearchForm(data)) self.assertEqual(response.status_code, 200) response = self.client.get('/cdr_view/?page=1') self.assertEqual(response.status_code, 200) response = self.client.get('/cdr_view/?page=1&sort_by=-caller_id_number') self.assertEqual(response.status_code, 200) response = self.client.get('/cdr_view/?page=1&sort_by=caller_id_number') self.assertEqual(response.status_code, 200) request = self.factory.post('/cdr_view/', data) request.user = self.user request.session = {} response = cdr_view(request) self.assertEqual(response.status_code, 200) request = self.factory.get('/cdr_export_csv/') request.user = self.user request.session = {} now = datetime.today() start_date = datetime(now.year, now.month, 1, 0, 0, 0, 0) end_date = datetime(now.year, now.month, now.day, 23, 59, 59, 99999) request.session['query_var'] = { 'start_uepoch': {'$gte': start_date, '$lt': end_date} } response = cdr_export_to_csv(request) self.assertEqual(response.status_code, 200)
def test_cdr_search_form(self): data = {'switch_id': 1, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'to_date': datetime.now().strftime("%Y-%m-%d"), 'destination': 'abc', 'destination_type': 1, 'accountcode': 'abc', 'caller': 'abc', 'duration': 'abc', 'duration_type': '>', 'direction': 'INBOUND', 'hangup_cause': 1, 'result': 1, 'records_per_page': 10} form = CdrSearchForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["duration"].errors, ['abc is not a valid duration.'])
def cdr_view(request): """List of CDRs **Attributes**: * ``template`` - cdr/list.html * ``form`` - CdrSearchForm **Logic Description**: * get the call records as well as daily call analytics from postgresql according to search parameters """ logging.debug('CDR View Start') result = 1 # default min switch_id = 0 # default all hangup_cause_id = 0 # default all destination, destination_type, accountcode = '', '', '' direction, duration, duration_type = '', '', '' caller_id_number, caller_id_number_type, country_id = '', '', '' action = 'tabs-1' menu = 'on' records_per_page = settings.PAGE_SIZE form = CdrSearchForm(request.POST or None) if form.is_valid(): logging.debug('CDR Search View') # set session var value field_list = ['destination', 'result', 'destination_type', 'accountcode', 'caller_id_number', 'caller_id_number_type', 'duration', 'duration_type', 'hangup_cause_id', 'switch_id', 'direction', 'country_id', 'export_query_var'] unset_session_var(request, field_list) from_date = getvar(request, 'from_date', setsession=False) to_date = getvar(request, 'to_date', setsession=False) result = getvar(request, 'result', setsession=True) destination = getvar(request, 'destination', setsession=True) destination_type = getvar(request, 'destination_type', setsession=True) accountcode = getvar(request, 'accountcode', setsession=True) caller_id_number = getvar(request, 'caller_id_number', setsession=True) caller_id_number_type = getvar(request, 'caller_id_number_type', setsession=True) duration = getvar(request, 'duration', setsession=True) duration_type = getvar(request, 'duration_type', setsession=True) direction = getvar(request, 'direction', setsession=True) if direction and direction != 'all' and direction != '0': request.session['session_direction'] = str(direction) switch_id = getvar(request, 'switch_id', setsession=True) hangup_cause_id = getvar(request, 'hangup_cause_id', setsession=True) records_per_page = getvar(request, 'records_per_page', setsession=True) country_id = form.cleaned_data.get('country_id') # convert list value in int country_id = [int(row) for row in country_id] if len(country_id) >= 1: request.session['session_country_id'] = country_id start_date = ceil_strdate(str(from_date), 'start', True) end_date = ceil_strdate(str(to_date), 'end', True) converted_start_date = start_date.strftime('%Y-%m-%d %H:%M') converted_end_date = end_date.strftime('%Y-%m-%d %H:%M') request.session['session_start_date'] = converted_start_date request.session['session_end_date'] = converted_end_date menu = show_menu(request) using_session = False # Display a specific page or sort if request.GET.get('page') or request.GET.get('sort_by'): using_session = True from_date = start_date = request.session.get('session_start_date') to_date = end_date = request.session.get('session_end_date') start_date = ceil_strdate(start_date, 'start', True) end_date = ceil_strdate(end_date, 'end', True) destination = request.session.get('session_destination') destination_type = request.session.get('session_destination_type') accountcode = request.session.get('session_accountcode') caller_id_number = request.session.get('session_caller_id_number') caller_id_number_type = request.session.get('session_caller_id_number_type') duration = request.session.get('session_duration') duration_type = request.session.get('session_duration_type') direction = request.session.get('session_direction') switch_id = request.session.get('session_switch_id') hangup_cause_id = request.session.get('session_hangup_cause_id') result = request.session.get('session_result') records_per_page = request.session.get('session_records_per_page') country_id = request.session['session_country_id'] # Set default cause we display page for the first time if request.method == 'GET' and not using_session: tday = datetime.today() from_date = datetime(tday.year, tday.month, 1, 0, 0, 0, 0) last_day = ((datetime(tday.year, tday.month, 1, 23, 59, 59, 999999) + relativedelta(months=1)) - relativedelta(days=1)).strftime('%d') # to_date = tday.strftime('%Y-%m-' + last_day + ' 23:59') to_date = datetime(tday.year, tday.month, int(last_day), 23, 59, 59, 999999) start_date = ceil_strdate(str(from_date), 'start', True) end_date = ceil_strdate(str(to_date), 'end', True) converted_start_date = start_date.strftime('%Y-%m-%d %H:%M') converted_end_date = end_date.strftime('%Y-%m-%d %H:%M') request.session['session_start_date'] = converted_start_date request.session['session_end_date'] = converted_end_date request.session['session_result'] = 1 field_list = [ 'destination', 'destination_type', 'accountcode', 'caller_id_number', 'caller_id_number_type', 'duration', 'duration_type', 'hangup_cause_id', 'switch_id', 'direction', 'country_id'] unset_session_var(request, field_list) request.session['session_records_per_page'] = records_per_page request.session['session_country_id'] = '' # Define no of records per page records_per_page = int(records_per_page) sort_col_field_list = ['id', 'caller_id_number', 'destination_number', 'starting_date'] page_vars = get_pagination_vars(request, sort_col_field_list, default_sort_field='id') # Build filter for CDR.object kwargs = {} if hangup_cause_id and hangup_cause_id != '0': kwargs['hangup_cause_id'] = int(hangup_cause_id) if switch_id and switch_id != '0': kwargs['switch_id'] = int(switch_id) if direction and direction != 'all' and direction != "0": kwargs['direction'] = direction if len(country_id) >= 1 and country_id[0] != 0: kwargs['country_id__in'] = country_id if start_date: kwargs['starting_date__gte'] = start_date if end_date: kwargs['starting_date__lte'] = end_date if destination: operator_query = get_filter_operator_str('destination_number', destination_type) kwargs[operator_query] = destination if duration: operator_query = get_filter_operator_int('duration', duration_type) kwargs[operator_query] = duration if caller_id_number: operator_query = get_filter_operator_str('caller_id_number', caller_id_number_type) kwargs[operator_query] = caller_id_number # user are restricted to their own CDRs if not request.user.is_superuser: kwargs['user_id'] = request.user.id if request.user.is_superuser and accountcode: try: user_prof = UserProfile.objects.get(accountcode=accountcode) kwargs['user_id'] = user_prof.user.id except UserProfile.DoesNotExist: # cannot find a user for this accountcode pass cdrs = CDR.objects.filter(**kwargs).order_by(page_vars['sort_order']) page_cdr_list = cdrs[page_vars['start_page']:page_vars['end_page']] cdr_count = cdrs.count() logging.debug('Create cdr result') # store query_var in session without date export_kwargs = kwargs.copy() if 'starting_date__gte' in export_kwargs: export_kwargs['starting_date__gte'] = export_kwargs['starting_date__gte'].strftime('%Y-%m-%dT%H:%M:%S') if 'starting_date__lte' in export_kwargs: export_kwargs['starting_date__lte'] = export_kwargs['starting_date__lte'].strftime('%Y-%m-%dT%H:%M:%S') request.session['session_export_kwargs'] = export_kwargs form = CdrSearchForm( initial={ 'from_date': from_date, 'to_date': to_date, 'destination': destination, 'destination_type': destination_type, 'accountcode': accountcode, 'caller_id_number': caller_id_number, 'caller_id_number_type': caller_id_number_type, 'duration': duration, 'duration_type': duration_type, 'result': result, 'direction': direction, 'hangup_cause_id': hangup_cause_id, 'switch_id': switch_id, 'country_id': country_id, 'records_per_page': records_per_page } ) template_data = { 'page_cdr_list': page_cdr_list, 'cdrs': cdrs, 'form': form, 'cdr_count': cdr_count, 'cdr_daily_data': {}, 'col_name_with_order': page_vars['col_name_with_order'], 'menu': menu, 'start_date': start_date, 'end_date': end_date, 'action': action, 'result': result, 'CDR_COLUMN_NAME': CDR_COLUMN_NAME, 'records_per_page': records_per_page, 'up_icon': '<i class="glyphicon glyphicon-chevron-up"></i>', 'down_icon': '<i class="glyphicon glyphicon-chevron-down"></i>' } logging.debug('CDR View End') return render_to_response('cdr/list.html', template_data, context_instance=RequestContext(request))
def cdr_view(request): """List of CDRs **Attributes**: * ``template`` - cdr/list.html * ``form`` - CdrSearchForm **Logic Description**: * get the call records as well as daily call analytics from postgresql according to search parameters """ logging.debug('CDR View Start') result = 1 # default min switch_id = 0 # default all hangup_cause_id = 0 # default all destination, destination_type, accountcode, call_record, custom_carrier = '', '', '', '', '' direction, duration, duration_type = '', '', '' caller_id_number, caller_id_number_type, country_id = '', '', '' action = 'tabs-1' menu = 'on' records_per_page = settings.PAGE_SIZE form = CdrSearchForm(request.POST or None) if form.is_valid(): logging.debug('CDR Search View') # set session var value field_list = ['destination', 'result', 'destination_type', 'accountcode', 'caller_id_number', 'caller_id_number_type', 'duration', 'duration_type', 'hangup_cause_id', 'switch_id', 'direction', 'country_id', 'export_query_var'] unset_session_var(request, field_list) from_date = getvar(request, 'from_date', setsession=False) to_date = getvar(request, 'to_date', setsession=False) result = getvar(request, 'result', setsession=True) destination = getvar(request, 'destination', setsession=True) destination_type = getvar(request, 'destination_type', setsession=True) accountcode = getvar(request, 'accountcode', setsession=True) caller_id_number = getvar(request, 'caller_id_number', setsession=True) caller_id_number_type = getvar(request, 'caller_id_number_type', setsession=True) duration = getvar(request, 'duration', setsession=True) duration_type = getvar(request, 'duration_type', setsession=True) direction = getvar(request, 'direction', setsession=True) if direction and direction != 'all' and direction != '0': request.session['session_direction'] = str(direction) switch_id = getvar(request, 'switch_id', setsession=True) hangup_cause_id = getvar(request, 'hangup_cause_id', setsession=True) records_per_page = getvar(request, 'records_per_page', setsession=True) logging.debug(records_per_page) country_id = form.cleaned_data.get('country_id') # convert list value in int country_id = [int(row) for row in country_id] if len(country_id) >= 1: request.session['session_country_id'] = country_id start_date = ceil_strdate(str(from_date), 'start', True) end_date = ceil_strdate(str(to_date), 'end', True) converted_start_date = start_date.strftime('%Y-%m-%d %H:%M') converted_end_date = end_date.strftime('%Y-%m-%d %H:%M') request.session['session_start_date'] = converted_start_date request.session['session_end_date'] = converted_end_date call_record = getvar(request, 'call_record', setsession=True) custom_carrier = getvar(request, 'custom_carrier', setsession=True) menu = show_menu(request) using_session = False # Display a specific page or sort if request.GET.get('page') or request.GET.get('sort_by'): using_session = True from_date = start_date = request.session.get('session_start_date') to_date = end_date = request.session.get('session_end_date') start_date = ceil_strdate(start_date, 'start', True) end_date = ceil_strdate(end_date, 'end', True) destination = request.session.get('session_destination') destination_type = request.session.get('session_destination_type') accountcode = request.session.get('session_accountcode') caller_id_number = request.session.get('session_caller_id_number') caller_id_number_type = request.session.get('session_caller_id_number_type') duration = request.session.get('session_duration') duration_type = request.session.get('session_duration_type') direction = request.session.get('session_direction') switch_id = request.session.get('session_switch_id') hangup_cause_id = request.session.get('session_hangup_cause_id') result = request.session.get('session_result') records_per_page = request.session.get('session_records_per_page') logging.debug(records_per_page) country_id = request.session['session_country_id'] #call_record = request.session['call_record'] # Set default cause we display page for the first time if request.method == 'GET' and not using_session: tday = datetime.today() from_date = datetime(tday.year, tday.month, 1, 0, 0, 0, 0) last_day = ((datetime(tday.year, tday.month, 1, 23, 59, 59, 999999) + relativedelta(months=1)) - relativedelta(days=1)).strftime('%d') # to_date = tday.strftime('%Y-%m-' + last_day + ' 23:59') to_date = datetime(tday.year, tday.month, int(last_day), 23, 59, 59, 999999) start_date = ceil_strdate(str(from_date), 'start', True) end_date = ceil_strdate(str(to_date), 'end', True) converted_start_date = start_date.strftime('%Y-%m-%d %H:%M') converted_end_date = end_date.strftime('%Y-%m-%d %H:%M') request.session['session_start_date'] = converted_start_date request.session['session_end_date'] = converted_end_date request.session['session_result'] = 1 field_list = [ 'destination', 'destination_type', 'accountcode', 'caller_id_number', 'caller_id_number_type', 'duration', 'duration_type', 'hangup_cause_id', 'switch_id', 'direction', 'country_id'] unset_session_var(request, field_list) request.session['session_records_per_page'] = records_per_page logging.debug("test 2: %s", request.session['session_records_per_page']) request.session['session_country_id'] = '' # Define no of records per page records_per_page = int(records_per_page) logging.debug(records_per_page) sort_col_field_list = ['id', 'caller_id_number', 'destination_number', 'starting_date'] page_vars = get_pagination_vars(request, sort_col_field_list, default_sort_field='id') # Build filter for CDR.object kwargs = {} if hangup_cause_id and hangup_cause_id != '0': kwargs['hangup_cause_id'] = int(hangup_cause_id) if switch_id and switch_id != '0': kwargs['switch_id'] = int(switch_id) if direction and direction != 'all' and direction != "0": kwargs['direction'] = direction if len(country_id) >= 1 and country_id[0] != 0: kwargs['country_id__in'] = country_id if start_date: kwargs['starting_date__gte'] = start_date if end_date: kwargs['starting_date__lte'] = end_date if destination: operator_query = get_filter_operator_str('destination_number', destination_type) kwargs[operator_query] = destination if duration: operator_query = get_filter_operator_int('duration', duration_type) kwargs[operator_query] = duration if caller_id_number: operator_query = get_filter_operator_str('caller_id_number', caller_id_number_type) kwargs[operator_query] = caller_id_number # kwargs[operator_query] = "asd" # user are restricted to their own CDRs if not request.user.is_superuser: kwargs['user_id'] = request.user.id if request.user.is_superuser and accountcode: try: acc = AccountCode.objects.get(accountcode=accountcode) kwargs['user_id'] = acc.user.id # on specific accountcode filter let only display that one kwargs['accountcode'] = accountcode except AccountCode.DoesNotExist: # cannot find a user for this accountcode pass #kwargs['call_record'] = destination cdrs = CDR.objects.filter(**kwargs).order_by(page_vars['sort_order']) if page_vars['PAGE_NUMBER']: page_vars['start_page'] = ( page_vars['PAGE_NUMBER'] - 1 ) * records_per_page page_vars['end_page'] = page_vars['start_page'] + records_per_page page_cdr_list = cdrs[page_vars['start_page']:page_vars['end_page']] cdr_count = cdrs.count() logging.debug('Create cdr result') # store query_var in session without date export_kwargs = kwargs.copy() if 'starting_date__gte' in export_kwargs: export_kwargs['starting_date__gte'] = export_kwargs['starting_date__gte'].strftime('%Y-%m-%dT%H:%M:%S') if 'starting_date__lte' in export_kwargs: export_kwargs['starting_date__lte'] = export_kwargs['starting_date__lte'].strftime('%Y-%m-%dT%H:%M:%S') request.session['session_export_kwargs'] = export_kwargs form = CdrSearchForm( initial={ 'from_date': from_date, 'to_date': to_date, 'destination': destination, 'destination_type': destination_type, 'accountcode': accountcode, 'caller_id_number': caller_id_number, 'caller_id_number_type': caller_id_number_type, 'duration': duration, 'duration_type': duration_type, 'result': result, 'direction': direction, 'hangup_cause_id': hangup_cause_id, 'switch_id': switch_id, 'country_id': country_id, 'records_per_page': records_per_page } ) template_data = { 'page_cdr_list': page_cdr_list, 'cdrs': cdrs, 'form': form, 'cdr_count': cdr_count, 'cdr_daily_data': {}, 'col_name_with_order': page_vars['col_name_with_order'], 'menu': menu, 'start_date': start_date, 'end_date': end_date, 'action': action, 'result': result, 'CDR_COLUMN_NAME': CDR_COLUMN_NAME, 'records_per_page': records_per_page, 'up_icon': '<i class="glyphicon glyphicon-chevron-up"></i>', 'down_icon': '<i class="glyphicon glyphicon-chevron-down"></i>' } logging.debug('CDR View End') return render_to_response('cdr/list.html', template_data, context_instance=RequestContext(request))