forked from cdr-stats/cdr-stats
/
voip_rate_api.py
225 lines (191 loc) · 8.03 KB
/
voip_rate_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#
from django.conf.urls import url
from django.db import connection
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from tastypie.authentication import BasicAuthentication
from tastypie.throttle import BaseThrottle
from tastypie.exceptions import BadRequest
from voip_billing.models import VoIPRetailRate
from cdr.functions_def import prefix_list_string
from voip_billing.function_def import prefix_allowed_to_call
from user_profile.models import UserProfile
import logging
logger = logging.getLogger('cdr-stats.filelog')
class VoipRateResource(ModelResource):
"""API to get voip call rate via dialcode or recipient_phone_no
**Attributes**:
* ``dialcode`` -
* ``recipient_phone_no`` -
**Create**:
CURL Usage::
curl -u username:password -H 'Accept: application/json' http://localhost:8000/api/v1/voip_rate/
or
curl -u username:password -H 'Accept: application/json' http://localhost:8000/api/v1/voip_rate/?dialcode=34
or
curl -u username:password -H 'Accept: application/json' http://localhost:8000/api/v1/voip_rate/?recipient_phone_no=34650784355
Response::
[
{
"prefix":34,
"prefix__destination":"Spain",
"retail_rate":"0.0350"
},
{
"prefix":33,
"prefix__destination":"France",
"retail_rate":"0.0984"
},
{
"prefix":32,
"prefix__destination":"Belgium",
"retail_rate":"0.0744"
},
{
"prefix":39,
"prefix__destination":"Italy",
"retail_rate":"0.0720"
}
]
Response::
[
{
"prefix":34,
"prefix__destination":"Spain",
"retail_rate":"0.0350"
}
]
"""
class Meta:
resource_name = 'voip_rate'
authorization = Authorization()
authentication = BasicAuthentication()
allowed_methods = ['get']
detail_allowed_methods = ['get']
throttle = BaseThrottle(throttle_at=1000, timeframe=3600) # default 1000 calls / hour
def prepend_urls(self):
"""prepend urls"""
return [
url(r'^(?P<resource_name>%s)/$' % (self._meta.resource_name),
self.wrap_view('read'), name="api_read"),
]
def read(self, request=None, **kwargs):
"""API to get voip call rate via dialcode or recipient_phone_no"""
logger.debug('Voip Rate GET API get called')
self.is_authenticated(request)
#check voipplan id for user
voipplan_id = UserProfile.objects.get(user=request.user).voipplan_id
if voipplan_id is None:
error_msg = "User is not attached with voip plan \n"
logger.error(error_msg)
raise BadRequest(error_msg)
dialcode = ''
recipient_phone_no = ''
if 'dialcode' in request.GET:
if request.GET.get('dialcode') != '':
dialcode = request.GET.get('dialcode')
else:
error_msg = "Please enter dialcode\n"
logger.error(error_msg)
raise BadRequest(error_msg)
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_msg = "Please enter recipient_phone_no\n"
logger.error(error_msg)
raise BadRequest(error_msg)
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 self.create_response(request, rate_list)
else:
error_msg = "Not allowed : %s" % recipient_phone_no
logger.error(error_msg)
raise BadRequest(error_msg)
# variables used for sorting
sort_field = ''
sort_order = ''
extension_query = ''
if request.GET.get('sort_field'):
sort_field = request.GET.get('sort_field')
sort_order = request.GET.get('sort_order')
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 + ' ' + sort_order
cursor = connection.cursor()
if dialcode:
try:
dialcode = int(dialcode)
except ValueError:
error_msg = "Wrong value for dialcode !"
logger.error(error_msg)
raise BadRequest(error_msg)
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)
logger.debug('Voip Rate API : result OK 200')
return self.create_response(request, result)