Esempio n. 1
0
def users():
    """
    generate users and data
    """
    user_emails = []
    data =[]
    for i in range(300):
        user_emails.append(fake.email())
    while True:
        email = user_emails.pop()
        user_set = {
              'username': fake.name(),
              'email': email,
              'password': User.encryptpassword('password'),
              'ct': fake.iso8601(tzinfo=None, end_datetime=None),
              'last_login_ip': fake.ipv4_private(),
              'current_login_ip': fake.ipv4_private()
              ''
        }
        data.append(user_set)
        if len(user_emails) <=0:
            break
    fisrt_admin = {
         'username': fake.name(),
         'email': app.config['SEED_ADMIN_EMAIL'],
         'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD']),
         'ct': fake.iso8601(tzinfo=None, end_datetime=None),
         'last_login_ip': fake.ipv4_private(),
         'current_login_ip': fake.ipv4_private()
    }
    data.append(fisrt_admin)
    with app.app_context():
        db.drop_all()
        db.create_all()
        # User.query.delete()   #批量删除
        # db.session.commit()
        print('删除了')
        db.engine.execute(User.__table__.insert(), data)
        all = User.query.all()
        add_role_account()
        admin = Role.query.filter_by(name='Admin').first()
        user = Role.query.filter_by(name='Normal_Users').first()
        for u in all:
            i = random.random()
            if i <= 0.05:
                admin.users.append(u)
            else:
                user.users.append(u)
        db.session.commit()
        print('insert success')
Esempio n. 2
0
def signup():
    form = signupForm()
    if form.validate_on_submit():
        print('received')
        u = User(email=form.email.data,
                 password=User.encryptpassword(form.password.data))
        u.save()
        print('saved user')
        if login_user(u):
            return redirect(url_for('user.welcome'))
    return render_template('signup.html', form=form)
Esempio n. 3
0
def users():
    """
    generate users and data
    """
    user_emails = []
    data = []
    for i in range(100):
        user_emails.append(fake.email())
    while True:
        email = user_emails.pop()
        user_set = {
            'username': fake.name(),
            'email': email,
            'password': User.encryptpassword('password')
        }
        data.append(user_set)
        if len(user_emails) <= 0:
            break
    fisrt_admin = {
        'username': fake.name(),
        'email': app.config['FIRST_ADMIN'],
        'password': User.encryptpassword(app.config['ADMIN_PWD'])
    }
    data.append(fisrt_admin)
    with app.app_context():
        db.drop_all()
        db.create_all()
        print('删除了')
        db.engine.execute(User.__table__.insert(), data)
        all = User.query.all()
        add_role_account()
        admin = Role.query.filter_by(name='Admin').first()
        user = Role.query.filter_by(name='Normal_Users').first()
        for u in all:
            i = random.random()
            if i <= 0.05:
                admin.users.append(u)
            else:
                user.users.append(u)
        db.session.commit()
        print('insert success')
Esempio n. 4
0
def firstuser():
    '''create the first user for test'''
    if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
        print('already  exist initial user')
        return None

    params = {
        'email': app.config['SEED_ADMIN_EMAIL'],
        'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD'])
    }

    return User(**params).save()
Esempio n. 5
0
def update_pwd():
    form = update_pwdForm()
    if form.validate_on_submit():
        u = User.find_by_identity(form.identity.data)
        if u and u.passwordmatch(password=form.curent_password.data):
            u.password = User.encryptpassword(form.curent_password.data)
            u.save()
            return redirect(url_for('user.settings'))
        else:
            return '密码或邮箱不正确'

    return render_template('update_pwd.html', form=form)
Esempio n. 6
0
def signup():
    form = signupForm()
    if form.validate_on_submit():
        print('received')
        u = User(email=form.email.data,
                 password=User.encryptpassword(form.password.data))
        u.ct = datetime.utcnow()
        u.save()
        if login_user(u):
            track_activity(u, request.remote_addr)
            return redirect(url_for('user.welcome'))
    return render_template('signup.html', form=form)
Esempio n. 7
0
def firstuser():
    '''create the first user for test'''
    if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
        print('already  exist initial user')
        return None

    params = {
        'email': app.config['SEED_ADMIN_EMAIL'],
        'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD'])
    }
    u= User(**params)
    admin = Role(name= 'Admin')
    db.session.add(admin)
    db.session.add(u)
    admin.users.append(u)
    db.session.commit()

    return None