Esempio n. 1
0
class Category(db.Model, ModelMixin):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)

    def __repr__(self):
        return '<Category %r>' % self.name

    def __str__(self):
        return self.name
Esempio n. 2
0
class Blog(db.Model, ModelMixin):
    __tablename__ = 'blogs'

    CONTENT_TYPE_MARKDOWN = '0'
    CONTENT_TYPE_ORGMODE = '1'

    CONTENT_TYPE = (('0', 'markdown'), ('1', 'org-mode'))

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50), nullable=False)
    created_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           nullable=False)
    updated_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           onupdate=datetime.utcnow())
    content = db.Column(db.Text, nullable=False)
    content_type = db.Column(db.String(10),
                             nullable=False,
                             default=CONTENT_TYPE_MARKDOWN)
    is_copy = db.Column(db.Boolean, nullable=True, default=False)
    category_id = db.Column(db.Integer,
                            db.ForeignKey('categories.id', ondelete="CASCADE"))
    category = db.relationship('Category',
                               backref=db.backref('blogs',
                                                  cascade='all,delete-orphan',
                                                  lazy='dynamic'))
    author_id = db.Column(db.Integer,
                          db.ForeignKey('users.id', ondelete="CASCADE"))
    author = db.relationship('User',
                             backref=db.backref('blogs',
                                                cascade='all,delete-orphan',
                                                lazy='dynamic'))

    __mapper_args__ = {"order_by": created_at.desc()}

    def __repr__(self):
        return "<Blog %r>" % self.title

    def __str__(self):
        return self.title

    def to_json(self):
        return {
            'id': self.id,
            'title': self.title,
            'category': self.category.name,
            'tags': ','.join([tag.name for tag in self.tags]),
            'author': self.author.username
        }

    @property
    def read_times(self):
        return Record.get(self.id)

    @read_times.setter
    def read_times(self, value):
        Record.set('article:{}'.format(self.id))
Esempio n. 3
0
class Images(db.Model):
    __tablename__ = 'images'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    path = db.Column(db.String(128), nullable=False, unique=True)
    url = db.Column(db.String(360), unique=True)

    def __repr__(self):
        return "<Images %r>" % self.name

    def __str__(self):
        return self.name
Esempio n. 4
0
class Notice(db.Model):
    __tablename__ = 'notices'
    id = db.Column(db.Integer, primary_key=True)
    notice = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           nullable=False)

    __mapper_args__ = {"order_by": created_at.desc()}

    def __repr__(self):
        return "<Notice %r>" % self.id
Esempio n. 5
0
class Tags(db.Model, ModelMixin):
    __tablename__ = 'tags'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    blogs = db.relationship('Blog',
                            secondary=tag_blog,
                            backref=db.backref('tags', lazy='dynamic'),
                            lazy='dynamic')

    def __repr__(self):
        return '<Tags %r>' % self.name

    def __str__(self):
        return self.name
Esempio n. 6
0
class User(db.Model, UserMixin):
    __tablename__ = 'users'
    roles = db.Column(db.String, nullable=False)
    confirmed_time = db.Column(db.DateTime, nullable=True)
    send_email_time = db.Column(db.DateTime, nullable=True)
    introduce = db.Column(db.Text, nullable=True)
    school = db.Column(db.String, nullable=True)

    def update_password(self, password):
        self.password = generate_password_hash(password)
        db.session.commit()

    def update_infor(self, form):
        self.school = form.school.data
        self.introduce = form.introduce.data
        db.session.commit()

    @property
    def is_logined(self):
        return self.is_authenticated

    @property
    def token(self):
        config = current_app.config
        secret_key = config.setdefault('SECRET_KEY')
        salt = config.setdefault('SECRET_KEY_SALT')
        serializer = URLSafeTimedSerializer(secret_key)
        token = serializer.dumps(self.username, salt=salt)
        return token

    @staticmethod
    def check_token(token, max_age=86400):
        config = current_app.config
        secret_key = config.setdefault('SECRET_KEY')
        salt = config.setdefault('SECRET_KEY_SALT')
        serializer = URLSafeTimedSerializer(secret_key)
        try:
            username = serializer.loads(token, salt=salt, max_age=max_age)
        except BadSignature:
            return False
        except SignatureExpired:
            return False
        user = User.query.filter_by(username=username).first()
        if user is None:
            return False
        return user
Esempio n. 7
0
class TimeLine(db.Model, ModelMixin):
    __tablename__ = 'timeline'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    hide = db.Column(db.Boolean, nullable=True, default=False)
    created_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           nullable=False)

    updated_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           onupdate=datetime.utcnow())
    author_id = db.Column(db.Integer,
                          db.ForeignKey('users.id', ondelete="CASCADE"))
    author = db.relationship(User,
                             backref=db.backref('timelines',
                                                cascade='all,delete-orphan',
                                                lazy='dynamic'))

    def __repr__(self):
        return "<TimeLine %r>" % self.content[:10]

    def __str__(self):
        return self.content[:10]

    def to_json(self):
        return {'id': self.id, 'content': self.content, 'hide': self.hide}
Esempio n. 8
0
class Comment(db.Model, ModelMixin):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           nullable=False)
    content = db.Column(db.Text, nullable=False)
    blog_id = db.Column(db.Integer,
                        db.ForeignKey('blogs.id', ondelete="CASCADE"))
    blog = db.relationship('Blog',
                           backref=db.backref('comments',
                                              cascade='all,delete-orphan',
                                              lazy='dynamic'))
    author_id = db.Column(db.Integer,
                          db.ForeignKey('users.id', ondelete="CASCADE"))
    author = db.relationship('User',
                             backref=db.backref('comments',
                                                cascade='all,delete-orphan',
                                                lazy='dynamic'))

    def __repr__(self):
        return "<Comment %r>" % self.content
