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)
 def get(self, request):
     if request.GET.get('term', '') != '':
         # Get auctions that match the search term
         term = request.GET['term'].lower().strip()
         auctions = AuctionModel.objects.filter(status='Active',
                                                title__contains=term)
         auctions_serialized = AuctionSerializer(auctions, many=True)
         return Response(auctions_serialized.data)
     else:
         # Get all auctions
         auctions = AuctionModel.objects.filter(status='Active')
         auctions_serialized = AuctionSerializer(auctions, many=True)
         return Response(auctions_serialized.data)
Exemplo n.º 4
0
def auction_checkcart(request):
    if request.method == 'GET':
        auctions = Auction.objects.filter(valid=True,
                                          exp_time__lt=datetime.now())

        serializerAuction = AuctionSerializer(auctions, many=True)

        for auction in auctions:
            itemId = auction.product_id
            lastest_price = auction.lastest_price

            try:
                cart = Cart.objects.get(customer_id=auction.customer_id)
            except:
                cart = Cart(customer_id=auction.customer_id,
                            time=datetime.now())
                cart.save()

            cartProduct = CartProduct(cart_id=cart.id,
                                      product_id=itemId,
                                      price=lastest_price,
                                      quantity=1,
                                      time=datetime.now())

            cartProduct.save()

            Auction.objects.filter(pk=auction.id).update(valid=False)

        return JsonResponse(serializerAuction.data, status=200, safe=False)
Exemplo n.º 5
0
    def get(self, request, auction_id, format=None):

        auction = self.get_object(auction_id)
        auction_serializer = AuctionSerializer(auction)
        auction_lot_serializer = self.get_lots(auction.type, auction.lots)

        auc_stuff = auction_serializer.data
        auc_stuff['lots'] = auction_lot_serializer.data

        return Response({'auction': auc_stuff}, status=status.HTTP_200_OK)
 def get(self, request, auction_id):
     # Get the auction with matching id. If none is found .filter() returns empty query set
     if AuctionModel.objects.filter(
             id=auction_id).exists() and AuctionModel.objects.filter(
                 id=auction_id, status='Active'):
         auction = AuctionModel.objects.get(id=auction_id)
         auction_serialized = AuctionSerializer(auction)
         return Response(auction_serialized.data)
     else:
         error_dict = {
             'message':
             'No active auctions with id=' + str(auction_id) + ' found.'
         }
         return JsonResponse(error_dict)
Exemplo n.º 7
0
    def get(self, request, item_id, format=None):

        auction_lot = self.get_object(item_id)
        auction = self.get_auction(auction_lot.lot_id)
        auction_serializer = AuctionSerializer(auction)
        auction_lot_serializer = self.get_lots(auction.type, auction.lots)

        payment_options = self.get_payments(auction.auction_id)
        pay_options_serializer = PaymentOptionsSerializer(payment_options)

        delivery_options = self.get_delivery_options(auction.auction_id)
        deliv_opts_serializer = DeliveryOptionsSerializer(delivery_options)

        auc_stuff = auction_serializer.data
        auc_stuff['lots'] = auction_lot_serializer.data
        auc_stuff['payment_options'] = pay_options_serializer.data
        auc_stuff['delivery_options'] = deliv_opts_serializer.data

        return Response({'auction': auc_stuff}, status=status.HTTP_200_OK)
Exemplo n.º 8
0
def product_detail(request, pk):
    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializerProduct = ProductSerializer(product)
        seller = Customer.objects.get(pk=product.owner_id)
        serializerSeller = CustomerSerializer(seller)
        attribute = ProductAttribute.objects.filter(product_id=product.id)
        serializerAttibute = ProductAttributeSerializer(attribute, many=True)
        image = ProductImage.objects.filter(product_id=product.id)
        serializerImage = ProductImageSerializer(image, many=True)

        data = serializerProduct.data
        data['seller'] = serializerSeller.data
        data['attributes'] = serializerAttibute.data
        data['images'] = serializerImage.data

        if product.auction == 1:
            auction = Auction.objects.get(product_id=pk)
            serializerAuction = AuctionSerializer(auction)
            data['auction'] = serializerAuction.data

        return JsonResponse(data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ProductSerializer(product, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        product.delete()
        return HttpResponse(status=204)
Exemplo n.º 9
0
def product_auction(request):
    if request.method == 'GET':
        product = Product.objects.all().filter(auction=1)
        serializer = ProductSerializer(product, many=True)
        data = {}
        data['data'] = serializer.data

        for item in data['data']:
            productId = item['productId']

            auction = Auction.objects.get(product_id=productId)
            serializerAuction = AuctionSerializer(auction)
            item['auction'] = serializerAuction.data

        return JsonResponse(data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ProductSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
 def get(self, request, title):
     auctions = AuctionModel.objects.filter(status='Active',
                                            title__contains=title)
     auctions_serialized = AuctionSerializer(auctions, many=True)
     return Response(auctions_serialized.data)
 def get(self, request):
     # Get all auctions
     auctions = AuctionModel.objects.filter(status='Active')
     auctions_serialized = AuctionSerializer(auctions, many=True)
     return Response(auctions_serialized.data)
Exemplo n.º 12
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.º 13
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)
Exemplo n.º 14
0
    def get(self, request, format=None):

        queryset = Auction.objects.all()
        serializer = AuctionSerializer(queryset, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 15
0
 def get(self, request, pk):
     auct = get_object_or_404(Auction, pk=pk)
     serializer = AuctionSerializer(auct)
     return Response(serializer.data)