當前位置: 首頁>>代碼示例>>Python>>正文


Python Form.get_form_by_random_like_string方法代碼示例

本文整理匯總了Python中formspree.forms.models.Form.get_form_by_random_like_string方法的典型用法代碼示例。如果您正苦於以下問題:Python Form.get_form_by_random_like_string方法的具體用法?Python Form.get_form_by_random_like_string怎麽用?Python Form.get_form_by_random_like_string使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在formspree.forms.models.Form的用法示例。


在下文中一共展示了Form.get_form_by_random_like_string方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_upgraded_user_access

# 需要導入模塊: from formspree.forms.models import Form [as 別名]
# 或者: from formspree.forms.models.Form import get_form_by_random_like_string [as 別名]
    def test_upgraded_user_access(self):
        httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json')

        # register user
        r = self.client.post('/register',
            data={'email': '[email protected]',
                  'password': 'banana'}
        )

        # upgrade user manually
        user = User.query.filter_by(email='[email protected]').first()
        user.upgraded = True
        DB.session.add(user)
        DB.session.commit()

        # create form
        r = self.client.post('/forms',
            headers={'Accept': 'application/json',
                     'Content-type': 'application/json'},
            data=json.dumps({'email': '[email protected]'})
        )
        resp = json.loads(r.data)
        form_endpoint = resp['random_like_string']

        # manually confirm the form
        form = Form.get_form_by_random_like_string(form_endpoint)
        form.confirmed = True
        DB.session.add(form)
        DB.session.commit()
        
        # submit form
        r = self.client.post('/' + form_endpoint,
            headers={'Referer': 'formspree.io'},
            data={'name': 'bruce', 'message': 'hi!'}
        )

        # test submissions endpoint (/forms/<random_like_string>/)
        r = self.client.get('/forms/' + form_endpoint + '/',
                            headers={'Accept': 'application/json'})
        submissions = json.loads(r.data)['submissions']

        self.assertEqual(len(submissions), 1)
        self.assertEqual(submissions[0]['name'], 'bruce')
        self.assertEqual(submissions[0]['message'], 'hi!')

        # test submissions endpoint with the user downgraded
        user.upgraded = False
        DB.session.add(user)
        DB.session.commit()
        r = self.client.get('/forms/' + form_endpoint + '/')
        self.assertEqual(r.status_code, 402) # it should fail

        # test submissions endpoint without a logged user
        self.client.get('/logout')
        r = self.client.get('/forms/' + form_endpoint + '/')
        self.assertEqual(r.status_code, 302) # it should return a redirect (via @user_required)
開發者ID:jean,項目名稱:formspree,代碼行數:58,代碼來源:test_archive_submissions.py

示例2: test_form_creation

# 需要導入模塊: from formspree.forms.models import Form [as 別名]
# 或者: from formspree.forms.models.Form import get_form_by_random_like_string [as 別名]
    def test_form_creation(self):
        # register user
        r = self.client.post('/register',
            data={'email': '[email protected]',
                  'password': 'banana'}
        )
        self.assertEqual(r.status_code, 302)
        self.assertEqual(r.location.endswith('/dashboard'), True)
        self.assertEqual(1, User.query.count())

        # fail to create form
        r = self.client.post('/forms',
            headers={'Content-type': 'application/json'},
            data={'email': '[email protected]'}
        )
        self.assertEqual(r.status_code, 402)
        self.assertIn('error', json.loads(r.data))
        self.assertEqual(0, Form.query.count())

        # upgrade user manually
        user = User.query.filter_by(email='[email protected]').first()
        user.upgraded = True
        DB.session.add(user)
        DB.session.commit()

        # successfully create form
        r = self.client.post('/forms',
            headers={'Accept': 'application/json', 'Content-type': 'application/json'},
            data=json.dumps({'email': '[email protected]'})
        )
        resp = json.loads(r.data)
        self.assertEqual(r.status_code, 200)
        self.assertIn('submission_url', resp)
        self.assertIn('random_like_string', resp)
        form_endpoint = resp['random_like_string']
        self.assertIn(resp['random_like_string'], resp['submission_url'])
        self.assertEqual(1, Form.query.count())
        self.assertEqual(Form.query.first().id, Form.get_form_by_random_like_string(resp['random_like_string']).id)

        # post to form
        httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json')

        r = self.client.post('/' + form_endpoint,
            headers={'Referer': 'formspree.io'},
            data={'name': 'bruce'}
        )
        self.assertIn("We've sent a link to your email", r.data)
        self.assertIn('confirm+your+email', httpretty.last_request().body)
        self.assertEqual(1, Form.query.count())

        # confirm form
        form = Form.query.first()
        self.client.get('/confirm/%s:%s' % (HASH(form.email, str(form.id)), form.get_random_like_string()))
        self.assertTrue(Form.query.first().confirmed)

        # send 5 forms (monthly limits should not apply to the upgraded user)
        self.assertEqual(settings.MONTHLY_SUBMISSIONS_LIMIT, 2)
        for i in range(5):
            r = self.client.post('/' + form_endpoint,
                headers={'Referer': 'formspree.io'},
                data={'name': 'ana',
                      'submission': '__%s__' % i}
            )
        form = Form.query.first()
        self.assertEqual(form.counter, 5)
        self.assertEqual(form.get_monthly_counter(), 5)
        self.assertIn('ana', httpretty.last_request().body)
        self.assertIn('__4__', httpretty.last_request().body)
        self.assertNotIn('You+are+past+our+limit', httpretty.last_request().body)

        # try (and fail) to submit from a different host
        r = self.client.post('/' + form_endpoint,
            headers={'Referer': 'bad.com'},
            data={'name': 'usurper'}
        )
        self.assertEqual(r.status_code, 403)
        self.assertIn('ana', httpretty.last_request().body) # no more data is sent to sendgrid
        self.assertIn('__4__', httpretty.last_request().body)
開發者ID:jean,項目名稱:formspree,代碼行數:80,代碼來源:test_users.py


注:本文中的formspree.forms.models.Form.get_form_by_random_like_string方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。