Exemplo n.º 1
0
 def post(self, request, format=None):
     # add a uuid to create request here
     request.data['auction_id'] = str(uuid.uuid4())
     serializer = AuctionSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def put(self, request, auction_id, format=None):
     auction = self.get_object(auction_id)
     # don't want to let user change auction id so make sure they can't
     # overwrite it in the json by overwriting it ourselves
     request.data['auction_id'] = auction_id
     serializer = AuctionSerializer(auction, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def process_single(self, request):

        # we need to do additional checks on some fields that we require when
        # creating an auction. these are allowed to be null in our data models
        # (and errors won't be captured by the serializer) but these fields
        # are required when creating an auction using this class

        required_fields = [
            'type', 'start_time', 'end_time', 'currency', 'quantity'
        ]

        missing = set(required_fields) - request.data.keys()
        if missing:
            return Response({'missing_fields': missing},
                            status=status.HTTP_400_BAD_REQUEST)

        # check if at least one payment type is included
        # TODO: put this list in either .env or a table
        payment_methods = {
            'pay_visa', 'pay_mastercard', 'pay_amex', 'pay_bank_transfer',
            'pay_venmo', 'pay_paypal', 'pay_cash', 'pay_cheque', 'pay_bitcoin'
        }

        input_set = set(request.data.keys())
        chosen_payment_options = payment_methods.intersection(input_set)

        if len(chosen_payment_options) == 0:
            return Response({'message': 'missing payment types'},
                            status=status.HTTP_400_BAD_REQUEST)

        delivery_methods = {'postage', 'delivery', 'collection'}
        chosen_delivery_options = delivery_methods.intersection(input_set)

        if len(chosen_delivery_options) == 0:
            return Response({'message': 'missing delivery options'},
                            status=status.HTTP_400_BAD_REQUEST)

        if 'name' in request.data.keys():
            del request.data['name']

        # add uuids to create request here
        request.data['auction_id'] = str(uuid.uuid4())
        request.data['lot_id'] = str(uuid.uuid4())
        # public_id is stored in django User.username
        request.data['public_id'] = request.user.get_username()
        request.data['multiple'] = False

        auctype = request.data['type'].upper()
        serializer_obj = None
        if auctype in serializer:
            _, serializer_obj = self.get_data_objects(auctype)
        else:
            return Response({'error': 'Unrecognized auction type'},
                            status=status.HTTP_400_BAD_REQUEST)

        lot_serializer = serializer_obj(data=request.data)

        if not lot_serializer.is_valid():
            return Response(lot_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        # just add a single id to lots array
        request.data['lots'] = [request.data['lot_id']]

        #TODO : set active flag to true if start time is now or in past

        delivery_options = DeliveryOptions(
            auction_id=request.data['auction_id'],
            collection='collection' in request.data,
            delivery='delivery' in request.data,
            postage='postage' in request.data)

        payment_options = PaymentOptions(auction_id=request.data['auction_id'],
                                         visa='pay_visa' in request.data,
                                         mastercard='pay_mastercard'
                                         in request.data,
                                         bank_transfer='pay_bank_transfer'
                                         in request.data,
                                         bitcoin='pay_bitcoin' in request.data,
                                         amex='pay_amex' in request.data,
                                         cash='pay_cash' in request.data,
                                         cheque='pay_cheque' in request.data,
                                         venmo='pay_venmo' in request.data,
                                         paypal='pay_paypal' in request.data)

        # we now need to create an auction and add our auction lot to it
        auction_serializer = AuctionSerializer(data=request.data)

        if auction_serializer.is_valid():

            # only save everything together
            try:
                auction_serializer.save()
                lot_serializer.save()
                delivery_options.save()
                payment_options.save()
            except Exception as err:
                logger.error(err)
                return Response(
                    {
                        'message':
                        'ooh err, it didn\'t like that, check ya logs'
                    },
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            return Response(
                {
                    'auction_id': request.data['auction_id'],
                    'lot_id': request.data['lot_id']
                },
                status=status.HTTP_201_CREATED)

        return Response(auction_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
    def process_multi(self, request):

        return Response({'message': 'multi-lot auctions not available yet'},
                        status=status.HTTP_100_CONTINUE)

        # we need to do additional checks on some fields that we require when
        # creating an auction. these are allowed to be null in our data models
        # (and errors won't be captured by the serializer) but these fields
        # are required when creating an auction using this class
        required_fields = [
            'name', 'type', 'start_time', 'end_time', 'currency', 'quantity'
        ]

        missing = set(required_fields) - request.data.keys()
        if missing:
            return Response({'missing_fields': missing},
                            status=status.HTTP_400_BAD_REQUEST)

        # for multi auctions we need to remove the start and end times for auction lots
        start_time = end_time = None
        start_time = request.data['start_time']
        end_time = request.data['end_time']
        del request.data['start_time']
        del request.data['end_time']

        # add uuids to create request here
        request.data['auction_id'] = str(uuid.uuid4())
        # public_id is stored in django User.username
        request.data['public_id'] = request.user.get_username()

        auctype = request.data['type'].upper()
        serializer_obj = None
        if auctype in serializer:
            _, serializer_obj = self.get_data_objects(auctype)
        else:
            return Response({'error': 'Unrecognized auction type'},
                            status=status.HTTP_400_BAD_REQUEST)

        # need to deal with multiple lots here
        lot_serializer = serializer_obj(data=request.data)

        #TODO: multi lots
        if not lot_serializer.is_valid():
            return Response(lot_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        # add back in data for auction
        request.data['start_time'] = start_time
        request.data['end_time'] = end_time
        request.data['multiple'] = True
        request.data['name'] = name

        # TODO: multi lots
        request.data['lots'] = [request.data['lot_id']]

        #TODO : set active flag to true if start time is now or in past

        # we now need to create an auction and add our auction lot to it
        auction_serializer = AuctionSerializer(data=request.data)
        if auction_serializer.is_valid():
            # only save when both are valid
            try:
                auction_serializer.save()
                lot_serializer.save()
            except Exception as err:
                logger.error(err)
                return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            # we know we have successfully saved both lots and auction at this
            # point so we can create the delivery and payment records

            return Response(
                {
                    'auction_id': request.data['auction_id'],
                    'lot_id': request.data['lot_id']
                },
                status=status.HTTP_201_CREATED)

        return Response(auction_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)