class User(db.Model): id = db.Column(db.Integer, primary_key=True) is_admin = db.Column(db.Boolean) location = db.Column(db.String(120)) name = db.Column(db.String()) phone = db.Column(db.String(255)) email = db.Column(db.String(), unique=True) notifications = db.Column(db.Boolean) have_their_id = db.Column(db.Boolean) requests = db.relationship('Request', back_populates='user') sockets = db.relationship('Socket', back_populates='user') items = db.relationship('Item', backref='user') def __init__(self, email, is_admin=False, name=None, phone=None): self.email = email self.is_admin = is_admin if name is not None: self.name = name if phone is not None: self.phone = phone self.notifications = False self.have_their_id = False def requires_id(self): for item in self.items: if item.entry.requires_checkout \ or item.entry.requires_lottery: return True return False
class Item(db.Model): id = db.Column(db.Integer, primary_key=True) item_id = db.Column(db.String()) entry_id = db.Column(db.Integer, db.ForeignKey('inventory_entry.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __init__(self, entry, id): self.user = None self.entry = entry self.item_id = id
class Request(db.Model): id = db.Column(db.Integer, primary_key=True) items = db.relationship('RequestItem', backref='request') status = db.Column(db.Enum(RequestStatus)) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) requires_id = db.Column(db.Boolean) requires_lottery = db.Column(db.Boolean) user = db.relationship('User', back_populates='requests') proposal = db.Column(db.String()) def __init__(self, items, user_id, proposal=''): self.status = RequestStatus.SUBMITTED self.items = items self.requires_id = self.check_requires_id() self.requires_lottery = self.check_requires_lottery() self.user_id = user_id self.user = User.query.get(user_id) self.timestamp = datetime.now() self.proposal = proposal def __str__(self): return self.user.email + ' ' + str(self.status) \ + ' ' + ', '.join([str(i) for i in self.items]) def check_requires_id(self): for item in self.items: if (item.entry.item_type == ItemType.LOTTERY or item.entry.item_type == ItemType.CHECKOUT): return True return False def check_requires_lottery(self): for item in self.items: if item.entry.item_type == ItemType.LOTTERY: return True return False
class User(db.Model): id = db.Column(db.Integer, primary_key=True) quill_id = db.Column(db.String(), unique=True) verified_email = db.Column(db.Boolean) verification_token = db.Column(db.String(), unique=True) is_admin = db.Column(db.Boolean) location = db.Column(db.String(120)) name = db.Column(db.String()) phone = db.Column(db.String(255)) email = db.Column(db.String()) password_hash = db.Column(db.String()) notifications = db.Column(db.Boolean) have_their_id = db.Column(db.Boolean) requests = db.relationship('Request', back_populates='user') sockets = db.relationship('Socket', back_populates='user') items = db.relationship('Item', backref='user') def __init__(self, quill_id, email, password_hash, verification_token, is_admin): self.quill_id = quill_id self.email = email self.password_hash = password_hash self.is_admin = is_admin self.name = '' self.location = '' self.phone = '' self.notifications = False self.have_their_id = False self.verified_email = False self.verification_token = verification_token def requires_id(self): for item in self.items: if item.entry.requires_checkout \ or item.entry.requires_lottery: return True return False
class InventoryEntry(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120)) description = db.Column(db.String()) category = db.Column(db.String()) link = db.Column(db.String()) max_request_quantity = db.Column(db.Integer) image_src = db.Column(db.String()) item_type = db.Column(db.Enum(ItemType)) is_visible = db.Column(db.Boolean) is_unique = db.Column(db.Boolean) items = db.relationship('Item', backref='entry') requests = db.relationship('Request', secondary=RequestItem.__table__, lazy='select', viewonly=True) def __init__(self, name, description, link, category, tags, image, qty, item_type = ItemType.FREE, max_request_qty = 3): self.name = name self.description = description self.link = link self.category = category self.tags = tags self.image_src = image self.items = [] for i in xrange(int(qty)): self.items.append( Item(self, self.name + " " + str(i+1))) self.max_request_quantity = max_request_qty self.item_type = item_type self.is_visible = True self.is_unique = False @property def quantity(self): """Returns quantity of items that have not been 'claimed' by a request""" requests = RequestItem.query \ .filter_by(entry_id=self.id) \ .join(hardwarecheckout.models.request.Request) \ .filter_by(status=hardwarecheckout.models.request.RequestStatus.APPROVED) \ .with_entities(func.sum(RequestItem.quantity)).scalar() if not requests: requests = 0 return Item.query.filter_by(entry_id = self.id, user = None).count() - requests @property def submitted_request_quantity(self): """Returns number of submitted requests for this entry""" requests = RequestItem.query \ .filter_by(entry_id=self.id) \ .join(hardwarecheckout.models.request.Request) \ .filter_by(status=hardwarecheckout.models.request.RequestStatus.SUBMITTED).count() return requests @property def requires_checkout(self): return self.item_type == ItemType.CHECKOUT @property def requires_lottery(self): return self.item_type == ItemType.LOTTERY def __str__(self): return str(self.name) + " [" + str(self.id) + "]"