def student_finance_list(request, tid): ''' List all students of a tenant and add a student into a tenant's database ''' try: tenant = Tenant.objects.get(tenant_id = tid) except Tenant.DoesNotExist: return HttpResponse("The tenant does not exist!", status=400) if request.method == 'GET': students = Student.objects.filter(tenant_id = tid) response = [] for student in students: item = {} item["tenant_id"] = student.tenant_id item["ssn"] = student.ssn item["first_name"] = student.first_name item["last_name"] = student.last_name item["balance"] = student.balance response.append(item) return JsonResponse(response) if request.method == 'POST': data = JSONParser().parse(request) attributes = TenantAttribute.objects.filter(tenant_id=data['tenant_id']) # insert common information for the student student = Student(ssn=data['ssn'], tenant_id=data['tenant_id'], first_name=data['first_name'], last_name=data['last_name'], balance=data['balance']) student.save() # insert extra information of the student(specified by the tenant) print data print attributes for attribute in attributes: print attribute.attribute_name if attribute.attribute_name in data: attribute_value = TenantAttributeValue(student_id=data['ssn'], tenant_id=data['tenant_id'], tenant_attribute_id=attribute.id, attribute_value=data[attribute.attribute_name]) attribute_value.save() return HttpResponse(status=200)
def student_finance(request, tid, ssn): ''' Cause the model of Django. "student_id" in TenantAttributeValue table refers to "ssn"(primary_key) in Student table ''' try: student = Student.objects.get(tenant_id = tid, ssn = ssn) except Student.DoesNotExist: return HttpResponse("The student record does not exist!", status=400) ''' List a student's finance info ''' if request.method == 'GET': response = {}; student = Student.objects.get(tenant_id = tid, ssn = ssn) response['ssn'] = student.ssn response['tenant_id'] = student.tenant_id response['first_name'] = student.first_name response['last_name'] = student.last_name response['balance'] = student.balance # the student's extra attributes(infromation) value_list = TenantAttributeValue.objects.filter(student_id = ssn, tenant_id=tid) for value_item in value_list: # retrieve the name of the attribute through the ID is attribute value item attribute = TenantAttribute.objects.get(id = value_item.tenant_attribute_id) response[attribute.attribute_name] = value_item.attribute_value return JsonResponse(response) ''' Delete a student's finance info ''' if request.method == 'DELETE': student = Student.objects.get(tenant_id = tid, ssn = ssn) student.delete() return HttpResponse(status=200) ''' Update a student's finance info ''' if request.method == 'PUT': # delete the student's record first student = Student.objects.get(tenant_id = tid, ssn = ssn) student.delete() # insert the student's new record into the database data = JSONParser().parse(request) attributes = TenantAttribute.objects.filter(tenant_id=data['tenant_id']) # insert common information for the student student = Student(ssn=data['ssn'], tenant_id=data['tenant_id'], first_name=data['first_name'], last_name=data['last_name'], balance=data['balance']) student.save() # insert extra information of the student(specified by the tenant) print data print attributes for attribute in attributes: print attribute.attribute_name if attribute.attribute_name in data: attribute_value = TenantAttributeValue(student_id=data['ssn'], tenant_id=data['tenant_id'], tenant_attribute_id=attribute.id, attribute_value=data[attribute.attribute_name]) attribute_value.save() return HttpResponse(status=200)