示例#1
0
文件: utils.py 项目: zhaiwei/project
def create_app(name):
    app = Flask(name)
    app.config.update(get_conf('app'))

    register_shutdown_handler(app)

    return app
示例#2
0
文件: base.py 项目: zhaiwei/project
 def __new__(cls, name, bases, attrs):
     module = attrs.get('__module__')
     if module not in cls.cache_config:
         try:
             cls.cache_config = get_conf(module)
         except:
             import traceback; traceback.print_exc()
     added_attrs = cls.cache_config.get(name, {})
     return super(AdminViewMeta, cls).__new__(cls, 
             name, bases, dict_merge(added_attrs, attrs))
示例#3
0
文件: base.py 项目: zhaiwei/project
    def __new__(cls, name, base, attrs):
        module = attrs.get('__module__')

        if module not in cls.conf_cache:
            try:
                cls.conf_cache[module] = get_conf(module)
            except:
                cls.conf_cache[module] = {}
        extend_attrs = cls.conf_cache[module].get(name, {})
        return super(AdminViewMeta, cls).__new__(cls, name, base,
                    dict_merge(extend_attrs, attrs))
示例#4
0
文件: utils.py 项目: zhaiwei/project
from flask import Flask

from bbs.conf import get_conf
from bbs.orm import register_teardown_handler

conf = get_conf('app')

def create_app(name):
    app = Flask(name)
    app.config.update(conf)
    register_teardown_handler(app)
    return app
示例#5
0
import bbs.apps

from werkzeug.wsgi import SharedDataMiddleware, DispatcherMiddleware

from bbs.register import register_app
from bbs.conf import get_conf

conf = get_conf(__name__)
apps = register_app(bbs.apps)

front_app, dispatcher = conf.get('DISPATCHER').popitem()

front_app = apps[front_app]

dispatcher = {v: apps[k] for k, v in dispatcher.items()}

app = DispatcherMiddleware(front_app, dispatcher)
app = SharedDataMiddleware(app, conf.get('SHAREDATA'))
示例#6
0
文件: models.py 项目: zhaiwei/project
import datetime, json, hashlib

from sqlalchemy import ForeignKey, Column, Integer, Unicode, \
            UnicodeText, and_, Boolean, DateTime, TypeDecorator,\
            Table, and_, func, select
from sqlalchemy.orm import relationship, synonym, backref, column_property
from sqlalchemy.ext.mutable import Mutable

from bbs.conf import get_conf
from bbs.orm import Base

SECRET_KEY = get_conf('app').get('SECRECT_KEY')

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    
    username = Column(Unicode(20), unique=True, nullable=False)
    _password = Column(Unicode(128), nullable=False)
    is_super = Column(Boolean, default=False)
    is_staff = Column(Boolean, default=False)

    added_at = Column(DateTime, default=datetime.datetime.now, nullable=False)
    
    secret_password = synonym('_password')

    def __repr__(self):
        return '<User {}-{}>'.format(self.id, self.username)

    def __str__(self):
示例#7
0
文件: models.py 项目: zhaiwei/project
    Unicode,
    UnicodeText,
    Boolean,
    Table,
    TypeDecorator,
    select,
    func,
    ForeignKey,
)
from sqlalchemy.orm import backref, relationship, column_property, synonym
from sqlalchemy.ext.mutable import Mutable

from bbs.orm import Base
from bbs.conf import get_conf

salt = get_conf("app").get("SECRET_KEY")


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, nullable=False)

    username = Column(Unicode(20), unique=True, nullable=False)
    _password = Column(Unicode(128), nullable=False)
    is_staff = Column(Boolean, default=False)
    is_super = Column(Boolean, default=False)

    added_at = Column(DateTime, default=datetime.datetime.now, nullable=False)
    secret_password = synonym("_password")
示例#8
0
文件: views.py 项目: zhaiwei/project
import os

from flask import session, Blueprint, request

from .base import ModelView
from .fields import ListField
from bbs.helper import generate_media_filename
from bbs.conf import get_conf

permission = get_conf('permission')
media_url, media_path = get_conf('bbs').get('SHAREDATA').popitem()

upload = Blueprint('upload', __name__)

@upload.route('/img/', methods=('POST', ))
def upload_img():
    ck_num = request.values.get('CKEditorFuncNum')

    img = request.files.get('upload')
    secret_name = generate_media_filename() + os.path.splitext(img.filename)[1]
    img.save(os.path.join(media_path, secret_name))
    img_url = '{}/{}'.format(media_url, secret_name)

    return '''
        <script>
            window.parent.CKEDITOR.tools.callFunction(%(ck_num)s, "%(path)s", "上传成功");
        </script>
    ''' % dict(ck_num=ck_num, path=img_url)


示例#9
0
文件: orm.py 项目: zhaiwei/project
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base

from bbs.conf import get_conf

conf = get_conf('orm')

db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False))

Base = declarative_base()
Base.query = db_session.query_property()

def _create_engine():
    engine = create_engine(conf.get('db_name'), echo=conf.get('echo'))
    return engine

def connect_db():
    engine = _create_engine()
    db_session.configure(bind=engine)

def init_db():
    from bbs import models
    Base.metadata.create_all(bind=_create_engine())

