def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template('/admin/create_user.html', form=form, message=gettext('Some errors were found')) if user_id is not None: # Edit a user user_contr.update({'id': user_id}, { 'nickname': form.nickname.data, 'password': form.password.data, 'automatic_crawling': form.automatic_crawling.data }) user = user_contr.get(id=user_id) flash( gettext('User %(nick)s successfully updated', nick=user.nickname), 'success') else: # Create a new user (by the admin) user = user_contr.create( nickname=form.nickname.data, pwdhash=generate_password_hash(form.password.data), automatic_crawling=form.automatic_crawling.data, is_admin=False, is_active=True) flash( gettext('User %(nick)s successfully created', nick=user.nickname), 'success') return redirect(url_for('admin.user_form', user_id=user.id))
def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template('/admin/create_user.html', form=form, message=gettext('Some errors were found')) role_user = Role.query.filter(Role.name == "user").first() if user_id is not None: # Edit a user user_contr.update({'id': user_id}, {'nickname': form.nickname.data, 'email': form.email.data, 'password': form.password.data, 'refresh_rate': form.refresh_rate.data}) user = user_contr.get(id=user_id) flash(gettext('User %(nick)s successfully updated', nick=user.nickname), 'success') else: # Create a new user user = user_contr.create(nickname=form.nickname.data, email=form.email.data, password=form.password.data, roles=[role_user], refresh_rate=form.refresh_rate.data, activation_key="") flash(gettext('User %(nick)s successfully created', nick=user.nickname), 'success') return redirect(url_for('admin.user_form', user_id=user.id))
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(request.POST, prefix='user') user_profile_form = UserProfileForm(request.POST, prefix='profile') cc_form = CreditCardForm(request.POST, prefix='cc') address_form = AddressForm(request.POST, prefix='address') if user_form.is_valid() and user_profile_form.is_valid() and cc_form.is_valid() and address_form.is_valid(): user_model = user_form.save() cc_model = cc_form.save() address_model = address_form.save() user_model.set_password(user_model.password) user_model.save() user_profile_model = user_profile_form.save(commit=False) user_profile_model.user = user_model user_profile_model.creditCard = cc_model user_profile_model.address = address_model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] user_profile_model.save() registered = True else: print user_form.errors, user_profile_form.errors, cc_form.errors, address_form.errors else: user_form = UserForm(prefix='user') user_profile_form = UserProfileForm(prefix='profile') cc_form = CreditCardForm(prefix='cc') address_form = AddressForm(prefix='address') return render_to_response('web/register.html',{'user_form': user_form, 'user_profile_form': user_profile_form, 'cc_form': cc_form, 'address_form': address_form, 'registered': registered}, context)
def process_user_form(user_id=None): """Edit a user.""" form = UserForm() if not form.validate(): return render_template('admin/edit_user.html', form=form) if user_id is not None: user = models.User.query.filter(models.User.id == user_id).first() form.populate_obj(user) if form.password.data: user.pwdhash = generate_password_hash(form.password.data) db.session.commit() flash('User {} successfully updated.'.format(form.login.data), 'success') return redirect(url_for('admin_bp.form_user', user_id=user.id)) # Create a new user new_user = models.User(login=form.login.data, is_active=form.is_active.data, is_admin=form.is_admin.data, is_api=form.is_api.data, pwdhash=generate_password_hash(form.password.data)) db.session.add(new_user) db.session.commit() flash('User {} successfully created.'.format(new_user.login), 'success') return redirect(url_for('admin_bp.form_user', user_id=new_user.id))
def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template('/admin/create_user.html', form=form, message=gettext('Some errors were found')) if user_id is not None: # Edit a user user_contr.update({'id': user_id}, {'nickname': form.nickname.data, 'email': form.email.data, 'password': form.password.data, 'automatic_crawling': form.automatic_crawling.data}) user = user_contr.get(id=user_id) flash(gettext('User %(nick)s successfully updated', nick=user.nickname), 'success') else: # Create a new user (by the admin) user = user_contr.create(nickname=form.nickname.data, email=form.email.data, pwdhash=generate_password_hash(form.password.data), automatic_crawling=form.automatic_crawling.data, is_admin=False, is_active=True) flash(gettext('User %(nick)s successfully created', nick=user.nickname), 'success') return redirect(url_for('admin.user_form', user_id=user.id))
def user_form(user_id=None): if user_id is not None: user = UserController().get(id=user_id) form = UserForm(obj=user) message = gettext("Edit the user <i>%(nick)s</i>", nick=user.nickname) else: form = UserForm() message = gettext("Add a new user") return render_template("/admin/create_user.html", form=form, message=message)
def register(request): if request.method == 'GET': #redirect to home if already logged in auth = request.COOKIES.get('auth') #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] logged_in = True return HttpResponseRedirect(reverse("home")) vendor_form = VendorForm() user_form = UserForm() next = request.GET.get('next') or reverse('home') return render(request, 'register.html', {'next':next, 'user_form': user_form, 'vendor_form': vendor_form}) user_form = UserForm(request.POST) vendor_form = VendorForm(request.POST) if not user_form.is_valid(): return render(request, "register.html", {'state': "Invalid Input", 'user_form': user_form, "vendor_form": vendor_form}) username = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] f_name = user_form.cleaned_data['first_name'] l_name = user_form.cleaned_data['last_name'] password = user_form.cleaned_data['password'] is_vendor = user_form.cleaned_data['is_vendor'] vendor_name = "" if (is_vendor == True): if not vendor_form.is_valid(): expire_view_cache(request, 'register') return render(request, "register.html", \ {'state': "Vendors must fill out company name", \ 'user_form': user_form, "vendor_form": vendor_form}) vendor_name = vendor_form.cleaned_data['vendor_name'] post_data = {'username': username, \ 'email': email, \ 'f_name': f_name, \ 'l_name': l_name, \ 'password': password, \ 'is_vendor': is_vendor, \ 'vendor_name': vendor_name \ } post_encoded = urllib.parse.urlencode(post_data).encode('utf-8') req = urllib.request.Request('http://' + settings.EXP_API + ':8000/api/register/', data=post_encoded, method='POST') resp_json = urllib.request.urlopen(req).read().decode('utf-8') resp = json.loads(resp_json) if (resp['ok'] == False): expire_view_cache(request, 'register') return render(request, "register.html", {'state': "Unsucessful registration", 'user_form': user_form, 'vendor_form':vendor_form}) expire_view_cache(request, 'login') if (is_vendor == True): expire_view_cache(request, 'home') return HttpResponseRedirect(reverse('login'), {'status': 'Registration sucessful. Please login.'})
def test_user_save_method(self): from web.forms import UserForm form = UserForm(data={ 'username': '******', 'password': '******' }) self.assertEqual(form.is_valid(), True) response = form.save() self.assertEqual(response.status_code, 201)
def post(self, request): form = UserForm(request.POST) if form.is_valid(): new_user = form.save( ) # We are gonna save the object and it will be back messages.add_message(request, messages.INFO, _('You have been registered succesfully!')) return redirect('login') context = { 'form': form, } return render(request, 'user/signup.html', context)
def signUpView(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') create_account = AccountService.CreateAccount(username, raw_password) messages.add_message(request, messages.INFO, 'Account created with success', 'alert-success') return redirect('login') else: form = UserForm() return render(request, 'sign_up.html', {'form': form})
def uploads(request): if request.method == 'POST': userform = UserForm(request.POST, request.FILES) # print(userform) if userform.is_valid(): fileinstall = File() fileinstall.fileName = userform.cleaned_data['fileName'] fileinstall.uploadFile = userform.cleaned_data['uploadFile'] fileinstall.save() return render_to_response('uploadOK.html') else: userform = UserForm(initial ={'fileName': 'sunshore'}) return render_to_response('upload.html', {'userform': userform})
def register(request): # information from www.tangowithdjango.com context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=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 else: user_form = UserForm() return render_to_response()
def register(request): ''' ''' if request.method == 'GET': obj = UserForm(request.POST) return render(request, 'register.html', {'obj': obj}) #context = {} elif request.method == 'POST': ret = {'status': False, 'error': None, 'data': None} form = UserForm(request.POST) if form.is_valid(): #获得表单数据 username = form.cleaned_data['username'] password = form.cleaned_data['password'] email = form.cleaned_data['email'] # 判断用户是否存在 user = auth.authenticate(username=username, password=password) if not user: #添加到数据库(还可以加一些字段的处理) user = User.objects.create_user(username=username, email=email, password=password) user.save() usernames = models.UserProfile.objects.create(user_id=user.id, name=username) usernames.save() #添加到session request.session['username'] = usernames.name #调用auth登录 login(request, user) ret['status'] = True ret['data'] = form.cleaned_data ret = json.dumps(ret, cls=JsonCustomEncoder) else: ret['error'] = {'user': '******'} ret = json.dumps(ret, cls=JsonCustomEncoder) else: ret['error'] = form.errors.as_data() ret = json.dumps(ret, cls=JsonCustomEncoder) return HttpResponse(ret)
def signup(request): context = RequestContext(request) # get last inputs registered = False if request.method == 'POST': # POST Method for save userdata to database 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() # save user to database profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True return HttpResponseRedirect(reverse('login')) else: print user_form.errors, profile_form.errors else: # Create User Form user_form = UserForm() profile_form = UserProfileForm() # Return to the same page return render_to_response( 'signup.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def form_user(user_id=None): action = "Add a user" head_titles = [action] form = UserForm() if user_id is None: return render_template('admin/edit_user.html', action=action, head_titles=head_titles, form=form) user = models.User.query.filter(models.User.id == user_id).first() form = UserForm(obj=user) action = "Edit user" head_titles = [action] head_titles.append(user.email) return render_template('admin/edit_user.html', action=action, head_titles=head_titles, form=form, user=user)
def userDetails(request, idk=None): if request.POST: if idk: user_form = EditUserForm(data=request.POST, instance=get_object_or_404(User, pk=idk)) else: user_form = UserForm(request.POST) if user_form.is_valid(): user_form.save() return redirect('users_list') else: if idk: user_form = EditUserForm(instance=get_object_or_404(User, pk=idk)) else: user_form = UserForm() return render(request, 'administration/user_details.html', { 'user_form': user_form, 'idk': idk })
def get(self, request): """ Show a user register form :param request: HttpRequest :return: HttpResponse """ form = UserForm() context = { 'form': form, } return render(request, 'user/signup.html', context)
def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template( "/admin/create_user.html", form=form, message=gettext("Some errors were found"), ) if user_id is not None: # Edit a user user_contr.update( {"id": user_id}, { "nickname": form.nickname.data, "password": form.password.data, "automatic_crawling": form.automatic_crawling.data, }, ) user = user_contr.get(id=user_id) flash( gettext("User %(nick)s successfully updated", nick=user.nickname), "success") else: # Create a new user (by the admin) user = user_contr.create( nickname=form.nickname.data, password=form.password.data, automatic_crawling=form.automatic_crawling.data, is_admin=False, is_active=True, ) flash( gettext("User %(nick)s successfully created", nick=user.nickname), "success") return redirect(url_for("admin.user_form", user_id=user.id))
def form_user(user_id=None): """Return a form to create and edit a user.""" action = "Create user" head_titles = [action] form = UserForm() if user_id is None: return render_template('admin/edit_user.html', action=action, head_titles=head_titles, form=form) user = models.User.query.filter(models.User.id == user_id).first() form = UserForm(obj=user) action = "Edit user" head_titles = [action] head_titles.append(user.login) return render_template('admin/edit_user.html', action=action, head_titles=head_titles, form=form, user=user)
def index(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/') else: form = UserForm() return render(request, 'index.html', {'form': form})
def change_user(request,pk=None): user = User.objects.filter(pk=pk).first() form_obj = UserForm(instance=user) title="编辑" if pk else "添加" if request.method == 'POST': form_obj = UserForm(request.POST,instance=user) if form_obj.is_valid(): form_obj.save() return JsonResponse({'status':0,'msg':f'{title}成功'}) else: return JsonResponse({'status':1,'msg':f'{title}失败,失败的原因是{form_obj.errors}'}) return render(request, 'create/user_create.html', {'form_obj':form_obj, "pk":pk})
def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template('/admin/create_user.html', form=form, message=gettext('Some errors were found')) role_user = Role.query.filter(Role.name == "user").first() if user_id is not None: # Edit a user user_contr.update({'id': user_id}, { 'nickname': form.nickname.data, 'email': form.email.data, 'password': form.password.data, 'refresh_rate': form.refresh_rate.data }) user = user_contr.get(id=user_id) flash( gettext('User %(nick)s successfully updated', nick=user.nickname), 'success') else: # Create a new user user = user_contr.create(nickname=form.nickname.data, email=form.email.data, password=form.password.data, roles=[role_user], refresh_rate=form.refresh_rate.data, activation_key="") flash( gettext('User %(nick)s successfully created', nick=user.nickname), 'success') return redirect(url_for('admin.user_form', user_id=user.id))
class UserFormTest(TestCase): def setUp(self): self.form = UserForm(data={ 'username': '******', 'password': '******' }) def tearDown(self): self.client = Client() def test_valid_form(self): self.assertTrue(self.form.is_valid()) def test_invalid_form(self): form = UserForm(data={'email': "", 'password': ""}) self.assertFalse(form.is_valid())
def register(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(): try: user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True # Invalid form or forms - mistakes or something else? username = user_form.cleaned_data.get('username') raw_password = user_form.cleaned_data.get('password') user = authenticate(username=username, password=raw_password) login(request, user) except Exception as e: messages.add_message(request, messages.ERROR, e) return redirect('/accounts/dashboard/') else: messages.add_message(request, messages.ERROR, user_form.errors) redirect('/home/') else: user_form = UserForm() profile_form = UserProfileForm() user_form.fields['first_name'].widget.attrs.update( {'placeholder': 'FirstName'}) user_form.fields['first_name'].widget.attrs.update({'tabindex': 0}) user_form.fields['last_name'].widget.attrs.update( {'placeholder': 'LastName'}) user_form.fields['username'].widget.attrs.update( {'placeholder': 'UserName'}) profile_form.fields['phone_number'].widget.attrs.update( {'placeholder': 'PhoneNumber Ex: +98xxxxxxx'}) user_form.fields['email'].widget.attrs.update( {'placeholder': 'Email Ex: [email protected]'}) user_form.fields['password'].widget.attrs.update( {'placeholder': 'Password'}) return render(request, 'signup.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): if request.POST: user_form = UserForm(request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() email = Emails() email.register_me(user.email) return redirect('login') else: messages.add_message(request, messages.INFO, 'Nieprawidłowe dane uwierzytelniające.') else: user_form = UserForm() return render(request, 'account/register.html', {'user_form': user_form})
def register(request): users = CustomUser.objects.top_5() tags = Tag.objects.popular() if request.method == "POST": form = UserForm(request.POST, request.FILES) if form.is_valid(): user = form.save() username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = authenticate(username=username, password=password) auth_login(request, user) return redirect("home") else: form = UserForm() context = { "auth": request.user, "form": form, "users": users, "tags": tags, } return render(request, "web/register.html", context)
def setUp(self): self.form = UserForm(data={ 'username': '******', 'password': '******' })
def test_invalid_form(self): form = UserForm(data={'email': "", 'password': ""}) self.assertFalse(form.is_valid())