Example #1
0
File: app.py Project: amponce/elsa
def saveResume():
	if not login.current_user.is_authenticated():
		return redirect(url_for('index'))

	form = eforms.Resume(request.form)
	resume = models.Resume()
	check = form.validate_resume(form)

	if not check:
		updated_resume = db.session.query(models.Resume).filter_by(user_id=login.current_user.id).first()
		updated_resume.resume = form.resume.data

		try:
			db.session.add(updated_resume)
			db.session.commit()
			#need to update the index.
			flash('Resume Updated!')
		except Exception as e:
			flash('Error updating resume: ', e)
		return redirect(url_for('home'))
	else:
		form.populate_obj(resume)

		try:
			db.session.add(resume)
			db.session.commit()
			search.addCandidate(resume.user_id)
			flash('Resume Saved!')
		except Exception as e:
			flash('Error saving Resume: ', e)
		return redirect(url_for('home'))
Example #2
0
    def test_hidden_field_none(self):
        class TestNullEntryForm(WTForm):
            blog = HiddenQueryField(query=Blog.select())

        form = TestNullEntryForm(FakePost({"blog": ""}))

        # check the htmlz for the form's hidden field
        html = form._fields["blog"]()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="">')

        self.assertTrue(form.validate())
        self.assertEqual(form.blog.data, None)

        entry = NullEntry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(NullEntry.select().count(), 0)

        entry.save()
        self.assertEqual(NullEntry.select().count(), 1)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, None)

        # edit entry a1
        form = TestNullEntryForm(FakePost({"blog": None}), obj=self.entry_a1)

        # check the htmlz for the form's hidden field
        html = form._fields["blog"]()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="">')

        self.assertTrue(form.validate())
Example #3
0
    def test_blog_form_saving(self):
        form = BlogForm(FakePost({"title": "new blog"}))
        self.assertTrue(form.validate())

        blog = Blog()
        form.populate_obj(blog)
        self.assertEqual(blog.title, "new blog")

        # no new blogs were created
        self.assertEqual(Blog.select().count(), 2)

        # explicitly calling save will create the new blog
        blog.save()

        # make sure we created a new blog
        self.assertEqual(Blog.select().count(), 3)

        form = BlogForm(FakePost({"title": "a edited"}), obj=self.blog_a)
        self.assertTrue(form.validate())
        form.populate_obj(self.blog_a)

        self.assertEqual(self.blog_a.title, "a edited")
        self.blog_a.save()

        # make sure no new blogs were created
        self.assertEqual(Blog.select().count(), 3)

        # grab it from the database
        a = Blog.get(title="a edited")
Example #4
0
    def test_blog_form_saving(self):
        form = BlogForm(FakePost({'title': 'new blog'}))
        self.assertTrue(form.validate())

        blog = Blog()
        form.populate_obj(blog)
        self.assertEqual(blog.title, 'new blog')

        # no new blogs were created
        self.assertEqual(Blog.select().count(), 2)

        # explicitly calling save will create the new blog
        blog.save()

        # make sure we created a new blog
        self.assertEqual(Blog.select().count(), 3)

        form = BlogForm(FakePost({'title': 'a edited'}), obj=self.blog_a)
        self.assertTrue(form.validate())
        form.populate_obj(self.blog_a)

        self.assertEqual(self.blog_a.title, 'a edited')
        self.blog_a.save()

        # make sure no new blogs were created
        self.assertEqual(Blog.select().count(), 3)

        # grab it from the database
        a = Blog.get(title='a edited')
