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 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}')"
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)