Ejemplo n.º 1
0
def change_profile(user_id):
    readonly=['nickname', 'truename']#, 'college', 'major', 'clazz', 'in_college_date') #不让在macro进行自动处理
    user = api_user.get(user_id)
    
    #已经有值的才真正的设置为readonly,否则还是可以编辑的
    for pro in readonly:
        val = getattr(user.profile, pro)
        if not val and not val.strip():
            readonly.remove(pro)    

    if request.method=='GET':
        form = ProfileForm(obj=user.profile)
        return render_template('security/create_profile.html', form=form, readonly=readonly,
                                   action_url=url_for('.change_profile', user_id=user_id))
    if request.method=='POST':
        form = ProfileForm()
        
        if form.validate_on_submit():
            for pro in readonly:
                delattr(form, pro) #把不让改的属性移除。
            profile = api_profile.get(form.id.data)
            api_profile.update(profile, **form.data)
            flash(u'更新个人信息成功')
            return redirect(url_for('.detail_profile', user_id=current_user.id))
        else:
            flash(u'更新失败,请检查内容', category='danger')
            return render_template('security/create_profile.html', form=form,
                           action_url=url_for('.change_profile', user_id=user_id))
Ejemplo n.º 2
0
def create_profile():
    """"""
    form = ProfileForm()
    if form.validate_on_submit():
        if api_profile.exist(nickname=form.nickname.data):
            flash(u'昵称已经被别人用了', category='danger')
            return render_template('security/create_profile.html', form=form,
                           action_url=url_for('.create_profile'))
        
        profile = api_profile.create(user_id=current_user.id, **form.data)
        return redirect(url_for('.detail_profile', user_id=current_user.id))
    if request.method=='POST':
        flash(u'更新失败,请检查', category='danger')
    return render_template('security/create_profile.html', form=form,
                           action_url=url_for('.create_profile'))