Example #1
0
 def insert(o, name):
     """ 填写部门信息 """
     with db.auto_commit():
         section = Section()
         # print(generate_id("{0}-{1}".format(o.name, name)))
         section.id = generate_id("{0}-{1}".format(o.name, name))
         section.name = name
         section.society_id = o.id
         db.session.add(section)
Example #2
0
def put(instance):
    if not instance.uuid:
        instance.uuid = generate_id()
        insert(instance)
        return instance

    if not exist(instance.__class__, uuid=instance.uuid):
        insert(instance)
        return instance

    update(instance, {'uuid': instance.uuid})
    return instance
Example #3
0
async def api_register_user(*, email, name, password):
    """
    用户注册API函数
    :param email: 用户邮箱
    :param name: 用户名
    :param password: 密码, 传送过来的密码值为: 用户邮箱混合原始密码进行SHA1加密
    :return:
    """
    _RE_EMAIL = re.compile(r'^[a-z0-9\.\-\_]+\@[a-z0-9\-\_]+(\.[a-z0-9\-\_]+){1,4}$')
    _RE_SHA1 = re.compile(r'^[0-9a-f]{40}$')

    # 检查用户数据是否合法
    if not name or not name.strip():
        raise APIValueError(name, u'用户名非法')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError(email, u'邮箱账号非法')
    if not password or not _RE_SHA1.match(password):
        raise APIValueError(password, '密码非法')

    # 检查用户邮箱是否已经被注册
    users = await User.find_all(where='email=?', args=[email])
    if len(users) > 0:
        raise APIError('register:fail', email, u'邮箱已经被使用')

    # 生成用户ID, 并且混合用户ID和密码进行SHA1加密
    uid = generate_id()
    sha1_password = generate_sha1_password(uid, password)

    # 生成头像图片URL
    head_img_url = '/static/img/head_%s.jpg' % random.randint(1, 15)

    # 将新用户数据保存到数据库中
    user = User(id=uid, name=name.strip(), email=email, password=sha1_password, image=head_img_url)
    await user.save()

    # 生成COOKIE
    cookie = generate_user_cookie(user, 86400)
    cookie_name = configs.session.cookie_name

    # 生成响应消息
    r = web.Response()
    r.set_cookie(cookie_name, cookie, max_age=86400, httponly=True)
    user['password'] = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Example #4
0
def file_upload():
    """
    # 文件上传
    # status: OVER
    :return:
    """
    success_count = 0
    file = request.files['file']
    if file and allowed_file(file.filename):
        data = xlrd.open_workbook(file_contents=file.read())
        if data.sheet_loaded(data.sheet_names()[-1]):
            table = data.sheets()[0]
            if not isinstance(table.cell_value(0, 1), str):
                raise FormError(u'文件上传失败,请检查表中格式是否正确')
            if '社团' not in table.cell_value(0, 1):
                raise FormError(u'文件上传失败,请检查表中格式是否正确')
            for num in range(1, table.nrows):
                count = 1  # 第一列
                for row in table.row_values(num):
                    # id = None
                    if count == 1: pass  # 序号列
                    elif count == 2:  # 社团列
                        id = generate_id(row)
                        if not Society.query.filter_by(name=row).first():
                            Society.insert(row)
                    else:
                        if not row:
                            continue
                        society = Society.query.get(id)
                        if not Section.query.filter_by(name=row,
                                                       society_id=id).first():
                            Section.insert(society, row)
                            success_count += 1
                    count += 1
    else:
        raise FormError(msg=u'文件上传失败,文件后缀错误')
    return RegisterSuccess(msg=u'数据导入成功', data={'count': success_count})
Example #5
0
 def insert(name):
     with db.auto_commit():
         society = Society()
         society.id = generate_id(name)
         society.name = name
         db.session.add(society)
Example #6
0
# -*- coding: utf8 -*-
from flask import Flask
from werkzeug.security import generate_password_hash
from models.admin import Admin
from init import create_app
from models import db, generate_id


app = create_app(Flask(__name__))


with app.app_context():
    with db.auto_commit():
        # 创建一个超级管理员
        user = Admin()
        user.account = 'super'
        user.password = generate_password_hash('123456')
        user.id = generate_id(u'超级管理员')
        user.auth = 2
        db.session.add(user)