def delete(self, customer_id): """ deletes an email query_parameters: is_primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 deleted_email = customer.delete_email( email_to_delete=request.json.get("email")) if deleted_email is None: response = { "result": "not found", "customer": customer_obj(customer) } return jsonify(response), 404 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 204
def put(self, customer_id): """ marks email address as primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 new_primary_email = customer.make_email_primary( new_primay_email=request.json.get("email")) if new_primary_email is None: response = { "result": "not found", "customer": customer_obj(customer) } return jsonify(response), 404 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 200
def post(self, customer_id): """ creates a new email query_parameters: is_primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) is_primary = False if request.args.get("is_primary") is not None: if request.args.get("is_primary").lower() == "true": is_primary = True if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 try: customer.add_email(new_email=request.json.get("email"), is_primary=is_primary) except DuplicateDataError: response = { "result": "already exists", "customer": customer_obj(customer) } return jsonify(response), 303 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 201
def put(self, customer_id, address_id): """ switch primary address :param customer_id: customer to update :param address_id: address to become primary :return: address object """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 address = customer.get_addresses().filter( address_id=address_id).first() if address is None: return jsonify({"error": ADDRESS_NOT_FOUND}), 404 address.make_primary() response = {"result": "ok", "address": address_obj(address)} return jsonify(response), 200
def post(self, customer_id): """ creates a new customer address. Overrides primary if is_primary included in request :param customer_id: customer whose address to be added to :return: address object """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 error = best_match( Draft4Validator(address_schema).iter_errors(request.json)) if error: return jsonify({"error": error.message}), 400 if request.json.get("is_primary") is not None: if request.json.get("is_primary").lower() == "true": response = { "result": "ok", "address": address_obj( customer.add_address(request=request, is_primary=True)) } return jsonify(response), 201 return jsonify( address_obj(customer.add_address(request=request, is_primary=False))), 201
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
def post(self, customer_id): """ bills the customers current open cart --Bills the current open cart for customer --Create Invoice Record and Invoice Line Item Records --Automatically applies giftcard and credits --closes cart as 'billed' --store.credit_order_preference: determines if credits are giftcards are applied first """ customer = Customer.get_customer(customer_id=customer_id, request=request) store = Store.objects.filter(app_id=request.headers.get('APP-ID'), deleted_at=None).first() if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 cart = Cart.objects.filter(customer_id=customer_id, invoice_created_at=None, closed_at=None).first() if cart is None: return jsonify({"error": NO_OPEN_CART}), 404 invoice = Invoice( invoice_id=str(uuid.uuid4().int), customer=customer, cart=cart ).save() invoice.create_invoice_line_items() if "coupon" in request.args: coupon = CouponCode.objects.filter(code=request.args.get("coupon"), store=store).first() if coupon is None: return jsonify({"error": "coupon code not found"}), 404 redemption = coupon.redeem(invoice=invoice) if redemption is None: return jsonify({"error": "coupon code not found"}), 404 if store.credit_order_preference == "credit": self.apply_credits(customer, invoice) self.apply_gift_cards(customer, invoice) else: self.apply_gift_cards(customer, invoice) self.apply_credits(customer, invoice) cart.state = "billed" cart.invoice_created_at = datetime.now() cart.save() response = { "result": "ok", "invoice": invoice_obj(invoice) } return jsonify(response), 201
def get(self, customer_id): """ returns extended customer object :return: customer snapshot object """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 obj = customer_obj(customer=customer) cart = Cart.objects.filter(customer_id=customer.customer_id, closed_at=None).first() if cart: obj['cart'] = cart_obj(cart) invoices = Invoice.objects.filter( customer=customer).order_by("-created_at").paginate(page=1, per_page=10) if invoices: obj["last_10_invoices"] = invoice_objs(invoices) orders = Order.get_orders(request=request, customer_id=customer_id).order_by("-created_at")\ .paginate(page=1, per_page=10) if orders: obj["last_10_orders"] = order_objs(orders) gift_cards = GiftCard.objects.filter(recipient_customer=customer, current_balance_in_cents__gt=0)\ .order_by("-created_at").paginate(page=1, per_page=10) if gift_cards: obj["active_giftcards"] = gift_card_objs(gift_cards) credits = Credit.objects.filter(customer=customer, current_balance_in_cents__gt=0) \ .order_by("-created_at").paginate(page=1, per_page=10) if credits: obj["active_credits"] = credit_objs(credits) products = Invoice.get_top_N_products(customer=customer, num_items=10, request=request) if products: obj["top_10_ordered_products"] = products response = {"result": "ok", "customer": obj} return jsonify(response), 200
def put(self, customer_id): """ sends the customer confirmation email :return: confirmation """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 customer.send_confirmation() return jsonify({"result": customer.confirmation_token}), 200
def delete(self, customer_id, credit_id): """ voids a credit :param credit_id: credit to be voided :param customer_id: customer whose credit is to be voided """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 credit = Credit.objects.filter(customer=customer, credit_id=credit_id).first() if credit is None: return jsonify({"error": CREDIT_NOT_FOUND}), 404 credit.void_credit() return jsonify({}), 204
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
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
def delete(self, customer_id, address_id): """ deletes an address :param customer_id: customer to be updated :param address_id: address to be delete :return: null """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 address = customer.get_addresses().filter( address_id=address_id).first() if address is None: return jsonify({"error": ADDRESS_NOT_FOUND}), 404 address.delete() return jsonify({}), 204