I am working on a donation app. In this app, There was a feature that sends an equal amount of donation money to every employee of the organization now they want to change this feature by sending a customized amount of donation to all or some of the employees.
I have worked on it: Now I am using a for loop which iterates over the list of employees id and gives the amount to each id. But What I want is to pass an object in a for loop, where the object has an id from list of ids and the amount received from payload. I want to keep that id with his specific amount locked in an object
Here is my code now:
class BusinessDonationController(BaseController):
def create(self, request, response, business):
employees =Employment.objects.filter(business=business, active=True)
ids = employees.values_list('id', flat=True)
donation_amount = []
for id in ids:
try:
amount = int(request.payload.get("amount"))
donation_amount.append({
"id" : id,
"amount" : amount
})
except ValueError:
return response.bad_request("Invalid donation amounts, %s, should be in whole dollars" % amount)
return donation_amount
Advertisement
Answer
This Worked for me!!
class BusinessDonationController(BaseController):
def create(self, request, response, business):
business = Business.objects.get(id=business)
employees =Employment.objects.filter(business=business, active=True)
ids = employees.values_list('id', flat=True)
donation_amount = []
try:
employee_id = int(request.payload.get("employee_id"))
amount = int(request.payload.get("amount"))
donation_amount.append({
"employee_id" : employee_id,
"amount" : amount
})
except ValueError:
return response.bad_request("Invalid donation amounts, %s, should be in whole dollars" % amount)
for employee in employees:
if employee.id == employee_id:
credit_account = CreditAccount.objects.create(deposit=deposit, total_amount=amount, current_amount=amount, employment=employee)
if error != '' and error is not None:
return response.bad_request(error)
response.set(**{'success': True})