Example #5
0
    def test_entry_form_saving(self):
        # check count of entries
        self.assertEqual(Entry.select().count(), 3)

        form = EntryForm(FakePost({
            'title': 'new entry',
            'content': 'some content',
            'pub_date-date': '2011-02-01',
            'pub_date-time': '00:00:00',
            'blog': self.blog_b.get_id(),
        }))
        self.assertTrue(form.validate())

        self.assertEqual(form.pub_date.data, datetime.datetime(2011, 2, 1))
        self.assertEqual(form.blog.data, self.blog_b)

        entry = Entry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(Entry.select().count(), 3)

        entry.save()
        self.assertEqual(Entry.select().count(), 4)
        self.assertEqual(self.blog_a.entry_set.count(), 2)
        self.assertEqual(self.blog_b.entry_set.count(), 2)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, self.blog_b)

        # edit entry a1
        form = EntryForm(FakePost({
            'title': 'a1 edited',
            'content': 'a1 content',
            'pub_date': '2011-01-01 00:00:00',
            'blog': self.blog_b.get_id(),
        }), obj=self.entry_a1)
        self.assertTrue(form.validate())

        form.populate_obj(self.entry_a1)
        self.entry_a1.save()

        self.assertEqual(self.entry_a1.blog, self.blog_b)

        self.assertEqual(self.blog_a.entry_set.count(), 1)
        self.assertEqual(self.blog_b.entry_set.count(), 3)

        # pull from the db just to be 100% sure
        a1 = Entry.get(title='a1 edited')

        form = EntryForm(FakePost({
            'title': 'new',
            'content': 'blah',
            'pub_date': '2011-01-01 00:00:00',
            'blog': 10000
        }))
        self.assertFalse(form.validate())
Example #6
0
    def test_hidden_field(self):
        class TestEntryForm(WTForm):
            blog = HiddenQueryField(query=Blog.select())
            title = wtfields.TextField()
            content = wtfields.TextAreaField()

        form = TestEntryForm(FakePost({
            'title': 'new entry',
            'content': 'some content',
            'blog': self.blog_b.get_id(),
        }))

        # check the htmlz for the form's hidden field
        html = form._fields['blog']()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="%s">' % self.blog_b.get_id())

        self.assertTrue(form.validate())
        self.assertEqual(form.blog.data, self.blog_b)

        entry = Entry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(Entry.select().count(), 3)

        entry.save()
        self.assertEqual(Entry.select().count(), 4)
        self.assertEqual(self.blog_a.entry_set.count(), 2)
        self.assertEqual(self.blog_b.entry_set.count(), 2)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, self.blog_b)

        # edit entry a1
        form = TestEntryForm(FakePost({
            'title': 'a1 edited',
            'content': 'a1 content',
            'blog': self.blog_b.get_id(),
        }), obj=self.entry_a1)

        # check the htmlz for the form's hidden field
        html = form._fields['blog']()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="%s">' % self.blog_b.get_id())

        self.assertTrue(form.validate())

        form.populate_obj(self.entry_a1)
        self.entry_a1.save()

        self.assertEqual(self.entry_a1.blog, self.blog_b)

        self.assertEqual(self.blog_a.entry_set.count(), 1)
        self.assertEqual(self.blog_b.entry_set.count(), 3)

        # pull from the db just to be 100% sure
        a1 = Entry.get(title='a1 edited')
Example #7
0
def register_view():
	form = RegistrationForm(request.form)
	if helpers.validate_form_on_submit(form):
		user = User()
		form.populate_obj(user)
		db_session.add(user)
		db_session.commit()
		login.login_user(user)
 		return redirect(url_for('index'))
	return render_template('form.html', form=form)
Example #8
0
def register_view():
    form = RegistrationForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User()

        form.populate_obj(user)
        user.save()

        login.login_user(user)
        return redirect(url_for('index'))

    return render_template('form.html', form=form)
Example #9
0
def register_view():
  form = RegistrationForm(request.form)
  if request.method == 'POST' and form.validate():
    user = User()

    form.populate_obj(user)
    user.save()

    login.login_user(user)
    return redirect(url_for('index'))

  return render_template('form.html', form=form)
Example #10
0
 def register_view(self):
     form = RegistrationForm(request.form)
     if helpers.validate_form_on_submit(form):
         user = User()
         form.populate_obj(user)
         user.password = form.password.data
         user.save()
         login.login_user(user)
         return redirect(url_for('.index'))
     link = '<p>Already have an account? <a href="' + url_for(
         '.login_view') + '">Click here to log in.</a></p>'
     self._template_args['form'] = form
     self._template_args['link'] = link
     return super(MyAdminIndexView, self).index()
