class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') password_hash = db.Column(db.String) #setting property so that the field can only be written into @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() def __repr__(self): return "<User {}>".format(self.username)
class Bookmark(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) description = db.Column(db.String(300)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) _tags = db.relationship('Tag', secondary=tags, lazy='joined', backref=db.backref('bookmarks', lazy='dynamic')) @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num) @property def tags(self): return ",".join([t.name for t in self._tags]) @tags.setter def tags(self, string): if string: self._tags = [ Tag.get_or_create(name) for name in string.split(',') ] else: self._tags = [] def __repr__(self): return "<Bookmark '{}': '{}'>".format(self.description, self.url)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), unique=True) email = db.Column(db.String(100), unique=True) # set the backref = user.. so in Bookmark it can use the User class named user. Don't need to use user_id bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') # this will stored the hashvalue for password password_hash = db.Column(db.String) # can only write not read, and this will not be stored in DB @property def password(self): raise AttributeError('Password is write-only field') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() def __repr__(self): return '<User %r>' % self.username @staticmethod def get_by_userid(userid): return User.query.get(int(userid))
class Bookmark(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) description = db.Column(db.String(300)) bkmrkusername = db.Column(db.String(400)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) _tags = db.relationship("Tag", secondary=tags, backref=db.backref("bookmarks", lazy="dynamic")) @property def tags(self): return ",".join([t.name for t in self._tags]) @tags.setter def tags(self, string): if string: self._tags = [Tag.get_or_create(name) for name in string.split(",")] @classmethod def newbookmarks(cls, num): return cls.query.filter_by(user=current_user).order_by(desc(cls.date)).limit(num) def __repr__(self): return "<Bookmark '{}': '{}'>".format(self.url, self.description)
class Bookmark(db.Model): id_bookmark = db.Column(db.Integer, primary_key=True) nm_url = db.Column(db.Text, nullable=False) dt_bookmark = db.Column(db.DateTime, default=datetime.utcnow) nm_description = db.Column(db.String(300)) id_user = db.Column(db.Integer, db.ForeignKey('user.id_user'), nullable=False) _tags = db.relationship('Tag', secondary=tags, lazy='joined', backref=db.backref('bookmarks', lazy='dynamic')) @property def tags(self): return ','.join(tag.nm_tag for tag in self._tags) @tags.setter def tags(self, sTags): if sTags: self._tags = [Tag.get_or_create(t) for t in sTags.split(',')] else: self._tags = [] @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.dt_bookmark)).limit(num) def __repr__(self): return "<Bookmark '{}': '{}'>".format(self.nm_description, self.nm_url)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') password_hash = db.Column(db.String) # Creates a property in the database that is write only and can't be # accessed by the user, only the app @property def password(self): raise AttributeError('password: write-only field') # Creates the password hash and saves it to the passwordHash variable @password.setter def password(self, password): self.password_hash = generate_password_hash(password) # Hashes the users current login password and compares it against the # hash in the database def checkPassword(self, password): return check_password_hash(self.password_hash, password) @staticmethod def getByUsername(username): return User.query.filter_by(username=username).first() @staticmethod def getByEmail(email): return User.query.filter_by(email=email).first() def __repr__(self): return "<User '{}'>".format(self.username)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') password_hash = db.Column(db.String(256)) @property def password(self): raise AttributeError( 'Password: Write Only Field. Check the models file if you are unsure' ) @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() # ToDo: Add a get_userid_by_user def __repr__(self): return '<User %r>' % self.username
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') def __repr__(self): return '<User %r>' % self.username
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship("Bookmark", backref="user", lazy="dynamic") def __repr__(self): return "<User '{}'>".format(self.username)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) user = db.Column(db.String(80), unique=True) email = db.Column(db.String(80), unique=True) bookmark = db.relationship('Bookmark', backref='User', lazy='dynamic') def __repr__(self): return "<Bookmark %r" % self.user
class User(db.Model, UserMixin): """ User ORM model """ id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') password_hash = db.Column(db.String) @property def password(self): """ getter not implemented """ raise AttributeError('password: write-only filed') @password.setter def password(self, password): """ password setter """ self.password_hash = generate_password_hash(password) def check_password(self, password): """ check password """ return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): """ get by username """ return User.query.filter_by(username=username).first() @staticmethod def get_by_id(userid): """ get by id """ return User.query.get(int(userid)) @staticmethod def get_by_email(email): """ get by email """ return User.query.filter_by(email=email).first() def __repr__(self): return "{}".format(self.username)
class Bookmark(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) description = db.Column(db.String(300)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # Set the relationship with the Tag class, need to use a string as it's not defined until later. ToDo: Move it up # and replace it with the class name for future proofing. The second argument to the relationship call is the # argument 'secondary' which tells the relationship to use our junction table called "tags" above. We also defined # a backref called 'bookmarks' which will add an attribute called bookmarks to the other side of the relationship. # So each tag will get a bookmarks attribute containing a list of the associated bookmarks. Dynamic loading in case # there are a large number of bookmarks associated with each tag. # Underscore as I don't want to access this directly from other classes. _tags = db.relationship('Tag', secondary=tags, lazy='joined', backref=db.backref('bookmarks', lazy='dynamic')) @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num) # In the view and the form, we handle the list of tags as a comma separated string. So it's convenient to create a # tag property that provides a list of strings as well. The getter takes the contents of the _tags list which holds # actual Tag model objects. Then it takes the name from each and joins it into a string. So when we ask for the # value of the tags property on a bookmark, we get a string, with a list of tag names. @property def tags(self): return ",".join([t.name for t in self._tags]) # When we pass a string with a list of tags to be set to this property, we need to find out for each of those tags # whether it already exists in the database. If it doesn't, we need to insert a new tag into the tag table and then # add the new model object to the tag list for this bookmark. If it does exist, we can simply retrieve it and put it # in the list. So if it exists, we create a new method get_or_create that takes the name of a tag and returns a tag # model instance by either creating or retrieving a tag with that name. We then do a for loop over all the words in # the string we received and then call the method on each of those words. The resulting list is a list of tag model # objects and we can assign that to the _tags attribute. Assigning a list of tag objects is all we have to do and # SQLAlchemy will take it from there and create all the relevant rows in the database. @tags.setter def tags(self, string): # Check if the tags are a string if string: self._tags = [ Tag.get_or_create(name) for name in string.split(',') ] # If not, set the list ot an empty list, otherwise, it will always fail the test. else: self._tags = [] def __repr__(self): return "Bookmark '{}': '{}'>".format(self.description, self.url)
class User(db.Model, UserMixin): id_user = db.Column(db.Integer, primary_key=True) nm_firstName = db.Column(db.String(120), nullable=False) nm_lastName = db.Column(db.String(120), nullable=False) nm_userName = db.Column(db.String(80), unique=True) nm_passwordHash = db.Column(db.String) nm_email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') def get_id(self): '''Overrride method from UserMixin''' try: return self.id_user except AttributeError: raise NotImplementedError('No `id` attribute - override `get_id`') @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.nm_passwordHash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.nm_passwordHash, password) @staticmethod def get_by_username(username, site=False): if (site): return User.query.filter_by(nm_userName=username).first_or_404() else: return User.query.filter_by(nm_userName=username).first() @staticmethod def get_by_email(email): return User.query.filter_by(nm_email=email).first() @staticmethod def get_by_id(userid): return User.query.get(userid) def __repr__(self): return '<User %r>' % self.nm_userName
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(300), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='dynamic') password_hash = db.Column(db.String) @property def password(self): raise ArithmeticError('password : write-only-field') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() def __repr__(self): return '<User %r>' % self.username
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship("Bookmark", backref="user", lazy="dynamic") password_hash = db.Column(db.String) @property def password(self): raise AttributeError("password: write-only field") @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() def __repr__(self): return "<User '{}'>".format(self.username)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), unique=True) email = db.Column(db.String(120), unique=True) password_hash = db.Column(db.String, nullable=False) bookmarks = db.relationship('Bookmark', backref="user", lazy="dynamic") def __repr__(self): return "<User {}>".format(self.username) @property def password(self): raise AttributeError("Password write-only field") @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def _checkpassword(self, password): return check_password_hash(self.password_hash, password=password) @classmethod def getuserbyusername(cls, username): return cls.query.filter_by(username=username).first()