-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapiviews.py
310 lines (209 loc) · 10 KB
/
apiviews.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from djangorestframework.views import ModelView, View;
from djangorestframework.mixins import ReadModelMixin, InstanceMixin
from loans.models import *
import datetime;
class InstanceReadOnlyModelView(InstanceMixin, ReadModelMixin, ModelView):
"""A view which provides default operations for read against a model instance."""
_suffix = 'Instance'
class ActiveLoans(View):
"""
This is an API function which takes customer id as the parameter and returns all the active loans of that particular customer.
Format of URL: http://localhost:8000/api/activeLoan/<customer_id>/
Examples: http://localhost:8000/api/activeLoan/1/
"""
def get(self, request, cust_id):
l = Loan.objects.filter(customer=cust_id)
ActiveLoansList = []
for lo in l:
ActiveLoansList.extend(ActiveLoan.objects.filter(loan=lo))
print ActiveLoansList
result=[]
for loan in ActiveLoansList:
result.append(loan.serialize()); #serialize all the payment objects so that they can be converted to JSON
return result #convert to JSON
class PaymentsBetween(View):
"""
This is an API function which takes customer id, start date (optional) and end date (optional) and returns a JSON object containing a list of all the payments made by the customer between start and end date.
If start data is missing, it returns all payments made BEFORE the end date.
If end date is missing, it returns all payments made AFTER start date.
If both are missing, it returns all payments made by the customer.
Format of URL: http://localhost:8000/api/paymentsBetween/<customer_id>/<start_date>/<end_date>
<start_date>: s<date>
<end_date>: e<date>
<date>:ddmmyyyy
<start_date> and <end_date> are optional
Examples:
http://localhost:8000/api/paymentsBetween/1
http://localhost:8000/api/paymentsBetween/1/s17112011
http://localhost:8000/api/paymentsBetween/1/e19112011
http://localhost:8000/api/paymentsBetween/1/s17112011/e19112011
Note: Don't reverse the order of <start_date> and <end_date>
"""
# start and end are in ddmmyyyy format
def get(self, request, cust_id, start=None, end=None):
# Get all the loans for the customer using the customer_id
loansList = Loan.objects.filter(customer=cust_id)
#parse the url to get start date and end date
def parse(datearg):
return datetime.date(int(datearg)%10000, (int(datearg)/10000)%100, (int(datearg)/1000000))
if (start != None):
start = parse(start)
if (end != None):
end = parse(end)
paymentsList = []
#loop over all the loans of the customer and get payments for every loan
if (start == None and end == None):
for l in loansList:
paymentsList += Payment.objects.filter(loan = l) #add the list of payments corresponding to every loan to paymentsList
elif (start != None and end == None):
for l in loansList:
paymentsList += Payment.objects.filter(loan = l).exclude(datePaid__lt=start) #excludes payments made BEFORE the start date
elif (start == None and end != None):
for l in loansList:
paymentsList += Payment.objects.filter(loan = l).exclude(datePaid__gt=end) #excludes payments made AFTER the end date
else:
for l in loansList:
paymentsList += Payment.objects.filter(loan = l).exclude(datePaid__gt=end).exclude(datePaid__lt=start) #excludes payments made before the start date and after the end date
result=[]
for payment in paymentsList:
result.append(payment.serialize()); #serialize all the payment objects so that they can be converted to JSON
return result
class PaymentHistoryOfLoan(View):
"""
This class takes a customer id and loan name and returns all the payments corresponding to that loan
Format of URL: http://localhost:8000/api/paymentHistoryOfLoan/<customer_id>/<loan_name>
Examples: http://localhost:8000/api/paymentHistoryOfLoan/1/NewTestLoan1
"""
def get(self, request, lname, cust_id):
l = Loan.objects.filter(customer=cust_id, name=lname) #get all loans with the given customer id and name
paymentsList = Payment.objects.filter(loan = l) #get all the payments associated with that loan
result = []
for payment in paymentsList:
result.append(payment.serialize()) #serialize all the payment objects
return result
class Defaulters(View):
"""
This class defines customers with more than 2 overdue installments as defaulters and returns a list of all such customers.
Format of URL: http://localhost:8000/api/defaulters
"""
def get(self, request):
overdue = OverdueInstallment.objects.all() #get all the overdue installments
users = []
for ins in overdue:
users.append(ins.loan.customer) #for every overdue installment, get the associated loan and the customer associated with that loan
defaulters = []
for user in users:
if (users.count(user) > 2): #condition for being a defaulter
defaulters.append(user)
result = []
for defaulter in defaulters:
result.append(defaulter) #serialize all the customer objects
return result
class PaymentHistoryAllLoans(View):
"""
Format of URL: http://localhost:8000/api/paymentHistoryAllLoans/<customer_id>
Example: http://localhost:8000/api/paymentHistoryAllLoans/1
"""
def get(self, request, cust_id):
# Get all the loans for the customer using the customer_id
loansList = Loan.objects.filter(customer=cust_id)
#the list to be returned
paymentsList = []
#loop over all the loans of the customer and get payments for every loan
for l in loansList:
paymentsList += Payment.objects.filter(loan = l) #add the list of payments corresponding to every loan to paymentsList
result = []
for payment in paymentsList:
result.append(payment.serialize()) #serialize
return result
class MonthlyInstallment(View):
"""
This class gives all the monthly installments of all the loans of a given customer.
Format of URL: http://localhost:8000/api/monthlyInstallment/<customer_id>
Example: http://localhost:8000/api/monthlyInstallment/1
"""
def get(self, request, cust_id):
loansList = Loan.objects.filter(customer=cust_id, isActive=True) #get active loan objects
result = []
for l in loansList:
activeLoan = ActiveLoan.objects.filter(loan = l) #for every loan, get the corresponding active loan
result.append({'cust_id': l.customer.name, 'loanType': l.loanType, 'MonthlyInstallment': activeLoan[0].monthlyInstallment, 'DueDate': activeLoan[0].nextInstallmentDueDate,}) #serialize
return result
class LoansTakenBetween(View):
"""
This method gives all the loans taken by a given customer between a given (optional) start date and a given (optional) end date.
If start data is missing, it returns all loans taken BEFORE the end date.
If end date is missing, it returns all loans taken AFTER start date.
If both are missing, it returns all loans taken by the customer.
Format of URL: http://localhost:8000/api/loansTakenBetween/<customer_id>/<start_date>/<end_date>
<start_date>: s<date>
<end_date>: e<date>
<date>: ddmmyyyy
<start_date> and <end_date> are optional.
Examples: http://localhost:8000/api/loansTakenBetween/1
http://localhost:8000/api/loansTakenBetween/1/s17112011
http://localhost:8000/api/loansTakenBetween/1/e19112011
http://localhost:8000/api/loansTakenBetween/1/s17112011/e19112011
Note: Don't reverse the order of <start_date> and <end_date>
"""
def get(self, request, cust_id, start=None, end=None):
#get all loans taken by the customer
loansList = Loan.objects.filter(customer=cust_id)
#parse the url to get start date and end date
def parse(datearg):
return datetime.date(int(datearg)%10000, (int(datearg)/10000)%100, (int(datearg)/1000000))
if (start != None):
start = parse(start)
if (end != None):
end = parse(end)
loansListFiltered = []
#filter loans based on start date and end date
if (start == None and end == None):
loansListFiltered = loansList
elif (start != None and end == None):
loansListFiltered = loansList.exclude(dateTaken__lt=start)
elif (start == None and end != None):
loansListFiltered = loansList.exclude(dateTaken__gt=end)
else:
loansListFiltered = loansList.exclude(dateTaken__lt=start, dateTaken__gt=end)
result=[]
for loan in loansListFiltered:
result.append(loan.serialize()); #serialize all the loan objects so that they can be converted to JSON
return result
class LoansWithOverdueInstallments(View):
"""
This class takes a customer_id (optional) and returns all the loans taken by the customer which have overdue installments. It returns all loans with overdue installments in case there is no customer_id.
Format of URL: http://localhost:8000/api/loansWithOverdueInstallments/<customer_id>
Examples: http://localhost:8000/api/loansWithOverdueInstallments
http://localhost:8000/api/loansWithOverdueInstallments/1
"""
def get(self, request, cust_id = None):
#get all the overdue installments
overdueInstallments = OverdueInstallment.objects.all()
loansList = []
#loop over all the overdue installments to get corresponding loans
if (cust_id == None):
for oi in overdueInstallments:
loansList.append(oi.loan)
#filter loans according to customer_id
else:
for oi in overdueInstallments:
if (int(oi.loan.customer.id) == int(cust_id)):
loansList.append(oi.loan)
result = []
for loan in loansList:
result.append(loan.serialize()) #serialize the loan objects
return result
class LoanHistory(View):
"""
This class takes a customer id and returns all the loans taken by that customer.
Format of URL: http://localhost:8000/api/loanHistory/<customer_id>
Example: http://localhost:8000/api/loanHistory/1
"""
def get(self, request, cust_id):
#get all loans taken by the customer
loansList = Loan.objects.filter(customer = cust_id)
result = []
for loan in loansList:
result.append(loan.serialize()) #serialize the loan objects
return result