Example #11
0
def register_view():
    form = RegistrationForm(request.form)
    if form.validate_on_submit():
        user = Admin()
        
        form.populate_obj(user)
        
        db.session.add(user)
        db.session.commit()
        
        login.login_user(user)
        return redirect(url_for('.login_view'))
    link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
    return  render_template('register.html', form=form, login=link)
Example #12
0
    def test_hidden_field(self):
        class TestEntryForm(WTForm):
            blog = HiddenQueryField(query=Blog.select())
            title = wtfields.TextField()
            content = wtfields.TextAreaField()

        form = TestEntryForm(FakePost({"title": "new entry", "content": "some content", "blog": self.blog_b.get_id()}))

        # check the htmlz for the form's hidden field
        html = form._fields["blog"]()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="%s">' % self.blog_b.get_id())

        self.assertTrue(form.validate())
        self.assertEqual(form.blog.data, self.blog_b)

        entry = Entry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(Entry.select().count(), 3)

        entry.save()
        self.assertEqual(Entry.select().count(), 4)
        self.assertEqual(self.blog_a.entry_set.count(), 2)
        self.assertEqual(self.blog_b.entry_set.count(), 2)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, self.blog_b)

        # edit entry a1
        form = TestEntryForm(
            FakePost({"title": "a1 edited", "content": "a1 content", "blog": self.blog_b.get_id()}), obj=self.entry_a1
        )

        # check the htmlz for the form's hidden field
        html = form._fields["blog"]()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="%s">' % self.blog_b.get_id())

        self.assertTrue(form.validate())

        form.populate_obj(self.entry_a1)
        self.entry_a1.save()

        self.assertEqual(self.entry_a1.blog, self.blog_b)

        self.assertEqual(self.blog_a.entry_set.count(), 1)
        self.assertEqual(self.blog_b.entry_set.count(), 3)

        # pull from the db just to be 100% sure
        a1 = Entry.get(title="a1 edited")
Example #13
0
def register_view():
    form = RegistrationForm(request.form)
    if helpers.validate_form_on_submit(form):
        user = User()

        form.populate_obj(user)

        db.session.add(user)
        db.session.commit()

        login.login_user(user)
        return redirect(url_for('index'))

    return render_template('form.html', form=form)
Example #14
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = AdminUsers()
            form.populate_obj(user)
            user.password = generate_password_hash(form.password.data)
            db_session.add(user)
            db_session.commit()

            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #15
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if request.method == 'POST' and form.validate():
            user = User()
            form.populate_obj(user)
            user["password"] = bcrypt.generate_password_hash(user["password"]).decode('utf-8')
            user.save()
            login_user(user)
            return redirect(url_for('.index'))

        self._template_args['form'] = form
        self._template_args['active'] = "Register"
        self._template_args['intro'] = ""
        self._template_args['link'] = '*Please do not enter a used username or password<p><p>Login? <a href="{}">Click here</a></p>'.format(url_for('.login_view'))
        return super(CustomAdminIndexView, self).index()
Example #16
0
def login_view():
    form = LoginForm(request.form)
    print 'here'
    if form.validate_on_submit():
        user = Admin()
        
        form.populate_obj(user) # populates obj attributes with form data
        
        db.session.add(user)
        db.session.commit()
        
        login.login_user(user)
        return redirect(url_for('.admin_index'))
    link = '<p>Don\'t have an account? <a href="' + url_for('.register_view') + '">Click here to register.</a></p>'
    return render_template('login.html', form=form, register=link)
Example #17
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            user.password_hash = generate_password_hash(form.password.data)

            db.session.add(user)
            db.session.commit()

            login.login_user(user)
            return redirect(url_for('.index'))
        self._template_args['form'] = form
        return super(MyAdminIndexView, self).index()
