class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(30), nullable=False, unique=True) email = db.Column(db.String(40), nullable=False, unique=True) password_hash = db.Column(db.String(40), nullable=False, unique=True) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) @property def prettier_budget(self): return "${:,.2f}".format(self.budget) @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') def can_purchase(self, item_obj): return self.budget >=item_obj.price def can_sell(self, item_obj): return item_obj in self.items def __repr__(self): return f"User: {self.username}"
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode("utf-8") def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash, attempted_password) def can_purchase(self, item_obj): return self.budget >= item_obj.price def can_sell(self, item_obj): return item_obj in self.items
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) price = db.Column(db.Integer(), nullable=False) barcode = db.Column(db.String(length=12), nullable=False, unique=True) description = db.Column(db.String(length=1024), nullable=False, unique=True) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) # To get the string representation of the database # Returns the name of the item def __repr__(self): return f'Item: {self.name}' #Function to assign ownership to a user def assign_owner(self, user_obj): self.owner = user_obj.id user_obj.budget -= self.price db.session.commit() #Function to change back ownership to market of the item def change_owner(self, user_obj): self.owner = None user_obj.budget += self.price db.session.commit()
class User(db.Model): id = db.Column(db.Integer(), primary_key = True) username = db.Column(db.String(length = 20),nullable = False, unique = True) email_address = db.Column(db.String(length = 40), nullable = False, unique = True) password_hash = db.Column(db.String(length = 60), nullable = False) budget = db.Column(db.Integer(), nullable = False, default = 1000) items = db.relationship('Item', backref = 'owned_user', lazy = True)
class Item(db.Model): # Columns for the table id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) price = db.Column(db.Integer(), nullable=False) barcode = db.Column(db.String(length=12), nullable=False, unique=True) description = db.Column(db.String(length=1024), nullable=False, unique=True) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) def __repr__(self): return '<Item %r>' % self.id # Purchase the item def setOwnership(self, user): # Set ownership of the item to current user self.owner = user.id # Decrease the budget of the user user.budget -= self.price db.session.commit() # Sell the item def sell(self, user): # Set ownership to Nobody self.owner = None # Give the money back to the user user.budget += self.price db.session.commit()
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) @property def prettier_budget(self): if len(str(self.budget)) >= 4: return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' else: return f'{self.budget}$' @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash, attempted_password)
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) price = db.Column(db.Integer(), nullable=False) barcode = db.Column(db.String(length=12), nullable=False, unique=True) description = db.Column(db.String(length=1024), nullable=False, unique=True) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) def __repr__(self): return f'Item {self.name}'
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(50), nullable=False) barcode = db.Column(db.String(50), nullable=False, unique=True) price = db.Column(db.Float(), nullable=False) description = db.Column(db.String(2000)) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) def __repr__(self): return f"{self.name}-{self.price}"
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship("Item", backref="owned_user", lazy=True) def __repr__(self): return f"{self.name}"
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=24), nullable=False, unique=True) email = db.Column(db.String(length=70), nullable=False, unique=True) hash_password = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=100000) items = db.relationship('Item', backref='owned_user', lazy=True) #To check if the user have enough budget to purchase the item def can_purchase(self, item_obj): return self.budget >= item_obj.price #To check if the user owns the item def can_sell(self, item_obj): return item_obj.owner == self.id # Returns the username of the User def __repr__(self): return self.username # Decorator property to return password to instances @property def password(self): return self.password # Decorator to execute code before we set a password @password.setter # plain_text_password is the filled in password def password(self, plain_text_password): # Assign hash_password field a hashed password generated from bcrypt instance self.hash_password = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.hash_password, attempted_password) #To format the user budget @property def format_budget(self): temp_lis = list(str(self.budget)) start = 2 if len(temp_lis) > 3: if len(temp_lis) % 2 == 0: start = 1 inc = 0 for i in range(start, len(temp_lis) - 1, 2): temp_lis.insert(i + inc, ',') inc += 1 return "₹" + "".join(temp_lis)
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30),nullable=False, unique=True) price = db.Column(db.Integer(),nullable=False) barcode = db.Column(db.String(length=12),nullable=False,unique=True) description = db.Column(db.String(length=1024),nullable=False,unique=True) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) def buy(self, buying_user): self.owner = buying_user.id buying_user.budget -= self.price db.session.commit() def __repr__(self): return f'Item {self.name}'
class User(db.Model, UserMixin): id = db.Column(db.Integer(),primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship("Item", backref="owned_user", lazy=True) def __repr__(self): return f"{self.username}" @property def prettier_budget(self): if len(str(self.budget)) == 4: n = self.budget b = [] s = str(n)[::-1] b[0:] = s output = "" for i in range(1, len(s) + 1): if i % 3 == 0: output += f"{s[i - 1]}," else: output += s[i - 1] if output[::-1][0] == ",": return f"{output[::-1][1:]}$" else: return f'{output[::-1]}$' else: return f"{self.budget}$" @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash,attempted_password) def can_purchase(self, item_obj): return self.budget >= item_obj.price def can_sell(self, item_obj ): return item_obj in self.items
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) password_hash = db.Column(db.String(100), nullable=False) name = db.Column(db.String(20), nullable=False) items = db.relationship('Item', backref='owned_user', lazy=True) budget = db.Column(db.Float(), nullable=False, default=1000.0) @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash, attempted_password) @property def prettier_budget(self): return "{:,}".format(self.budget) + " $" def __repr__(self): return f"{self.name}"
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) price = db.Column(db.Integer(), nullable=False) barcode = db.Column(db.String(length=12), nullable=False, unique=True) description = db.Column(db.String(length=1024), nullable=False, unique=True) owner = db.Column(db.Integer(), db.ForeignKey('user.id')) def buy(self, user): self.owner = user.id user.budget -= self.price db.session.commit() def sell(self,user): self.owner = None user.budget += self.price db.session.commit()
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8')
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) # this is the one that creates the relationship with the model, but is not stored as a column # backref is the field to relate to all the items the user has. Also to see the # owner of specific item # I need lazy=True to grab all the user's items in one shot items = db.relationship('Item', backref='owned_user', lazy=True) # property to format the budget @property def prettier_budget(self): if len(str(self.budget)) >= 4: return f'$ {str(self.budget)[:-3]},{str(self.budget)[-3:]}' else: return f"$ {self.budget}" # the next lines are for hashing the passwordjj # create an attribute that will be accesable from each instance. @property def password(self): return self.password @password.setter def password(self, plain_text_password): # overwrite what is going to be store in password_hash self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') # to check if the psw is valid while login def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash, attempted_password) def can_purchase(self, item_obj): return self.budget >= item_obj.price def can_sell(self, item_obj): return item_obj in self.items
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) email = db.Column(db.String(length=50), nullable=False, unique=True) password = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) @property def prettier_budget(self): if len(str(self.budget)) >=4: return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}' else: return f'{self.budget}' def can_purchase(self, item_obj): return self.budget >= item_obj.price def can_sell(self,item_obj): return item_obj in self.items
class Item(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) price = db.Column(db.Integer(), nullable=False, unique=True) barcode = db.Column(db.String(length=12), nullable=False, unique=True) description = db.Column(db.String(1024), nullable=False, unique=True) # relationship to the user. 'user.id' must be in lower case owner = db.Column(db.Integer(), db.ForeignKey('user.id')) # I'm using this to return the value in name field def __repr__(self): return f'Item {self.name}' def buy(self, user): self.owner = user.id user.budget -= self.price db.session.commit() def sell(self, user): self.owner = None user.budget += self.price db.session.commit()
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1500) items = db.relationship('Item', backref='user', lazy=True) def __repr__(self): return self.username @property def password(self): return self.password @password.setter def password(self, plain): self.password_hash = bcrypt.generate_password_hash(plain).decode( 'utf-8') def check_password(self, passc): return bcrypt.check_password_hash(self.password_hash, passc)
class User(db.Model, UserMixin): # Columns for the table id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email_address = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) items = db.relationship('Item', backref='owned_user', lazy=True) # We can define additional properties of our model like this @property # Puts a comma after thousands def prettier_budget(self): if len(str(self.budget)) >= 4: return f'₹{str(self.budget)[:-3]},{str(self.budget)[-3:]}' else: return f'₹{self.budget}' @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): return bcrypt.check_password_hash(self.password_hash, attempted_password) # Check if user can purchase the item def can_purchase(self, item_obj): return self.budget >= item_obj.price # Check if user can sell the item def can_sell(self, item_obj): return item_obj in self.items
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=30), nullable=False, unique=True) email = db.Column(db.String(length=50), nullable=False, unique=True) # hashed hashed_password = db.Column(db.String(length=50), nullable=False) budget = db.Column(db.Integer(), nullable=False, default=1000) # Relationship between two tables. User can own items. # owned_user would be an attribute in the 'Item' object # Lazy==true => items should be loaded lazily when the property is first accessed items = db.relationship('Item', backref='owned_user', lazy=True) # method used to represent a class’s objects as a string def __repr__(self): return f'User {self.username}' @property def password(self): return self.password @password.setter def password(self, plain_text_password): self.hashed_password = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')