from .. import db
from datetime import datetime
import pytz
from sqlalchemy import CheckConstraint
from flask.ext.login import current_user


class CartItem(db.Model):
    ''' Functions as association table between listings and merchants '''
    __tablename__ = "cartItems"
    __table_args__ = (
        CheckConstraint('quantity > 0'),
    )
    merchant_id = db.Column(db.Integer, db.ForeignKey('users.id'),
                            primary_key=True)
    listing_id = db.Column(db.Integer, db.ForeignKey('listings.id'),
                           primary_key=True)
    quantity = db.Column(db.Integer)

    listing = db.relationship("Listing")

    @staticmethod
    def delete_cart_items():
        for cart_item in current_user.cart_items:
            db.session.delete(cart_item)
        db.session.commit()


class Status:
    PENDING = 0
    APPROVED = 1
    DECLINED = 2


class Order(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True)

    date = db.Column(db.DateTime)
    status = db.Column(db.Integer)

    def __init__(self, cart_items):
        self.date = datetime.now(pytz.timezone('US/Eastern'))
        self.status = Status.PENDING

        for item in cart_items:
            vendor_id = item.listing.vendor_id
            listing_id = item.listing.id
            quantity = item.quantity
            item_name = item.listing.name
            item_price = item.listing.price
            p = Purchase(vendor_id, listing_id, self, quantity, item_name, item_price)
            db.session.add(p)

        db.session.commit()

    def __repr__(self):
        return "<Order: {}>".format(self.id)



class Purchase(db.Model):
    __tablename__ = 'purchases'
    id = db.Column(db.Integer, primary_key=True)

    # model relationships
    vendor_id = db.Column(db.Integer)
    order_id = db.Column(db.Integer, db.ForeignKey('orders.id'))
    order = db.relationship("Order", backref="purchases")
    listing_id = db.Column(db.Integer)

    # purchase properties
    quantity = db.Column(db.Integer)
    item_name = db.Column(db.String(64))
    item_price = db.Column(db.Float)

    def __init__(self, vendor_id, listing_id, order, quantity, item_name, item_price):
        self.vendor_id = vendor_id
        self.listing_id = listing_id
        self.order = order
        self.quantity = quantity
        self.item_name = item_name
        self.item_price = item_price


    def __repr__(self):
        return "<Purchase: {} Listing: {}>".format(self.id, self.listing_id)

@@ -1151,45 +1151,8 @@
 n'))
-%0A        self.status = Status.PENDING
 %0A%0A  
@@ -1458,32 +1458,57 @@
 tity, item_name,
+%0A                        
  item_price)%0A   
@@ -1634,16 +1634,494 @@
 lf.id)%0A%0A
+    def getAllPurchases(self):%0A        return Purchase.query.filty_by(order_id=self.id)%0A%0A    def getPurchasesByVendor(self, vendor_id):%0A        purchases = Purchase.query.filter_by(order_id=self.id,%0A                                             vendor_id=vendor_id).all()%0A%0A        return purchases%0A%0A    def setStatusByVendor(self, vendor_id, status):%0A        for purchase in self.getPurchasesByVendor(vendor_id):%0A            purchase.status = status%0A%0A        db.session.commit()%0A
 %0A%0Aclass 
@@ -2218,24 +2218,60 @@
 _key=True)%0A%0A
+    status = db.Column(db.Integer)%0A%0A
     # model 
@@ -2704,21 +2704,75 @@
 ame,
- item_price):
+%0A                 item_price):%0A        self.status = Status.PENDING
 %0A   
@@ -2931,24 +2931,24 @@
 = item_name%0A
+
         self
@@ -2973,17 +2973,16 @@
 _price%0A%0A
-%0A
     def 

Put status in Purchase instead of Order. Added a getPurchasesByVendor method to Order