class Socket(db.Model): sid = db.Column(db.String(), primary_key=True) user = db.relationship('User', back_populates='sockets') user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __init__(self, sid, user): self.sid = sid self.user = user
class User(db.Model): id = db.Column(db.Integer, primary_key=True) hackerapi_id = 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()) 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, hackerapi_id, email, name, phone, is_admin): self.hackerapi_id = hackerapi_id self.email = email self.is_admin = is_admin self.name = name self.phone = phone self.location = '' 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 RequestItem(db.Model): entry_id = db.Column(db.Integer, db.ForeignKey('inventory_entry.id'), primary_key=True) request_id = db.Column(db.Integer, db.ForeignKey('request.id'), primary_key=True) quantity = db.Column(db.Integer) entry = db.relationship('InventoryEntry') def __init__(self, entry, quantity): self.entry = entry self.quantity = quantity def __str__(self): return str(self.quantity) + 'x ' + self.entry.name
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 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 range(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(cog.models.request.Request) \ .filter_by(status=cog.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(cog.models.request.Request) \ .filter_by(status=cog.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 @property def requires_mlh(self): return self.item_type == ItemType.MLH def __str__(self): return str(self.name) + " [" + str(self.id) + "]"