Example #18
0
def register_view():
    form = RegistrationForm(request.form)
    if form.validate_on_submit():
        user = Admin()

        form.populate_obj(user)

        db.session.add(user)
        db.session.commit()

        login.login_user(user)
        return redirect(url_for('.login_view'))
    link = '<p>Already have an account? <a href="' + url_for(
        '.login_view') + '">Click here to log in.</a></p>'
    return render_template('register.html', form=form, login=link)
Example #19
0
def login_view():
    form = LoginForm(request.form)
    print 'here'
    if form.validate_on_submit():
        user = Admin()

        form.populate_obj(user)  # populates obj attributes with form data

        db.session.add(user)
        db.session.commit()

        login.login_user(user)
        return redirect(url_for('.admin_index'))
    link = '<p>Don\'t have an account? <a href="' + url_for(
        '.register_view') + '">Click here to register.</a></p>'
    return render_template('login.html', form=form, register=link)
Example #20
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            db.session.add(user)
            db.session.commit()

            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<p>已有账号? <a href="' + \
            url_for('.login_view') + '">点击登录</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #21
0
File: app.py Project: amponce/elsa
def addJob():
	if not login.current_user.is_authenticated():
		return redirect(url_for('index'))

	form = eforms.jobsForm(request.form)
	job = models.Jobs()
	form.populate_obj(job)

	try:
		db.session.add(job)
		db.session.commit()
		search.addJob(job.id)
		flash('Successfully added job!')
		return redirect(url_for('home'))
	except Exception as e:
		flash('Error posting job: %s' % e)
		return redirect(url_for('home'))
Example #22
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            # we hash the users password to avoid saving it as plaintext in the db,
            # remove to use plain text:
            user.password = generate_password_hash(form.password.data)

            user.save()

            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #23
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()
            form.populate_obj(user)
            user.password = generate_password_hash(form.password.data)
            db.session.add(user)
            db.session.commit()
            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<div class="center-block" style="text-align: center;"> \
	                <p>Already have an account? <a href="' + url_for(
            '.login_view') + '">Click here to log in.</a></p> \
                </div>'

        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #24
0
def home():

    finds = None

    form = BookSearch(request.form)

    if helpers.validate_form_on_submit(form):

        book = Book()

        form.populate_obj(book)

        if not (book.title or book.author):
            flash('To find books you need set title or author')
        else:
            finds = book.find_books_or_authors()

    return render_template('home.html', form=form, title='What are you looking for', finds=finds)
Example #25
0
def register_view():
    form = Registration_Form(request.form)

    if helpers.validate_form_on_submit(form):
        user = User()

        form.populate_obj(user)
        user.password = user.hash_password(user.password)

        # Set user permissions
        user.usertype = 0;

        db.session.add(user)
        db.session.commit()

        login.login_user(user)
        return redirect(url_for('login_view'))

    return render_template('register.html', form=form)
Example #26
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            # we hash the users password to avoid saving it as plaintext in the db,
            # remove to use plain text:
            user.password = generate_password_hash(form.password.data)

            user.save()

            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<p>Already have an account? <a href="' + url_for(
            '.login_view') + '">Click here to log in.</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #27
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if admin_helpers.validate_form_on_submit(form):
            adminuser = AdminUser()

            form.populate_obj(adminuser)

            adminuser.password = generate_password_hash(form.password.data)

            db.session.add(adminuser)

            try:
                db.session.commit()
            except:
                db.session.rollback()

            login.login_user(adminuser)
            return redirect(url_for('.index'))
        return self.render("admin/register.html", form=form)
