Ejemplo n.º 1
0
class Article(db.Model):
    url = db.Column(db.String(2048), primary_key=True)
    source = db.Column(db.String(100))
    author = db.Column(db.String(70))
    title = db.Column(db.String(250), nullable=False)
    description = db.Column(db.String(1000))
    thumbnail_url = db.Column(db.String(2048),
                              nullable=False,
                              default="/static/default_thumbnail.jpg")
    publish_date = db.Column(db.DateTime)
    chars_num = db.Column(db.Integer)
    is_fake_news = db.Column(db.Boolean, default=False)
    time_added = db.Column(db.DateTime, nullable=False, default=datetime.now())

    # referenced by other tables:
    articleTags = db.relationship('ArticleTag',
                                  backref='ArticleTag_article',
                                  lazy=True)
    articleKeywords = db.relationship('ArticleKeyword',
                                      backref='ArticleKeyword_article',
                                      lazy=True)
    articleActions = db.relationship('ArticleAction',
                                     backref='ArticleAction_article',
                                     lazy=True)

    def __repr__(self):
        return f"Article({self.url})"
Ejemplo n.º 2
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    followed = db.relationship('User',
                               secondary=followers,
                               primaryjoin=(followers.c.follower_id == id),
                               secondaryjoin=(followers.c.followed_id == id),
                               backref=db.backref('followers', lazy='dynamic'),
                               lazy='dynamic')

    def __repr__(self):
        return f'<User {self.username}>'

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def avatar(self, size):
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return f'https://www.gravatar.com/avatar/{digest}?d=identicon&s={size}'
Ejemplo n.º 3
0
class User(UserMixin, db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), index=True, unique=False)
    email = db.Column(db.String(120), index=True, unique=True)
    paths = db.relationship('Path', backref='creator', lazy='dynamic')
    steps = db.relationship('Step', backref='creator', lazy='dynamic')

    def __repr__(self):
        return '<User {}>'.format(self.email)
Ejemplo n.º 4
0
class User(db.Model, UserMixin
           ):  # This class inherits from both 'db.model' and 'UserMixin'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), unique=True, nullable=False)
    password = db.Column(db.String, nullable=False)
    firstname = db.Column(db.String(100), nullable=False)
    lastname = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(120), nullable=False)
    statesVisited = db.relationship('State',
                                    secondary=statesVisited,
                                    lazy='subquery',
                                    backref=db.backref('users', lazy=True))
    pictures = db.relationship('Picture')

    def __repr__(
        self
    ):  # This method already has default behavior defined for all classes. This method returns a value to represent this class
        return f"User('{self.firstname}', '{self.lastname}')"
Ejemplo n.º 5
0
class Path(db.Model):
    __tablename__ = 'path'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), index=True, unique=False)
    description = db.Column(db.String(400), index=True, unique=False)
    is_public = db.Column(db.Boolean, default=True, nullable=False)
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    steps = db.relationship('Step', backref='path', lazy='dynamic')

    def __repr__(self):
        return '<Path {}>'.format(self.name)
Ejemplo n.º 6
0
class Tag(db.Model):
    text = db.Column(db.String(500), primary_key=True)
    is_confirmed = db.Column(db.Boolean, nullable=False)
    time_added = db.Column(db.DateTime, nullable=False, default=datetime.now())

    # referenced by other tables:
    articleTags = db.relationship('ArticleTag',
                                  backref='ArticleTag_tag',
                                  lazy=True)

    def __repr__(self):
        return f"Tag('{self.text}', {'confirmed' if self.is_confirmed else 'potential'})"
