Exemplo n.º 1
0
class Node(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, unique=True, index=True)
    slug = db.Column(db.String(100), nullable=False, unique=True, index=True)
    description = db.Column(db.String(500), nullable=True, default="")
    icon = db.Column(db.String(100), nullable=True, default="")

    def __str__(self):
        return self.name

    def __repr__(self):
        return '<Node: %s>' % self.name

    @classmethod
    def query_all(cls):
        return cls.query.order_by(Node.name.asc()).all()

    def to_dict(self):
        return dict(id=self.id,
                    name=self.name,
                    slug=self.slug,
                    description=self.description,
                    icon=self.icon)

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self
Exemplo n.º 2
0
class Node(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, unique=True, index=True)
    slug = db.Column(db.String(100), nullable=False, unique=True, index=True)
    description = db.Column(db.String(500), nullable=True, default="")
    icon = db.Column(db.String(100), nullable=True, default="")

    def __str__(self):
        return self.name

    def __repr__(self):
        return '<Node: %s>' % self.name

    @classmethod
    def query_all(cls):
        return cls.query.order_by(Node.name.asc()).all()

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self

    def delete(self):
        from gather.topic.models import Topic
        for topic in Topic.query.filter_by(node=self).all():
            topic.delete()
        db.session.delete(self)
        db.session.commit()
        return self
Exemplo n.º 3
0
class ReadTopic(db.Model):
    __table_args__ = (db.UniqueConstraint('user_id',
                                          'topic_id',
                                          name='uc_user_read_topic'), )
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('account.id'), index=True)
    user = db.relationship(Account)
    topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'), index=True)
    topic = db.relationship("Topic")