Example #28
0
    def register_view(self):

        '''
            Handle user register
        '''
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User(is_finish_setup=False)

            form.populate_obj(user)

            db.session.add(user)
            db.session.commit()

            login.login_user(user)
            return redirect(url_for('.index'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()
Example #29
0
File: app.py Project: amponce/elsa
def addTest():
	if not login.current_user.is_authenticated():
		return redirect(url_for('index'))

	form = eforms.testForm(request.form)
	tests = models.ABTests()
	form.populate_obj(tests)

	resume = db.session.query(models.Resume).filter_by(user_id=login.current_user.id).first()

	try:
		db.session.add(tests)
		db.session.commit()
		flash('test saved')
		return render_template('recipes.html', test_id=tests.id
							   , user_id=login.current_user.id
							   , resume=resume)
	except Exception as e:
		flash('error: ', e)
		return redirect(url_for('home'))
Example #30
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            # hash password, before populating User object
            form.password.data = hash(form.password.data)
            form.populate_obj(user)

            # activate the admin user
            if user.email == app.config['ADMIN_USER']:
                user.is_active = True

            db.session.add(user)
            db.session.commit()

            flash('Please wait for your new account to be activated.', 'info')
            return redirect(url_for('.login_view'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        return self.render('admin/home.html', form=form, link=link, register=True)
Example #31
0
    def register_view(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            if form.email.data == app.config['ADMIN_USER']:
                user = User()

                # hash password, before populating User object
                form.password.data = hash(form.password.data)
                form.populate_obj(user)

                db.session.add(user)
                db.session.commit()

                login.login_user(user)
                if login.current_user.is_authenticated():
                    return redirect(url_for('.index'))
            else:
                flash('You cannot be registered.', 'info')
                return redirect(url_for('.login_view'))
        link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
        return self.render('admin/home.html', form=form, link=link, register=True)
Example #32
0
    def test_non_int_pk(self):
        form = NonIntPKForm()
        self.assertEqual(form.data, {'value': None, 'id': None})
        self.assertFalse(form.validate())

        obj = NonIntPKModel(id='a', value='A')
        form = NonIntPKForm(obj=obj)
        self.assertEqual(form.data, {'value': 'A', 'id': 'a'})
        self.assertTrue(form.validate())

        form = NonIntPKForm(FakePost({'id': 'b', 'value': 'B'}))
        self.assertTrue(form.validate())

        obj = NonIntPKModel()
        form.populate_obj(obj)
        self.assertEqual(obj.id, 'b')
        self.assertEqual(obj.value, 'B')

        self.assertEqual(NonIntPKModel.select().count(), 0)
        obj.save(True)
        self.assertEqual(NonIntPKModel.select().count(), 1)
Example #33
0
    def test_non_int_pk(self):
        form = NonIntPKForm()
        self.assertEqual(form.data, {"value": None, "id": None})
        self.assertFalse(form.validate())

        obj = NonIntPKModel(id="a", value="A")
        form = NonIntPKForm(obj=obj)
        self.assertEqual(form.data, {"value": "A", "id": "a"})
        self.assertTrue(form.validate())

        form = NonIntPKForm(FakePost({"id": "b", "value": "B"}))
        self.assertTrue(form.validate())

        obj = NonIntPKModel()
        form.populate_obj(obj)
        self.assertEqual(obj.id, "b")
        self.assertEqual(obj.value, "B")

        self.assertEqual(NonIntPKModel.select().count(), 0)
        obj.save(True)
        self.assertEqual(NonIntPKModel.select().count(), 1)
Example #34
0
    def register_view(self):

        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = User()

            form.populate_obj(user)
            # we hash the users password to avoid saving it as plaintext in the db,
            user.hash_password(form.password.data)

            db.session.add(user)
            db.session.commit()

            login.login_user(user)
            log.info("redirect rv")
            return redirect(url_for(".index"))
        link = '<p>Already have an account? <a href="' + url_for(".login_view") + '">Click here to log in.</a></p>'
        self._template_args["form"] = form
        self._template_args["link"] = link

        return super(MyAdminIndexView, self).index()
Example #35
0
    def test_null_form_saving(self):
        form = NullFieldsModelForm(FakePost({"c": ""}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, None)

        # this is a bit odd, but since checkboxes do not send a value if they
        # are unchecked this will evaluate to false (and passing in an empty
        # string evalutes to true) since the wtforms booleanfield blindly coerces
        # to bool
        self.assertEqual(nfm.b, False)

        form = NullFieldsModelForm(FakePost({"c": "", "b": ""}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, None)

        # again, this is for the purposes of documenting behavior -- nullable
        # booleanfields won't work without a custom field class
        # Passing an empty string will evalute to False
        # https://bitbucket.org/simplecodes/wtforms/commits/35c5f7182b7f0c62a4d4db7a1ec8719779b4b018
        self.assertEqual(nfm.b, False)

        form = NullFieldsModelForm(FakePost({"c": "test"}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, "test")
Example #36
0
    def test_null_form_saving(self):
        form = NullFieldsModelForm(FakePost({'c': ''}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, None)

        # this is a bit odd, but since checkboxes do not send a value if they
        # are unchecked this will evaluate to false (and passing in an empty
        # string evalutes to true) since the wtforms booleanfield blindly coerces
        # to bool
        self.assertEqual(nfm.b, False)

        form = NullFieldsModelForm(FakePost({'c': '', 'b': ''}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, None)

        # again, this is for the purposes of documenting behavior -- nullable
        # booleanfields won't work without a custom field class
        # Passing an empty string will evalute to False
        # https://bitbucket.org/simplecodes/wtforms/commits/35c5f7182b7f0c62a4d4db7a1ec8719779b4b018
        self.assertEqual(nfm.b, False)

        form = NullFieldsModelForm(FakePost({'c': 'test'}))
        self.assertTrue(form.validate())

        nfm = NullFieldsModel()
        form.populate_obj(nfm)
        self.assertEqual(nfm.c, 'test')
Example #37
0
File: app.py Project: amponce/elsa
def register():
	form = eforms.RegistrationForm(request.form)
	if helpers.validate_form_on_submit(form):
		user = models.User()
		if form.validate_login(user):
			form.populate_obj(user)

			user.password = generate_password_hash(form.password.data)

			db.session.add(user)
			db.session.commit()

			login.login_user(user)
			flash('logged in!')

			return redirect(url_for('home'))
	else:
		flash('user exists.')
		return redirect(url_for('signup'))

	flash(form.name.data)
	return render_template('debug.html', msg='error')
Example #38
0
    def test_hidden_field_none(self):
        class TestNullEntryForm(WTForm):
            blog = HiddenQueryField(query=Blog.select())

        form = TestNullEntryForm(FakePost({
            'blog': '',
        }))

        # check the htmlz for the form's hidden field
        html = form._fields['blog']()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="">')

        self.assertTrue(form.validate())
        self.assertEqual(form.blog.data, None)

        entry = NullEntry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(NullEntry.select().count(), 0)

        entry.save()
        self.assertEqual(NullEntry.select().count(), 1)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, None)

        # edit entry a1
        form = TestNullEntryForm(FakePost({
            'blog': None,
        }), obj=self.entry_a1)

        # check the htmlz for the form's hidden field
        html = form._fields['blog']()
        self.assertEqual(html, u'<input id="blog" name="blog" type="hidden" value="">')

        self.assertTrue(form.validate())
Example #39
0
    def add(self):
        form = RegistrationForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = UserModel()

            form.populate_obj(user)
            # we hash the users password to avoid saving it as plaintext in the db,
            # remove to use plain text:
            user.password = generate_password_hash(form.password.data)
            user.t = int(time.time())
            user.ut = int(time.time())

            user.save()

            return '1'
        else:
            errors = {}
            for f in form:
                if f.errors:
                    errors[f.name] = f.errors[0]

            return json.dumps(errors)

        return "0"
Example #40
0
    def register_view(self):
        form = add_user_from(request.form)

        if request.method == "POST":
            if form.validate():
                user = User()

                form.populate_obj(user)
                user.first_name = form.username.data

                user.login = user.first_name
                user.email = form.email.data

                user.password = generate_password_hash("{}".format(form.password.data))
                # if not db.session.query(User).filter_by(first_name=user.first_name).count() > 0:

                db.session.add(user)
                db.session.commit()
                flash("User was successfully submitted", category="success")

                # else:
                #     flash('Duplicate username')

        return self.render("register.html", form=form)
Example #41
0
File: app.py Project: amponce/elsa
def apply():
	if not login.current_user.is_authenticated():
		return redirect(url_for('index'))

	form = eforms.Pipeline(request.form)
	add_application = models.Pipeline()

	if not form.validate_application(form):
		flash('application exists already')
		return redirect(url_for('home'))

	perspective = db.session.query(models.Views).filter((models.Views.recruiter_id==login.current_user.id)&(models.Views.candidate_id==form.applicant.data)).first()
	if perspective:
		#look up the existing view and return it
		recipe = db.session.query(models.Recipes).filter_by(id=perspective.recipe_id).first()
		form.resume.data = recipe.id
	else:
		coin_flip = random.randrange(0, 100)
		control_flag = True if coin_flip < 51 else False
		if control_flag:
			recipe = db.session.query(models.Recipes).filter((models.Recipes.test_id==form.resume.data)&(models.Recipes.recipe=='Control')).first()
			form.resume.data = recipe.id
		else:
			recipe = db.session.query(models.Recipes).filter((models.Recipes.test_id==form.resume.data)&(models.Recipes.recipe<>'Control')).first()
			form.resume.data = recipe.id

	form.populate_obj(add_application)
	try:
		db.session.add(add_application)
		db.session.commit()

		flash('Successfully applied!')
		return redirect(url_for('home'))
	except Exception as e:
		flash('Error applying to job: %s' % e)
		return redirect(url_for('home'))
Example #42
0
def register():
    error = None
    if request.method == 'POST':
        form = RegistrationForm(request.form)
        user = User()
        form.populate_obj(user)
        user.login = request.form['username']
        user.password = generate_password_hash(form.password.data)
        #user.password = generate_password_hash(request.form['password'])
        user.admin = False
        #login = request.form['username']

        #user = User.query.filter_by(username=request.form['username']).first()

        #user = User(login, password, admin)
        db.session.add(user)
        try:
            db.session.commit()
        except:
            flash(len(categorys))
        else:
            flash('注册成功。')
            return redirect(url_for('show_entries'))
    return render_template('register.html', error=error)
Example #43
0
    def test_entry_form_saving(self):
        # check count of entries
        self.assertEqual(Entry.select().count(), 3)

        form = EntryForm(
            FakePost(
                {
                    "title": "new entry",
                    "content": "some content",
                    "pub_date-date": "2011-02-01",
                    "pub_date-time": "00:00:00",
                    "blog": self.blog_b.get_id(),
                }
            )
        )
        self.assertTrue(form.validate())

        self.assertEqual(form.pub_date.data, datetime.datetime(2011, 2, 1))
        self.assertEqual(form.blog.data, self.blog_b)

        entry = Entry()
        form.populate_obj(entry)

        # ensure entry count hasn't changed
        self.assertEqual(Entry.select().count(), 3)

        entry.save()
        self.assertEqual(Entry.select().count(), 4)
        self.assertEqual(self.blog_a.entry_set.count(), 2)
        self.assertEqual(self.blog_b.entry_set.count(), 2)

        # make sure the blog object came through ok
        self.assertEqual(entry.blog, self.blog_b)

        # edit entry a1
        form = EntryForm(
            FakePost(
                {
                    "title": "a1 edited",
                    "content": "a1 content",
                    "pub_date": "2011-01-01 00:00:00",
                    "blog": self.blog_b.get_id(),
                }
            ),
            obj=self.entry_a1,
        )
        self.assertTrue(form.validate())

        form.populate_obj(self.entry_a1)
        self.entry_a1.save()

        self.assertEqual(self.entry_a1.blog, self.blog_b)

        self.assertEqual(self.blog_a.entry_set.count(), 1)
        self.assertEqual(self.blog_b.entry_set.count(), 3)

        # pull from the db just to be 100% sure
        a1 = Entry.get(title="a1 edited")

        form = EntryForm(
            FakePost({"title": "new", "content": "blah", "pub_date": "2011-01-01 00:00:00", "blog": 10000})
        )
        self.assertFalse(form.validate())
Example #44
0
 async def do_create(cls, form, request):
     instance = cls.model_class()
     form.populate_obj(instance)
     instance.set_password(form.password.data)
     instance.save()
     return instance