def register_shutdown_handler(app):
    def shutdown_session(exception=None):
        db_session.remove()
    app.teardown_appcontext(shutdown_session)
示例#10
0
import bbs.apps
from werkzeug.wsgi import SharedDataMiddleware, DispatcherMiddleware

from bbs.register import register_app
from bbs.conf import get_conf

conf = get_conf('bbs')

apps = register_app(bbs.apps)

front_app, dispatch_apps = conf.get('Dispatcher').popitem()

front_app = apps.get(front_app)

dispatch_apps = {v: apps[k] for k, v in dispatch_apps.items()}

app = DispatcherMiddleware(front_app, dispatch_apps)
app = SharedDataMiddleware(app, conf.get('SharedData'))
示例#11
0
文件: orm.py 项目: zhaiwei/project
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker 
from sqlalchemy.ext.declarative import declarative_base

from bbs.conf import get_conf

orm_config = get_conf('orm')

db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False))

Base = declarative_base()
Base.query = db_session.query_property()

def _create_engine():
    return create_engine(orm_config['db_name'], echo=orm_config['echo'])

def create_all():
    from bbs import models
    Base.metadata.create_all(bind=_create_engine())

def connect_db():
    db_session.configure(bind=_create_engine())

def register_shutdown_handler(app):
    def shutdown_session(exception=None):
        db_session.remove()
    app.teardown_appcontext(shutdown_session)
示例#12
0
文件: views.py 项目: zhaiwei/project
import os

from flask import session, Blueprint, request

from .base import ModelView
from .fields import ListField

from bbs.conf import get_conf
from bbs.helpers import generate_media_filename

permissions = [(k, v) for k, v in sorted(get_conf('permissions').items(), key=lambda x: x[0])]
media_url, media_dir = get_conf('bbs').get('SHAREDATA').popitem()

upload = Blueprint('upload', __name__)

@upload.route('/img/', methods=('POST',))
def upload_img():
    ck_num = request.values['CKEditorFuncNum']
    upload_file = request.files.get('upload')
    file_name = generate_media_filename() + os.path.splitext(upload_file.filename)[1]
    full_path = os.path.join(media_dir, file_name)
    upload_file.save(full_path)
    img_url = '{}/{}'.format(media_url, file_name)

    return '''
        <script>
            window.parent.CKEDITOR.tools.callFunction(%(ck_num)s, "%(path)s", "上传成功");
        </script>
    ''' % dict(ck_num=ck_num, path=img_url)   

class UserView(ModelView):
示例#13
0
文件: views.py 项目: zhaiwei/project
import os

from flask import Blueprint, request, session

from .base import ModelView
from .fields import ListMutipleField

from bbs.conf import get_conf
from bbs.helpers import generate_media_filename

permissions = get_conf('permission')
media_url, media_path = get_conf('bbs').get('SharedData').popitem()

upload = Blueprint('upload', __name__)

@upload.route('/img/', methods=('POST', ))
def upload_img():
    ck_num = request.values.get('CKEditorFuncNum')
    upload_img = request.files.get('upload')
    file_name = generate_media_filename() + os.path.splitext(upload_img.filename)[1]
    upload_img.save(os.path.join(media_path, file_name))
    img_url = media_url + '/' + file_name

    return '''
        <script>
            window.parent.CKEDITOR.tools.callFunction(%(ck_num)s, "%(path)s", "上传成功");
        </script>
    ''' % dict(ck_num=ck_num, path=img_url)
    

class UserView(ModelView):
示例#14
0
文件: views.py 项目: zhaiwei/project
import os

from flask import Blueprint, request, session
from .base import ModelView
from .fields import CheckboxField

from bbs.conf import get_conf
from bbs.helpers import generate_random_name

upload = Blueprint("upload", __name__)
media_path = get_conf("bbs").get("SHAREDATA").popitem()


@upload.route("/upload/img", methods=("POST",))
def upload_img():
    ck_num = request.values.get("CKEditorFuncNum")
    f = request.files.get("upload")
    file_name = generate_random_name() + os.path.splitext(f.filename)[1]
    file_path = os.path.join(media_path[1], file_name)
    f.save(file_path)
    img_url = "{}/{}".format(media_path[0], file_name)

    return """
        <script>
            window.parent.CKEDITOR.tools.callFunction(%(ck_num)s, "%(path)s", "上传成功");
        </script>
    """ % dict(
        ck_num=ck_num, path=img_url
    )

示例#15
0
文件: models.py 项目: zhaiwei/project
import hashlib, datetime, json
from sqlalchemy import Integer, Unicode, UnicodeText, \
                        Column, ForeignKey, and_, select,\
                        DateTime, TypeDecorator, Boolean, \
                        Table, func
from sqlalchemy.orm import backref, relationship, column_property,\
                            synonym 
from sqlalchemy.ext.mutable import Mutable

from bbs.conf import get_conf
from bbs.orm import Base

salt = get_conf('app').get('SECRET_KEY')

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True, nullable=False)

    username = Column(Unicode(20), unique=True, nullable=False)
    _password = Column(Unicode(128), nullable=False)
    is_super = Column(Boolean, default=False)
    is_staff = Column(Boolean, default=False)

    added_at = Column(DateTime, default=datetime.datetime.now, nullable=False)

    secret_password = synonym('_password')

    def __str__(self):
        return self.username