def add_bill(self, request, pk=None): person = self.get_object() serializer = BillSerializer(data=request.data) if serializer.is_valid(): bill_instance = serializer.save() person.bills.add(bill_instance) person.save() return Response(PersonSerializer(person), status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def add_bill(self, request, pk=None): """ :param request: should contain the data in bill serializer format :param pk: :return: Person Serialized format of person instance and errors otherwise """ person = self.get_object() qr = BillService.prepare_data(request.data) bill_info = BillService.get_info(qr) serializer = BillSerializer(data=bill_info) if serializer.is_valid(): bill_instance = serializer.save() person.bills.add(bill_instance) person.save() return Response(PersonSerializer(person).data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def api_create_bill_view(request): bill_post = Bill(owner_id=request.user) account_user = Account.objects.get(email=request.user) if request.method == 'POST': django_statsd.incr('api.createBill') django_statsd.start('api.createBill.time.taken') serializer = BillSerializer(bill_post, data=request.data) data = {} if serializer.is_valid(): categories_list = serializer.validated_data['categories'] if len(categories_list) != len(set(categories_list)): return Response({'response': "Categories must be unique."}, status=status.HTTP_400_BAD_REQUEST) django_statsd.start('api.createBill.db') bill = serializer.save() django_statsd.stop('api.createBill.db') data['response'] = 'successfully added a new bill.' data['uuid_bill_id'] = bill.uuid_bill_id data['created_ts'] = bill.created_ts data['updated_ts'] = bill.updated_ts data['owner_id'] = account_user.uuid_id data['vendor'] = bill.vendor data['bill_date'] = bill.bill_date data['due_date'] = bill.due_date data['amount_due'] = bill.amount_due data['categories'] = bill.categories data['payment_status'] = bill.payment_status logger.info("POST: Added Bill") django_statsd.stop('api.createBill.time.taken') return Response(data, status=status.HTTP_201_CREATED) logger.error("ERROR: Something Happened: %s", serializer.errors) django_statsd.stop('api.createBill.time.taken') return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)