def remove_payment(request, payment_id): if request.method == 'DELETE': with connect() as conn: dbpayments = Payments(conn) dbpayments.remove(payment_id) return HttpResponse(status=200) return HttpResponse(status=400)
def get_payments(request): if request.method == 'GET': with connect() as conn: dbpayments = Payments(conn) payments = dbpayments.readall() return JsonResponse(payments, safe=False, encoder=ModelsJSONEncoder, json_dumps_params={'indent': 2}) return HttpResponse(status=400)
def search_payment(request): if request.method == 'GET': with connect() as conn: dbpayments = Payments(conn) salarymaxrange = request.GET.get('max', None) salaryminrange = request.GET.get('min', None) payments = dbpayments.search(salarymaxrange, salaryminrange) return JsonResponse(payments, safe=False, encoder=ModelsJSONEncoder) return HttpResponse(status=400)
def update_payment(request, payment_id): if request.method == 'PUT': try: payment = _req_body_to_payment(request) except (TypeError, KeyError) as e: print(e) return HttpResponse(status=400) with connect() as conn: dbpayments = Payments(conn) dbpayments.update(payment, payment_id) return HttpResponse(status=200) return HttpResponse(status=400)
def add_payment(request): if request.method == 'POST': try: payment = _req_body_to_payment(request) except (TypeError, KeyError) as e: print(e) return HttpResponse(status=400) with connect() as conn: dbpayments = Payments(conn) dbpayments.add(payment) return HttpResponse(status=200) return HttpResponse(status=400)
def import_payments(request): if request.method == 'POST': req_body = request.FILES['file'].read() payments_dict = simplexml.loads(req_body)['payments'] payments = [ Payment(0, p['method'], p['raw_salary'], p['tax_rate']) for p in payments_dict ] with connect() as conn: dbpayments = Payments(conn) dbpayments.importall(payments) return HttpResponse(status=200) return HttpResponse(status=400)
def export_payments(request): with connect() as conn: dbpayments = Payments(conn) payments = dbpayments.readall() return export(request, payments, 'payments')