Esempio n. 1
0
    def get(self, invoice_id=None):
        """
        returns an invoice number or list of invoice numbers

        params:
        closed: true if want to include closed invoices
        startdate: date range filter
        enddate: date range filter
        """
        if invoice_id:
            invoice = Invoice.objects.filter(invoice_id=invoice_id).first()
            store = Store.objects.filter(app_id=request.headers.get('APP-ID'), deleted_at=None).first()

            if invoice is None:
                return jsonify({"error": INVOICE_NOT_FOUND}), 404

            if invoice.customer.store_id != store:
                return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

            response = {
                "result": "ok",
                "invoice": invoice_obj(invoice)
            }
            return jsonify(response), 200
        else:
            try:
                invoices = Invoice.get_all_invoices(request=request)
            except IncorrectDateFormat:
                return jsonify({"error": INCORRECT_TIME_FORMAT})

            return paginated_results(objects=invoices, collection_name='invoice', request=request
                                     , per_page=self.PER_PAGE, serialization_func=invoice_objs), 200
Esempio n. 2
0
    def get(self, query):
        query = query.replace("_", " ")

        try:
            search_results, score_list = Product.search(expression=query,
                                                        max=100)
        except TypeError:
            return jsonify({}), 404

        if search_results is None:
            return jsonify({}), 404

        if "available" in request.args:
            if request.args.get("available").lower() == "true":
                search_results = search_results.filter(inventory__gt=0)

        results = paginated_results(objects=search_results,
                                    collection_name='product',
                                    request=request,
                                    per_page=self.PER_PAGE,
                                    serialization_func=products_obj,
                                    dictionary=True)

        for product in results["products"]:
            for id in score_list:
                if product["product_id"] == id[0]:
                    product["search_score"] = id[1]

        results["products"] = sorted(
            results["products"],
            key=lambda product: product["search_score"],
            reverse=True)

        return jsonify(results), 200
Esempio n. 3
0
    def get_top_N_products(cls, customer, num_items, request):
        """
        returns the top 10 products purchased by customer

        :param customer: customer of note
        :return: Produce query object
        """
        invoices = Invoice.objects.filter(customer=customer, state="collected")
        invoice_line_items = InvoiceLineItem.objects.filter(invoice__in=invoices).all()

        products = {}
        for invoice_line_item in invoice_line_items:
            if invoice_line_item.product.product_id in products.keys():
                products[invoice_line_item.product.product_id] += invoice_line_item.quantity
            else:
                products[invoice_line_item.product.product_id] = invoice_line_item.quantity

        product_counts = dict(Counter(products).most_common(num_items))

        results = Product.objects.filter(product_id__in=product_counts.keys())
        results = paginated_results(objects=results, collection_name='product', request=request
                                    , per_page=10, serialization_func=products_obj, dictionary=True)

        for product in results["products"]:
            product["num_ordered"] = product_counts[product["product_id"]]

        return sorted(results["products"], key=lambda product: product["num_ordered"], reverse=True)
Esempio n. 4
0
    def get(self, customer_id):
        """
        returns a list of customer gift cards
        """
        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        customer = Customer.objects.filter(customer_id=customer_id,
                                           store_id=store,
                                           deleted_at=None).first()

        if customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        gift_cards = GiftCard.objects.filter(recipient_customer=customer)

        if "active" in request.args:
            if request.args['active'].lower() == 'true':
                gift_cards = gift_cards.filter(current_balance_in_cents__gt=0)

        return paginated_results(objects=gift_cards,
                                 collection_name='gift_card',
                                 request=request,
                                 per_page=self.PER_PAGE,
                                 serialization_func=gift_card_objs), 200
Esempio n. 5
0
    def get(self, customer_id):
        """
        return primary address or list of all addresses

        :param customer_id: to return list of addresses for
        :return: addresses
        """
        customer = Customer.get_customer(customer_id=customer_id,
                                         request=request)

        if customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        if request.args.get("is_primary"):
            if request.args.get("is_primary").lower() == "true":
                response = {
                    "result": "ok",
                    "address": address_obj(customer.get_primary_address())
                }
                return jsonify(response), 200

        return paginated_results(
            objects=customer.get_addresses(),
            collection_name='address',
            request=request,
            per_page=self.PER_PAGE,
            serialization_func=addresses_obj_for_pagination), 200
