def simulator(request): """Client Simulator To view rate according to VoIP Plan & Destination No. **Attributes**: * ``template`` - voip_billing/simulator.html * ``form`` - SimulatorForm **Logic Description**: get min call rates for destination from rate_engine and display them in template """ data = [] form = SimulatorForm(request.user, request.POST or None) # Get Voip Plan ID according to USER if form.is_valid(): # IS recipient_phone_no/destination no is valid prefix # (Not banned Prefix) ? destination_no = request.POST.get("destination_no") if hasattr(request.user, 'userprofile'): voipplan_id = request.user.userprofile.voipplan_id allowed = prefix_allowed_to_call(destination_no, voipplan_id) if allowed: rates = rate_engine(voipplan_id=voipplan_id, dest_number=destination_no) for rate in rates: r_r_plan = VoIPRetailRate.objects.get(id=rate.rrid) data.append( (voipplan_id, r_r_plan.voip_retail_plan_id.name, rate.retail_rate)) data = { 'form': form, 'data': data, } return render_to_response('voip_billing/simulator.html', data, context_instance=RequestContext(request))
def simulator(request): """Client Simulator To view rate according to VoIP Plan & Destination No. **Attributes**: * ``template`` - voip_billing/simulator.html * ``form`` - SimulatorForm **Logic Description**: get min call rates for destination from rate_engine and display them in template """ data = [] form = SimulatorForm(request.user, request.POST or None) # Get Voip Plan ID according to USER if form.is_valid(): # IS recipient_phone_no/destination no is valid prefix # (Not banned Prefix) ? destination_no = request.POST.get("destination_no") if hasattr(request.user, 'userprofile'): voipplan_id = request.user.userprofile.voipplan_id allowed = prefix_allowed_to_call(destination_no, voipplan_id) if allowed: rates = rate_engine(voipplan_id=voipplan_id, dest_number=destination_no) for rate in rates: r_r_plan = VoIPRetailRate.objects.get(id=rate.rrid) data.append((voipplan_id, r_r_plan.voip_retail_plan_id.name, rate.retail_rate)) data = { 'form': form, 'data': data, } return render_to_response('voip_billing/simulator.html', data, context_instance=RequestContext(request))
def get(self, request, format=None): """ Voip Rate GET """ logger.debug('Voip Rate GET API get called') error = {} # check voipplan id for user try: voipplan_id = request.user.userprofile.voipplan_id except: error_msg = "User is not attached with voip plan" error['error'] = error_msg return Response(error) dialcode = '' recipient_phone_no = '' if 'dialcode' in request.GET and request.GET.get('dialcode') != '': dialcode = request.GET.get('dialcode') try: dialcode = int(dialcode) except ValueError: error['error'] = "Wrong value for dialcode !" logger.error(error['error']) return Response(error) if 'recipient_phone_no' in request.GET: if request.GET.get('recipient_phone_no') != '': recipient_phone_no = request.GET.get('recipient_phone_no') else: error['error'] = "Please enter recipient_phone_no" logger.error(error['error']) return Response(error) if recipient_phone_no: # Check if recipient_phone_no is not banned allowed = prefix_allowed_to_call(recipient_phone_no, voipplan_id) if allowed: # Get Destination prefix list e.g (34,346,3465,34657) destination_prefix_list = prefix_list_string( str(recipient_phone_no)) prefixlist = destination_prefix_list.split(",") # Get Rate List rate_list = VoIPRetailRate.objects\ .values('prefix', 'retail_rate', 'prefix__destination')\ .filter(prefix__in=[int(s) for s in prefixlist]) logger.debug('Voip Rate API : result OK 200') return Response(rate_list) else: error_msg = "Not allowed : %s" % recipient_phone_no error['error'] = error_msg logger.error(error_msg) return Response(error) sort_field = '' order = '' if request.GET.get('sort_field'): sort_field = request.GET.get('sort_field') if request.GET.get('order'): order = request.GET.get('order') # call the find rates function result = find_rates(voipplan_id, dialcode, sort_field, order) logger.debug('Voip Rate API : result OK 200') return Response(result)
def find_rates(voipplan_id, dialcode, sort_field, order): """ function to retrieve list of rates belonging to a voipplan """ cursor = connection.cursor() # variables used for sorting extension_query = '' if sort_field == 'prefix': sort_field = 'voip_retail_rate.prefix' if sort_field == 'retail_rate': sort_field = 'minrate' if sort_field == 'destination': sort_field = 'dialcode_prefix.destination' if sort_field: extension_query = "ORDER BY " + sort_field + ' ' + order cursor = connection.cursor() if dialcode: sqldialcode = str(dialcode) + '%' sql_statement = ( "SELECT voip_retail_rate.prefix, " "Min(retail_rate) as minrate, dialcode_prefix.destination " "FROM voip_retail_rate " "INNER JOIN voipplan_voipretailplan " "ON voipplan_voipretailplan.voipretailplan_id = " "voip_retail_rate.voip_retail_plan_id " "LEFT JOIN dialcode_prefix ON dialcode_prefix.prefix = " "voip_retail_rate.prefix " "WHERE voipplan_id=%s " "AND CAST(voip_retail_rate.prefix AS TEXT) LIKE %s " "GROUP BY voip_retail_rate.prefix, dialcode_prefix.destination " + extension_query) cursor.execute(sql_statement, [voipplan_id, sqldialcode]) else: sql_statement = ( "SELECT voip_retail_rate.prefix, " "Min(retail_rate) as minrate, dialcode_prefix.destination " "FROM voip_retail_rate " "INNER JOIN voipplan_voipretailplan " "ON voipplan_voipretailplan.voipretailplan_id = " "voip_retail_rate.voip_retail_plan_id " "LEFT JOIN dialcode_prefix ON dialcode_prefix.prefix = " "voip_retail_rate.prefix " "WHERE voipplan_id=%s " "GROUP BY voip_retail_rate.prefix, dialcode_prefix.destination " + extension_query) cursor.execute(sql_statement, [voipplan_id]) row = cursor.fetchall() result = [] for record in row: # Not banned Prefix allowed = prefix_allowed_to_call(record[0], voipplan_id) if allowed: modrecord = {} modrecord['prefix'] = record[0] modrecord['retail_rate'] = record[1] modrecord['prefix__destination'] = record[2] result.append(modrecord) return result
def get(self, request, format=None): """ Voip Rate GET """ logger.debug('Voip Rate GET API get called') error = {} # check voipplan id for user try: voipplan_id = request.user.userprofile.voipplan_id except: error_msg = "User is not attached with voip plan" error['error'] = error_msg return Response(error) dialcode = '' recipient_phone_no = '' if 'dialcode' in request.GET and request.GET.get('dialcode') != '': dialcode = request.GET.get('dialcode') try: dialcode = int(dialcode) except ValueError: error['error'] = "Wrong value for dialcode !" logger.error(error['error']) return Response(error) if 'recipient_phone_no' in request.GET: if request.GET.get('recipient_phone_no') != '': recipient_phone_no = request.GET.get('recipient_phone_no') else: error['error'] = "Please enter recipient_phone_no" logger.error(error['error']) return Response(error) if recipient_phone_no: # Check if recipient_phone_no is not banned allowed = prefix_allowed_to_call(recipient_phone_no, voipplan_id) if allowed: # Get Destination prefix list e.g (34,346,3465,34657) destination_prefix_list = prefix_list_string(str(recipient_phone_no)) prefixlist = destination_prefix_list.split(",") # Get Rate List rate_list = VoIPRetailRate.objects\ .values('prefix', 'retail_rate', 'prefix__destination')\ .filter(prefix__in=[int(s) for s in prefixlist]) logger.debug('Voip Rate API : result OK 200') return Response(rate_list) else: error_msg = "Not allowed : %s" % recipient_phone_no error['error'] = error_msg logger.error(error_msg) return Response(error) sort_field = '' order = '' if request.GET.get('sort_field'): sort_field = request.GET.get('sort_field') if request.GET.get('order'): order = request.GET.get('order') # call the find rates function result = find_rates(voipplan_id, dialcode, sort_field, order) logger.debug('Voip Rate API : result OK 200') return Response(result)