示例#1
0
def add_purchase():
    buyer_id = request.json["buyer_id"]
    new_purchase = Purchase(buyer_id=buyer_id, status_id=1)
    # commit new_purchase to database to get purchase id
    db.session.add(new_purchase)
    db.session.commit()

    # This is placeholder code so we can test.
    # cart_items = request.json["cart_items"]
    # return jsonify({
    #     "purchase": {
    #         "buyer_id": buyer_id,
    #         "items": cart_items,
    #         "test": cart_items[0]['qty']
    #     },
    #     "total": 1250
    # })

    cart_items = request.json["cart_items"]
    # add purchase_items to new_purchase
    for item in cart_items:
        new_p_item = P_Item(qty=item['qty'],
                            product_id=item['product_id'],
                            purchase_id=new_purchase.id)
        db.session.add(new_p_item)

    new_purchase.total = new_purchase.set_total()
    db.session.add(new_purchase)
    # commit both purchase item and purchase total to db.
    db.session.commit()

    # return purchase
    resp = purchase_schema.dump(new_purchase)
    return jsonify({'msg': 'Your purchase was received', 'purchase': resp})
示例#2
0
 def test_update_purchase(self):
     p, m, u = self._create_purchase_with_member_with_user()
     self.assertEqual(p.price, self.price)
     new_price = 77
     p.update(price=new_price)
     self.assertEqual(p.price, new_price)
     self.assertEqual(Purchase.get_by_id(p.id).price, new_price)
示例#3
0
 def test_delete_purchase(self):
     """
     Purchase deleted -> Member NOT deleted -> User NOT deleted
     """
     p, m, u = self._create_purchase_with_member_with_user()
     p.delete()
     self.assertEqual(Purchase.query.count(), 0)
     assert not Purchase.get_by_id(p.id) and Member.get_by_id(m.id) and User.get_by_id(u.id)
     self.assertEqual(len(m.purchases), 0)
示例#4
0
 def test_delete_membership(self):
     """
     Member deleted -> Purchases by that member deleted -> User NOT deleted
     """
     p, m, u = self._create_purchase_with_member_with_user()
     m.delete()
     self.assertIsNone(Purchase.get_by_id(p.id))
     self.assertIsNone(Member.get_by_id(m.id))
     self.assertIsNotNone(User.get_by_id(u.id))
     self.assertEqual(u.members, [])
示例#5
0
 def _create_purchase_with_member_with_user(self):
     u = User.create(account_id=self.account_id, secret_key=self.secret_key)
     m = Member.create(email=self.email, account_id=u.account_id)
     p = Purchase.create(title='TEST', price=100, member_id=m.id)
     return [p, m, u]
示例#6
0
def purchase_model(purchase_data, create_user) -> Purchase:
    data = deepcopy(purchase_data)
    return Purchase(**data, user_id=create_user.id)