Esempio n. 6
0
    def get(self, gift_card_id=None):
        """
        returns a gift_card object of list of gift_card objects
        """
        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        if gift_card_id:
            gift_card = GiftCard.objects.filter(
                gift_card_id=gift_card_id).first()

            if gift_card is None:
                return jsonify({}), 404

            if gift_card.recipient_customer.store_id != store:
                return jsonify({}), 404

            response = {"result": "ok", "gift_card": gift_card_obj(gift_card)}
            return jsonify(response), 200
        else:
            gift_cards = GiftCard.objects()

            if "giftercustomerid" not in request.args and "recipientcustomerid" not in request.args:
                return jsonify({"error": MISSING_PARAMETERS}), 400

            if "giftercustomerid" in request.args:
                gifter_customer = Customer.objects.filter(
                    customer_id=request.args['giftercustomerid'],
                    store_id=store,
                    deleted_at=None).first()
                if gifter_customer is None:
                    return jsonify({"error": CUSTOMER_NOT_FOUND}), 404
                gift_cards = gift_cards.filter(gifter_customer=gifter_customer)

            if "recipientcustomerid" in request.args:
                recipient_customer = Customer.objects.filter(
                    customer_id=request.args['recipientcustomerid'],
                    store_id=store,
                    deleted_at=None).first()

                if recipient_customer is None:
                    return jsonify({"error": CUSTOMER_NOT_FOUND}), 404
                gift_cards = gift_cards.filter(
                    recipient_customer=recipient_customer)

            if "active" in request.args:
                if request.args['active'].lower() == 'true':
                    gift_cards = gift_cards.filter(
                        current_balance_in_cents__gt=0)

            return paginated_results(objects=gift_cards,
                                     collection_name='gift_card',
                                     request=request,
                                     per_page=self.PER_PAGE,
                                     serialization_func=gift_card_objs), 200
Esempio n. 7
0
    def get(self, customer_id):
        """
        return a list of invoice for customer

        params:
        closed: true if want to include closed invoices
        startdate: date range filter
        enddate: date range filter
        """
        try:
            invoices = Invoice.get_all_invoices(request=request)
        except IncorrectDateFormat:
            return jsonify({"error": INCORRECT_TIME_FORMAT})

        customer = Customer.get_customer(customer_id=customer_id, request=request)

        if customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        invoices = invoices.filter(customer=customer_id)

        return paginated_results(objects=invoices, collection_name='invoice', request=request
                                 , per_page=self.PER_PAGE, serialization_func=invoice_objs), 200
Esempio n. 8
0
    def get(self, customer_id):
        """
        returns a list of customer credits

        params:
        active (true only includes credits with balance more that zero)
        """
        customer = Customer.get_customer(customer_id=customer_id,
                                         request=request)

        if customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        credits = Credit.objects.filter(customer=customer)

        if "active" in request.args:
            if request.args['active'].lower() == 'true':
                credits = credits.filter(current_balance_in_cents__gt=0)

        return paginated_results(objects=credits,
                                 collection_name='credit',
                                 request=request,
                                 per_page=self.PER_PAGE,
                                 serialization_func=credit_objs), 200
Esempio n. 9
0
    def get(self, customer_id=None):
        """
        Gets a customer by providing customer_id

        Endpoint: /customer/209485626598208918917359936593901836672

        Example Response:

        {
            "customer": {
                "addresses": [
                    {
                        "address_id": "160725188092123457335884996198595450510",
                        "city": "townsville",
                        "created_at": "Sun, 13 Jan 2019 19:48:27 GMT",
                        "deleted_at": null,
                        "is_primary": true,
                        "state": "CA",
                        "street": "1236 Main Street",
                        "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT",
                        "zip": "1234"
                    }
                ],
                "created_at": "Sun, 13 Jan 2019 19:48:27 GMT",
                "currency": "USD",
                "customer_id": "209485626598208918917359936593901836672",
                "deleted_at": null,
                "email": "*****@*****.**",
                "first_name": "John",
                "last_name": "Smith4",
                "last_order_date": null,
                "links": [
                    {
                        "href": "/customer/209485626598208918917359936593901836672",
                        "rel": "self"
                    }
                ],
                "total_spent": "0.00",
                "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT"
            },
            "result": "ok"
        }
        """

        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        if customer_id:
            customer = Customer.objects.filter(customer_id=customer_id,
                                               deleted_at=None,
                                               store_id=store).first()
            if customer:
                response = {"result": "ok", "customer": customer_obj(customer)}
                return jsonify(response), 200
            else:
                return jsonify({}), 404
        else:
            customers = Customer.objects.filter(store_id=store,
                                                deleted_at=None)
            return paginated_results(objects=customers,
                                     collection_name='customer',
                                     request=request,
                                     per_page=self.PER_PAGE,
                                     serialization_func=customer_objs), 200