Merge pull request #134 from rohitdatta/ajax-marking

Mark AJAX forms
This commit is contained in:
Rohit Datta 2017-02-10 22:31:57 +01:00 committed by GitHub
commit 13c07cf20a
6 changed files with 47 additions and 3 deletions

View File

@ -8,7 +8,7 @@ from urlparse import urljoin
from flask import request, g
from formspree import settings
from formspree.app import redis_store
from formspree.app import redis_store, DB
CAPTCHA_URL = 'https://www.google.com/recaptcha/api/siteverify'
CAPTCHA_VAL = 'g-recaptcha-response'
@ -112,6 +112,11 @@ def verify_captcha(form_data, request):
}, timeout=2)
return r.ok and r.json().get('success')
def assign_ajax(form, sent_using_ajax):
if form.uses_ajax is None:
form.uses_ajax = sent_using_ajax
DB.session.add(form)
DB.session.commit()
def temp_store_hostname(hostname, referrer):
nonce = uuid.uuid4()

View File

@ -3,7 +3,7 @@ import datetime
from formspree.app import DB, redis_store
from formspree import settings
from formspree.utils import send_email, unix_time_for_12_months_from_now, \
next_url, IS_VALID_EMAIL
next_url, IS_VALID_EMAIL, request_wants_json
from flask import url_for, render_template, g
from sqlalchemy.sql.expression import delete
from werkzeug.datastructures import ImmutableMultiDict, \
@ -26,6 +26,7 @@ class Form(DB.Model):
counter = DB.Column(DB.Integer)
owner_id = DB.Column(DB.Integer, DB.ForeignKey('users.id'))
captcha_disabled = DB.Column(DB.Boolean)
uses_ajax = DB.Column(DB.Boolean)
owner = DB.relationship('User') # direct owner, defined by 'owner_id'
# this property is basically useless. use .controllers
@ -73,6 +74,7 @@ class Form(DB.Model):
self.confirmed = False
self.counter = 0
self.disabled = False
self.uses_ajax = request_wants_json()
def __repr__(self):
return '<Form %s, email=%s, host=%s>' % (self.id, self.email, self.host)

View File

@ -16,7 +16,7 @@ from formspree.utils import request_wants_json, jsonerror, IS_VALID_EMAIL
from helpers import http_form_to_dict, ordered_storage, referrer_to_path, \
remove_www, referrer_to_baseurl, sitewide_file_check, \
verify_captcha, temp_store_hostname, get_temp_hostname, \
HASH, EXCLUDE_KEYS
HASH, EXCLUDE_KEYS, assign_ajax
from models import Form, Submission
@ -80,6 +80,9 @@ def send(email_or_string):
form = Form.get_with_hashid(hashid)
if form:
# Check if it has been assigned about using AJAX or not
assign_ajax(form, request_wants_json())
if form.disabled:
# owner has disabled the form, so it should not receive any submissions
if request_wants_json():
@ -133,6 +136,10 @@ def send(email_or_string):
# get the form for this request
form = Form.query.filter_by(hash=HASH(email, host)).first() \
or Form(email, host) # or create it if it doesn't exists
# Check if it has been assigned about using AJAX or not
assign_ajax(form, request_wants_json())
if form.disabled:
g.log.info('submission rejected. Form is disabled.')
if request_wants_json():

View File

@ -20,6 +20,7 @@ def request_wants_json():
if 'json' in request.headers.get('Content-Type', '') and \
not accept_better('html', 'json'):
return True
return False
def accept_better(subject, against):

View File

@ -0,0 +1,26 @@
"""empty message
Revision ID: f99d5e2ab036
Revises: 745fe4b160a3
Create Date: 2017-02-10 17:43:31.532821
"""
# revision identifiers, used by Alembic.
revision = 'f99d5e2ab036'
down_revision = '745fe4b160a3'
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('forms', sa.Column('uses_ajax', sa.Boolean(), nullable=True))
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('forms', 'uses_ajax')
### end Alembic commands ###

View File

@ -69,6 +69,9 @@ class TestFormCreationFromDashboard(FormspreeTestCase):
self.client.get('/confirm/%s:%s' % (HASH(form.email, str(form.id)), form.hashid))
self.assertTrue(Form.query.first().confirmed)
# Make sure that it marks the first form as AJAX
self.assertTrue(Form.query.first().uses_ajax)
# send 5 forms (monthly limits should not apply to the upgraded user)
self.assertEqual(settings.MONTHLY_SUBMISSIONS_LIMIT, 2)
for i in range(5):