def update(request, id): user = User.objects.get(id=id) if request.POST: user_form = UserForm(request.POST, instance=user) setpassword_form = SetPasswordForm(user, request.POST) new_password1 = request.POST.get('new_password1', '') new_password2 = request.POST.get('new_password2', '') if not new_password1 and not new_password2: if user_form.is_valid(): user_form.save() return HttpResponseRedirect(reverse('account_index')) else: if user_form.is_valid() and setpassword_form.is_valid(): user_form.save() setpassword_form.save() return HttpResponseRedirect(reverse('account_index')) else: user_form = UserForm(instance=user) setpassword_form = SetPasswordForm(user) return { 'user_form':user_form, 'setpassword_form':setpassword_form, 'id':id }
def test_forms(self): form_data = {'username': '******', 'email':'*****@*****.**', 'password':'******', 'captcha_0':'dummy-value', 'captcha_1':'PASSED'} user_form = UserForm(data=form_data) if not user_form.is_valid(): print user_form.errors else: self.assertTrue(user_form.is_valid())
def signup(request): registered = False if request.method == 'POST': user_form = UserForm(request.POST) account_form = AccountInfoForm(request.POST) if user_form.is_valid() and account_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() account = account_form.save(commit=False) account.user = user if 'profile_pic' in request.FILES: account.profile_pic = request.FILES['profile_pic'] account.save() return HttpResponseRedirect(reverse('sportsbook:home')) else: pass else: user_form = UserForm() account_form = AccountInfoForm() return render( request, 'account/signup.html', { 'user_form': user_form, 'account_form': account_form, 'registered': registered })
def login(req): if req.method == 'POST': uf = UserForm(req.POST) if uf.is_valid(): #获取表单用户密码 username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #获取的表单数据与数据库进行比较 success = AdminInfo.objects.filter(username__exact=username, password__exact=password) if success: # username = uf.cleaned_data['username'] # 把获取表单的用户名传递给session对象 req.session['username'] = UserProfile.objects.filter( id__exact=success.values('userpro')[0]['userpro']).values( 'name')[0]['name'] req.session['user'] = username return HttpResponseRedirect('/procedures/index/') else: #比较失败,还在login return render_to_response('account/login.html', { 'error': '用户名或密码错误!', 'uf': uf }) else: uf = UserForm() return render_to_response('account/login.html', {'uf': uf})
def post(self, request, *args, **kwargs): user = User.objects.get(username=request.user.username) userform = UserForm(request.POST, instance=user) profileform = ProfileForm(request.POST, instance=user.profile) if request.POST.get("email") != user.email: user.profile.isVerified = False user.profile.save() if not (userform.is_valid() and profileform.is_valid()): kwargs["userform"] = userform kwargs["profileform"] = profileform return super(CProfile, self).post(request, *args, **kwargs) if userform.cleaned_data.get("email") != user.email: user.profile.isVerified = False user.profile.save() profileform.save() userform.save() password = userform.cleaned_data.get("password") if password: user.set_password(password) user.save() log = authenticate(username=user.username, password=password) login(request, log) messages.success(request, "會員資料修改成功") return redirect(reverse("account:profile"))
def register(request): registered = False if request.method == 'POST': # Get info from "both" forms # It appears as one form to the user on the .html page user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # Check to see both forms are valid if user_form.is_valid() and profile_form.is_valid(): # Save User Form to Database user = user_form.save() # Hash the password user.set_password(user.password) # Update with Hashed password user.save() # Now we deal with the extra info! # Can't commit yet because we still need to manipulate profile = profile_form.save(commit=False) # Set One to One relationship between # UserForm and UserProfileInfoForm profile.user = user # Check if they provided a profile picture if 'profile_pic' in request.FILES: print('found it') # If yes, then grab it from the POST form reply profile.profile_pic = request.FILES['profile_pic'] # Now save model profile.save() # Registration Successful! registered = True else: # One of the forms was invalid if this else gets called. print(user_form.errors, profile_form.errors) else: # Was not an HTTP post so we just render the forms as blank. user_form = UserForm() profile_form = UserProfileInfoForm() # This is the render and context dictionary to feed # back to the registration.html file page. return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def save_profile_js(request): """ Save user profile information """ response = {} status = 405 if request.is_ajax() and request.method == 'POST': form_data = simplejson.loads(request.POST['form_data']) user_object = User.objects.get(pk=request.user.pk) user_form = UserForm(form_data['user'], instance=user_object) if user_form.is_valid(): user_form.save() status = 200 else: response['errors'] = user_form.errors status = 422 ''' currently not used profile_object = user_object.profile profile_form = UserProfileForm(form_data['profile'],instance=profile_object) if profile_form.is_valid(): if status == 200: user_form.save() profile_form.save() else: if status == 200: response['errors']=profile_form.errors status = 422 else: response['errors']+=profile_form.errors ''' return HttpResponse(simplejson.dumps(response), content_type='application/json; charset=utf8', status=status)
def signUp(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: print user_form.errors else: user_form = UserForm() return render_to_response('account/signup.html', {}, context) return render_to_response( 'account/signin.html', {'user_form': user_form, 'registered': registered}, context)
def register(request): if request.method == 'GET': form = UserForm() return render(request, 'register.html', {'form': form}) else: form = UserForm() email = request.POST.get('email') try: credentials = None credentials = userInfo.objects.get(email=email) except Exception: print("Good To Go") if credentials is not None: messages.error(request, 'Email Already Taken') return render(request, 'register.html', {'form': form}) else: form = UserForm(request.POST, request.FILES) if form.is_valid(): form.save() # request.session['email']=email # print(request.session.get('email')) messages.success(request, 'Successfully Registered In') return redirect('register') else: messages.error(request, 'Invalid Credentials') return redirect('register')
def post(self, request, *args, **kwargs): user_form = UserForm(data=request.POST, prefix='user') profile_form = UserProfileForm(data=request.POST, prefix='profile') if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.userprofile = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'profile-userprofile_picture' in request.FILES: profile.userprofile_picture = request.FILES['profile-userprofile_picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True return HttpResponseRedirect('/order/') else: return HttpResponse('%s %s' % (user_form, profile_form))
def myself_edit(request): user = User.objects.get(username=request.user.username) userprofile = UserProfile.objects.get(user=user) userinfo = UserInfo.objects.get(user=user) if request.method == 'POST': user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) userinfo_form = UserInfoForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid() * userinfo_form.is_valid(): user_cd = user_form.cleaned_data userprofile_cd = userprofile_form.cleaned_data userinfo_cd = userinfo_form.cleaned_data user.email = user_cd['email'] userprofile.birth = userprofile_cd['birth'] userprofile.phone = userprofile_cd['phone'] userinfo.school = userinfo_cd['school'] userinfo.company = userinfo_cd['company'] userinfo.profession = userinfo_cd['profession'] userinfo.address = userinfo_cd['address'] userinfo.about_me = userinfo_cd['about_me'] user.save() userprofile.save() userinfo.save() return HttpResponseRedirect('/account/my-information/') else: user_form = UserForm(instance=request.user) userprofile_form = UserProfileForm(initial={'birth': userprofile.birth, 'phone': userprofile.phone}) userinfo_form = UserInfoForm(initial={'school': userinfo.school, 'company': userinfo.company, 'profession': userinfo.profession, 'address': userinfo.address, 'about_me': userinfo.about_me}) return render(request, 'account/myself_edit.html', {'user_form': user_form, 'userprofile_form': userprofile_form, 'userinfo_form': userinfo_form})
def post(self, request): user_form = UserForm(request.POST) if user_form.is_valid(): user = user_form.save(commit=False) user.set_password(user.password) user.save() confirmation_token = ConfirmationToken.objects.create( user=user, confirmation_key=secrets.token_hex(16)) send_confirmation_email.delay( confirmation_token.confirmation_key, user.id, user.email, ) messages.success(request, 'Usuário cadastrado com sucesso.') return redirect('meetup:meetup_list') messages.warning(request, 'Verifique todos os dados antes de prosseguir.') context = { "form": user_form, } return render(request, "account/register.html", context)
def register_doctor(request): form = UserForm(request.POST or None) form_profile = DoctorProfileForm(request.POST or None) if form.is_valid() and form_profile.is_valid(): user = form.save(commit=False) username = form.cleaned_data['username'] password = form.cleaned_data['password'] confirm_password = form.cleaned_data['confirm_password'] user.set_password(password) user.save() profile = form_profile.save(commit=False) profile.user = user profile.save() user = authenticate(username=username, password=password) if user is not None: if user.is_active: return render(request, 'account/login.html') context = { 'form_user': form, 'form_profile': form_profile } return render(request, 'account/register_doctor.html', context)
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, { 'userForm': UserForm(), 'userProfileForm': UserProfileForm() }) # POST userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not userForm.is_valid() or not userProfileForm.is_valid(): return render(request, template, { 'userForm': userForm, 'userProfileForm': userProfileForm }) user = userForm.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎加入') return redirect('main:main')
def post(self, request, *args, **kwargs): userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if request.POST.get('betacode') != "Ew0Xav08L4": kwargs['error'] = "* 註冊碼錯誤" kwargs['userForm'] = userForm kwargs['userProfileForm'] = userProfileForm return super(SignUp, self).post(request, *args, **kwargs) if not (userForm.is_valid() and userProfileForm.is_valid()): kwargs['userForm'] = userForm kwargs['userProfileForm'] = userProfileForm return super(SignUp, self).post(request, *args, **kwargs) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.type = 1 #2 = Manager userProfile.isAuth = False userProfile.save() #messages.success(request, '註冊成功') messages.success(request, '註冊成功') return redirect(reverse('main:main'))
def signup(request): registered = False if request.method == 'POST': user_form = UserForm(request.POST) account_form = AccountInfoForm(request.POST) if user_form.is_valid() and account_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() account = account_form.save(commit=False) account.user = user if 'profile_pic' in request.FILES: account.profile_pic = request.FILES['profile_pic'] account.save() registered = True else: print(user_form.errors, account_form.errors) else: user_form = UserForm() account_form = AccountInfoForm() return render( request, 'account/signup.html', { 'user_form': user_form, 'account_form': account_form, 'registered': registered })
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, { 'userForm': UserForm(), 'userProfileForm': UserProfileForm() }) # POST userForm = UserForm(request.POST) # Create your views here. userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, { 'userForm': userForm, 'userProfileForm': userProfileForm }) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎註冊') return redirect('main:main')
def user_register(request): request.POST = json.loads(request.body.decode('utf-8')) username = request.POST['username'] password = request.POST['password'] gender = request.POST['gender'] nickname = request.POST['nickname'] user_form_data = { 'username': username, 'password': password, } user_form = UserForm(data=user_form_data) json_dict = { 'err_code': 0, 'message': "Register success", 'data': {} } if user_form.is_valid(): user = user_form.save(commit=False) user.set_password(user.password) user.save() account = Account( user = user, gender = gender, nickname = nickname, ) account.save() return JsonResponse(json_dict) json_dict['err_code'] = -1 json_dict['message'] = "User form is not valid" return JsonResponse(json_dict)
def signupuser(request): context = {} if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() new_user = authenticate( username=request.POST.get('username'), password=request.POST.get('password'), ) login(request, new_user) return redirect('home') else: context['user_form'] = UserForm() context['profile_form'] = UserProfileInfoForm() return render(request, 'account/register.html', context)
def signUp(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: print user_form.errors else: user_form = UserForm() return render_to_response('account/signup.html', {}, context) return render_to_response('account/signin.html', { 'user_form': user_form, 'registered': registered }, context)
def post(self, request, *args, **kwargs): user = User.objects.get(username=request.user.username) userform = UserForm(request.POST, instance=user) profileform = ProfileForm(request.POST, instance=user.profile) if request.POST.get('email')!=user.email: user.profile.isVerified=False user.profile.save() if not (userform.is_valid() and profileform.is_valid()): kwargs['userform'] = userform kwargs['profileform'] = profileform return super(CProfile, self).post(request, *args, **kwargs) if userform.cleaned_data.get('email')!=user.email: user.profile.isVerified=False user.profile.save() profileform.save() userform.save() password = userform.cleaned_data.get('password') if password: user.set_password(password) user.save() log = authenticate(username=user.username, password=password) login(request, log) messages.success(request, '會員資料修改成功') return redirect(reverse('account:profile'))
def update(update_response, id): obj = userInfo.objects.get(id=id) if update_response.method == 'GET': form = UserForm(instance=obj) return render(update_response, 'update.html', {'form': form}) else: form = UserForm() email = update_response.POST.get('email') try: credentials = None credentials = userInfo.objects.get(email=email) except Exception: print("Good To Go") if credentials is not None: messages.error(update_response, 'Email Already Taken!') form = UserForm(update_response.POST, update_response.FILES, instance=obj) return render(update_response, 'update.html', {'form': form}) else: form = UserForm(update_response.POST, update_response.FILES, instance=obj) if form.is_valid(): form.save() return redirect('welcome')
def register(request): """ user registration form """ if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): human = True user = user_form.save(commit=False) user.set_password(user.password) user.save() return store.views.profile(request) else: print user_form.errors # return HttpResponse("Your registration is failed.") raise Http404("Your registration is failed.") # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: # for visitor, generate empty menu menu = MenuService.visitor_menu() user_form = UserForm() requestContext = RequestContext(request, {'menu':menu, 'user_form': user_form, 'page_title': 'Register'} ) # Render the template depending on the context. return render_to_response('register.html', requestContext)
def account_user_update(request, pk): user = User.objects.get(id=pk) account = Account.objects.get(user=pk) user_form = UserForm(request.POST or None, instance=user) user_account_form = AccountForm(request.POST or None, instance=account) if request.method == 'POST': if user_form.is_valid(): user.username = request.POST.get('username') user.first_name = request.POST.get('first_name') user.last_name = request.POST.get('last_name') user.email = request.POST.get('email') user.password = make_password(request.POST.get('password')) user.save() if user_account_form.is_valid(): account.mobile = request.POST.get('mobile') account.gender = request.POST.get('gender') account.start_date = request.POST.get('start_date') account.end_date = request.POST.get('end_date') account.role = request.POST.get('role') account.save() return redirect('account:accountuser_list') else: user_form = UserForm(instance=user) user_account_form = AccountForm(instance=account) return render(request, 'account/accountuser_update.html', { 'user_form': user_form, 'user_account_form': user_account_form })
def register(request): if request.method == "POST": user_form = UserForm(request.POST) if user_form.is_valid(): username = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] password = user_form.cleaned_data['password2'] first_name = user_form.cleaned_data['first_name'] user = User.objects.create_user(username=username, password=password, email=email, first_name=first_name) user.set_password(user.password) user_profile = UserProfile(user=user) user_profile.save() return HttpResponseRedirect(reverse('account:user_login')) else: print(user_form.errors) else: user_form = UserForm() return render(request, 'account/registration.html', { 'user_form': user_form, })
def register(request): registered = False if request.method == "POST": user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request, 'account/registration.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
def register_user(request): if request.method == 'POST': user_form = UserForm(request.POST or None) profile_form = UserProfileInfoForm(request.POST, request.FILES or None) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() print(user_form) profile = profile_form.save(commit=False) profile.user = user profile.save() create_action(user, " has created a new account") print(profile_form) return redirect("register_done") #registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() con = { 'user_form': user_form, 'profile_form': profile_form, #'registered': registered } return render(request, 'account/register.html', con)
def register(request): if request.user.is_authenticated(): return HttpResponse("You are already logged in.") else: # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. # if 'picture' in request.FILES: # profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'account/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def post(self, *args, **kwargs): form = UserForm(self.request.POST, prefix="user") company_form = CompanyForm(self.request.POST, prefix="company") if form.is_valid() and company_form.is_valid(): return self.form_valid(form, company_form) else: return self.render_to_response( self.get_context_data(form=form, company_form=company_form))
def post(self, request, *args, **kwargs): form = UserForm(data=request.POST) if form.is_valid(): user_service = UserService() user_service.create_user_object(form) user_service.log_user_in(request, form.instance.username, form.instance.password) return HttpResponseRedirect(reverse('songthread_list')) else: return render_to_response(self.template_name, {'form': form}, context_instance=RequestContext(request))
def create(request): if request.method =="POST": form = UserForm(request.POST) if form.is_valid(): user = form.save() return HttpResponseRedirect('/home/') else: form = UserForm() return render(request,'account/create.html',{'form':form})
def register(request): template= 'account/register.html' if request.method == 'GET': return render(request, template, {'userForm':UserForm()}) #POST userForm = UserForm(request.POST) if not userForm.is_valid(): return render(request, template, {'userForm':userForm}) userForm.save() messages.success(request, '註冊成功') return redirect('main:main')
def update_user(request): response_data = {'status' : 'failed', 'message' : 'unknown deletion error'} if request.is_ajax(): if request.method == 'POST': form = UserForm(instance=request.user, data=request.POST) if form.is_valid(): form.instance.username = form.instance.email form.save() response_data = {'status' : 'success', 'message' : 'updated user'} else: response_data = {'status' : 'failed', 'message' : json.dumps(form.errors)} return HttpResponse(json.dumps(response_data), content_type="application/json")
def update_user(request): response_data = {'status' : 'failed', 'message' : 'unknown deletion error'} if request.is_ajax(): if request.method == 'POST': form = UserForm(instance=request.user, data=request.POST) if form.is_valid(): form.instance.username = form.instance.email form.save() response_data = {'status' : 'success', 'message' : 'updated user'} else: response_data = {'status' : 'failed', 'message' : json.dumps(form.errors)} return HttpResponse(json.dumps(response_data), content_type="application/json")
def userUpdate(request, pk, template_name='account/register.html'): user = get_object_or_404(User, pk=pk) userForm = UserForm(request.POST or None, instance=user) ctx ={} if userForm.is_valid(): userForm.save(commit=True) messages.success(request, '用戶資料已更新!') username = userForm.cleaned_data.get('username') password = userForm.cleaned_data.get('password') user = authenticate(username=username, password=password) auth_login(request, user) return redirect('account:userUpdate', user.id) ctx['userForm'] = userForm return render(request, template_name, ctx)
def register(request): if request.method == "POST": uf = UserForm(request.POST) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] user = User() user.username = username user.password = password user.save() return render_to_response('success.html',{'username':username}, context_instance = RequestContext(request)) else: uf = UserForm() return render_to_response('register.html', {'uf' : uf}, context_instance = RequestContext(request))
def userCreate(request, template_name='account/register.html'): userForm = UserForm(request.POST or None) if userForm.is_valid(): userForm.save(commit=True) messages.success(request, '歡迎註冊!') username = userForm.cleaned_data.get('username') password = userForm.cleaned_data.get('password') user = authenticate(username=username, password=password) auth_login(request, user) messages.success(request, '登入成功!') return redirect('main:main') ctx = {} ctx['userForm'] = userForm ctx['title'] = 'New' return render(request, template_name, ctx)
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, {'userForm': UserForm()}) # POST userForm = UserForm(request.POST) if not userForm.is_valid(): return render(request, template, {'userForm': userForm}) userForm.save() return redirect('account:main')
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, {'form': UserForm()}) # POST form = UserForm(request.POST) if not form.is_valid(): return render(request, template, {'form': form}) form.save() messages.success(request, '歡迎註冊,請登入') return redirect('product:product')
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, {'userForm':UserForm()}) # POST userForm = UserForm(request.POST) if not userForm.is_valid(): return render(request, template, {'userForm':userForm}) userForm.save() messages.success(request, '歡迎註冊') return redirect('article:article')
def myself_edit(request): userprofile = UserProfile.objects.get(user=request.user) if \ hasattr(request.user, 'userprofile') else UserProfile.objects.create(user=request.user) userinfo = UserInfo.objects.get(user=request.user) if hasattr(request.user, "userinfo") \ else UserInfo.objects.create(user=request.user) if request.method == "POST": user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) userinfo_form = UserInfoForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid( ) * userinfo_form.is_valid(): user_cd = user_form.cleaned_data userinfo_cd = userinfo_form.cleaned_data userprofile_cd = userprofile_form.cleaned_data request.user.email = user_cd["email"] userprofile.birth = userprofile_cd["birth"] userprofile.phone = userprofile_cd["phone"] userinfo.school = userinfo_cd["school"] userinfo.company = userinfo_cd["company"] userinfo.profession = userinfo_cd["profession"] userinfo.address = userinfo_cd["address"] userinfo.aboutme = userinfo_cd["aboutme"] request.user.save() userprofile.save() userinfo.save() return HttpResponseRedirect('/account/my-information/') else: user_form = UserForm(instance=request.user) userprofile_form = UserProfileForm(initial={ "birth": userprofile.birth, "phone": userprofile.phone }) userinfo_form = UserInfoForm( initial={ "school": userinfo.school, "company": userinfo.company, "profession": userinfo.profession, "address": userinfo.address, "aboutme": userinfo.aboutme }) return render( request, "account/myself_edit.html", { "user_form": user_form, "userprofile_form": userprofile_form, "userinfo_form": userinfo_form })
def profile(request): profile = request.user.profile if request.POST: user_form = UserForm(request.POST, instance=request.user) user_profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and user_profile_form.is_valid(): user_form.save() user_profile_form.save() else: user_form = UserForm(instance=request.user) user_profile_form = UserProfileForm(instance=profile) context = { 'user_form': user_form, 'user_profile_form': user_profile_form, 'profile': profile } return render(request, 'account/profile.html', context)
def register(request): template = "account/register.html" if request.method == "GET": return render(request, template, {"userForm": UserForm(), "userProfileForm": UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {"userForm": userForm, "userProfileForm": userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, "歡迎註冊") return redirect(reverse("main:main"))
def register(request): template = 'account/register.html' if request.method=='GET': return render(request, template, {'userForm':UserForm(),'userProfileForm':UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {'userForm':userForm,'userProfileForm':userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎註冊') return redirect(reverse('main:main'))
def PNewUser(request): json_data=status.objects.filter(status='ERR',MSG='PD') errors="" if request.method == 'POST': #userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) #if userprofile_form.is_valid() and user_form.is_valid(): if user_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.first_name=request.POST['firstname'] created_user.last_name=request.POST['lastname'] created_user.is_active = False created_user.save() pinHash = str(hash("CLT"+ created_user.username + created_user.email))[3:9] userprofile = UserProfile(user=created_user, hash=pinHash, pwdhash=0) #hash=hashlib.sha224("CLT" + created_user.username + created_user.email).hexdigest()) #userprofile.user = created_user #userprofile.phone_num1 = userprofile_form.cleaned_data['phone_num1'] #userprofile.hash = hashlib.sha224("CLT" + created_user.username + created_user.email).hexdigest() userprofile.save() #subject = "new provider notice" #accept_link = 'http://cl.kazav.net/account/validate_prov/' + str(created_user.id) + '/' + userprofile.hash + '/' #html_message = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">Welcome to CLT<BR> Name: ' + created_user.first_name + ' ' + created_user.last_name + '<BR> <a href="' + accept_link + '"> Validate Me </a> ' #text_message = 'Welcome to CLT. Name: ' + created_user.first_name + ' ' + created_user.last_name + ' Validate yourself at: ' + accept_link #user_mail=created_user.email #msg = EmailMultiAlternatives(subject, text_message, 'CLT Server<*****@*****.**>', [user_mail]) #msg.attach_alternative(html_message,"text/html") #msg.send() textmessage="Hi " + request.POST['firstname'] + " and welcome to CLT. This is your PIN code for activating your account: " + pinHash account_sid = "AC442a538b44777e2897d4edff57437a24" auth_token = "be3a4e5fbf058c5b27a2904efd05d726" client = TwilioRestClient(account_sid, auth_token) #DEL COMMENT TO ENABLE SMS message = client.sms.messages.create(body=textmessage,to="+"+created_user.username,from_="+16698005705") #new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) #login(request, new_user) json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') if user_form.errors.items() : errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_form.errors.items()])) + "]" #if userprofile_form.errors.items(): # errors += ",[" + str(dict([(k, v[0].__str__()) for k, v in userprofile_form.errors.items()])) + "]" json_dump = "[" + serializers.serialize("json", json_data) json_dump += errors + "]" return HttpResponse(json_dump.replace('\'','"'))
def edit(request, user_id): user = get_object_or_404(User, pk=user_id) profile = user.profile if request.POST: user_form = UserForm(request.POST, instance=user) user_profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and user_profile_form.is_valid(): user_form.save() user_profile_form.save() else: user_form = UserForm(instance=user) user_profile_form = UserProfileForm(instance=profile) context = { 'user_form': user_form, 'user_profile_form': user_profile_form, 'profile': profile } return render(request, 'account/profile.html', context)
def create_P_provider(request): json_data=status.objects.filter(status='ERR',MSG=None) errors="" if request.method == 'POST': userprofile_form = ProvProfileForm(request.POST) user_form = UserForm(request.POST) if userprofile_form.is_valid() and user_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.first_name=request.POST['firstname'] created_user.last_name=request.POST['lastname'] created_user.is_active = False created_user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = created_user userprofile.level = userprofile_form.cleaned_data['level'] userprofile.is_client = False userprofile.phone_num1 = userprofile_form.cleaned_data['phone_num1'] userprofile.phone_num2 = userprofile_form.cleaned_data['phone_num2'] userprofile.address = userprofile_form.cleaned_data['address'] userprofile.birthday = userprofile_form.cleaned_data['birthday'] userprofile.area_id = userprofile_form.cleaned_data['area_id'] userprofile.hash = hashlib.sha224("OOS" + created_user.username + created_user.email).hexdigest() userprofile.save() subject = "new provider notice" accept_link = 'http://ws.kazav.net/account/accept_prov/' + str(created_user.id) + '/' + userprofile.hash + '/' reject_link = 'http://ws.kazav.net/account/reject_prov/' + str(created_user.id) + '/' + userprofile.hash + '/' html_message = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">New provider want access.<BR> Name: ' + created_user.first_name + ' ' + created_user.last_name + '<BR> <a href="' + accept_link + '"> ACCEPT </a> or <a href="' + reject_link + '"> REJECT </a>' text_message = 'New provider want access. Name: ' + created_user.first_name + ' ' + created_user.last_name + ' ACCEPT it at: ' + accept_link + ' or REJECT it ati: ' + reject_link user_mail="*****@*****.**" msg = EmailMultiAlternatives(subject, text_message, 'OOS Server<*****@*****.**>', [user_mail]) msg.attach_alternative(html_message,"text/html") msg.send() json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_form.errors.items()])) + "]" errors += ",[" + str(dict([(k, v[0].__str__()) for k, v in userprofile_form.errors.items()])) + "]" else: json_data=list(status.objects.filter(status='ERR',MSG='PD')) json_dump = "[" + serializers.serialize("json", json_data) json_dump += errors + "]" return HttpResponse(json_dump.replace('\'','"'))
def register(request, tmpl): syserr = False if request.user.is_authenticated(): return HttpResponseRedirect('/account/home/') if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): try: new_user = User.objects.create_user( username=form.cleaned_data['username'], email=form.cleaned_data['email'] ) new_user.set_password(form.cleaned_data['password']) new_user.first_name = form.cleaned_data['first_name'] new_user.last_name = form.cleaned_data['last_name'] new_user.save() badge = form.cleaned_data['badge_type'] if not badge.userchoice: badge = Badge.objects.filter(default=True)[0] if request.session.get('register_data'): notes = "REGISTER_DATA:%s\n" % (request.session.get('register_data')) else: notes = "" new_profile = UserProfile( user=new_user, gender=form.cleaned_data['gender'], address=form.cleaned_data['address'], zipcode=form.cleaned_data['zipcode'], city=form.cleaned_data['city'], country=form.cleaned_data['country'], language=form.cleaned_data['language'], badge_type = badge, badge_text=form.cleaned_data['badge_text'], comments=form.cleaned_data['comments'], fingerprint=form.cleaned_data['fingerprint'], notes = notes ) new_profile.save() except Exception, e: print e syserr = True else: return HttpResponseRedirect('/account/register/complete/')
def register(request): if request.method == "POST": uf = UserForm(request.POST) if uf.is_valid(): #获取表单信息 username = uf.cleaned_data['username'] passworld = uf.cleaned_data['passworld'] email = uf.cleaned_data['email'] #将表单写入数据库 user = User() user.username = username user.passworld = passworld user.email = email user.save() #返回注册成功页面 return render_to_response('success.html',{'username':username}) else: uf = UserForm() return render_to_response('register.html',{'uf':uf})
def signup(request): context = RequestContext(request) registered = False if request.method == "POST": user_form = UserForm(request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: print(user_form.errors) return HttpResponse("Form is not properly filled in.") else: user_form = UserForm() return render_to_response('account/signup.html', {'user_form': user_form, 'registered': registered}, context)
def register(request): context = {} registered = False if request.method == 'POST': user_form = UserForm(request.POST, prefix='user') profile_form = UserProfileForm(request.POST, prefix='profile') if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user_profile = user if 'profile-user_profile_picture' in request.FILES: profile.user_profile_picture = request.FILES['profile-user_profile_picture'] profile.save() # Flag para el formulario de registro registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm(prefix='user') profile_form = UserProfileForm(prefix='profile') context.update({'user_form': user_form, 'profile_form': profile_form, 'registered': registered}) return render(request, 'account_register.html', context)
def register_view(request): context = RequestContext(request); registered = False if request.method =='POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user=user_form.save() user.set_password(user.password) user.save() profile=profile_form.save(commit=False) profile.user=user if 'picture' in request.FILES: profile.picture=request.FILES['picture'] profile.save() messages.success(request,'Successfully Registered') registered=True return HttpResponseRedirect('/guide/index.html') else: user_form=UserForm() profile_form=UserProfileForm() return render_to_response('guide/home.html',{'user_form': user_form, 'profile_form': profile_form,'registered': registered},context)
def save_profile_js(request): """ Save user profile information """ response = {} status = 405 if request.is_ajax() and request.method == 'POST': form_data = simplejson.loads(request.POST['form_data']) user_object = User.objects.get(pk=request.user.pk) user_form = UserForm(form_data['user'], instance=user_object) if user_form.is_valid(): user_form.save() status = 200 else: response['errors'] = user_form.errors status = 422 ''' currently not used profile_object = user_object.profile profile_form = UserProfileForm(form_data['profile'],instance=profile_object) if profile_form.is_valid(): if status == 200: user_form.save() profile_form.save() else: if status == 200: response['errors']=profile_form.errors status = 422 else: response['errors']+=profile_form.errors ''' return HttpResponse( simplejson.dumps(response), content_type = 'application/json; charset=utf8', status=status )
def PNewUser(request): json_data=status.objects.filter(status='ERR',MSG='PD') errors="" if request.method == 'POST': #userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) #if userprofile_form.is_valid() and user_form.is_valid(): if user_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.first_name=request.POST['firstname'] created_user.last_name=request.POST['lastname'] #created_user.is_active = False created_user.save() pinHash = str(hash("CLT"+ created_user.username + created_user.email))[3:9] userprofile = UserProfile(user=created_user, hash=pinHash, pwdhash=0) #hash=hashlib.sha224("CLT" + created_user.username + created_user.email).hexdigest()) #userprofile.user = created_user #userprofile.phone_num1 = userprofile_form.cleaned_data['phone_num1'] #userprofile.hash = hashlib.sha224("CLT" + created_user.username + created_user.email).hexdigest() userprofile.save() textmessage="Hi " + request.POST['firstname'] + " and welcome to Cofix. This is your PIN code for activating your account: " + pinHash account_sid = "AC442a538b44777e2897d4edff57437a24" auth_token = "be3a4e5fbf058c5b27a2904efd05d726" client = TwilioRestClient(account_sid, auth_token) message = client.sms.messages.create(body=textmessage,to="+"+created_user.username,from_="+16698005705") #new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) #login(request, new_user) json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') if user_form.errors.items() : errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_form.errors.items()])) + "]" #if userprofile_form.errors.items(): # errors += ",[" + str(dict([(k, v[0].__str__()) for k, v in userprofile_form.errors.items()])) + "]" json_dump = "[" + serializers.serialize("json", json_data) json_dump += errors + "]" return HttpResponse(json_dump.replace('\'','"'))
def update_profile(request): # get user profile profile = request.user.get_profile() if request.method == 'POST': profile_form = ProfileForm(request.POST, request.FILES, instance=profile) user_form = UserForm(request.POST, instance=request.user) if profile_form.is_valid() and user_form.is_valid(): profile_form.save() user_form.save() messages.success(request, _("Profile updated succesfully.")) return redirect('update_profile') else: profile_form = ProfileForm(instance=profile) user_form = UserForm(instance=request.user) return render(request, 'account/profile.html', { 'profile_form': profile_form, 'user_form': user_form })
def create_P_user(request): json_data=status.objects.filter(status='ERR',MSG=None) errors="" if request.method == 'POST': userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) if userprofile_form.is_valid() and user_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.first_name=request.POST['firstname'] created_user.last_name=request.POST['lastname'] created_user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = created_user userprofile.level = 0 userprofile.is_client = True userprofile.phone_num1 = userprofile_form.cleaned_data['phone_num1'] userprofile.phone_num2 = userprofile_form.cleaned_data['phone_num2'] userprofile.address = userprofile_form.cleaned_data['address'] userprofile.birthday = userprofile_form.cleaned_data['birthday'] userprofile.area_id = userprofile_form.cleaned_data['area_id'] userprofile.save() new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, new_user) json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') if user_form.errors.items() : errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_form.errors.items()])) + "]" if userprofile_form.errors.items(): errors += ",[" + str(dict([(k, v[0].__str__()) for k, v in userprofile_form.errors.items()])) + "]" else: json_data=list(status.objects.filter(status='ERR',MSG='PD')) json_dump = "[" + serializers.serialize("json", json_data) json_dump += errors + "]" return HttpResponse(json_dump.replace('\'','"'))
def create_user(request): if request.method == 'POST': agree_form = AgreeForm(request.POST) userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) if userprofile_form.is_valid() and user_form.is_valid() and agree_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = created_user userprofile.minLevel = 0 userprofile.isCustomer = True userprofile.phoneNum = userprofile_form.cleaned_data['phoneNum'] userprofile.save() new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, new_user) return HttpResponseRedirect('/account/is_login') else: userprofile_form = UserProfileForm() user_form = UserForm() agree_form = AgreeForm() return render_to_response('registration/create_user.html', { 'userprofile_form': userprofile_form, 'user_form': user_form, 'agree_form': agree_form}, context_instance=RequestContext(request))
def profile(request): """ This view show user`s profile page and it allows the users update their profiles Works of this view mainly if request method is "POST", it takes all data and looks they are valid or not if form is valid data are saving the table which are user_profile and user in the database :param request: :return: it returns profile page with users information """ # try: user_profile = request.user.get_profile() if request.method == "POST": data = request.POST.copy() new_user_form = UserForm(data) new_user_profile_form = UserProfileForm(data) # checking form validation if new_user_form.is_valid() and new_user_profile_form.is_valid(): picture = request.FILES.get("picture") # checking email change or not # if email was changed, new activation # code will send the new email address if user_profile.user.email != data["email"]: activation_key = tasks.generate_activation_key( user_profile.user.username ) key_expires_date = tasks.generate_key_expires_date() tasks.send_activation_code.delay( activation_key, data["email"] ) user_profile.key_expires = key_expires_date user_profile.activation_key = activation_key user_profile.user.is_active = False user_profile.user.email = data["email"] user_profile.user.first_name = data["first_name"] user_profile.user.last_name = data["last_name"] user_profile.birth_date = data["birth_date"] user_profile.url = data["url"] # checking picture if it is not empty # delete previous one if picture is not None: if picture is not "not-found.png": user_profile.picture.delete(save=False) user_profile.picture = picture # saving new user profile and this profile`s user user_profile.save() user_profile.user.save() # determining success message messages.success( request, _('Profile details updated successfully.'), fail_silently=True ) # if profile saves successfully, # profile picture resizing with pil by using celery task if picture is not None: tasks.resize_image.delay(user_profile.picture, 240, 240) # sending form to template with initial data # because i do not send any extra data to view # it needs first data new_user_form = UserForm(initial={ "email": user_profile.user.email, "username": user_profile.user.username, "first_name": user_profile.user.first_name, "last_name": user_profile.user.last_name} ) new_user_profile_form = UserProfileForm(initial={ "picture": user_profile.picture, "url": user_profile.url, "birth_date": user_profile.birth_date} ) else: user_profile = request.user.get_profile() # determining error message messages.error( request, _('Profile details failed.'), fail_silently=True ) new_user_form = UserForm(data) new_user_profile_form = UserProfileForm(data) else: user_profile = request.user.get_profile() # user_profile = UserProfile.objects.get(slug=slug) new_user_form = UserForm(initial={ "email": user_profile.user.email, "username": user_profile.user.username, "first_name": user_profile.user.first_name, "last_name": user_profile.user.last_name} ) new_user_profile_form = UserProfileForm(initial={ "picture": user_profile.picture, "url": user_profile.url, "birth_date": user_profile.birth_date} ) c = {"user_profile": user_profile, "user_profile_form": new_user_profile_form, "user_form": new_user_form} if user_profile is not None: return render(request, "profile.html", c) else: return redirect(reverse('index'))