@@ -257,3 +257,101 @@ def create_payment(request):
257
257
258
258
response = JsonResponse (post_params )
259
259
return response
260
+
261
+
262
+ @staff_member_required
263
+ def backup_to_csv (request ):
264
+ data = {}
265
+ data ['referral' ] = ReferralPartner
266
+ data ['user' ] = UserProfile
267
+ data ['exchange' ] = CryptoExchange
268
+ data ['payments' ] = UserPayments
269
+ data ['policy' ] = InsurancePolicy
270
+ data ['case' ] = InsuranceCase
271
+ data ['additional' ] = AdditionalData
272
+ cursor = connection .cursor ()
273
+ cursor .execute ('''SELECT insurance_policy.id AS Policy_number,
274
+ insurance_policy.request_date AS Policy_date,
275
+ user_profile.first_name AS First_name,
276
+ user_profile.last_name AS Last_name,
277
+ user_profile.email AS Email,
278
+ insurance_policy.start_date AS Start_date,
279
+ insurance_policy.expiration_date AS Expiration_date,
280
+ insurance_policy.expiration_date - \
281
+ insurance_policy.start_date AS Number_of_days,
282
+ crypto_exchange.name AS Crypto_exchange_name,
283
+ crypto_exchange.coverage_limit AS Limit_BTC,
284
+ insurance_policy.cover AS Insured_Limit,
285
+ insurance_policy.fee AS Premium_paid,
286
+ user_payments.amount AS User_paid,
287
+ user_payments.currency AS User_currency,
288
+ crypto_exchange.rate AS Premium_rate,
289
+ user_payments.update_date AS Premium_payment_date,
290
+ insurance_case.loss_value AS Outstanding_claim_BTC,
291
+ insurance_case.incident_date AS Date_of_claim,
292
+ insurance_case.refund_paid AS Paid_claim_BTC,
293
+ insurance_case.request_date AS Date_of_claim_payment,
294
+ insurance_policy.status AS Insurance_policy_status,
295
+ user_payments.status AS User_payments_status,
296
+ insurance_case.status AS Insurance_case_status
297
+ FROM insurance_policy
298
+ LEFT JOIN user_profile ON user_profile.id = \
299
+ insurance_policy.user
300
+ LEFT JOIN crypto_exchange ON crypto_exchange.id = \
301
+ insurance_policy.exchange
302
+ LEFT JOIN user_payments ON user_payments.id = \
303
+ insurance_policy.payment_id
304
+ LEFT JOIN insurance_case ON \
305
+ insurance_case.insurance = insurance_policy.id
306
+ ''' )
307
+ insurance_report = cursor .fetchall ()
308
+
309
+ if request .method == 'GET' :
310
+ datasets = {}
311
+ datasets ['referral' ] = not bool (request .GET .get ('referral' ))
312
+ datasets ['user' ] = not bool (request .GET .get ('user' ))
313
+ datasets ['exchange' ] = not bool (request .GET .get ('exchange' ))
314
+ datasets ['payments' ] = not bool (request .GET .get ('payments' ))
315
+ datasets ['policy' ] = not bool (request .GET .get ('policy' ))
316
+ datasets ['case' ] = not bool (request .GET .get ('case' ))
317
+ datasets ['additional' ] = not bool (request .GET .get ('additional' ))
318
+ response = HttpResponse (content_type = 'application/zip' )
319
+ response ['Content-Disposition' ] = 'attachment; filename=backup.csv.zip'
320
+ z = zipfile .ZipFile (response , 'w' )
321
+ for key in datasets :
322
+ if datasets [key ] is True :
323
+ output = StringIO ()
324
+ writer = csv .writer (output , dialect = 'excel' )
325
+ query = data [key ].objects .all ().values ()
326
+ if query .count () > 0 :
327
+ keys = list (query [0 ])
328
+ writer .writerow (sorted (keys ))
329
+ for row in query :
330
+ writer .writerow ([row [k ] for k in sorted (keys )])
331
+ else :
332
+ writer .writerow (['NULL TABLE' ])
333
+ z .writestr ("%s.csv" % key , output .getvalue ())
334
+
335
+ out = StringIO ()
336
+ writer = csv .writer (out )
337
+ header = [
338
+ 'Policy_number' , 'Policy_date' , 'Name' , 'Surname' , 'E-mail' ,
339
+ 'Policy_start_date' , 'Policy_expiry_date' , 'Number_of_days' ,
340
+ 'Crypto_exchange_name' , 'Limit_BTC' , 'Insured_Limit' , 'Premium_paid_BTC' ,
341
+ 'User_paid' , 'User_currency' , 'Premium_rate_%' ,
342
+ 'Premium_payment_date' , 'Outstanding_claim_BTC' , 'Date_of_claim' ,
343
+ 'Paid_claim_BTC' , 'Date_of_claim_payment' ,
344
+ 'Insurance_policy_status' , 'User_payments_status' ,
345
+ 'Insurance_case_status'
346
+ ]
347
+
348
+ writer .writerow (header )
349
+ for row in insurance_report :
350
+ writer .writerow (row )
351
+ z .writestr ("insurance_report.csv" , out .getvalue ())
352
+ try :
353
+ if not z .testzip ():
354
+ responseData = {'error' : True , 'message' : 'Nothing to backup' }
355
+ return JsonResponse (responseData )
356
+ except Exception :
357
+ return response
0 commit comments