Exemplo n.º 4
0
class History(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    diff_content = db.Column(db.Text(), nullable=True, default="")
    author_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    author = db.relationship(Account)
    topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'), index=True)
    topic = db.relationship(Topic)
    reply_id = db.Column(db.Integer, db.ForeignKey('reply.id'), index=True)
    reply = db.relationship(Reply)
    created = db.Column(db.DateTime, default=datetime.utcnow)

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self
Exemplo n.º 5
0
class Reply(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text(), nullable=True, default="")
    author_id = db.Column(
        db.Integer,
        db.ForeignKey('account.id'), nullable=False
    )
    author = db.relationship(Account)
    topic_id = db.Column(
        db.Integer,
        db.ForeignKey('topic.id'), index=True, nullable=False
    )
    topic = db.relationship("Topic")
    created = db.Column(db.DateTime, default=datetime.utcnow)
    changed = db.Column(
        db.DateTime,
        nullable=True,
        onupdate=datetime.utcnow
    )

    def to_dict(self):
        return {
            "id": self.id,
            "content": self.content,
            "author": self.author_id,
            "topic": self.topic_id,
            "created": self.created,
            "changed": self.changed
        }

    def save(self):
        if self.id:
            # Update reply
            self.changed = datetime.utcnow()
        else:
            topic = self.topic
            topic.updated = datetime.utcnow()
            topic.clear_read()
            topic.save()
        db.session.add(self)
        db.session.commit()
        return self
Exemplo n.º 6
0
class Topic(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text(), nullable=True, default="")
    author_id = db.Column(
        db.Integer,
        db.ForeignKey('account.id'), index=True, nullable=False
    )
    author = db.relationship(Account)
    node_id = db.Column(
        db.Integer,
        db.ForeignKey('node.id'), index=True, nullable=False
    )
    node = db.relationship(Node)
    created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    replies = db.relationship(
        "Reply", lazy='dynamic',
        order_by=Reply.id.asc()
    )
    updated = db.Column(
        db.DateTime,
        default=datetime.utcnow,
        onupdate=datetime.utcnow
    )
    changed = db.Column(
        db.DateTime,
        nullable=True
    )

    def __str__(self):
        return self.title

    def __repr__(self):
        return '<Topic: %s>' % self.title

    @property
    def last_page(self):
        return Reply.query.filter_by(topic=self).paginate(1).pages or 1

    def to_dict(self):
        return {
            "id": self.id,
            "title": self.title,
            "author": self.author_id,
            "node": self.node_id,
            "content": self.content,
            "created": self.created,
            "repliy_count": self.replies.count(),
            "updated": self.updated,
            "changed": self.changed,
            "replies": [reply.to_dict() for reply in self.replies]
        }

    @property
    def read_cache_key(self):
        return "read_topic_%s" % self.id

    def have_read(self, user):
        read_list = cache.get(self.read_cache_key)
        if read_list and user.id in read_list:
            return True
        return ReadTopic.query.filter_by(topic=self, user=user).count()

    def mark_read(self, user):
        if self.have_read(user):
            return
        read_list = cache.get(self.read_cache_key)
        if read_list:
            read_list.append(user.id)
        else:
            read_list = [user.id]
        cache.set(self.read_cache_key, read_list)
        read_mark = ReadTopic(topic=self, user=user)
        db.session.add(read_mark)
        db.session.commit()

    def clear_read(self):
        cache.delete(self.read_cache_key)
        ReadTopic.query.filter_by(topic=self).delete()

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self

    def delete(self):
        self.replies.delete()
        self.clear_read()
        db.session.delete(self)
        db.session.commit()
        return self
Exemplo n.º 7
0
class Account(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100),
                         nullable=False,
                         unique=True,
                         index=True)
    email = db.Column(db.String(100), nullable=False, unique=True, index=True)
    password = db.Column(db.String(100), nullable=False)
    role = db.Column(db.String(10), default="user")

    website = db.Column(db.String(100), nullable=True, default="")
    description = db.Column(db.String(500), nullable=True, default="")
    css = db.Column(db.String(), nullable=True, default="")

    created = db.Column(db.DateTime, default=datetime.utcnow)
    token = db.Column(db.String(20), nullable=True, default="")

    api_token = db.Column(db.String(40))
    feeling_lucky = db.Column(db.Boolean, default=False, nullable=True)

    def __init__(self, **kwargs):
        self.token = self.create_token(16)

        if 'password' in kwargs:
            raw = kwargs.pop('password')
            self.password = self.create_password(raw)

        if 'username' in kwargs:
            username = kwargs.pop('username')
            self.username = username.lower()

        if 'email' in kwargs:
            email = kwargs.pop('email')
            self.email = email.lower()

        for k, v in kwargs.items():
            setattr(self, k, v)

    def __str__(self):
        return self.username

    def __repr__(self):
        return '<Account: %s>' % self.username

    def avatar(self, size=48):
        size *= 2  # Retina
        md5email = hashlib.md5(self.email).hexdigest()
        query = "%s?s=%s" % (md5email, size)
        return current_app.config['GRAVATAR_BASE_URL'] + query

    @staticmethod
    def create_password(raw):
        passwd = '%s%s' % (raw, current_app.config['PASSWORD_SECRET'])
        return security.generate_password_hash(passwd)

    @staticmethod
    def create_token(length=16):
        return security.gen_salt(length)

    @property
    def is_staff(self):
        return self.is_admin or self.role == "staff"

    @property
    def is_admin(self):
        return self.id == 1 or self.role == "admin"

    def check_password(self, raw):
        passwd = '%s%s' % (raw, current_app.config['PASSWORD_SECRET'])
        return security.check_password_hash(self.password, passwd)

    def change_password(self, raw):
        self.password = self.create_password(raw)
        self.token = self.create_token()

    def to_dict(self):
        return {
            "id": self.id,
            "username": self.username,
            "email_md5": hashlib.md5(self.email).hexdigest(),
            "role": self.role,
            "website": self.website,
            "description": self.description
        }

    def generate_api_token(self):
        token = security.gen_salt(40)
        while Account.query.filter_by(api_token=token).count():
            token = security.gen_salt(40)
        self.api_token = token
        self.save()
        return self

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self

    @classmethod
    def clean_junk_users(cls):
        a_month_ago = datetime.utcnow() - timedelta(days=30)
        from gather.topic.models import Topic, Reply
        for user in cls.query.all():
            if user.created >= a_month_ago:
                pass
            if not Topic.query.filter_by(author=user).count():
                if not Reply.query.filter_by(author=user).count():
                    db.session.delete(user)
        db.session.commit()