本文整理汇总了Python中formspree.forms.models.Form.get_with方法的典型用法代码示例。如果您正苦于以下问题:Python Form.get_with方法的具体用法?Python Form.get_with怎么用?Python Form.get_with使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类formspree.forms.models.Form
的用法示例。
在下文中一共展示了Form.get_with方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rate_limiting_on_form_posts
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def test_rate_limiting_on_form_posts(client, msend):
# confirm a form
client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={"name": "john"},
)
form = Form.get_with(host="somewhere.com", email="[email protected]")
form.confirmed = True
DB.session.add(form)
DB.session.commit()
# submit form many times
replies = []
for _ in range(1000):
r = client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={"name": "attacker"},
)
replies.append(r.status_code)
limit = int(settings.RATE_LIMIT.split(" ")[0])
# the number of submissions should not be more than the rate limit
form = Form.get_with(host="somewhere.com", email="[email protected]")
assert form.counter < limit
# should have gotten some 302 and then many 429 responses
assert replies.count(302) <= limit
assert replies.count(429) >= 900 - limit
示例2: test_a_confusing_case
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def test_a_confusing_case(client, msend):
"""
Final boss.
"""
prepare()
try:
_, uf = create_user_and_form(client)
DB.session.add(Form(email=uf.email, confirmed=False, host="example.com"))
DB.session.add(Form(email=uf.email, confirmed=True, host="example.com/contact"))
DB.session.add(Form(email=uf.email, confirmed=True, host="www.example.com/"))
DB.session.commit()
assert Form.query.count() == 4
form = Form.get_with(email=uf.email, host="example.com/")
assert form
assert form.confirmed
assert form.host == "www.example.com/"
form2 = Form.get_with(email=uf.email, host="www.example.com")
assert form2
assert form2.confirmed
assert form.host == "www.example.com/"
contact = form.get_with(email=uf.email, host="www.example.com/contact/")
assert contact
assert contact.host == "example.com/contact"
assert form.id != contact.id
assert form.id == form2.id
r = client.post(
"/" + uf.email,
headers={"Referer": "http://example.com/"},
data={"name": "example"},
)
assert r.status_code == 302
assert "next" in r.location
r = client.post(
"/" + uf.email,
headers={"Referer": "www.example.com"},
data={"name": "example"},
)
assert r.status_code == 302
assert "next" in r.location
assert msend.call_count == 2
assert Form.query.count() == 4
form3 = Form.get_with(email=uf.email, host="example.com")
assert 2 == form.submissions.count()
assert form3.id == form2.id == form.id
finally:
revert()
示例3: test_backwards_none_confirmed
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def test_backwards_none_confirmed(client, msend):
"""
This time no form is confirmed.
"""
prepare()
try:
for host in hosts:
f = Form(email=email, confirmed=False, host=host)
DB.session.add(f)
DB.session.commit()
for referer in hosts:
r = client.post(
"/" + email,
headers={"Referer": "http://" + referer},
data={"name": "example"},
)
assert r.status_code == 200
assert b"confirm" in r.get_data()
assert Form.get_with(email=email, host=referer).host == hosts[0]
assert Form.query.count() == len(hosts)
assert Form.query.filter_by(confirmed=True).count() == 0
assert Submission.query.count() == 0
finally:
revert()
示例4: google_call
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def google_call(hashid):
form = Form.get_with(hashid=hashid)
session["ggl:form"] = form.hashid
hint = form.email
if not form.email.endswith("@gmail.com") and current_user.email.endswith(
"@gmail.com"
):
hint = current_user.email
flow = google_auth_oauthlib.flow.Flow.from_client_config(
settings.GOOGLE_CLIENT_CONFIG,
scopes=["https://www.googleapis.com/auth/drive.file"],
)
flow.redirect_uri = url_for(
"google_callback",
_external=True,
_scheme="https", # can't figure out how to get this to honor PREFERRED_URL_SCHEME
)
authorization_url, _ = flow.authorization_url(
access_type="offline",
login_hint=hint,
prompt="consent",
state=hashid,
include_granted_scopes="true",
)
return redirect(authorization_url)
示例5: google_callback
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def google_callback():
hashid = session["ggl:form"]
del session["ggl:form"]
if hashid != request.args.get("state"):
return script_error("oauth-failed")
form = Form.get_with(hashid=hashid)
flow = google_auth_oauthlib.flow.Flow.from_client_config(
settings.GOOGLE_CLIENT_CONFIG,
state=hashid,
scopes=["https://www.googleapis.com/auth/drive.file"],
)
flow.redirect_uri = url_for("google_callback", _external=True, _scheme="https")
flow.fetch_token(authorization_response=request.url.replace("http://", "https://"))
plugin = Plugin(form_id=form.id, kind=PluginKind.google_sheets)
plugin.access_token = json.dumps(
{
"token": flow.credentials.token,
"refresh_token": flow.credentials.refresh_token,
"token_uri": flow.credentials.token_uri,
"id_token": flow.credentials.id_token,
"client_id": flow.credentials.client_id,
"client_secret": flow.credentials.client_secret,
}
)
spreadsheet_title = (
form.name
if form.name
else f"{settings.SERVICE_NAME} submissions for {form.hashid}"
)[:128]
google_creds = json.loads(plugin.access_token)
credentials = google.oauth2.credentials.Credentials(**google_creds)
spreadsheet_id, worksheet_id = create_google_sheet(
form, spreadsheet_title, credentials
)
plugin.plugin_data = {
"spreadsheet_id": spreadsheet_id,
"worksheet_id": worksheet_id,
}
DB.session.add(plugin)
DB.session.commit()
return script_data({"spreadsheet_id": spreadsheet_id})
示例6: validate_user_form
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def validate_user_form(hashid):
"""
Gets a form from a hashid, created on the dashboard.
Checks to make sure the submission can be accepted by this form.
"""
form = Form.get_with(hashid=hashid)
if not form:
raise SubmitFormError(errors.bad_hashid_error(hashid))
if form.disabled:
raise SubmitFormError(errors.disabled_error())
return form
示例7: mailchimp_callback
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def mailchimp_callback():
error = request.args.get("error")
if error:
return script_error(error if error == "access-denied" else "oauth-failed")
hashid = session["mcp:form"]
del session["mcp:form"]
if hashid != request.args.get("state"):
return script_error("oauth-failed")
code = request.args.get("code")
r = requests.post(
"https://login.mailchimp.com/oauth2/token",
data={
"grant_type": "authorization_code",
"client_id": settings.MAILCHIMP_CLIENT_ID,
"client_secret": settings.MAILCHIMP_CLIENT_SECRET,
"code": code,
"redirect_uri": url_for(
"mailchimp_callback", _external=True, _scheme="https"
),
},
)
if not r.ok:
return script_error("oauth-failed")
data = r.json()
form = Form.get_with(hashid=hashid)
plugin = Plugin(form_id=form.id, kind=PluginKind.mailchimp)
r = requests.get(
"https://login.mailchimp.com/oauth2/metadata",
headers={"Authorization": "OAuth " + data["access_token"]},
)
if not r.ok:
return script_error("oauth-failed")
meta = r.json()
plugin.access_token = data["access_token"]
plugin.plugin_data = {"api_endpoint": meta["api_endpoint"], "dc": meta["dc"]}
plugin.enabled = False
DB.session.add(plugin)
DB.session.commit()
return script_data({"ok": True})
示例8: mailchimp_call
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def mailchimp_call(hashid):
form = Form.get_with(hashid=hashid)
session["mcp:form"] = form.hashid
return redirect(
"https://login.mailchimp.com/oauth2/authorize?"
+ urlencode(
{
"response_type": "code",
"client_id": settings.MAILCHIMP_CLIENT_ID,
"redirect_uri": url_for(
"mailchimp_callback", _external=True, _scheme="https"
),
"state": form.hashid,
}
)
)
示例9: slack_call
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def slack_call(hashid):
form = Form.get_with(hashid=hashid)
session["slk:form"] = form.hashid
return redirect(
"https://slack.com/oauth/authorize?"
+ urlencode(
{
"client_id": settings.SLACK_CLIENT_ID,
"scope": "incoming-webhook",
"redirect_uri": url_for(
"slack_callback", _external=True, _scheme="https"
),
"state": form.hashid,
}
)
)
示例10: export_submissions
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def export_submissions(hashid, format=None):
if not current_user.has_feature("dashboard"):
return jsonify({"error": "Please upgrade your account."}), 402
form = Form.get_with(hashid=hashid)
if not form.controlled_by(current_user):
return abort(401)
submissions, fields = form.submissions_with_fields(with_ids=False)
if format == "json":
return Response(
json.dumps(
{
"host": form.host,
"email": form.email,
"fields": fields,
"submissions": submissions,
},
sort_keys=True,
indent=2,
),
mimetype="application/json",
headers={
"Content-Disposition": "attachment; filename=form-%s-submissions-%s.json"
% (hashid, datetime.datetime.now().isoformat().split(".")[0])
},
)
elif format == "csv":
out = io.BytesIO()
w = csv.DictWriter(out, fieldnames=fields, encoding="utf-8")
w.writeheader()
for sub in submissions:
w.writerow(sub)
return Response(
out.getvalue(),
mimetype="text/csv",
headers={
"Content-Disposition": "attachment; filename=form-%s-submissions-%s.csv"
% (hashid, datetime.datetime.now().isoformat().split(".")[0])
},
)
示例11: get_or_create_form
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def get_or_create_form(email, host):
"""
Gets the form if it already exits, otherwise checks to ensure
that this is a valid new form submission. If so, creates a
new form.
"""
form = Form.get_with(email=email, host=host)
if not form:
if request_wants_json():
# Can't create a new ajax form unless from the dashboard
ajax_error_str = (
"To prevent spam, only "
+ settings.UPGRADED_PLAN_NAME
+ " accounts may create AJAX forms."
)
raise SubmitFormError((jsonify({"error": ajax_error_str}), 400))
if (
url_domain(settings.SERVICE_URL) in host
and host.rstrip("/") != settings.TEST_URL
):
# Bad user is trying to submit a form spoofing formspree.io
g.log.info(
"User attempting to create new form spoofing SERVICE_URL. Ignoring."
)
raise SubmitFormError(
(
render_template(
"error.html", title="Unable to submit form", text="Sorry."
),
400,
)
)
# all good, create form
form = Form(email, host=host, confirmed=False, normalize=True)
if form.disabled:
raise SubmitFormError(errors.disabled_error())
return form
示例12: trello_call
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def trello_call(hashid):
form = Form.get_with(hashid=hashid)
session["trl:form"] = form.hashid
return redirect(
"https://trello.com/1/authorize?{}".format(
urlencode(
{
"return_url": url_for(
"fragment_postmessage", _external=True, _scheme="https"
),
"expiration": "never",
"scope": "read,write",
"name": settings.SERVICE_NAME,
"key": settings.TRELLO_API_KEY,
"callback_method": "fragment",
"response_type": "fragment",
}
)
)
)
示例13: slack_callback
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def slack_callback():
error = request.args.get("error")
if error:
return script_error(error if error == "access-denied" else "oauth-failed")
hashid = session["slk:form"]
del session["slk:form"]
if hashid != request.args.get("state"):
return script_error("oauth-failed")
code = request.args.get("code")
r = requests.get(
"https://slack.com/api/oauth.access",
params={
"client_id": settings.SLACK_CLIENT_ID,
"client_secret": settings.SLACK_CLIENT_SECRET,
"code": code,
"redirect_uri": url_for("slack_callback", _external=True, _scheme="https"),
},
)
if not r.ok:
return script_error("oauth-failed")
data = r.json()
form = Form.get_with(hashid=hashid)
plugin = Plugin(form_id=form.id, kind=PluginKind.slack)
plugin.access_token = data["access_token"]
plugin.plugin_data = {
"team_name": data["team_name"],
"team_id": data["team_id"],
"incoming_webhook": data["incoming_webhook"],
}
plugin.enabled = True
DB.session.add(plugin)
DB.session.commit()
return script_data(
{"channel": data["incoming_webhook"]["channel"], "team": data["team_name"]}
)
示例14: test_form_creation
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def test_form_creation(client, msend):
# register user
r = client.post(
"/register", data={"email": "[email protected]", "password": "banana"}
)
assert r.status_code == 302
assert 1 == User.query.count()
# fail to create form
r = client.post(
"/api-int/forms",
headers={"Content-type": "application/json", "Referer": settings.SERVICE_URL},
data=json.dumps({"email": "[email protected]"}),
)
assert r.status_code == 402
assert "error" in json.loads(r.data.decode("utf-8"))
assert 0 == Form.query.count()
# upgrade user manually
user = User.query.filter_by(email="[email protected]").first()
user.plan = Plan.gold
DB.session.add(user)
DB.session.commit()
# verify email
email = Email.query.filter_by(
address="[email protected]", owner_id=user.id
).first()
email.verified = True
DB.session.add(email)
DB.session.commit()
# successfully create form
msend.reset_mock()
r = client.post(
"/api-int/forms",
headers={
"Accept": "application/json",
"Content-type": "application/json",
"Referer": settings.SERVICE_URL,
},
data=json.dumps({"email": "[email protected]"}),
)
# no email should have been sent
assert not msend.called
# should return success payload
resp = json.loads(r.data.decode("utf-8"))
assert r.status_code == 201
assert "submission_url" in resp
assert "hashid" in resp
form_endpoint = resp["hashid"]
assert resp["hashid"] in resp["submission_url"]
# a form should now exist in the db
assert 1 == Form.query.count()
assert Form.query.first().id == Form.get_with(hashid=resp["hashid"]).id
assert Form.query.first().confirmed
# post to the form
r = client.post(
"/" + form_endpoint,
headers={"Referer": "http://testsite.com"},
data={"name": "bruce"},
follow_redirects=True,
)
# Should get thank-you page
assert "Thanks!" in r.data.decode("utf-8")
assert "You're only one step away" not in msend.call_args[1]["text"]
# send 5 forms (monthly limits should not apply to the gold user)
assert settings.MONTHLY_SUBMISSIONS_LIMIT == 2
for i in range(5):
r = client.post(
"/" + form_endpoint,
headers={"Referer": "http://testsite.com"},
data={"name": "ana", "submission": "__%s__" % i},
)
# form should now have 6 submissions
form = Form.query.first()
assert form.counter == 6
# check last submission email
assert "ana" in msend.call_args[1]["text"]
assert "__4__" in msend.call_args[1]["text"]
assert "past the limit" not in msend.call_args[1]["text"]
# submit form from a different host -- this is OK now
r = client.post(
"/" + form_endpoint,
headers={"Referer": "http://different.com"},
data={"name": "permitted"},
follow_redirects=True,
)
assert r.status_code == 200
assert "permitted" in msend.call_args[1]["text"]
#.........这里部分代码省略.........
示例15: test_automatically_created_forms
# 需要导入模块: from formspree.forms.models import Form [as 别名]
# 或者: from formspree.forms.models.Form import get_with [as 别名]
def test_automatically_created_forms(
client, host="somewhere.com", email="[email protected]"
):
form = create_and_activate_form(
client, host="somewhere.com", email="[email protected]xample.com"
)
# add four filler submissions
DB.session.add(Submission(form.id))
DB.session.add(Submission(form.id))
DB.session.add(Submission(form.id))
DB.session.commit()
# submit again
client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={
"_replyto": "[email protected]",
"_next": "http://google.com",
"name": "johannes",
"message": "yahoo!",
},
)
form = Form.query.first()
assert form.submissions.count() == 4
# check archived values
submissions = form.submissions.all()
assert 4 == len(submissions)
assert "message" in submissions[0].data
assert "_next" not in submissions[0].data
# check if submissions over the limit are correctly deleted
assert settings.ARCHIVED_SUBMISSIONS_LIMIT == 4
client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={"which-submission-is-this": "the fifth!"},
)
form = Form.query.first()
assert 4 == form.submissions.count()
newest = form.submissions.first() # first should be the newest
assert newest.data["which-submission-is-this"] == "the fifth!"
client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={"which-submission-is-this": "the sixth!"},
)
client.post(
"/[email protected]",
headers={"referer": "http://somewhere.com"},
data={"which-submission-is-this": "the seventh!"},
)
form = Form.query.first()
assert 4 == form.submissions.count()
submissions = form.submissions.all()
assert submissions[0].data["which-submission-is-this"] == "the seventh!"
assert submissions[3].data["message"] == "yahoo!"
#
# try another form (to ensure that a form is not deleting wrong submissions)
client.post(
"/[email protected]",
headers={"referer": "http://here.com"},
data={"name": "send me the confirmation!"},
)
secondform = Form.get_with(host="here.com", email="[email protected]")
assert secondform is not None
# this form wasn't confirmed, so it still has no submissions
assert secondform.submissions.count() == 0
# confirm
secondform.confirmed = True
DB.session.add(form)
DB.session.commit()
# submit more times and test
client.post(
"/[email protected]",
headers={"referer": "http://here.com"},
data={"name": "leibniz"},
)
secondform = Form.query.filter_by(
host="here.com", email="[email protected]"
).first()
assert 1 == secondform.submissions.count()
assert secondform.submissions.first().data["name"] == "leibniz"
client.post(
"/[email protected]",
#.........这里部分代码省略.........