def set_profile(request): '''修改用户信息,及用户配置''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 验证 user 表单的数据 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) # 验证 profile 表单的数据 if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 修改用户数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) # 修改 profile 数据 Profile.objects.update_or_create(id=request.uid, defaults=profile_form.cleaned_data) # 更新缓存 key = keys.MODEL_K % (Profile.__name__, request.uid) rds.delete(key) return render_json()
def modify_profile(request): '''修改个人资料、及交友资料''' # 定义两个 Form 表单 user_form = forms.UserForm(request.POST) profile_form = forms.ProfileForm(request.POST) # 检查 user_form 和 profile_form if not user_form.is_valid() or not profile_form.is_valid(): errors = {} errors.update(user_form.errors) errors.update(profile_form.errors) raise err.ProfileErr(errors) # 更新 user # update user set nickname='xxx', gender='male' where id=1; User.objects.filter(id=request.uid).update(**user_form.cleaned_data) # 更新或创建 profile Profile.objects.update_or_create(id=request.uid, defaults=profile_form.cleaned_data) # 更新缓存 key = keys.USER_RPROFILE_K % request.uid rds.delete(key) return render_json()
def set_profile(request): '''修改个人资料''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查数据有效性 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 删除旧的缓存 key = keys.PROFILE_K % request.uid rds.delete(key) # 文强 + 程超 的思路 # user = User.objects.get(id=request.uid) # user.__dict__.update(user_form.cleaned_data) # user.save() return render_json()
def modify_profile(request): '''修改个人资料及交友资料''' # 定义两个Form表单 user_form = forms.UserForm(request.POST) profile_form = forms.ProfileForm(request.POST) # 检查user_form 和 profile_form if not user_form.is_valid() or not profile_form.is_valid(): errors = {} errors.update(user_form.errors) errors.update(profile_form.errors) raise err.ProfileErr(errors) # 更新user数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) # 更新profile数据 Profile.objects.update_or_create(id=request.uid, defaults=profile_form.cleaned_data) key = keys.USER_PROFILE_K % request.uid rds.delete(key) return render_json()
def set_profile(request): '''修改个人资料''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查数据有效性 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) print('-----------------') print(user_form.cleaned_data) print(request.uid) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 删除旧的缓存 key = keys.PROFILE_K % request.uid model_user_key = 'Model-User-%s'%(request.uid) model_profile_key = 'Model-Profile-%s'%(request.uid) rds.delete(key) rds.delete(model_user_key) rds.delete(model_profile_key) #方法2 # user = User.objects.get(id=request.uid) # user.__dict__.update(user_form.cleaned_data) # user.save() return render_json()
def update_profile(request): # 定义form对象,,注意两个模型的字段不能同名 user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查验证数据 if user_form.is_valid() and profile_form.is_valid(): uid = request.uid # 更新用户个人信息-------- **字典名,可以按照相同的key进行传值 User.objects.filter(id=uid).update(**user_form.cleaned_data) Profile.objects.filter(id=uid).update(**profile_form.cleaned_data) inf_log.debug('删除旧缓存') rds.delete(keys.PROFILE_K % uid) return render_json(data='修改信息成功') else: err = {} err.update(user_form.errors) err.update(profile_form.errors) raise errors.ProfileErr(data=err)
def set_profile(request): '''修改个人资料''' user_form = UserFrom(request.POST) profile_form = ProfileForm(request.POST) if not user_form.is_valid(): print(user_form.errors) raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 数据修改过后删除旧的缓存 key = keys.PROFILE_KEY % request.uid rds.delete(key) return render_json()
def update_profile(request): """更新个人资料""" # 定义 form 对象 user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查验证数据 if user_form.is_valid() and profile_form.is_valid(): uid = request.session['uid'] User.objects.filter(id=uid).update(**user_form.cleaned_data) Profile.objects.update_or_create(id=uid, defaults=profile_form.cleaned_data) inf_log.debug('删除旧缓存') rds.delete(keys.PROFILE_K % uid) return render_json() else: err = {} err.update(user_form.errors) err.update(profile_form.errors) raise errors.ProfileErr(data=err)
def set_vip(request): vip_id = request.POST.get('vip_id') #获取vip 的id User.objects.filter(id=request.uid).update(vip_id=vip_id) # 删除旧缓存 key = keys.PROFILE_K % request.uid model_user_key = 'Model-User-%s' % (request.uid) model_profile_key = 'Model-Profile-%s' % (request.uid) rds.delete(key) rds.delete(model_user_key) rds.delete(model_profile_key) return render_json()
def upload_avatar(request): print('xxxx') avatar_file = request.FILES.get('avatar') print('你来了?') print(avatar_file) filename, filepath = save_avatar(request.uid, avatar_file) # 文件保存到本地 print(filename, filepath) avatar_url = upload_to_qiniu(filename, filepath) # 文件上传到七牛 User.objects.filter(id=request.uid).update(avatar=avatar_url) # 保存 URL # os.remove(filepath) # 删除本地临时文件 # 删除旧的缓存 key = keys.PROFILE_K % request.uid model_user_key = 'Model-User-%s' % (request.uid) model_profile_key = 'Model-Profile-%s' % (request.uid) rds.delete(key) rds.delete(model_user_key) rds.delete(model_profile_key) return render_json() # http://img.jj20.com/up/allimg/tx06/07112019921.jpg