class User(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(64)) age = db.Column(db.Integer, default=0) email = db.Column(db.String(512)) city = db.Column(db.String(64))
class User(flask_login.UserMixin, db.Model): user_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) age = db.Column(db.Integer) pwd = db.Column(db.String(64)) def get_id(self): return self.user_id
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(64)) age = db.Column(db.Integer, default=0) email = db.Column(db.String(512)) city = db.Column(db.String(64)) def __repr__(self): return "<User {}>".format(self.name)
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64)) content = db.Column(db.String(512)) date = db.Column(db.DateTime, default=datetime.datetime.utcnow) author = db.Column(db.Integer, db.ForeignKey('user.user_id')) def get_human_date(self): return "{}/{}/{}".format(self.date.day, self.date.month, self.date.year)
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(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) # posts = backref for Post relationship @classmethod def get_or_create(cls, name): tag = cls.query.filter_by(name=name).first() if not tag: tag = cls(name=name) db.session.add(tag) return tag
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), ) content = db.Column(db.Text()) pub_date = db.Column(db.DateTime(), default=datetime.datetime.now()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) tags = db.relationship('Tag', backref="posts", secondary=tag_to_post) def pub_date_as_str(self): return "{}/{}/{} at {}:{}".format( self.pub_date.day, self.pub_date.month, self.pub_date.year, self.pub_date.hour, self.pub_date.minute, ) def parse_tags(self): tag_regex = r'#(\w+)' to_parse = self.content to_parse.replace('#', ' #') to_parse += " " tags = re.findall(tag_regex, to_parse) return tags def add_tags_from_content(self): tags = self.parse_tags() for tag in tags: # Get the tag id (create it if it doesnt exist) tag_obj = Tag.get_or_create(tag) # Add it to the tags list self.tags.append(tag_obj) def get_formatted_content(self): if not self.content: return self.content repl = lambda matchobj: '<span class="post-tag">{}</span>'.format( matchobj.group(0)) regex = r'#\w+' return re.sub(regex, repl, self.content)
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), ) content = db.Column(db.Text()) pub_date = db.Column(db.DateTime(), default=datetime.datetime.now()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def pub_date_as_str(self): return "{}/{}/{} at {}:{}".format( self.pub_date.day, self.pub_date.month, self.pub_date.year, self.pub_date.hour, self.pub_date.minute, )
class Post(db.Model): """ The Post model inherits from db.Model and the UserMixin. UserMixin handles the user session and several other complex concepts whereas the db.Model handles the databse management. User takes in several arguments that can be found below: - id (auto-generated, primary-key) - title (required) - date_posted (required) - content (required) - user_id ("one to many relation", ForeignKey => "user.id") """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(64)) age = db.Column(db.Integer, default=0) email = db.Column(db.String(512)) city = db.Column(db.String(64)) pwd_hash = db.Column(db.String(254)) def add_password(self, password): self.pwd_hash = security.generate_password_hash(password) def check_password(self, password): return security.check_password_hash(self.pwd_hash, password) def add_to_db(self): self.name = self.name.title() db.session.add(self) db.session.commit() @classmethod def check_signin(cls, username, password): # Retrieve user user = cls.query.filter_by(name=username).first() if not user: return False # Checking his password return user.check_password def __repr__(self): return "<User {}>".format(self.name)
class User(db.Model, UserMixin): """ The User model inherits from db.Model and the UserMixin. UserMixin handles the user session and several other complex concepts whereas the db.Model handles the databse management. User takes in several arguments that can be found below: - id (auto-generated) - username (unique usernames, required) - email (unique email, required) - image_file (required, with a default image) - password (required) - posts ("one to many relation", alias = "author", lazy set to True (default -> "explicit is better than implicit")) """ id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.png') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def get_reset_token(self, expires_seconds=1800): s = Serializer(current_app.config['SECRET_KEY'], expires_seconds) return s.dumps({'user_id': self.id}).decode('utf8') @staticmethod def verify_reset_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class User(flask_login.UserMixin, db.Model): user_id = db.Column(db.Integer, primary_key=True) status = db.Column(db.String(1024), default="") name = db.Column(db.String(64)) email = db.Column(db.String(128)) age = db.Column(db.Integer) pwd = db.Column(db.String(64)) posts = db.relationship("Post", backref='user', lazy='dynamic') def get_id(self): return self.user_id def update_profile(self, status, name, email, age): self.status = status self.name = name self.email = email.lower() self.age = age db.session.commit()
class User(db.Model): user_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) age = db.Column(db.Integer) pwd = db.Column(db.String(64))
if not user: return False # Checking his password if user.check_password(password): return user return False def __repr__(self): return "<User {}>".format(self.name) tag_to_post = db.Table( 'tags', db.Column('post_id', db.Integer, db.ForeignKey('post.id'), primary_key=True), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), ) content = db.Column(db.Text()) pub_date = db.Column(db.DateTime(), default=datetime.datetime.now()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) tags = db.relationship('Tag', backref="posts", secondary=tag_to_post)