示例#1
0
def user(request, key):
    user = UserProfile.get(key)
    form = UserForm()
    if request.method == 'POST':
        if form.validate(request.form):
            user.user_name = request.form['user_name']
            user.first_name = request.form['first_name']
            user.last_name = request.form['last_name']
            user.email = request.form['email']
            user.newsletter_type = int(request.form['newsletter_type'])
            manager_key = request.form['manager']
            if manager_key:
                manager = Manager.get(manager_key)
                user.manager = manager
            try:
                if request.form['activated'] == 'on':
                    user.activated = True
            except:
                user.activated = False
            try:
                if request.form['is_admin'] == 'on':
                    user.is_admin = True
            except:
                user.is_admin = False
            if request.form['password']:
                user.set_password(request.form['password'])
            else:
                user.put()
            return redirect(url_for('admin/users'))

    manager_ = None
    try:
        if user.manager:
            manager_ = user.manager
    except Exception:
        pass
    managers_ = list(Manager.all())
    if not manager_:
        managers = [('', '-')]
    else:
        managers = [('', manager_)]
    for manager in managers_:
        if manager_ is None or  manager.key() != manager_.key():
            managers.append((manager.key(), manager))
    form = UserForm(
        initial={
            'user_name': user.user_name,
            'activated': user.activated,
            'is_admin': user.is_admin,
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'newsletter_type': user.newsletter_type
        })
    form.manager.choices = managers
    return render_to_response(
        'admin/user.html', {
            'user': user,
            'form': form.as_widget()
        })
示例#2
0
def add_user(request):
    form = UserForm(initial={'password':gen_password(6)})
    if request.method == 'POST':
        if form.validate(request.form):
            user_name = request.form['user_name'].lower()
            new_user = create_new_user(user_name=user_name, password=request.form['password'])
            new_user.email=request.form['email']
            new_user.first_name = request.form['first_name']
            new_user.last_name = request.form['last_name']
            manager_key = request.form['manager']
            if manager_key:
                manager = Manager.get(manager_key)
                new_user.manager = manager
            try:
                new_user.newsletter_type = int(request.form['newsletter_type'])
            except:
                pass
            try:
                if request.form['activated'] == 'on':
                    new_user.activated = True
            except:
                new_user.activated = False
            try:
                if request.form['is_admin'] == 'on':
                    new_user.is_admin = True
            except:
                new_user.is_admin = False
            new_user.put()
            return redirect(url_for('admin/users'))

    return render_to_response('admin/add_user.html', {'form': form.as_widget()})