Ejemplo n.º 7
0
class User(UserMixin, db.Model): 
 
	'''	db.Model is a base class for all models from flask-sqlalchemy.
    	The UserMixin class includes generic implementations that are appropriate 
    	for most user model classes( is_authenticated, is_active etc.)
    '''

	id = db.Column(db.Integer, primary_key=True)
	username = db.Column(db.String(64), index=True, unique=True)
	email = db.Column(db.String(120), index=True, unique=True)
	password_hash = db.Column(db.String(128))

	# This field is defined only here for the one-to-many relationship (one user, multiple posts):
	posts = db.relationship('Post', backref='author', lazy='dynamic') 

	about_me = db.Column(db.String(140))
	last_seen = db.Column(db.DateTime, default=datetime.utcnow)

	# Defining the followers table relationship; the .c. refers to the 'column' in the table:
	followed = db.relationship('User', secondary=followers, 
		primaryjoin=(followers.c.follower_id == id),
		secondaryjoin=(followers.c.followed_id == id),
		backref=db.backref('followers', lazy='dynamic'), lazy='dynamic'
		)


	def __repr__(self):
		return f'<User {self.username}>'

	def set_password(self, password):
		self.password_hash = generate_password_hash(password)

	def check_password(self, password):
		return check_password_hash(self.password_hash, password)



	def avatar(self, size):

		# Encoding the string as bytes and then passing it to the hash function
		digest = md5(self.email.lower().encode('utf-8')).hexdigest() 

		# The 'identicon' part is added to generate a default avatar for users who don't have one
		return f'https://gravatar.com/avatar/{digest}?d=identicon&s={size}' 




	def follow(self, user):
		if not self.is_following(user):
			self.followed.append(user)

	def unfollow(self, user):
		if self.is_following(user):
			self.followed.remove(user)

	def is_following(self, user):
		return self.followed.filter(followers.c.followed_id == user.id).count() > 0

	def followed_posts(self):
		followed = Post.query.join(
			followers, followers.c.followed_id == Post.user_id).filter(
				followers.c.follower_id == self.id)
		own = Post.query.filter_by(user_id=self.id) # User's own posts
		return followed.union(own).order_by(Post.timestamp.desc())

	def get_reset_password_token(self, expires_in=600):
		return jwt.encode(
			{'reset_password': self.id, 'exp': time() + expires_in},
			current_app.config['SECRET_KEY'], algorithm = 'HS256').decode('utf-8')

	@staticmethod 
	def verify_reset_password_token(token):
		try:
			id = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password']
		except:
			return
		return User.query.get(id)
Ejemplo n.º 8
0
class State(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    abreviation = db.Column(db.String(2), nullable=False, unique=True)
    pictures = db.relationship('Picture')
Ejemplo n.º 9
0
class OAuth(OAuthConsumerMixin, db.Model):
    provider_user_id = db.Column(db.String(256), unique=True, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
    user = db.relationship(User)
Ejemplo n.º 10
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), index=True, unique=True)
    email = db.Column(db.String(50), index=True, unique=True)
    password_hash = db.Column(db.String(50), index=True, unique=True)
    aboutme = db.Column(db.String(400))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)

    posts = db.relationship('Post', backref='author', lazy='dynamic')

    followed = db.relationship("User",
                               secondary=followers,
                               primaryjoin=(followers.c.follower_id == id),
                               secondaryjoin=(followers.c.followed_id == id),
                               backref=db.backref("followers", lazy="dynamic"),
                               lazy='dynamic')

    def __repr__(self):
        return "< user {}>".format(self.username)

    def avatar(self, size):
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
            digest, size)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    '''
        判断是否关注
    '''

    def is_following(self, user):
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    '''
       关注用户
    '''

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    '''
        取消关注
    '''

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    '''
        获取关注用户以及自己的文章,发布时间倒序排列
    '''

    def followed_posts(self):
        # followed_post = Post.query.join("followers", 'followers.c.followed_id=Post.id').filter(followers.c.follower_id=self.id).order_by(Post.timestamp.desc())
        # return followed_post
        # 没有要 order by
        followed = Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id)

        own = Post.query.filter_by(user_id=self.id)
        return followed.union(own).order_by(Post.timestamp.desc())

        # followed = Post.query.join(
        #     followers, (followers.c.followed_id == Post.user_id)).filter(
        #         followers.c.follower_id == self.id)
        # own = Post.query.filter_by(user_id=self.id)
        # return followed.union(own).order_by(Post.timestamp.desc())

    def get_reset_password_token(self, expire_in=600):
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': time.time() + expire_in
            },
            app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            id = jwt.decode(token, app.config['SECRET_KEY'],
                            algorithm="HS256")['reset_password']
        except:
            return
        return User.query.get(id)