Esempio n. 9
0
class Books(db.Model):
    __tablename__ = 'books'
    id = db.Column(db.Integer, primary_key=True)
    tag = db.Column(db.Text)
    name = db.Column(db.Text)
    title = db.Column(db.Text)
    author = db.Column(db.Text)
    content = db.Column(db.Text)

    def __init__(self, tag, name, title, author, content):
        self.name = name
        self.tag = tag
        self.title = title
        self.author = author
        self.content = content

    def __repr__(self):
        return '<Books %r>' % self.name
Esempio n. 10
0
class Question(db.Model, ModelMixin):
    __tablename__ = 'questions'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(48), nullable=False)
    describ = db.Column(db.Text, nullable=False)
    answer = db.Column(db.Text, nullable=False)
    is_private = db.Column(db.Boolean, default=False, nullable=False)
    created_at = db.Column(db.DateTime,
                           default=datetime.utcnow(),
                           nullable=False)
    author_id = db.Column(db.Integer,
                          db.ForeignKey('users.id', ondelete="CASCADE"))
    author = db.relationship('User',
                             backref=db.backref('questions',
                                                cascade='all,delete-orphan',
                                                lazy='dynamic'))

    __mapper_args__ = {"order_by": created_at.desc()}

    def __repr__(self):
        return "<Question %r>" % self.title

    def __str__(self):
        return self.title
Esempio n. 11
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# *************************************************************************
#   Copyright © 2015 JiangLin. All rights reserved.
#   File Name: models.py
#   Author:JiangLin
#   Mail:[email protected]
#   Created Time: 2015-11-08 06:42:40
# *************************************************************************
from maple.extensions import db
from datetime import datetime
from flask_maple.models import ModelMixin
from maple.utils import Record

tag_blog = db.Table(
    'tag_blog', db.Column('tags_id', db.Integer, db.ForeignKey('tags.id')),
    db.Column('blogs_id', db.Integer, db.ForeignKey('blogs.id')))


class Tags(db.Model, ModelMixin):
    __tablename__ = 'tags'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    blogs = db.relationship('Blog',
                            secondary=tag_blog,
                            backref=db.backref('tags', lazy='dynamic'),
                            lazy='dynamic')

    def __repr__(self):
        return '<Tags %r>' % self.name
Esempio n. 12
0
class User(db.Model, UserMixin, BaseModel):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True)
    email = db.Column(db.String, unique=True)
    password = db.Column(db.String, nullable=False)
    roles = db.Column(db.String, nullable=False)
    is_superuser = db.Column(db.Boolean, default=False)
    is_confirmed = db.Column(db.Boolean, nullable=False, default=False)
    registered_time = db.Column(db.DateTime,
                                default=datetime.utcnow(),
                                nullable=False)
    confirmed_time = db.Column(db.DateTime, nullable=True)
    send_email_time = db.Column(db.DateTime, nullable=True)
    introduce = db.Column(db.Text, nullable=True)
    school = db.Column(db.String, nullable=True)
    groups = db.relationship(Group,
                             secondary=group_user,
                             backref=db.backref('users'))

    def update_password(self, password):
        self.password = generate_password_hash(password)
        db.session.commit()

    def update_infor(self, form):
        self.school = form.school.data
        self.introduce = form.introduce.data
        db.session.commit()

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

    def __repr__(self):
        return "<User %r>" % self.username

    def __str__(self):
        return self.username

    @staticmethod
    def set_password(password):
        passwd_hash = generate_password_hash(password)
        return passwd_hash

    @staticmethod
    def load_by_name(name):
        user = User.query.filter_by(username=name).first()
        return user

    @staticmethod
    def load_by_email(email):
        user = User.query.filter_by(email=email).first()
        return user

    @property
    def token(self):
        config = current_app.config
        secret_key = config.setdefault('SECRET_KEY')
        salt = config.setdefault('SECURITY_PASSWORD_SALT')
        serializer = URLSafeTimedSerializer(secret_key)
        token = serializer.dumps(self.username, salt=salt)
        return token

    @staticmethod
    def check_token(token, max_age=86400):
        config = current_app.config
        secret_key = config.setdefault('SECRET_KEY')
        salt = config.setdefault('SECURITY_PASSWORD_SALT')
        serializer = URLSafeTimedSerializer(secret_key)
        try:
            username = serializer.loads(token, salt=salt, max_age=max_age)
        except BadSignature:
            return False
        except SignatureExpired:
            return False
        user = User.query.filter_by(username=username).first()
        if user is None:
            return False
        return user
Esempio n. 13
0
from maple.extensions import db
from maple.common.models import BaseModel
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, \
     check_password_hash
from datetime import datetime
from flask_maple.permission.models import Group
from itsdangerous import (URLSafeTimedSerializer, BadSignature,
                          SignatureExpired)
from flask import current_app

ROLES = [('admin', 'admin'), ('editor', 'editor'), ('writer', 'writer'),
         ('visitor', 'visitor')]

group_user = db.Table(
    'group_user', db.Column('group_id', db.Integer,
                            db.ForeignKey('groups.id')),
    db.Column('user_id', db.Integer, db.ForeignKey('users.id')))


class User(db.Model, UserMixin, BaseModel):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True)
    email = db.Column(db.String, unique=True)
    password = db.Column(db.String, nullable=False)
    roles = db.Column(db.String, nullable=False)
    is_superuser = db.Column(db.Boolean, default=False)
    is_confirmed = db.Column(db.Boolean, nullable=False, default=False)
    registered_time = db.Column(db.DateTime,
                                default=datetime.utcnow(),
                                nullable=False)