예제 #1
0
class Question(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    subject = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text(), nullable=False)

    create_date = db.Column(db.DateTime(), nullable=False)

    user_id = db.Column(db.Integer,
                        db.ForeignKey('users.id', ondelete='CASCADE'),
                        nullable=False)
    user = db.relationship('User', backref=db.backref('question_set'))

    modify_date = db.Column(db.DateTime(), nullable=True)

    voter = db.relationship('User',
                            secondary=question_voter,
                            backref=db.backref('question_voter_set'))
예제 #2
0
class Answer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    question_id = db.Column(db.Integer,
                            db.ForeignKey('question.id', ondelete='CASCADE'))
    question = db.relationship('Question', backref=db.backref('answer_set', ))
    content = db.Column(db.Text(), nullable=False)
    create_date = db.Column(db.DateTime(), nullable=False)

    user_id = db.Column(db.Integer,
                        db.ForeignKey('users.id', ondelete='CASCADE'),
                        nullable=False)
    user = db.relationship('User', backref=db.backref('answer_set'))

    modify_date = db.Column(db.DateTime(), nullable=True)

    voter = db.relationship('User',
                            secondary=answer_voter,
                            backref=db.backref('answer_voter_set'))
예제 #3
0
class BlogPost(db.Model):

    __tablename__ = 'blogpost'

    users = db.relationship(User)

    id = db.Column(db.Integer,primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    comments = db.relationship('Comment', backref='post', lazy=True)

    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    title = db.Column(db.String(140),nullable=False, unique=True)
    text = db.Column(db.Text,nullable=False)
    slug = db.Column(db.String(140), nullable=False)

    tags = db.relationship(
        'Tag', secondary=post_tag,
        backref=db.backref('posts', lazy='dynamic'), lazy='dynamic'
    )

    def __init__(self,title,text,user_id, slug):
        self.title = title
        self.text = text
        self.user_id = user_id
        self.slug = slug

    def __repr__(self):
        return "Post ID: {}".format(self.id)

    def tag(self, tag):
        if not self.tagged(tag):
            self.tags.append(tag)

    def untag(self, tag):
        if self.tagged(tag):
            self.tags.remove(tag)

    def tagged(self, tag):
        return self.tags.filter(
            post_tag.c.tag_id == tag.id).count() > 0
예제 #4
0
class User(db.Model, UserMixin):

    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    profile_image = db.Column(
        db.Text,
        nullable=False,
        default='https://' + str(app.config['AWS_BUCKET']) +
        '.s3-ap-northeast-1.amazonaws.com/default_profile.png')
    email = db.Column(db.String(140), unique=True, index=True)
    username = db.Column(db.String(140), unique=True, index=True)
    name = db.Column(db.String(140), nullable=True)
    club_name = db.Column(db.String(140), nullable=True)
    event = db.Column(db.Text, nullable=True)
    facebook = db.Column(db.String(140), nullable=True)
    twitter = db.Column(db.String(140), nullable=True)
    instagram = db.Column(db.String(140), nullable=True)
    info = db.Column(db.Text, nullable=True)
    club_number = db.Column(db.String(140), nullable=True)
    club_place = db.Column(db.String(140), nullable=True)
    club_active = db.Column(db.String(140), nullable=True)
    money = db.Column(db.String(140), nullable=True)
    url = db.Column(db.String(140), nullable=True)
    university = db.Column(db.Integer, nullable=False)
    type = db.Column(db.Integer, nullable=False)
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    password_hash = db.Column(db.String(128))
    authenticated = db.Column(db.Boolean, default=False)
    email_confirmation_sent_on = db.Column(db.DateTime, nullable=True)
    email_confirmed = db.Column(db.Boolean, nullable=True, default=False)
    email_confirmed_on = db.Column(db.DateTime, nullable=True)
    posts = db.relationship('BlogPost', backref='author', lazy=True)
    comments = db.relationship('Comment', backref='poster', lazy=True)
    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')

    liked = db.relationship('PostLike',
                            foreign_keys='PostLike.user_id',
                            backref='user',
                            lazy='dynamic')

    def __init__(self,
                 email,
                 username,
                 name,
                 club_name,
                 type,
                 university,
                 password,
                 email_confirmation_sent_on=None):
        self.email = email
        self.username = username
        self.name = name
        self.club_name = club_name
        self.type = type
        self.university = university
        self.password_hash = generate_password_hash(password)
        self.email_confirmation_sent_on = email_confirmation_sent_on
        self.email_confirmed = False
        self.email_confirmed_on = None
        self.authenticated = False

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

    def is_authenticated(self):
        return self.authenticated

    def __repr__(self):
        return f"Username {self.username}"

    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 = BlogPost.query.join(
            followers, (followers.c.followed_id == BlogPost.user_id)).filter(
                followers.c.follower_id == self.id)
        return followed.union(self.posts).order_by(BlogPost.date.desc())

    def like_post(self, blog_post):
        if not self.has_liked_post(blog_post):
            like = PostLike(user_id=self.id, post_id=blog_post.id)
            db.session.add(like)

    def unlike_post(self, blog_post):
        if self.has_liked_post(blog_post):
            PostLike.query.filter_by(user_id=self.id,
                                     post_id=blog_post.id).delete()

    def has_liked_post(self, blog_post):
        return PostLike.query.filter(PostLike.user_id == self.id,
                                     PostLike.post_id
                                     == blog_post.id).count() > 0